@nordbyte/nordrelay 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/.env.example +17 -0
  2. package/README.md +67 -6
  3. package/dist/access-control.js +6 -1
  4. package/dist/activity-events.js +2 -2
  5. package/dist/bot-preferences.js +1 -0
  6. package/dist/bot.js +77 -6
  7. package/dist/channel-adapter.js +11 -5
  8. package/dist/channel-command-catalog.js +88 -0
  9. package/dist/channel-command-service.js +214 -1
  10. package/dist/channel-mirror-registry.js +77 -0
  11. package/dist/channel-peer-prompt.js +95 -0
  12. package/dist/channel-runtime.js +12 -5
  13. package/dist/codex-state.js +114 -78
  14. package/dist/config-metadata.js +15 -0
  15. package/dist/config.js +31 -6
  16. package/dist/context-key.js +10 -0
  17. package/dist/discord-bot.js +85 -26
  18. package/dist/discord-command-surface.js +11 -73
  19. package/dist/index.js +20 -0
  20. package/dist/metrics.js +46 -0
  21. package/dist/peer-auth.js +85 -0
  22. package/dist/peer-client.js +256 -0
  23. package/dist/peer-context.js +21 -0
  24. package/dist/peer-identity.js +127 -0
  25. package/dist/peer-runtime-service.js +636 -0
  26. package/dist/peer-server.js +220 -0
  27. package/dist/peer-store.js +294 -0
  28. package/dist/peer-types.js +52 -0
  29. package/dist/relay-runtime-helpers.js +208 -0
  30. package/dist/relay-runtime.js +72 -274
  31. package/dist/remote-prompt.js +98 -0
  32. package/dist/telegram-command-menu.js +3 -53
  33. package/dist/telegram-general-commands.js +14 -0
  34. package/dist/telegram-preference-commands.js +23 -127
  35. package/dist/web-api-contract.js +8 -0
  36. package/dist/web-dashboard-pages.js +12 -0
  37. package/dist/web-dashboard-peer-routes.js +204 -0
  38. package/dist/web-dashboard-ui.js +1 -0
  39. package/dist/web-dashboard.js +12 -0
  40. package/dist/webui-assets/dashboard.js +427 -14
  41. package/package.json +3 -2
  42. package/plugins/nordrelay/scripts/nordrelay.mjs +373 -7
