2021-10-02 00:16:34 +00:00
|
|
|
"use strict";
|
|
|
|
|
2022-01-08 16:59:08 +00:00
|
|
|
import fs from 'fs';
|
2021-10-02 00:16:34 +00:00
|
|
|
import os from 'os';
|
2022-01-08 23:47:52 +00:00
|
|
|
import path from 'path';
|
2021-10-02 00:16:34 +00:00
|
|
|
|
2022-01-09 16:30:42 +00:00
|
|
|
import log from '../io/NamespacedLog.mjs'; const l = log("agent");
|
2021-10-02 00:16:34 +00:00
|
|
|
|
2022-01-08 21:37:03 +00:00
|
|
|
import settings from '../../settings.mjs';
|
|
|
|
|
2022-01-08 16:59:08 +00:00
|
|
|
import PeerServer from './PeerServer.mjs';
|
|
|
|
import parse_peer_name from '../parse/peer_name.mjs';
|
2022-01-08 21:55:18 +00:00
|
|
|
import hostuuid from '../io/hostuuid.mjs';
|
2021-10-02 00:16:34 +00:00
|
|
|
|
|
|
|
class Agent {
|
2022-01-08 16:59:08 +00:00
|
|
|
constructor(config) {
|
|
|
|
this.config = config;
|
2021-10-02 00:16:34 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 16:59:08 +00:00
|
|
|
async find_secret() {
|
2022-01-08 23:47:52 +00:00
|
|
|
if(this.config.secret_join_filepath !== "CHANGE_ME") {
|
|
|
|
let filepath = path.resolve(
|
|
|
|
path.dirname(settings.cli.config),
|
|
|
|
this.config.secret_join_filepath
|
|
|
|
);
|
|
|
|
if(fs.existsSync(filepath))
|
|
|
|
return await fs.promises.readFile(filepath, "utf-8");
|
|
|
|
}
|
2022-01-08 16:59:08 +00:00
|
|
|
|
|
|
|
if(this.config.secret_join !== "CHANGE_ME")
|
|
|
|
return this.config.secret_join;
|
2021-10-02 00:16:34 +00:00
|
|
|
|
2022-01-08 23:47:52 +00:00
|
|
|
throw new Error(`Error: Failed to find join secret.`);
|
2022-01-08 16:59:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2022-01-09 20:28:58 +00:00
|
|
|
///
|
|
|
|
// 1: Create our local environment
|
|
|
|
///
|
2021-10-02 00:16:34 +00:00
|
|
|
/** Our peer id - calculated automatically from the system's uuid */
|
2022-01-09 00:57:06 +00:00
|
|
|
this.peer_id = await hostuuid(this.config.net.port);
|
2021-10-02 00:16:34 +00:00
|
|
|
this.peer_name = os.hostname();
|
|
|
|
|
2022-01-09 20:28:58 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
// 2: Create the peer server
|
|
|
|
///
|
2022-01-08 16:59:08 +00:00
|
|
|
this.server = new PeerServer(
|
|
|
|
this.peer_id,
|
|
|
|
await this.find_secret()
|
|
|
|
);
|
|
|
|
this.server.retries = this.config.net.peer_retries;
|
2021-10-02 00:16:34 +00:00
|
|
|
|
2022-01-09 20:28:58 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
// 3: Attach message handling listeners
|
|
|
|
///
|
|
|
|
this.server.on("message-peerlist-request", this.__send_peer_list.bind(this));
|
|
|
|
this.server.on("message-peerlist-response", this.__handle_peer_list.bind(this));
|
|
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
// 4: Start listening
|
|
|
|
///
|
2022-01-09 16:30:42 +00:00
|
|
|
l.log(`Starting peer listener....`);
|
2022-01-08 16:59:08 +00:00
|
|
|
await this.server.listen(
|
|
|
|
this.config.net.port,
|
|
|
|
this.config.net.bind_address
|
|
|
|
);
|
2022-01-09 16:30:42 +00:00
|
|
|
l.log(`Listening on ${this.config.net.bind_address}:${this.config.net.port}`);
|
2021-10-02 00:16:34 +00:00
|
|
|
|
2022-01-09 20:28:58 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
// 5: Add initial peers to kick things off
|
|
|
|
///
|
2022-01-08 16:59:08 +00:00
|
|
|
await this.server.add_peers(...this.config.peers.map(
|
|
|
|
peer => parse_peer_name(peer)
|
|
|
|
));
|
2022-01-09 17:02:26 +00:00
|
|
|
|
|
|
|
l.log(`Added ${this.server.peers().length} / ${this.config.peers.length} initial peers`);
|
2022-01-08 16:59:08 +00:00
|
|
|
if(this.config.peers.length < 1)
|
|
|
|
l.warn(`No initial peers were specified! It's recommended that you specify at least 1 on every host.`);
|
2022-01-09 20:28:58 +00:00
|
|
|
|
|
|
|
// Ask for more peers
|
|
|
|
await this.server.broadcast("peerlist-request", {});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a list of known peers to the given peer.
|
|
|
|
* @param {Peer} peer The peer to send the list to.
|
|
|
|
* @return {Promise} A Promise that resolves whent he message has been sent.
|
|
|
|
*/
|
|
|
|
async __send_peer_list(peer, _msg) {
|
|
|
|
l.log(`Sending peer list to ${peer.id_short}`);
|
|
|
|
await peer.send("peerlist-response", {
|
|
|
|
peers: this.server.peers()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async __handle_peer_list(peer, msg) {
|
|
|
|
l.log(`Received peer list from ${peer.id_short}`);
|
|
|
|
if(!(msg.peers instanceof Array)) {
|
|
|
|
l.warn(`Encountered invalid peer list message from peer ${peer.id_short}.`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let peerids_connected = this.server.peers().map(el => el.id);
|
|
|
|
let new_peers = await this.server.add_peers(...msg.peers
|
|
|
|
.filter(el => !peerids_connected.includes(el.id) && el.id !== this.peer_id)
|
|
|
|
.map(el => { return { address: el.listening_address, port: el.listening_port }; }));
|
|
|
|
if(new_peers.length > 0)
|
|
|
|
l.log(`Connected to ${new_peers.length} / ${msg.peers.length} peers from peerlist sent by ${peer.id_short}`);
|
|
|
|
else
|
|
|
|
l.log(`No new peers in peerlist sent by ${peer.id_short}`);
|
2021-10-02 00:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Agent;
|