From dff0b8c6e4a6c1cb195757f58f817714e2add3b2 Mon Sep 17 00:00:00 2001 From: Marcus Date: Thu, 1 Apr 2021 01:04:17 -0400 Subject: [PATCH] settings and better stuff --- lib/Identity.js | 4 +++- lib/config.js | 37 ++++++++++++++++++++++++++++++ lib/node.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++--- lib/upnp.js | 22 ++++++++++-------- lib/vns.js | 0 package.json | 7 +++++- relay/index.js | 20 ++++++----------- yarn.lock | 20 +++++++++++++++++ 8 files changed, 143 insertions(+), 27 deletions(-) create mode 100644 lib/config.js delete mode 100644 lib/vns.js diff --git a/lib/Identity.js b/lib/Identity.js index 811f079..0ceba8d 100644 --- a/lib/Identity.js +++ b/lib/Identity.js @@ -13,9 +13,11 @@ 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({ - filename: `${os.tmpdir()}/valnet/${module}/${id}.json` + filename: `${appdata}/valnet/${module}/${id}.json` }) }); diff --git a/lib/config.js b/lib/config.js new file mode 100644 index 0000000..8fae8eb --- /dev/null +++ b/lib/config.js @@ -0,0 +1,37 @@ +const pkg = require('./../package.json'); +const { readFileSync, writeFileSync } = require('fs'); + +const appdata = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.local/share") +const filepath = `${appdata}/valnet/relay/config.json`; + +const configObject = {} + +module.exports.config = configObject; + +module.exports.write = write; + +function write() { + writeFileSync(filepath, JSON.stringify(configObject, null, 2)); +} + +function importFromPackage() { + loadObject(pkg.config); +} + +function loadObject(obj) { + for(const key in obj) { + configObject[key] = obj[key]; + } + + write(); +} + +try { + const json = readFileSync(filepath); + const data = JSON.parse(json); + + loadObject(data); + +} catch(e) { + importFromPackage(); +} \ No newline at end of file diff --git a/lib/node.js b/lib/node.js index f260c5d..f04dd2c 100644 --- a/lib/node.js +++ b/lib/node.js @@ -1,14 +1,68 @@ const EventEmitter = require('events') - +const stp = require('./STP'); +const upnp = require('./upnp'); +const md5 = require('md5'); +const pkg = require('./../package.json'); +const { config, write } = require('./config.js'); class Node extends EventEmitter { - constructor() { - + clients = []; + hash = null; + name = null; + readyPromise = null; + + constructor(identity) { + super(); + this.hash = md5(identity.publicKey); + this.name = `valnet node - ${this.hash}`; + // stp.createServer({ + // identity, + // port + // }, socket => { + // log.info('secured connection from ' + socket.remoteAddress); + // this.clients.push(socket); + // }); + this.readyPromise = this.negotiatePort(); + } + + async negotiatePort() { + await upnp.map(5600, 60 * 5, 'other application'); + + const mappings = await upnp.mappings(); + + const alreadyMapped = mappings.filter(mapping => { + return mapping.description === this.name + }).length > 0; + + if(alreadyMapped) { + console.log('already mapped!'); + return; + } + + const takenPorts = mappings.map(mapping => mapping.public.port); + + for(let port = config.ports.relay; port <= config.ports.relayEnd; port ++) { + if(takenPorts.indexOf(port) === -1) { + console.log('registering to port ' + port); + await upnp.map(port, 10, this.name); + break; + } else { + console.log('port ' + port + ' is taken...'); + } + } + + + + // console.log(mappings, this.hash); } static get Node() { return Node; } + + get ready() { + return this.readyPromise; + } } diff --git a/lib/upnp.js b/lib/upnp.js index 49ccfe3..ca466f8 100644 --- a/lib/upnp.js +++ b/lib/upnp.js @@ -1,31 +1,34 @@ const natUpnp = require('nat-upnp'); const client = natUpnp.createClient(); -module.exports.map = function(port) { + +module.exports.map = function(port, ttl = 10, name = 'upnp application') { return new Promise((res, rej) => { client.portMapping({ private: port, public: port, - ttl: 10, - description: 'valnet' + ttl, + description: name }, (err) => { if(err) rej(err); res(); }); }); -} -module.exports.mapIndefinite = function(port) { +}; + +module.exports.mapIndefinite = function(port, name) { return new Promise((res, rej) => { client.portMapping({ private: port, public: port, ttl: 0, - description: 'valnet' + description: name }, (err) => { if(err) rej(err); res(); }); }); -} +}; + module.exports.unmap = function(port) { return new Promise((res, rej) => { client.portUnmapping({ @@ -36,7 +39,8 @@ module.exports.unmap = function(port) { res(); }); }); -} +}; + module.exports.mappings = function() { return new Promise((res, rej) => { client.getMappings((err, mappings) => { @@ -44,7 +48,7 @@ module.exports.mappings = function() { res(mappings); }); }); -} +}; /* diff --git a/lib/vns.js b/lib/vns.js deleted file mode 100644 index e69de29..0000000 diff --git a/package.json b/package.json index e8ea459..5aa39b2 100644 --- a/package.json +++ b/package.json @@ -6,12 +6,16 @@ "config": { "ports": { "relay": 5600, + "relayEnd": 5699, "http": 5700, "service": 5000 }, "addresses": { "relay": "valnet.xyz" - } + }, + "endpoints": [ + "valnet.xyz:5500" + ] }, "scripts": { "relay": "supervisor -w relay,lib -n exit relay/index.js", @@ -26,6 +30,7 @@ "ip": "^1.1.5", "keyv": "^4.0.3", "keyv-file": "^0.2.0", + "md5": "^2.3.0", "nat-upnp": "^1.1.1", "nedb": "^1.8.0", "node-rsa": "^1.1.1", diff --git a/relay/index.js b/relay/index.js index 05f9528..3a537a1 100644 --- a/relay/index.js +++ b/relay/index.js @@ -2,38 +2,32 @@ const { title } = require('../lib/title'); const net = require('net'); const log = require('signale').scope('relay'); -const { config } = require('../package.json'); 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 clients = []; +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] -stp.createServer({ - identity: identity, - port: config.ports.relay -}, socket => { - log.info('secured connection from ' + socket.remoteAddress); - clients.push(socket); -}); + +const node = new Node(identity); function connectNetwork(t = 1000) { if(t > 60000) t /= 2; const client = stp.connect({ identity, - port: config.ports.relay, - ip: config.addresses.relay + port: config.endpoints[0].split(':')[1], + ip: config.endpoints[0].split(':')[0] }); client.on('ready', () => { - log.success('connectd to relay!'); + log.success('connected to relay!'); t = 500; }) client.on('error', e => { diff --git a/yarn.lock b/yarn.lock index 5af0417..2474848 100644 --- a/yarn.lock +++ b/yarn.lock @@ -175,6 +175,10 @@ chardet@^0.7.0: version "0.7.0" resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e" +charenc@0.0.2: + version "0.0.2" + resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667" + cli-cursor@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-3.1.0.tgz#264305a7ae490d1d03bf0c9ba7c925d1753af307" @@ -253,6 +257,10 @@ core-util-is@1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" +crypt@0.0.2: + version "0.0.2" + resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b" + dashdash@^1.12.0: version "1.14.1" resolved "https://registry.yarnpkg.com/dashdash/-/dashdash-1.14.1.tgz#853cfa0f7cbe2fed5de20326b8dd581035f6e2f0" @@ -746,6 +754,10 @@ is-arrayish@^0.2.1: version "0.2.1" resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d" +is-buffer@~1.1.6: + version "1.1.6" + resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be" + is-callable@^1.1.4, is-callable@^1.2.2: version "1.2.2" resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.2.2.tgz#c7c6715cd22d4ddb48d3e19970223aceabb080d9" @@ -900,6 +912,14 @@ lodash@^4.17.14, lodash@^4.17.19: version "4.17.20" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52" +md5@^2.3.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/md5/-/md5-2.3.0.tgz#c3da9a6aae3a30b46b7b0c349b87b110dc3bda4f" + dependencies: + charenc "0.0.2" + crypt "0.0.2" + is-buffer "~1.1.6" + media-typer@0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"