Fix some bugs, write some comments

This commit is contained in:
Starbeamrainbowlabs 2022-01-08 21:37:03 +00:00
parent 046821f0ac
commit 19f2003fbf
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
7 changed files with 55 additions and 9 deletions

2
.gitignore vendored
View file

@ -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

11
package-lock.json generated
View file

@ -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",

View file

@ -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",

View file

@ -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;

View file

@ -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);

View file

@ -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';

View file

@ -11,4 +11,4 @@ class ErrorWrapper extends Error {
}
}
export ErrorWrapper;
export default ErrorWrapper;