*Add dedicated logging library

This commit is contained in:
Derrick Hammer 2022-07-23 20:24:19 -04:00
parent 46fdc78082
commit 9b59415463
6 changed files with 37 additions and 4 deletions

View File

@ -33,6 +33,7 @@
"json-stable-stringify": "^1.0.1", "json-stable-stringify": "^1.0.1",
"libskynet": "^0.0.62", "libskynet": "^0.0.62",
"libskynetnode": "^0.1.3", "libskynetnode": "^0.1.3",
"loglevel": "^1.8.0",
"minimatch": "^5.1.0", "minimatch": "^5.1.0",
"msgpackr": "^1.6.1", "msgpackr": "^1.6.1",
"node-cache": "^5.1.2", "node-cache": "^5.1.2",

View File

@ -28,6 +28,7 @@ switch (os.platform()) {
config.inject({ config.inject({
relayPort: 8080, relayPort: 8080,
config: configLocation, config: configLocation,
logLevel: "info",
}); });
config.load({ config.load({

View File

@ -9,6 +9,7 @@ import { pack } from "msgpackr";
import config from "./config.js"; import config from "./config.js";
import { hashDataKey } from "@lumeweb/kernel-utils"; import { hashDataKey } from "@lumeweb/kernel-utils";
import { errorExit } from "./error.js"; import { errorExit } from "./error.js";
import log from "loglevel";
const { createHash } = await import("crypto"); const { createHash } = await import("crypto");
@ -28,6 +29,8 @@ async function ipUpdate() {
await fetch(domain.url[0].toString()); await fetch(domain.url[0].toString());
activeIp = domain.address[0]; activeIp = domain.address[0];
log.info(`Updated DynDNS hostname ${config.str("domain")} to ${activeIp}`);
} }
export async function start() { export async function start() {

View File

@ -1,5 +1,7 @@
import log from "loglevel";
export function errorExit(msg: string): void { export function errorExit(msg: string): void {
console.error(msg); log.error(msg);
process.exit(1); process.exit(1);
} }

View File

@ -1,5 +1,9 @@
import { start as startRpc } from "./rpc.js"; import { start as startRpc } from "./rpc.js";
import { start as startRelay } from "./relay.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 startRpc();
await startRelay(); await startRelay();

View File

@ -23,6 +23,9 @@ import {
overwriteIndependentFileSmall, overwriteIndependentFileSmall,
} from "./file.js"; } from "./file.js";
import { seedPhraseToSeed } from "libskynet"; import { seedPhraseToSeed } from "libskynet";
import log from "loglevel";
import { AddressInfo } from "net";
import { sprintf } from "sprintf-js";
let sslCtx: tls.SecureContext = tls.createSecureContext(); let sslCtx: tls.SecureContext = tls.createSecureContext();
const sslParams: tls.SecureContextOptions = { cert: "", key: "" }; const sslParams: tls.SecureContextOptions = { cert: "", key: "" };
@ -62,7 +65,8 @@ export async function start() {
await new Promise((resolve) => { await new Promise((resolve) => {
httpServer.listen(80, "0.0.0.0", function () { 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); resolve(null);
}); });
}); });
@ -76,7 +80,11 @@ export async function start() {
await new Promise((resolve) => { await new Promise((resolve) => {
httpsServer.listen(relayPort, "0.0.0.0", function () { 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); resolve(null);
}); });
}); });
@ -91,6 +99,7 @@ async function setupSSl() {
let exists = false; let exists = false;
let domainValid = false; let domainValid = false;
let dateValid = false; let dateValid = false;
let configDomain = config.str("domain");
if (sslCert && sslKey) { if (sslCert && sslKey) {
sslParams.cert = Buffer.from((sslCert as IndependentFileSmall).fileData); sslParams.cert = Buffer.from((sslCert as IndependentFileSmall).fileData);
@ -108,7 +117,7 @@ async function setupSSl() {
dateValid = true; dateValid = true;
} }
if (certInfo?.domains.commonName === config.str("domain")) { if (certInfo?.domains.commonName === configDomain) {
domainValid = true; domainValid = true;
} }
@ -122,6 +131,7 @@ async function setupSSl() {
if (dateValid && domainValid) { if (dateValid && domainValid) {
sslCtx = tls.createSecureContext(sslParams); sslCtx = tls.createSecureContext(sslParams);
log.info(`Loaded SSL Certificate for ${configDomain}`);
return; return;
} }
@ -135,6 +145,16 @@ async function createOrRenewSSl(
oldCert?: IndependentFileSmall, oldCert?: IndependentFileSmall,
oldKey?: 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({ const [certificateKey, certificateRequest] = await acme.forge.createCsr({
commonName: config.str("domain"), commonName: config.str("domain"),
}); });
@ -172,6 +192,8 @@ async function saveSsl(
): Promise<void> { ): Promise<void> {
const seed = getSeed(); const seed = getSeed();
log.info(`Saving SSL Certificate for ${config.str("domain")}`);
if (oldCert) { if (oldCert) {
await overwriteIndependentFileSmall( await overwriteIndependentFileSmall(
oldCert, oldCert,