better settings overrides

canary
Marcus 2021-04-02 19:35:29 -04:00
parent 185aa45e4f
commit 2937e85f39
5 changed files with 61 additions and 36 deletions

View File

@ -3,7 +3,7 @@ const Keyv = require('keyv');
const { KeyvFile } = require('keyv-file');
const { Signale } = require('signale');
const log = new Signale().scope('GTWY');
const interactive = new Signale({ interactive: true, scope: 'GTWY' });
// const interactive = new Signale({ interactive: true, scope: 'GTWY' });
const stp = require('./STP');
const appdata = require('./appdata');
@ -39,7 +39,9 @@ class Gateway {
}
log.info('gateway endpoints:');
log.info('\t' + (await this.endpoints.get('cache')).join('\n\t'));
for(const endpoint of (await this.endpoints.get('cache'))) {
log.info(`\t${endpoint}`);
}
}
async networkTest() {
@ -54,13 +56,20 @@ class Gateway {
}
async testEndpoint(host, port) {
const log = new Signale({ scope: `${host}:${port}` });
const interactive = new Signale({ interactive: true, scope: `${host}:${port}` });
await new Promise(async (res, rej) => {
let pings = [];
let maxPings = 10;
let maxPings = 100;
let connectionAttempts = 0;
log.info(`Connecting to ${host}:${port}...`);
let wasConnected = false;
while (connectionAttempts < 10 && pings.length < maxPings) {
const done = _ => connectionAttempts === 10 || pings.length === maxPings
log.info('Starting connection test...');
while (!done()) {
await new Promise(async (res) => {
const client = stp.connect({
@ -72,25 +81,41 @@ class Gateway {
client.on('error', _ => _);
client.on('ready', async () => {
wasConnected = true;
while (pings.length < maxPings) {
interactive.info(`[${pings.length + 1}/${maxPings}] Testing connection...`);
interactive.info(`[${pings.length + 1}/${maxPings}] Testing connection`);
pings.push(await client.ping());
// pings.push(10);
await new Promise(res => setTimeout(res, 100));
// await new Promise(res => setTimeout(res, 1000));
}
const average = Math.round(pings.reduce((a, v) => a + v, 0) / maxPings);
interactive.success(`Test complete. Average Ping: ${average}`)
client.tcpSocket.destroy();
res();
});
client.on('close', () => {
connectionAttempts ++;
if(!done() && wasConnected) {
log.warn(`Lost connection, Retrying...`);
}
wasConnected = false;
res();
});
});
}
if (pings.length === maxPings) {
const average = Math.round(pings.reduce((a, v) => a + v, 0) / maxPings);
const pingRecord = new PingRecord(average, pings.length, new Date().getTime());
const endpointRecord = new EndpointRecord(host, port, pingRecord, 'online');
await this.endpoints.set(`${host}:${port}`, endpointRecord);
interactive.success(`Test complete. Average Ping: ${average}ms`);
} else {
log.error(`Could not complete connection test`)
}
});
}
}

View File

@ -1,39 +1,35 @@
const pkg = require('./../package.json');
const { readFileSync, writeFileSync } = require('fs');
const { readFileSync, writeFileSync, existsSync } = require('fs');
const { ensureDirSync } = require('fs-extra');
const { config } = require('../package.json');
const deepmerge = require('deepmerge');
const appdata = require('./appdata');
ensureDirSync(`${appdata}/valnet/relay`);
const filepath = `${appdata}/valnet/relay/config.json`;
const configObject = {}
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 {
if(!existsSync(filepath))
writeFileSync(filepath, JSON.stringify({}, null, 2));
const json = readFileSync(filepath);
const data = JSON.parse(json);
loadObject(data);
loadObject(deepmerge(config, data, {
arrayMerge: (_, sourceArray, __) => sourceArray
}));
} catch(e) {
importFromPackage();
}

View File

@ -66,12 +66,15 @@ class Node extends EventEmitter {
// log.success('Node successfully registered!');
}
async serviceUp({name, address, port, protocol}) {
log.debug(`Found ${name} @ ${address}:${port} using ${protocol}`);
async serviceUp(device) {
this.multicastDevices.push(device);
}
async serviceDown({name, address, port, protocol}) {
log.debug(`Lost ${name} @ ${address}:${port} using ${protocol}`);
async serviceDown(device) {
this.multicastDevices = this.multicastDevices.filter(testDevice => {
return testDevice.host !== device.host
|| testDevice.port !== device.port
})
}
async negotiatePort() {

View File

@ -14,7 +14,8 @@
"relay": "valnet.xyz"
},
"endpoints": [
"valnet.xyz:5500"
"valnet.xyz:5500",
"35.196.210.135:5600"
]
},
"scripts": {
@ -24,6 +25,7 @@
},
"dependencies": {
"bonjour": "^3.5.0",
"deepmerge": "^4.2.2",
"express": "^4.17.1",
"express-ws": "^4.0.0",
"font-ascii": "^1.2.1",

View File

@ -102,7 +102,6 @@ asynckit@^0.4.0:
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"
@ -321,6 +320,10 @@ deep-equal@^1.0.1:
object-keys "^1.1.1"
regexp.prototype.flags "^1.2.0"
deepmerge@^4.2.2:
version "4.2.2"
resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.2.2.tgz#44d2ea3679b8f4d4ffba33f03d865fc1e7bf4955"
define-properties@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/define-properties/-/define-properties-1.1.3.tgz#cf88da6cbee26fe6db7094f61d870cbd84cee9f1"
@ -664,7 +667,6 @@ fs-extra@^4.0.1:
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"
@ -708,7 +710,6 @@ graceful-fs@^4.1.2, graceful-fs@^4.1.6:
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"
@ -942,7 +943,6 @@ jsonfile@^4.0.0:
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:
@ -1633,7 +1633,6 @@ universalify@^0.1.0:
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"