-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathws-server.js
More file actions
170 lines (154 loc) · 5.48 KB
/
Copy pathws-server.js
File metadata and controls
170 lines (154 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// WS server for the in-Foundry bridge. Hello first, then work.
import { WebSocketServer } from 'ws';
import { randomUUID } from 'node:crypto';
// Set name -> allowed methods. DESIGN.md §5.1 is truth - keep in sync.
const CAPABILITY_SETS = {
debug: new Set([
'ping',
'query.actor',
'query.scene',
'query.macro',
'query.journal',
'query.user',
'logs.subscribe',
'logs.unsubscribe',
'eval', // reads free; writes gated; HP/DB-journal refused (eval-guard.js)
'damage', // constrained HP primitive, absolute >=1 HP floor
'refactor.get', // Workshop's live editor content
]),
// 'aagm' set arrives in Phase 3.
};
export function startWsServer({ config, dispatcher, audit }) {
const { host, port } = config.ws;
if (host !== '127.0.0.1' && host !== 'localhost' && host !== '::1') {
throw new Error(`refusing to bind WS server to non-localhost address "${host}"`);
}
const wss = new WebSocketServer({ host, port });
wss.on('listening', () => {
console.log(`[ws] listening on ws://${host}:${port}`);
});
wss.on('connection', (socket, req) => handleConnection(socket, req, { config, dispatcher, audit }));
return {
close: () => wss.close(),
server: wss,
};
}
function handleConnection(socket, req, { config, dispatcher, audit }) {
const remote = req.socket.remoteAddress;
if (remote && !isLoopback(remote)) { // belt to the bind's suspenders
audit.log('ws.reject_nonlocal', { remote });
socket.close(4003, 'non-localhost');
return;
}
let sessionId = null, userId = null, helloSeen = false;
const send = (msg) => {
if (socket.readyState !== socket.OPEN) return;
try {
socket.send(JSON.stringify(msg));
} catch (err) {
console.error('[ws] send failed:', err);
}
};
socket.on('message', (raw) => {
let msg;
try {
msg = JSON.parse(raw.toString());
} catch {
send({ jsonrpc: '2.0', error: { code: -32700, message: 'parse error' }, id: null });
return;
}
if (!helloSeen) {
if (msg.method !== 'hello') {
send({ jsonrpc: '2.0', error: { code: -33003, message: 'expected hello first' }, id: msg.id ?? null });
socket.close(4000, 'expected hello');
return;
}
const result = validateHello(msg, { config, dispatcher, audit });
if (!result.ok) {
send({ jsonrpc: '2.0', error: { code: -33003, message: result.reason }, id: msg.id ?? null });
socket.close(result.closeCode, result.reason);
return;
}
helloSeen = true;
sessionId = result.sessionId;
userId = result.userId;
// Metadata validated + send closure bound: register.
dispatcher.registerBridge({
sessionId,
userId,
userName: result.userName,
capabilitySet: result.capabilitySet,
capabilities: result.capabilities,
auditLogging: result.auditLogging,
foundryVersion: result.foundryVersion,
moduleVersion: result.moduleVersion,
worldId: result.worldId,
isGM: result.isGM,
send,
});
send({
jsonrpc: '2.0',
result: { sessionId, capabilitySet: result.capabilitySet, auditLogging: result.auditLogging },
id: msg.id ?? null,
});
return;
}
// Post-hello: a response to us, or a notification.
if (msg.id != null && (msg.result !== undefined || msg.error !== undefined)) {
dispatcher.resolveResponse(msg);
} else if (msg.method) {
dispatcher.routeNotification(msg);
}
});
socket.on('close', (code, reasonBuf) => {
const reason = reasonBuf?.toString?.() || '';
if (sessionId) dispatcher.unregisterBridge(sessionId, `socket close ${code} ${reason}`);
audit.log('ws.close', { sessionId, userId, code, reason });
});
socket.on('error', (err) => {
audit.log('ws.error', { sessionId, userId, message: err.message });
});
}
function validateHello(msg, { config, dispatcher, audit }) {
const params = msg.params || {};
const userId = params.userId;
const userName = params.userName;
if (!userId) {
audit.log('hello.reject', { reason: 'missing userId' });
return { ok: false, reason: 'missing userId', closeCode: 4001 };
}
const userCfg = config.users?.[userId];
if (!userCfg) {
audit.log('hello.reject', { reason: 'unknown userId', userId, userName });
return { ok: false, reason: 'no capability set for userId', closeCode: 4001 };
}
const capabilitySet = userCfg.capabilitySet;
const caps = CAPABILITY_SETS[capabilitySet];
if (!caps) {
audit.log('hello.reject', { reason: 'unknown capability set', userId, capabilitySet });
return { ok: false, reason: `capability set "${capabilitySet}" is not defined`, closeCode: 4001 };
}
// One connection per userId (alignment-phase rule).
for (const rec of dispatcher.bridges.values()) {
if (rec.userId === userId) {
audit.log('hello.reject', { reason: 'duplicate userId', userId, existingSessionId: rec.sessionId });
return { ok: false, reason: 'duplicate-session', closeCode: 4002 };
}
}
return {
ok: true,
sessionId: `sess-${randomUUID()}`,
userId,
userName: userName || userCfg.userName || userId,
capabilitySet,
capabilities: caps,
auditLogging: capabilitySet === 'aagm',
foundryVersion: params.foundryVersion,
moduleVersion: params.moduleVersion,
worldId: params.worldId,
isGM: !!params.isGM,
};
}
function isLoopback(addr) {
return addr === '127.0.0.1' || addr === '::1' || addr === '::ffff:127.0.0.1' || addr.startsWith('127.');
}