*Initial version

This commit is contained in:
Derrick Hammer 2022-12-22 10:39:50 -05:00
parent ea6d51e42d
commit 852abf5b6a
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
7 changed files with 133 additions and 0 deletions

5
.yarnrc.yml Normal file
View File

@ -0,0 +1,5 @@
nodeLinker: node-modules
plugins:
- path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs
spec: "@yarnpkg/plugin-typescript"

9
build.js Normal file
View File

@ -0,0 +1,9 @@
import esbuild from "esbuild";
esbuild.buildSync({
entryPoints: ["src/index.ts"],
outfile: "dist/discovery-irc.js",
format: "cjs",
bundle: true,
platform: "node",
});

22
package.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "@lumeweb/relay-plugin-discovery-irc",
"version": "0.1.0",
"type": "module",
"scripts": {
"build": "node build.js"
},
"devDependencies": {
"@lumeweb/peer-discovery": "https://git.lumeweb.com/LumeWeb/peer-discovery.git",
"@lumeweb/relay-types": "https://git.lumeweb.com/LumeWeb/relay-types.git",
"@types/b4a": "^1.6.0",
"@types/prettier": "^2",
"esbuild": "^0.16.10",
"prettier": "^2.8.1",
"rollup": "^3.7.5"
},
"dependencies": {
"@ctrl/irc": "^2.1.0",
"b4a": "^1.6.1",
"json-stringify-deterministic": "^1.0.8"
}
}

13
pkg/discovery-irc.json Normal file
View File

@ -0,0 +1,13 @@
{
"plugin": {
"discoveryirc": {
"networks": [
{
"host": "irc.ergo.chat",
"port": 6697,
"ssl" : true
}
]
}
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["discovery-irc"]
}

68
src/index.ts Normal file
View File

@ -0,0 +1,68 @@
import type { Plugin, PluginAPI } from "@lumeweb/relay-types";
import type { Peer } from "@lumeweb/peer-discovery";
import { IrcClient } from "@ctrl/irc";
import b4a from "b4a";
import jsonStringify from "json-stringify-deterministic";
import type { Message } from "@ctrl/irc/dist/src/parseMessage";
interface SignedPeerResponse extends Peer {
timestamp: number;
signature?: string;
}
interface Network {
host: string;
port: number;
ssl: boolean;
}
const plugin: Plugin = {
name: "discovery-irc",
async plugin(api: PluginAPI): Promise<void> {
for (const network of api.pluginConfig.array("networks", []) as Network[]) {
let client = new IrcClient(undefined, api.identity.fingerprintHex, {
host: network.host,
port: network.port,
secure: network.ssl,
channels: ["#lumeweb"],
});
client.addListener("message", function (from, to, message) {
const pubkey = b4a.from(message, "hex");
if (!b4a.equals(api.identity.publicKeyRaw, pubkey)) {
return;
}
const host = api.config.str("domain");
const port = api.config.uint("port");
const timestamp = Date.now();
let data = {
host,
port,
timestamp,
} as SignedPeerResponse;
let json = jsonStringify(data);
data.signature = b4a
.from(api.identity.sign(b4a.from(jsonStringify(data))))
.toString("hex");
api.logger.info(api.identity.verify(b4a.from(json), data.signature));
client.say(from, jsonStringify(data));
});
client.addListener("error", (message: Message) => {
api.logger.error(message);
});
client.connect();
process.on("SIGTERM", () => {
client.end();
});
}
},
};
export default plugin;

13
tsconfig.json Normal file
View File

@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "esnext",
"target": "esnext",
"esModuleInterop": true,
"outDir": "dist",
"moduleResolution": "node"
},
"include": ["src"],
"exclude": [
"node_modules"
]
}