settings and better stuff
parent
99fc805557
commit
dff0b8c6e4
|
|
@ -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`
|
||||
})
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
58
lib/node.js
58
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
22
lib/upnp.js
22
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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 => {
|
||||
|
|
|
|||
20
yarn.lock
20
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"
|
||||
|
|
|
|||
Reference in New Issue