From 966dc965f2a92c61c7372c6f1d2f65e2598530a8 Mon Sep 17 00:00:00 2001 From: Bronwen Date: Thu, 1 Apr 2021 23:01:24 -0400 Subject: [PATCH] cleanup --- lib/Gateway.js | 6 +++--- lib/Identity.js | 2 +- lib/STP/index.js | 4 +++- lib/appdata.js | 3 +++ lib/config.js | 4 +++- package.json | 1 + relay/index.js | 40 ++++------------------------------------ yarn.lock | 34 ++++++++++++++++++++++++++++++++++ 8 files changed, 52 insertions(+), 42 deletions(-) create mode 100644 lib/appdata.js diff --git a/lib/Gateway.js b/lib/Gateway.js index 00edcf2..79ae0e0 100644 --- a/lib/Gateway.js +++ b/lib/Gateway.js @@ -5,11 +5,11 @@ const { Signale } = require('signale'); const log = new Signale().scope('GTWY'); const interactive = new Signale({interactive: true, scope: 'GTWY'}); const stp = require('./STP'); +const appdata = require('./appdata'); class Gateway { constructor(identity, endpoints) { this.identity = identity; - const appdata = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.local/share") this.endpoints = new Keyv({ store: new KeyvFile({ @@ -58,7 +58,7 @@ class Gateway { let pings = []; let maxPings = 10; let connectionAttempts = 0; - log.info(`Conencting to ${host}:${port}...`); + log.info(`Connecting to ${host}:${port}...`); while(connectionAttempts < 10 && pings.length < maxPings) { @@ -76,7 +76,7 @@ class Gateway { interactive.info(`[${pings.length + 1}/${maxPings}] Testing connection...`); pings.push(await client.ping()); // pings.push(10); - await new Promise(res => setTimeout(res, 500)); + await new Promise(res => setTimeout(res, 100)); } const average = Math.round(pings.reduce((a, v) => a + v, 0) / maxPings); interactive.success(`Test complete. Average Ping: ${average}`) diff --git a/lib/Identity.js b/lib/Identity.js index a563073..a4256d6 100644 --- a/lib/Identity.js +++ b/lib/Identity.js @@ -4,6 +4,7 @@ const { hri } = require('human-readable-ids'); const os = require('os'); const NodeRSA = require('node-rsa'); let log = require('signale').scope('Identity(null)'); +const appdata = require('./appdata'); module.exports.Identity = class Identity { kv; @@ -13,7 +14,6 @@ module.exports.Identity = class Identity { /// ASYNC CONSTRUCTOR constructor(module, id) { return new Promise(async (res, rej) => { - const appdata = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.local/share") const kv = new Keyv({ store: new KeyvFile({ diff --git a/lib/STP/index.js b/lib/STP/index.js index 64bd6f2..45bdce7 100644 --- a/lib/STP/index.js +++ b/lib/STP/index.js @@ -136,7 +136,9 @@ class STPSocket extends EventEmitter { } if (this.readyState === this.SECURED && obj.cmd === 'PING') { - this.tcpSocket.write(new PongPacket(obj.data.id).toBuffer()); + setTimeout(() => { + this.tcpSocket.write(new PongPacket(obj.data.id).toBuffer()); + }, 1000); return; } diff --git a/lib/appdata.js b/lib/appdata.js new file mode 100644 index 0000000..cac3379 --- /dev/null +++ b/lib/appdata.js @@ -0,0 +1,3 @@ +const appdata = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.local/share"); + +module.exports = appdata; \ No newline at end of file diff --git a/lib/config.js b/lib/config.js index 8fae8eb..f4167dd 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,7 +1,9 @@ const pkg = require('./../package.json'); const { readFileSync, writeFileSync } = require('fs'); +const { ensureDirSync } = require('fs-extra'); -const appdata = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.local/share") +const appdata = require('./appdata'); +ensureDirSync(`${appdata}/valnet/relay`); const filepath = `${appdata}/valnet/relay/config.json`; const configObject = {} diff --git a/package.json b/package.json index 749ac3a..374d28d 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "express": "^4.17.1", "express-ws": "^4.0.0", "font-ascii": "^1.2.1", + "fs-extra": "^9.1.0", "gradient-string": "^1.2.0", "human-readable-ids": "^1.0.4", "ip": "^1.1.5", diff --git a/relay/index.js b/relay/index.js index 9d0ee50..ac6016a 100644 --- a/relay/index.js +++ b/relay/index.js @@ -1,49 +1,17 @@ (async () => { const { title } = require('../lib/title'); -const net = require('net'); const log = require('signale').scope('RLAY'); const { Identity } = require('../lib/Identity'); -const stp = require('../lib/STP'); title('relay', false); const identity = await new Identity('relay', 'default'); -const upnp = require('../lib/upnp'); const Node = require('../lib/node'); -const { config, write } = require('../lib/config'); - -// const client = stp.connect(identity, config.ports.relay, '127.0.0.1'); - -// upnp.mapIndefinite(5600); - -// ==================================== [STP SERVER] +const { config } = require('../lib/config'); +const { ensureDirSync } = require('fs-extra'); +const appdata = require('../lib/appdata'); +ensureDirSync(`${appdata}/valnet/relay`); const node = new Node(identity); -function connectNetwork(t = 1000) { - if(t > 60000) t /= 2; - - const poopoo = config.endpoints[0].split(':'); - // console.log(poopoo); - - const client = stp.connect({ - identity, - port: parseInt(poopoo[1]), - ip: poopoo[0] - }); - client.on('ready', () => { - log.success('connected to relay!'); - t = 500; - }) - client.on('error', e => { - }); - client.on('close', e => { - t *= 2; - setTimeout(connectNetwork.bind(global, t), t); - log.warn('disconnected from relay'); - log.warn('retrying connection... ' + (t / 1000) + 's'); - }); -} -// connectNetwork(); - // ==================================== [EXPRESS] const express = require('express'); const app = express(); diff --git a/yarn.lock b/yarn.lock index 1e3f858..9145a6e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -99,6 +99,11 @@ asynckit@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" +at-least-node@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/at-least-node/-/at-least-node-1.0.0.tgz#602cd4b46e844ad4effc92a8011a3c46e0238dc2" + integrity sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg== + aws-sign2@~0.7.0: version "0.7.0" resolved "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.7.0.tgz#b46e890934a9591f2d2f6f86d7e6a9f1b3fe76a8" @@ -656,6 +661,16 @@ fs-extra@^4.0.1: jsonfile "^4.0.0" universalify "^0.1.0" +fs-extra@^9.1.0: + version "9.1.0" + resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.1.0.tgz#5954460c764a8da2094ba3554bf839e6b9a7c86d" + integrity sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ== + dependencies: + at-least-node "^1.0.0" + graceful-fs "^4.2.0" + jsonfile "^6.0.1" + universalify "^2.0.0" + function-bind@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d" @@ -690,6 +705,11 @@ graceful-fs@^4.1.2, graceful-fs@^4.1.6: version "4.2.4" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.4.tgz#2256bde14d3632958c465ebc96dc467ca07a29fb" +graceful-fs@^4.2.0: + version "4.2.6" + resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.6.tgz#ff040b2b0853b23c3d31027523706f1885d76bee" + integrity sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ== + gradient-string@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/gradient-string/-/gradient-string-1.2.0.tgz#93f39f2c7c8dcb095608c2ccf0aac24aa315fbac" @@ -919,6 +939,15 @@ jsonfile@^4.0.0: optionalDependencies: graceful-fs "^4.1.6" +jsonfile@^6.0.1: + version "6.1.0" + resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.1.0.tgz#bc55b2634793c679ec6403094eb13698a6ec0aae" + integrity sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ== + dependencies: + universalify "^2.0.0" + optionalDependencies: + graceful-fs "^4.1.6" + jsprim@^1.2.2: version "1.4.1" resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2" @@ -1601,6 +1630,11 @@ universalify@^0.1.0: version "0.1.2" resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66" +universalify@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717" + integrity sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ== + unpipe@1.0.0, unpipe@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"