slopstream 0.0.0-stage → 0.2.1
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.
- package/.claude-plugin/marketplace.json +13 -0
- package/.claude-plugin/plugin.json +18 -0
- package/LICENSE +202 -0
- package/NOTICE +8 -0
- package/PRIVACY.md +17 -0
- package/README.md +114 -2
- package/SECURITY.md +34 -0
- package/THREAT_MODEL.md +166 -0
- package/bridge/cli.js +433 -0
- package/bridge/config.js +28 -0
- package/bridge/demo.js +146 -0
- package/bridge/gate.js +152 -0
- package/bridge/http.js +18 -0
- package/bridge/init.js +132 -0
- package/bridge/login.js +51 -0
- package/bridge/net.js +89 -0
- package/bridge/server.js +365 -0
- package/bridge/simulate.js +78 -0
- package/bridge/status.js +54 -0
- package/commands/demo.md +15 -0
- package/commands/get-started.md +19 -0
- package/commands/init.md +17 -0
- package/commands/links.md +10 -0
- package/commands/status.md +14 -0
- package/package.json +57 -4
- package/product.md +163 -0
- package/relay/admin.js +31 -0
- package/relay/core.js +555 -0
- package/relay/html.js +44 -0
- package/relay/kv-file.js +41 -0
- package/relay/kv-mem.js +9 -0
- package/relay/server.js +93 -0
package/relay/server.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// slopstream relay: Node host.
|
|
3
|
+
// All protocol logic lives in relay/core.js (RelayCore). This file only provides what a host must:
|
|
4
|
+
// sockets (ws, on the same port as a small HTTP API), a KV store (JSON files in DATA_DIR), and a periodic sweep.
|
|
5
|
+
|
|
6
|
+
import http from 'node:http';
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import os from 'node:os';
|
|
10
|
+
import { WebSocketServer } from 'ws';
|
|
11
|
+
import { RelayCore, configFromEnv } from './core.js';
|
|
12
|
+
import { FileKV } from './kv-file.js';
|
|
13
|
+
|
|
14
|
+
const PORT = Number(process.env.PORT || 8787);
|
|
15
|
+
const HOST = process.env.HOST || '127.0.0.1';
|
|
16
|
+
const DATA = path.resolve(process.env.DATA_DIR || './data');
|
|
17
|
+
const config = configFromEnv(process.env);
|
|
18
|
+
if (!config.publicUrl) config.publicUrl = `http://${HOST}:${PORT}`;
|
|
19
|
+
const MAX_GROUP_SIZE = config.maxGroupSize;
|
|
20
|
+
|
|
21
|
+
const core = new RelayCore({ kv: new FileKV(DATA), config });
|
|
22
|
+
|
|
23
|
+
const MAX_BODY = 4096;
|
|
24
|
+
const server = http.createServer((req, res) => {
|
|
25
|
+
const chunks = []; let size = 0; let tooBig = false;
|
|
26
|
+
req.on('data', (c) => {
|
|
27
|
+
size += c.length;
|
|
28
|
+
if (size > MAX_BODY) { tooBig = true; return; }
|
|
29
|
+
chunks.push(c);
|
|
30
|
+
});
|
|
31
|
+
req.on('end', async () => {
|
|
32
|
+
if (tooBig) { res.writeHead(413, { 'content-type': 'text/plain' }); return res.end('payload too large'); }
|
|
33
|
+
try {
|
|
34
|
+
const url = new URL(req.url, 'http://x');
|
|
35
|
+
const out = await core.handleHttp({ method: req.method, path: url.pathname, body: Buffer.concat(chunks).toString('utf8'), ip: req.socket.remoteAddress });
|
|
36
|
+
res.writeHead(out.status, out.headers || {});
|
|
37
|
+
res.end(out.body);
|
|
38
|
+
} catch (e) {
|
|
39
|
+
res.writeHead(500, { 'content-type': 'text/plain' });
|
|
40
|
+
res.end('internal error');
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const wss = new WebSocketServer({ server, maxPayload: 64 * 1024 });
|
|
46
|
+
|
|
47
|
+
wss.on('connection', (ws, req) => {
|
|
48
|
+
ws.isAlive = true;
|
|
49
|
+
ws.lastActivity = Date.now();
|
|
50
|
+
const conn = {
|
|
51
|
+
meta: {},
|
|
52
|
+
saveMeta() {},
|
|
53
|
+
send(o) { if (ws.readyState === 1) ws.send(JSON.stringify(o)); },
|
|
54
|
+
close(c, r) { try { ws.close(c, r); } catch { /* already closing */ } },
|
|
55
|
+
lastActivity: () => ws.lastActivity,
|
|
56
|
+
};
|
|
57
|
+
ws.on('pong', () => { ws.isAlive = true; ws.lastActivity = Date.now(); });
|
|
58
|
+
ws.on('message', (raw) => { ws.lastActivity = Date.now(); core.onMessage(conn, raw.toString()); });
|
|
59
|
+
ws.on('close', () => core.onClose(conn));
|
|
60
|
+
core.openConn(conn, { ip: req.socket.remoteAddress });
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
setInterval(() => {
|
|
64
|
+
for (const ws of wss.clients) {
|
|
65
|
+
if (!ws.isAlive) { ws.terminate(); continue; }
|
|
66
|
+
ws.isAlive = false; ws.ping();
|
|
67
|
+
}
|
|
68
|
+
}, 15000).unref();
|
|
69
|
+
setInterval(() => core.sweep(), 5000).unref();
|
|
70
|
+
|
|
71
|
+
// Pidfile, so `slopstream stop` can find this process on any OS. The old lookup shelled out to
|
|
72
|
+
// `pgrep`, which only exists on Unix. Written to the data dir and to a fixed path in the home
|
|
73
|
+
// directory, because the CLI does not always know which DATA_DIR the relay was started with.
|
|
74
|
+
const PIDFILES = [path.join(DATA, 'relay.pid'), path.join(os.homedir(), '.slopstream-relay.pid')];
|
|
75
|
+
|
|
76
|
+
function writePidFiles() {
|
|
77
|
+
const body = JSON.stringify({ pid: process.pid, host: HOST, port: PORT, data: DATA, startedAt: new Date().toISOString() });
|
|
78
|
+
for (const f of PIDFILES) { try { fs.writeFileSync(f, body, { mode: 0o600 }); } catch { /* best effort */ } }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Only remove a pidfile this process owns, so restarting does not delete a newer relay's file.
|
|
82
|
+
function clearPidFiles() {
|
|
83
|
+
for (const f of PIDFILES) {
|
|
84
|
+
try { if (JSON.parse(fs.readFileSync(f, 'utf8')).pid === process.pid) fs.rmSync(f, { force: true }); } catch { /* ignore */ }
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
process.on('exit', clearPidFiles);
|
|
89
|
+
for (const sig of ['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGBREAK']) {
|
|
90
|
+
try { process.on(sig, () => { clearPidFiles(); process.exit(0); }); } catch { /* not every signal exists on every OS */ }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
server.listen(PORT, HOST, () => { writePidFiles(); console.log(`slopstream relay listening on ws://${HOST}:${PORT} data=${DATA} max group size=${MAX_GROUP_SIZE}`); });
|