systemquery/src/lib/agent/Agent.mjs
Starbeamrainbowlabs 135b2e8d1b
Write a bunch more glue code
....but it's all still untested. I'm getting kinda nervous here
2022-01-08 16:59:08 +00:00

58 lines
1.5 KiB
JavaScript

"use strict";
import fs from 'fs';
import os from 'os';
import log from 'log'; const l = log.get("Agent");
import systeminfo from 'systeminformation';
import PeerServer from './PeerServer.mjs';
import hash from '../crypto/hash.mjs';
import parse_peer_name from '../parse/peer_name.mjs';
class Agent {
constructor(config) {
this.config = config;
}
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");
if(this.config.secret_join !== "CHANGE_ME")
return this.config.secret_join;
return null;
}
async init() {
/** Our peer id - calculated automatically from the system's uuid */
this.peer_id = hash("sha256", "base64", await systeminfo.system().serial)
.replace(/[+/=]/g, "");
this.peer_name = os.hostname();
this.server = new PeerServer(
this.peer_id,
await this.find_secret()
);
this.server.retries = this.config.net.peer_retries;
l.log(`Starting peer listener....`);
await this.server.listen(
this.config.net.port,
this.config.net.bind_address
);
l.log(`Listening on ${this.config.net.bind_address}:${this.config.net.port}`);
await this.server.add_peers(...this.config.peers.map(
peer => parse_peer_name(peer)
));
l.log(`Added ${this.config.peers.length} initial peers`);
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.`);
}
}
export default Agent;