Compare commits

...

14 Commits

6 changed files with 63 additions and 20 deletions

View File

@ -5,7 +5,7 @@ import * as fs from "fs";
import path from "path";
import log from "./log.js";
const config = new Config("lumeweb-relay");
const config = new Config("lumeweb-relay", "core.confdir");
let configDir;
@ -24,11 +24,11 @@ switch (os.platform()) {
}
config.inject({
"core.confdir": configDir,
"core.confDir": configDir,
"core.port": 8080,
"core.apport": 80,
"core.loglevel": "info",
"core.plugindir": path.resolve(configDir, "..", "plugins"),
"core.appPort": 80,
"core.logLevel": "info",
"core.pluginDir": path.resolve(configDir, "..", "plugins"),
});
config.load();

View File

@ -7,9 +7,11 @@ import { start as startSwarm, get as getSwarm } from "./modules/swarm.js";
import * as bip39 from "@scure/bip39";
import { wordlist } from "@scure/bip39/wordlists/english";
if (!config.str("seed")) {
if (!config.str("core.seed")) {
config.save("account", {
seed: bip39.generateMnemonic(wordlist),
core: {
seed: bip39.generateMnemonic(wordlist),
},
});
}

View File

@ -4,6 +4,7 @@ import fastify from "fastify";
import type { FastifyInstance } from "fastify";
import { getKeyPair } from "../lib/seed.js";
import config from "../config";
import { getPluginAPI } from "./plugin";
let app: FastifyInstance;
@ -17,5 +18,13 @@ export async function start() {
res.send(Buffer.from(keyPair.publicKey).toString("hex"));
});
await getPluginAPI().emitAsync("core.appServer.buildRoutes");
await app.listen({ port: config.uint("core.appport"), host: "0.0.0.0" });
getPluginAPI().emit("core.appServer.started");
}
export function get(): FastifyInstance {
return app;
}

View File

@ -17,6 +17,7 @@ import {
ProtocolManager,
} from "./swarm.js";
import { get as getSSl, SSLManager } from "./ssl.js";
import { get as getApp } from "./app.js";
import type { HDKey } from "micro-ed25519-hdkey";
import corePlugins from "../plugins";
import Util from "./plugin/util";
@ -95,6 +96,10 @@ class PluginAPI extends EventEmitter2 {
return getProtocolManager();
}
get app() {
return getApp();
}
public loadPlugin(
moduleName: string
): (moduleName: string) => Promise<Plugin> {
@ -142,28 +147,39 @@ export class PluginAPIManager {
}
let plugin: Plugin;
let pluginPath = paths.shift();
try {
plugin = require(paths.shift() as string) as Plugin;
plugin = require(pluginPath as string) as Plugin;
} catch (e) {
throw e;
}
log.debug("Loaded plugin %s", moduleName);
return this.loadPluginInstance(plugin);
const instance = await this.loadPluginInstance(plugin);
if (!instance) {
throw new Error(`Corrupt plugin found at ${pluginPath}`);
}
return instance as Plugin;
}
public async loadPluginInstance(plugin: Plugin): Promise<Plugin> {
public async loadPluginInstance(plugin: Plugin): Promise<Plugin | boolean> {
if ("default" in plugin) {
plugin = plugin?.default as Plugin;
}
if (!("name" in plugin)) {
return false;
}
plugin.name = sanitizeName(plugin.name);
this.registeredPlugins.set(plugin.name, plugin);
try {
plugin.plugin(
await plugin.plugin(
// @ts-ignore
new Proxy<PluginAPI>(getPluginAPI(), {
get(target: PluginAPI, prop: string): any {

View File

@ -4,24 +4,30 @@ import log from "../log.js";
import config from "../config.js";
export type SSLManagerRenewHandler = (domain: string) => Promise<boolean>;
type SSLCert = string | Buffer | Array<string | Buffer>;
export class SSLManager {
private _key?: Buffer;
private _domain: string;
constructor(domain: string) {
this._domain = domain;
}
private _domain: string;
get domain(): string {
return this._domain;
}
private _context?: tls.SecureContext;
get context(): tls.SecureContext {
return this._context as tls.SecureContext;
}
private _cert?: Buffer;
private _cert?: SSLCert;
set cert(cert: Buffer) {
set cert(cert: SSLCert) {
this._cert = cert;
this._maybeUpdateContext();
}
@ -61,11 +67,21 @@ export class SSLManager {
}
private _maybeUpdateContext() {
if (b4a.isBuffer(this._cert) && b4a.isBuffer(this._key)) {
this._context = tls.createSecureContext({
cert: this._cert,
const valid = (value: any) =>
b4a.isBuffer(value) || typeof value === "string" || Array.isArray(value);
if (valid(this._cert) && valid(this._key)) {
const opts: tls.SecureContextOptions = {
key: this._key,
});
};
if (Array.isArray(this._cert)) {
opts.ca = this._cert.slice(1);
opts.cert = this._cert[0];
} else {
opts.cert = this._cert;
}
this._context = tls.createSecureContext(opts);
}
}
}

View File

@ -618,13 +618,13 @@ __metadata:
"@lumeweb/cfg@https://git.lumeweb.com/LumeWeb/cfg.git":
version: 0.1.7
resolution: "@lumeweb/cfg@https://git.lumeweb.com/LumeWeb/cfg.git#commit=e1d4785240c3815a5b0e376e96e0fb0edfd0c064"
resolution: "@lumeweb/cfg@https://git.lumeweb.com/LumeWeb/cfg.git#commit=62856686f20c2a627f4bafa92be24ef7f2d49a0d"
dependencies:
arg: "npm:^5.0.2"
bsert: "npm:~0.0.10"
deep-to-flat-object: "npm:^1.0.1"
object-path: "npm:^0.11.8"
checksum: 1b1120ca829d80713b1ed24d3a7827d82fad78c903ce47a0e289bf5799ce791c0e95a967340e83812e290b2749c5c326f4584a6ec33d8dbacaa8d0c04c5291f5
checksum: 5124fda214790212f99fbb6cea6eaf8b6240bbc3c026e1bd4cab9bd441b8be3ccfc2ea1a77cae1fd50d957d1f902f9c955067805d8233168771286e9dacc0d3d
languageName: node
linkType: hard