From 19f2003fbf24dffbee4310a5e7115c9d86a92e02 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 8 Jan 2022 21:37:03 +0000 Subject: [PATCH] Fix some bugs, write some comments --- .gitignore | 2 ++ package-lock.json | 11 +++++++++++ package.json | 1 + src/lib/agent/Agent.mjs | 11 +++++++++-- src/lib/agent/Peer.mjs | 35 ++++++++++++++++++++++++++++++----- src/lib/agent/PeerServer.mjs | 2 +- src/lib/core/ErrorWrapper.mjs | 2 +- 7 files changed, 55 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 8e54cdc..67699b9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ /target +config/ + # Created by https://www.toptal.com/developers/gitignore/api/rust,git # Edit at https://www.toptal.com/developers/gitignore?templates=rust,git diff --git a/package-lock.json b/package-lock.json index 1432e30..1f9ce0f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "MPL-2.0", "dependencies": { + "@ltd/j-toml": "^1.24.0", "applause-cli": "^1.7.0", "jpake": "^1.0.1", "log": "^6.2.0", @@ -20,6 +21,11 @@ "tweetnacl": "^1.0.3" } }, + "node_modules/@ltd/j-toml": { + "version": "1.24.0", + "resolved": "https://registry.npmjs.org/@ltd/j-toml/-/j-toml-1.24.0.tgz", + "integrity": "sha512-XWEvSNLJ2YeCvlNDd/DQfTMldJqBZWX8+RKgPMY2i3MShqsB9XvtY2UfHqcuPb+eRrNO1WR7w4xlNfzCzop+Ig==" + }, "node_modules/@types/crypto-js": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/@types/crypto-js/-/crypto-js-4.0.2.tgz", @@ -481,6 +487,11 @@ } }, "dependencies": { + "@ltd/j-toml": { + "version": "1.24.0", + "resolved": "https://registry.npmjs.org/@ltd/j-toml/-/j-toml-1.24.0.tgz", + "integrity": "sha512-XWEvSNLJ2YeCvlNDd/DQfTMldJqBZWX8+RKgPMY2i3MShqsB9XvtY2UfHqcuPb+eRrNO1WR7w4xlNfzCzop+Ig==" + }, "@types/crypto-js": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/@types/crypto-js/-/crypto-js-4.0.2.tgz", diff --git a/package.json b/package.json index 0ca6492..e02a45a 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "homepage": "https://github.com/sbrl/systemquery#readme", "dependencies": { + "@ltd/j-toml": "^1.24.0", "applause-cli": "^1.7.0", "jpake": "^1.0.1", "log": "^6.2.0", diff --git a/src/lib/agent/Agent.mjs b/src/lib/agent/Agent.mjs index 26da19c..89914e6 100644 --- a/src/lib/agent/Agent.mjs +++ b/src/lib/agent/Agent.mjs @@ -3,9 +3,11 @@ import fs from 'fs'; import os from 'os'; -import log from 'log'; const l = log.get("Agent"); +import log from 'log'; const l = log.get("agent"); import systeminfo from 'systeminformation'; +import settings from '../../settings.mjs'; + import PeerServer from './PeerServer.mjs'; import hash from '../crypto/hash.mjs'; import parse_peer_name from '../parse/peer_name.mjs'; @@ -18,7 +20,12 @@ class Agent { async find_secret() { if(this.config.secret_join_filepath !== "CHANGE_ME" && fs.existsSync(this.config.secret_join_filepath)) - return await fs.promises.readFile(this.config.secret_join_filepath, "utf-8"); + return await fs.promises.readFile( + path.resolve( + path.dirname(settings.cli.config), + this.config.secret_join_filepath + ), + "utf-8"); if(this.config.secret_join !== "CHANGE_ME") return this.config.secret_join; diff --git a/src/lib/agent/Peer.mjs b/src/lib/agent/Peer.mjs index 96c099c..533f401 100644 --- a/src/lib/agent/Peer.mjs +++ b/src/lib/agent/Peer.mjs @@ -2,7 +2,7 @@ import { EventEmitter, once } from 'events'; -import log from 'log'; const l = log.get("Peer"); +import log from 'log'; const l = log.get("peer"); import Connection from '../transport/Connection.mjs'; @@ -52,7 +52,8 @@ class Peer extends EventEmitter { async __accept(connection) { this.connection = connection; const [ msg ] = await once(this.connection, "message-hello"); - this.__handle_hello(msg); + if(!this.__handle_hello(msg)) + await this.destroy(); this.emit("connect"); } @@ -64,15 +65,27 @@ class Peer extends EventEmitter { async __initiate() { await this.__send_hello(); const [ msg ] = await once(this.connection, "message-hello"); - this.__handle_hello(msg); + if(!this.__handle_hello(msg)) + await this.destroy(); this.emit("connect"); } + /** + * Handles a given hello messaage from this peer. + * @param {Object} msg The hello message to process. + * @return {boolean} Whether the peer should stay connected or not. + */ __handle_hello(msg) { this.id = msg.id; this.known_peers = msg.peers; + if(msg.id === this.server.our_id) return false; + return true; } + /** + * Sends a hello message to this peer. + * @return {Promise} A Promise that resolves when the sending of the message is complete. + */ async __send_hello() { await this.send("hello", { id: this.server.our_id, @@ -80,12 +93,24 @@ class Peer extends EventEmitter { }); } + /** + * Sends a message to this peer. + * @param {string} event_name The event name to attach to the message. + * @param {Object} msg The message to send. Can be of any type, but an Object is recommended for increased flexibility later down the line. + * @return {Promise} A Promise that resolves whent he message has been sent. + */ async send(event_name, msg) { await this.connection.send(event_name, msg); } /////////////////////////////////////////////////////////////////////////// + /** + * Closes the connection to this peer. + * Once called, in order to reinstate the connection it's best to + * instantiate a new Peer class instance. + * @return {Promise} A Promise that resolves once the connection is closed. + */ async destroy() { await this.connection.destroy(); this.emit("destroy"); @@ -93,14 +118,14 @@ class Peer extends EventEmitter { } } -Peer.Initiate = function(server, address, port) { +Peer.Initiate = async function(server, address, port) { const conn = await Connection.Create(server.secret_join, address, port); const peer = new Peer(server, conn); await peer.__initiate(); return peer; } -Peer.Accept = function(server, connection) { +Peer.Accept = async function(server, connection) { const peer = new Peer(server); await peer.__accept(connection); diff --git a/src/lib/agent/PeerServer.mjs b/src/lib/agent/PeerServer.mjs index be0f899..4221d73 100644 --- a/src/lib/agent/PeerServer.mjs +++ b/src/lib/agent/PeerServer.mjs @@ -4,7 +4,7 @@ import { EventEmitter, once } from 'events'; import net from 'net'; import p_retry from 'p-retry'; -import log from 'log'; const l = l.get("PeerServer"); +import log from 'log'; const l = log.get("peerserver"); import Connection from '../transport/Connection.mjs'; import ErrorWrapper from '../core/ErrorWrapper.mjs'; diff --git a/src/lib/core/ErrorWrapper.mjs b/src/lib/core/ErrorWrapper.mjs index 5382109..27fb432 100644 --- a/src/lib/core/ErrorWrapper.mjs +++ b/src/lib/core/ErrorWrapper.mjs @@ -11,4 +11,4 @@ class ErrorWrapper extends Error { } } -export ErrorWrapper; +export default ErrorWrapper;