diff --git a/package.json b/package.json index 5cb821a..86b930b 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "json-stable-stringify": "^1.0.1", "libskynet": "^0.0.62", "libskynetnode": "^0.1.3", + "loglevel": "^1.8.0", "minimatch": "^5.1.0", "msgpackr": "^1.6.1", "node-cache": "^5.1.2", diff --git a/src/config.ts b/src/config.ts index ddb3db0..cd69527 100644 --- a/src/config.ts +++ b/src/config.ts @@ -28,6 +28,7 @@ switch (os.platform()) { config.inject({ relayPort: 8080, config: configLocation, + logLevel: "info", }); config.load({ diff --git a/src/dns.ts b/src/dns.ts index 9614171..382ddd2 100644 --- a/src/dns.ts +++ b/src/dns.ts @@ -9,6 +9,7 @@ import { pack } from "msgpackr"; import config from "./config.js"; import { hashDataKey } from "@lumeweb/kernel-utils"; import { errorExit } from "./error.js"; +import log from "loglevel"; const { createHash } = await import("crypto"); @@ -28,6 +29,8 @@ async function ipUpdate() { await fetch(domain.url[0].toString()); activeIp = domain.address[0]; + + log.info(`Updated DynDNS hostname ${config.str("domain")} to ${activeIp}`); } export async function start() { diff --git a/src/error.ts b/src/error.ts index 8c4bac4..e04b074 100644 --- a/src/error.ts +++ b/src/error.ts @@ -1,5 +1,7 @@ +import log from "loglevel"; + export function errorExit(msg: string): void { - console.error(msg); + log.error(msg); process.exit(1); } diff --git a/src/index.ts b/src/index.ts index b8ec677..1dae0e2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,9 @@ import { start as startRpc } from "./rpc.js"; import { start as startRelay } from "./relay.js"; +import log from "loglevel"; +import config from "./config"; + +log.setDefaultLevel(config.str("log-level")); await startRpc(); await startRelay(); diff --git a/src/relay.ts b/src/relay.ts index 4b48841..8d63584 100644 --- a/src/relay.ts +++ b/src/relay.ts @@ -23,6 +23,9 @@ import { overwriteIndependentFileSmall, } from "./file.js"; import { seedPhraseToSeed } from "libskynet"; +import log from "loglevel"; +import { AddressInfo } from "net"; +import { sprintf } from "sprintf-js"; let sslCtx: tls.SecureContext = tls.createSecureContext(); const sslParams: tls.SecureContextOptions = { cert: "", key: "" }; @@ -62,7 +65,8 @@ export async function start() { await new Promise((resolve) => { httpServer.listen(80, "0.0.0.0", function () { - console.info("HTTP Listening on ", httpServer.address()); + const address = httpServer.address() as AddressInfo; + log.info("HTTP Server started on ", `${address.address}:${address.port}`); resolve(null); }); }); @@ -76,7 +80,11 @@ export async function start() { await new Promise((resolve) => { httpsServer.listen(relayPort, "0.0.0.0", function () { - console.info("Relay started on ", httpsServer.address()); + const address = httpServer.address() as AddressInfo; + log.info( + "DHT Relay Server started on ", + `${address.address}:${address.port}` + ); resolve(null); }); }); @@ -91,6 +99,7 @@ async function setupSSl() { let exists = false; let domainValid = false; let dateValid = false; + let configDomain = config.str("domain"); if (sslCert && sslKey) { sslParams.cert = Buffer.from((sslCert as IndependentFileSmall).fileData); @@ -108,7 +117,7 @@ async function setupSSl() { dateValid = true; } - if (certInfo?.domains.commonName === config.str("domain")) { + if (certInfo?.domains.commonName === configDomain) { domainValid = true; } @@ -122,6 +131,7 @@ async function setupSSl() { if (dateValid && domainValid) { sslCtx = tls.createSecureContext(sslParams); + log.info(`Loaded SSL Certificate for ${configDomain}`); return; } @@ -135,6 +145,16 @@ async function createOrRenewSSl( oldCert?: IndependentFileSmall, oldKey?: IndependentFileSmall ) { + const existing = oldCert && oldKey; + + log.info( + sprintf( + "%s SSL Certificate for %s", + existing ? "Renewing" : "Creating", + config.str("domain") + ) + ); + const [certificateKey, certificateRequest] = await acme.forge.createCsr({ commonName: config.str("domain"), }); @@ -172,6 +192,8 @@ async function saveSsl( ): Promise { const seed = getSeed(); + log.info(`Saving SSL Certificate for ${config.str("domain")}`); + if (oldCert) { await overwriteIndependentFileSmall( oldCert,