@@ -0,0 +1,127 @@
1
+ import { createHash, generateKeyPairSync, randomBytes, sign, verify, } from "node:crypto";
2
+ import { chmodSync, existsSync, mkdirSync, readFileSync } from "node:fs";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import selfsigned from "selfsigned";
6
+ import { readJsonFileWithBackup, writeJsonFileAtomic, writeTextFileAtomic } from "./persistence.js";
7
+ const DEFAULT_HOME = path.join(os.homedir(), ".nordrelay");
8
+ export function loadOrCreatePeerIdentity(home = process.env.NORDRELAY_HOME || DEFAULT_HOME, name) {
9
+ const filePath = path.join(home, "identity.json");
10
+ const existing = readJsonFileWithBackup(filePath).value;
11
+ if (existing?.nodeId && existing.publicKey && existing.privateKey && existing.fingerprint) {
12
+ return {
13
+ public: {
14
+ nodeId: existing.nodeId,
15
+ name: existing.name || defaultNodeName(),
16
+ publicKey: existing.publicKey,
17
+ fingerprint: existing.fingerprint,
18
+ createdAt: existing.createdAt,
19
+ },
20
+ privateKey: existing.privateKey,
21
+ };
22
+ }
23
+ const pair = generateKeyPairSync("ed25519", {
24
+ publicKeyEncoding: { type: "spki", format: "pem" },
25
+ privateKeyEncoding: { type: "pkcs8", format: "pem" },
26
+ });
27
+ const publicKey = pair.publicKey.toString();
28
+ const createdAt = new Date().toISOString();
29
+ const identity = {
30
+ nodeId: createNodeId(publicKey),
31
+ name: name?.trim() || defaultNodeName(),
32
+ publicKey,
33
+ privateKey: pair.privateKey.toString(),
34
+ fingerprint: fingerprintForPublicKey(publicKey),
35
+ createdAt,
36
+ };
37
+ mkdirSync(path.dirname(filePath), { recursive: true });
38
+ writeJsonFileAtomic(filePath, identity);
39
+ chmodSync(filePath, 0o600);
40
+ return {
41
+ public: {
42
+ nodeId: identity.nodeId,
43
+ name: identity.name,
44
+ publicKey: identity.publicKey,
45
+ fingerprint: identity.fingerprint,
46
+ createdAt: identity.createdAt,
47
+ },
48
+ privateKey: identity.privateKey,
49
+ };
50
+ }
51
+ export function ensurePeerTlsFiles(home = process.env.NORDRELAY_HOME || DEFAULT_HOME, identity) {
52
+ const certDir = path.join(home, "tls");
53
+ const certPath = path.join(certDir, "peer.crt");
54
+ const keyPath = path.join(certDir, "peer.key");
55
+ if (existsSync(certPath) && existsSync(keyPath)) {
56
+ const cert = readFileSync(certPath, "utf8");
57
+ const key = readFileSync(keyPath, "utf8");
58
+ return { certPath, keyPath, cert, key, fingerprint: fingerprintForCertificate(cert) };
59
+ }
60
+ mkdirSync(certDir, { recursive: true });
61
+ const attrs = [{ name: "commonName", value: identity?.nodeId ?? "nordrelay-peer" }];
62
+ const generated = selfsigned.generate(attrs, {
63
+ algorithm: "sha256",
64
+ days: 3650,
65
+ keySize: 2048,
66
+ extensions: [
67
+ { name: "basicConstraints", cA: false },
68
+ { name: "keyUsage", digitalSignature: true, keyEncipherment: true },
69
+ { name: "extKeyUsage", serverAuth: true },
70
+ {
71
+ name: "subjectAltName",
72
+ altNames: [
73
+ { type: 2, value: "localhost" },
74
+ { type: 7, ip: "127.0.0.1" },
75
+ { type: 7, ip: "::1" },
76
+ ],
77
+ },
78
+ ],
79
+ });
80
+ writeTextFileAtomic(certPath, generated.cert);
81
+ writeTextFileAtomic(keyPath, generated.private);
82
+ chmodSync(certPath, 0o600);
83
+ chmodSync(keyPath, 0o600);
84
+ return {
85
+ certPath,
86
+ keyPath,
87
+ cert: generated.cert,
88
+ key: generated.private,
89
+ fingerprint: fingerprintForCertificate(generated.cert),
90
+ };
91
+ }
92
+ export function signPeerPayload(privateKey, payload) {
93
+ return sign(null, Buffer.from(payload, "utf8"), privateKey).toString("base64url");
94
+ }
95
+ export function verifyPeerPayload(publicKey, payload, signature) {
96
+ try {
97
+ return verify(null, Buffer.from(payload, "utf8"), publicKey, Buffer.from(signature, "base64url"));
98
+ }
99
+ catch {
100
+ return false;
101
+ }
102
+ }
103
+ export function createPairingSignaturePayload(nodeId, timestamp, code) {
104
+ return `${nodeId}\n${timestamp}\n${code}`;
105
+ }
106
+ export function createSharedSecret() {
107
+ return randomBytes(32).toString("base64url");
108
+ }
109
+ export function fingerprintForPublicKey(publicKey) {
110
+ return formatFingerprint(createHash("sha256").update(publicKey).digest("hex"));
111
+ }
112
+ export function fingerprintForCertificate(certPem) {
113
+ const body = certPem
114
+ .replace(/-----BEGIN CERTIFICATE-----/g, "")
115
+ .replace(/-----END CERTIFICATE-----/g, "")
116
+ .replace(/\s+/g, "");
117
+ return formatFingerprint(createHash("sha256").update(Buffer.from(body, "base64")).digest("hex"));
118
+ }
119
+ function createNodeId(publicKey) {
120
+ return createHash("sha256").update(publicKey).digest("hex").slice(0, 16);
121
+ }
122
+ function defaultNodeName() {
123
+ return `${os.hostname()} (${process.platform})`;
124
+ }
125
+ function formatFingerprint(hex) {
126
+ return hex.match(/.{1,2}/g)?.join(":") ?? hex;
127
+ }