File size: 37,636 Bytes
ef0480c | 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 | #!/usr/bin/env node
/**
* Self-Hosted DSH Remote Relay Server (Zero-Dependency Pure Node.js >= 18)
* Wire-compatible with `dsh-go` ("自建服务器"), `ds-harness-remote` HostPluginRuntime,
* and DSH Remote Protocol v1 (REST + WSS Control / Noise_IK Relay / WebRTC Signaling).
*/
import { createServer } from 'node:http'
import { createHash, createHmac, randomBytes, randomUUID, scryptSync, timingSafeEqual } from 'node:crypto'
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const __dirname = dirname(fileURLToPath(import.meta.url))
// ---------------------------------------------------------------------------
// 1. Minimal .env loader (works on Node 18 / 20 / 22 / 24 without --env-file)
// ---------------------------------------------------------------------------
function loadEnvFile(envPath) {
if (!existsSync(envPath)) return
const lines = readFileSync(envPath, 'utf8').split(/\r?\n/)
for (const raw of lines) {
const line = raw.trim()
if (!line || line.startsWith('#')) continue
const eq = line.indexOf('=')
if (eq <= 0) continue
const key = line.slice(0, eq).trim()
let val = line.slice(eq + 1).trim()
if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
val = val.slice(1, -1)
}
if (process.env[key] === undefined) process.env[key] = val
}
}
// ---------------------------------------------------------------------------
// 2. Constants & Helpers
// ---------------------------------------------------------------------------
const PROTOCOL_VERSION = 1
const MAX_CONTROL_FRAME_BYTES = 64 * 1024
const MAX_RELAY_FRAME_BYTES = 1024 * 1024
const SERVER_SHUTDOWN_CLOSE_CODE = 1001
const SERVER_SHUTDOWN_CLOSE_REASON = 'SERVER_SHUTDOWN'
const SUPPORTED_TRANSPORTS = ['transport.lan', 'transport.p2p', 'transport.turn', 'transport.relay']
const TRANSPORT_NAMES = ['lan', 'p2p', 'turn', 'relay']
class ApiError extends Error {
constructor(code, status = 400) {
super(code)
this.code = code
this.status = status
}
}
const sha256Hex = (v) => createHash('sha256').update(v).digest('hex')
const randomSecret = () => randomBytes(32).toString('base64url')
const constEqual = (a, b) => timingSafeEqual(Buffer.from(sha256Hex(a)), Buffer.from(sha256Hex(b)))
function createControlFrame(type, payload) {
return { v: PROTOCOL_VERSION, id: randomUUID(), type, timestamp: Date.now(), payload }
}
function deriveSuccessor(token, nonce, deviceId) {
const derive = (kind) =>
createHmac('sha256', token)
.update(JSON.stringify(['ds-harness-remote/refresh/v1', deviceId, nonce, kind]))
.digest('base64url')
return { accessToken: derive('access'), refreshToken: derive('refresh') }
}
// ---------------------------------------------------------------------------
// 3. Persistent Single-Account Store
// ---------------------------------------------------------------------------
export class RelayStore {
constructor(file, account, password) {
this.file = file
this.account = account
this.onInvalidate = () => {}
let previous
if (existsSync(file)) {
try {
previous = JSON.parse(readFileSync(file, 'utf8'))
} catch {
previous = undefined
}
}
const salt = previous?.salt ?? randomSecret()
const verifier = scryptSync(password, salt, 32).toString('hex')
this.state = previous?.version === 1
? previous
: { version: 1, account, salt, verifier, devices: {}, tokens: {} }
if (this.state.account !== account) this.state.devices = {}
if (this.state.account !== account || !constEqual(this.state.verifier, verifier)) {
this.state.tokens = {}
}
this.state.account = account
this.state.salt = salt
this.state.verifier = verifier
this.save()
}
save() {
mkdirSync(dirname(this.file), { recursive: true })
const tmp = `${this.file}.tmp`
writeFileSync(tmp, JSON.stringify(this.state, null, 2), { mode: 0o600 })
renameSync(tmp, this.file)
}
get(id) {
const d = Object.hasOwn(this.state.devices, id) ? this.state.devices[id] : undefined
if (!d) throw new ApiError('DEVICE_NOT_FOUND', 404)
if (d.revoked) throw new ApiError('DEVICE_REVOKED', 403)
return d
}
list() {
return Object.values(this.state.devices).filter((d) => !d.revoked)
}
register(descriptor) {
if (!descriptor || typeof descriptor.deviceId !== 'string' || !descriptor.deviceId) {
throw new ApiError('INVALID_MESSAGE', 400)
}
if (descriptor.role !== 'host' && descriptor.role !== 'client') {
throw new ApiError('INVALID_MESSAGE', 400)
}
if (typeof descriptor.identityKey !== 'string' || descriptor.identityKey.length !== 43) {
throw new ApiError('INVALID_MESSAGE', 400)
}
const old = this.state.devices[descriptor.deviceId]
if (old?.revoked) throw new ApiError('DEVICE_REVOKED', 403)
if (old && (old.descriptor.identityKey !== descriptor.identityKey || old.descriptor.role !== descriptor.role)) {
throw new ApiError('PEER_IDENTITY_MISMATCH', 409)
}
this.state.devices[descriptor.deviceId] = {
descriptor: {
deviceId: descriptor.deviceId,
name: String(descriptor.name || 'Device').slice(0, 128),
role: descriptor.role,
platform: String(descriptor.platform || 'unknown').slice(0, 64),
identityKey: descriptor.identityKey,
clientVersion: String(descriptor.clientVersion || '0.6.7').slice(0, 64),
...(descriptor.role === 'host' && descriptor.harnessVersion
? { harnessVersion: String(descriptor.harnessVersion).slice(0, 64) }
: {}),
},
revoked: false,
lastSeenAt: old?.lastSeenAt ?? 0,
}
this.invalidate(descriptor.deviceId)
return this.issue(descriptor.deviceId)
}
issue(deviceId, refreshTokenExpiresAt = Date.now() + 30 * 86400_000, previous) {
const tokens = { ...this.state.tokens }
const now = Date.now()
for (const [k, v] of Object.entries(tokens)) {
if (v.expires <= now) delete tokens[k]
}
const accessTokenExpiresAt = now + 60 * 60_000
const nonce = randomSecret()
const pair = previous
? deriveSuccessor(previous.token, nonce, deviceId)
: { accessToken: randomSecret(), refreshToken: randomSecret() }
if (previous) {
tokens[sha256Hex(previous.token)] = {
...previous.record,
used: true,
recovery: { nonce, accessExpires: accessTokenExpiresAt },
}
}
tokens[sha256Hex(pair.accessToken)] = { kind: 'access', deviceId, expires: accessTokenExpiresAt, used: false }
tokens[sha256Hex(pair.refreshToken)] = { kind: 'refresh', deviceId, expires: refreshTokenExpiresAt, used: false }
const before = this.state.tokens
this.state.tokens = tokens
try {
this.save()
} catch (err) {
this.state.tokens = before
throw err
}
return {
accessToken: pair.accessToken,
refreshToken: pair.refreshToken,
accessTokenExpiresAt,
refreshTokenExpiresAt,
}
}
authenticate(token) {
const rec = this.state.tokens[sha256Hex(token)]
if (!rec || rec.kind !== 'access') throw new ApiError('AUTH_INVALID', 401)
if (rec.expires <= Date.now()) throw new ApiError('TOKEN_EXPIRED', 401)
return this.get(rec.deviceId)
}
refresh(deviceId, token) {
this.get(deviceId)
const rec = this.state.tokens[sha256Hex(token)]
if (!rec || rec.kind !== 'refresh' || rec.deviceId !== deviceId) throw new ApiError('AUTH_INVALID', 401)
if (rec.expires <= Date.now()) throw new ApiError('TOKEN_EXPIRED', 401)
if (!rec.used) return this.issue(deviceId, rec.expires, { token, record: rec })
if (!rec.recovery) throw new ApiError('AUTH_INVALID', 401)
const pair = deriveSuccessor(token, rec.recovery.nonce, deviceId)
const next = this.state.tokens[sha256Hex(pair.refreshToken)]
if (!next || next.kind !== 'refresh' || next.deviceId !== deviceId || next.expires !== rec.expires) {
throw new ApiError('INTERNAL_ERROR', 503)
}
if (next.used) throw new ApiError('AUTH_INVALID', 401)
return {
...pair,
accessTokenExpiresAt: rec.recovery.accessExpires,
refreshTokenExpiresAt: next.expires,
}
}
invalidate(deviceId) {
for (const [k, v] of Object.entries(this.state.tokens)) {
if (v.deviceId === deviceId) delete this.state.tokens[k]
}
this.onInvalidate(deviceId)
this.save()
}
revoke(id) {
this.get(id).revoked = true
this.invalidate(id)
}
rename(id, name) {
this.get(id).descriptor.name = String(name).trim().slice(0, 80)
this.save()
}
touch(id, clientVersion, harnessVersion) {
const d = this.get(id)
d.lastSeenAt = Date.now()
if (clientVersion) d.descriptor.clientVersion = String(clientVersion).slice(0, 64)
if (d.descriptor.role === 'host' && harnessVersion) {
d.descriptor.harnessVersion = String(harnessVersion).slice(0, 64)
}
this.save()
}
}
// ---------------------------------------------------------------------------
// 4. Minimal RFC 6455 WebSocket Connection Wrapper (Zero External Deps)
// ---------------------------------------------------------------------------
const WS_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
class SimpleWebSocket {
constructor(socket, head) {
this.socket = socket
this.readyState = 1 // OPEN
this.handlers = { message: [], close: [], error: [] }
this.buffer = Buffer.alloc(0)
this.fragments = []
this.fragmentOpcode = 0
socket.setNoDelay(true)
socket.on('data', (chunk) => this._onData(chunk))
socket.on('error', (err) => this._emit('error', err))
socket.on('close', () => {
if (this.readyState !== 3) {
this.readyState = 3
this._emit('close', 1006, '')
}
})
if (head && head.length > 0) this._onData(head)
}
get bufferedAmount() {
return this.socket.writableLength || 0
}
on(event, fn) {
if (this.handlers[event]) this.handlers[event].push(fn)
}
_emit(event, ...args) {
for (const fn of this.handlers[event] || []) {
try {
fn(...args)
} catch (err) {
console.error('[ws handler error]', err)
}
}
}
_encodeFrame(opcode, payloadBuf) {
const len = payloadBuf.length
let header
if (len < 126) {
header = Buffer.alloc(2)
header[0] = 0x80 | opcode
header[1] = len
} else if (len < 65536) {
header = Buffer.alloc(4)
header[0] = 0x80 | opcode
header[1] = 126
header.writeUInt16BE(len, 2)
} else {
header = Buffer.alloc(10)
header[0] = 0x80 | opcode
header[1] = 127
header.writeBigUInt64BE(BigInt(len), 2)
}
return Buffer.concat([header, payloadBuf])
}
send(text) {
if (this.readyState !== 1) return
const payload = Buffer.from(text, 'utf8')
this.socket.write(this._encodeFrame(0x1, payload))
}
pong(payload = Buffer.alloc(0)) {
if (this.readyState !== 1) return
this.socket.write(this._encodeFrame(0xA, payload))
}
close(code = 1000, reason = '') {
if (this.readyState >= 2) return
this.readyState = 2
const reasonBuf = Buffer.from(reason, 'utf8').subarray(0, 123)
const payload = Buffer.alloc(2 + reasonBuf.length)
payload.writeUInt16BE(code, 0)
reasonBuf.copy(payload, 2)
try {
this.socket.write(this._encodeFrame(0x8, payload))
} catch {}
try {
this.socket.end()
} catch {}
}
terminate() {
this.readyState = 3
try {
this.socket.destroy()
} catch {}
}
_onData(chunk) {
this.buffer = this.buffer.length === 0 ? chunk : Buffer.concat([this.buffer, chunk])
while (this.buffer.length >= 2) {
const b0 = this.buffer[0]
const b1 = this.buffer[1]
const fin = (b0 & 0x80) !== 0
const opcode = b0 & 0x0f
const masked = (b1 & 0x80) !== 0
let payloadLen = b1 & 0x7f
let offset = 2
if (payloadLen === 126) {
if (this.buffer.length < 4) return
payloadLen = this.buffer.readUInt16BE(2)
offset = 4
} else if (payloadLen === 127) {
if (this.buffer.length < 10) return
const bigLen = this.buffer.readBigUInt64BE(2)
if (bigLen > BigInt(MAX_RELAY_FRAME_BYTES + 65536)) {
this.close(1009, 'FRAME_TOO_LARGE')
return
}
payloadLen = Number(bigLen)
offset = 10
}
if (payloadLen > MAX_RELAY_FRAME_BYTES + 65536) {
this.close(1009, 'FRAME_TOO_LARGE')
return
}
const maskLen = masked ? 4 : 0
const totalLen = offset + maskLen + payloadLen
if (this.buffer.length < totalLen) return
let payload
if (masked) {
const mask = this.buffer.subarray(offset, offset + 4)
payload = Buffer.allocUnsafe(payloadLen)
const start = offset + 4
for (let i = 0; i < payloadLen; i++) {
payload[i] = this.buffer[start + i] ^ mask[i & 3]
}
} else {
payload = Buffer.from(this.buffer.subarray(offset, offset + payloadLen))
}
this.buffer = this.buffer.subarray(totalLen)
if (opcode === 0x8) {
// Close frame
const code = payload.length >= 2 ? payload.readUInt16BE(0) : 1000
const reason = payload.length > 2 ? payload.subarray(2).toString('utf8') : ''
this.close(code, reason)
this.readyState = 3
this._emit('close', code, reason)
return
}
if (opcode === 0x9) {
// Ping -> Pong
this.pong(payload)
continue
}
if (opcode === 0xA) {
// Pong -> ignore
continue
}
if (opcode === 0x1 || opcode === 0x2) {
this.fragmentOpcode = opcode
this.fragments = [payload]
} else if (opcode === 0x0) {
this.fragments.push(payload)
} else {
this.close(1002, 'INVALID_OPCODE')
return
}
if (fin) {
const full = this.fragments.length === 1 ? this.fragments[0] : Buffer.concat(this.fragments)
const isBinary = this.fragmentOpcode === 0x2
this.fragments = []
this.fragmentOpcode = 0
this._emit('message', full, isBinary)
}
}
}
}
// ---------------------------------------------------------------------------
// 5. DSH Remote Control & Relay Gateway
// ---------------------------------------------------------------------------
class RelayGateway {
constructor(httpServer, store, options = {}) {
this.store = store
this.peers = new Map()
this.links = new Map()
this.sockets = new Set()
this.helloTimeoutMs = options.helloTimeoutMs ?? 10_000
this.heartbeatIntervalMs = options.heartbeatIntervalMs ?? 25_000
this.heartbeatDisconnectMs = options.heartbeatDisconnectMs ?? 75_000
this.turnEnabled = Boolean(options.turnEnabled)
this.closed = false
this.store.onInvalidate = (id) => this.disconnect(id, 'AUTH_INVALID')
httpServer.on('upgrade', (req, socket, head) => {
const path = (req.url || '').split('?')[0]
if (path !== '/ws/v1/connect') {
socket.end('HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\n')
return
}
const key = req.headers['sec-websocket-key']
if (!key) {
socket.end('HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n')
return
}
const accept = createHash('sha1').update(key + WS_GUID).digest('base64')
socket.write(
'HTTP/1.1 101 Switching Protocols\r\n' +
'Upgrade: websocket\r\n' +
'Connection: Upgrade\r\n' +
`Sec-WebSocket-Accept: ${accept}\r\n\r\n`
)
const ws = new SimpleWebSocket(socket, head)
this.sockets.add(ws)
this._accept(ws)
})
this.heartbeat = setInterval(() => {
if (this.closed) return
const now = Date.now()
for (const peer of this.peers.values()) {
if (now - peer.lastPong > this.heartbeatDisconnectMs) {
console.log(`[gateway] heartbeat timeout: ${peer.role} ${peer.id}`)
this.disconnect(peer.id, 'CONNECTION_FAILED')
continue
}
const nonce = randomUUID()
peer.pendingNonces.push(nonce)
if (peer.pendingNonces.length > 3) peer.pendingNonces.shift()
this.send(peer, createControlFrame('ping', { nonce }))
}
for (const link of this.links.values()) {
if (link.stage !== 'ready' && now - link.created > 30_000) {
this.dropLink(link, 'CONNECTION_FAILED')
}
}
}, this.heartbeatIntervalMs)
this.heartbeat.unref()
}
send(peer, frame) {
if (peer.ws.readyState !== 1) return
if (peer.ws.bufferedAmount > 4 * MAX_RELAY_FRAME_BYTES) {
this.disconnect(peer.id, 'SLOW_CONSUMER')
return
}
peer.ws.send(JSON.stringify(frame))
}
dropLink(link, code) {
this.links.delete(link.id)
for (const id of [link.host, link.client]) {
const p = this.peers.get(id)
if (p) this.send(p, createControlFrame('error', { code, message: code, connectionId: link.id, retryable: true }))
}
}
disconnect(id, code) {
const peer = this.peers.get(id)
this.peers.delete(id)
for (const link of [...this.links.values()]) {
if (link.host === id || link.client === id) this.dropLink(link, code)
}
if (!peer) return
console.log(`[gateway] peer disconnected: ${peer.role} ${id} (${code})`)
peer.ws.close(code === 'CONNECTION_REPLACED' ? 4003 : 4001, code)
}
authorizeIce(deviceId, connectionId) {
const link = this.links.get(connectionId)
if (!link) return false
if (link.host !== deviceId && link.client !== deviceId) return false
if (link.stage === 'pending') return false
return this.peers.has(link.host) && this.peers.has(link.client)
}
_negotiatedCapabilities(offered) {
const allowed = SUPPORTED_TRANSPORTS.filter((c) => c !== 'transport.turn' || this.turnEnabled)
const list = Array.isArray(offered) ? offered : []
return allowed.filter((c) => list.includes(c))
}
_accept(ws) {
let state = 'awaiting_hello'
let peer
const deadline = setTimeout(() => {
if (state !== 'awaiting_hello') return
state = 'closing'
ws.send(JSON.stringify(createControlFrame('error', {
code: 'HELLO_TIMEOUT',
message: 'Hello handshake timed out.',
retryable: true,
})))
ws.close(4001, 'HELLO_TIMEOUT')
}, this.helloTimeoutMs)
deadline.unref()
ws.on('close', () => {
clearTimeout(deadline)
this.sockets.delete(ws)
state = 'closed'
if (peer && this.peers.get(peer.id) === peer) {
this.disconnect(peer.id, 'CONNECTION_FAILED')
}
})
ws.on('message', (raw, isBinary) => {
if (state !== 'awaiting_hello' && state !== 'authenticated') return
let frame
try {
if (isBinary) throw new ApiError('INVALID_MESSAGE')
frame = JSON.parse(raw.toString('utf8'))
if (!frame || frame.v !== PROTOCOL_VERSION || typeof frame.type !== 'string') {
throw new ApiError('UNSUPPORTED_VERSION')
}
if (state === 'awaiting_hello') {
if (frame.type !== 'hello') throw new ApiError('AUTH_REQUIRED')
const p = frame.payload || {}
const device = this.store.authenticate(p.accessToken).descriptor
if (device.deviceId !== p.deviceId || device.role !== p.role) throw new ApiError('AUTH_INVALID')
if (!Array.isArray(p.protocols) || !p.protocols.includes(1)) throw new ApiError('UNSUPPORTED_VERSION')
clearTimeout(deadline)
state = 'authenticated'
this.disconnect(device.deviceId, 'CONNECTION_REPLACED')
const caps = this._negotiatedCapabilities(p.capabilities)
if (!caps.includes('transport.relay')) caps.push('transport.relay')
peer = {
ws,
id: device.deviceId,
role: device.role,
capabilities: caps,
version: p.clientVersion || device.clientVersion || '0.6.7',
lastPong: Date.now(),
pendingNonces: [],
}
this.peers.set(peer.id, peer)
this.store.touch(peer.id, p.clientVersion, p.harnessVersion)
console.log(`[gateway] ONLINE: ${peer.role} "${device.name}" (${peer.id})`)
const webrtcEnabled = caps.some((c) => c === 'transport.lan' || c === 'transport.p2p' || c === 'transport.turn')
this.send(peer, createControlFrame('hello.ack', {
protocol: 1,
serverVersion: 'self-hosted-antigravity/1.0.0',
connectionSessionId: randomUUID(),
heartbeatIntervalMs: this.heartbeatIntervalMs,
maxControlFrameBytes: MAX_CONTROL_FRAME_BYTES,
maxRelayFrameBytes: MAX_RELAY_FRAME_BYTES,
capabilities: caps,
webrtcEnabled,
webrtcFallbackTimeoutMs: webrtcEnabled && !this.turnEnabled ? 3_000 : 12_000,
}))
return
}
if (frame.type === 'hello') throw new ApiError('INVALID_MESSAGE')
if (!peer || this.peers.get(peer.id) !== peer) throw new ApiError('AUTH_INVALID')
this._handleFrame(peer, frame)
} catch (err) {
const code = err instanceof ApiError ? err.code : 'INVALID_MESSAGE'
const connId = frame?.payload?.connectionId
if (peer && typeof connId === 'string') {
const link = this.links.get(connId)
if (link && (link.host === peer.id || link.client === peer.id)) {
this.dropLink(link, code)
return
}
this.send(peer, createControlFrame('error', { code, message: code, connectionId: connId, retryable: false }))
return
}
state = 'closing'
clearTimeout(deadline)
ws.send(JSON.stringify(createControlFrame('error', { code, message: code, retryable: false })))
if (peer) this.disconnect(peer.id, code)
else ws.close(4001, code)
}
})
}
_handleFrame(peer, frame) {
const p = frame.payload || {}
if (frame.type === 'ping') {
this.send(peer, createControlFrame('pong', { nonce: p.nonce }))
return
}
if (frame.type === 'pong') {
if (typeof p.nonce === 'string') {
const idx = peer.pendingNonces.indexOf(p.nonce)
if (idx !== -1) {
peer.lastPong = Date.now()
peer.pendingNonces.splice(0, idx + 1)
}
}
return
}
if (frame.type === 'connect.request') {
if (peer.role !== 'client') throw new ApiError('MEMBERSHIP_REQUIRED')
const host = this.peers.get(p.hostDeviceId)
if (!host || host.role !== 'host') {
this.send(peer, createControlFrame('error', { code: 'HOST_OFFLINE', message: 'Host is offline.', retryable: true }))
return
}
this.store.get(host.id)
const reqTransports = Array.isArray(p.preferredTransports) ? p.preferredTransports : ['relay']
const preferredTransports = [...new Set(reqTransports)].filter(
(t) => peer.capabilities.includes(`transport.${t}`) && host.capabilities.includes(`transport.${t}`)
)
if (preferredTransports.length === 0) preferredTransports.push('relay')
for (const old of [...this.links.values()]) {
if (old.client === peer.id) this.dropLink(old, 'CONNECTION_REPLACED')
}
const link = {
id: randomUUID(),
host: host.id,
client: peer.id,
stage: 'pending',
created: Date.now(),
counters: new Map(),
preferredTransports,
}
this.links.set(link.id, link)
this.send(host, createControlFrame('connect.incoming', {
connectionId: link.id,
clientDeviceId: peer.id,
clientIdentityKey: this.store.get(peer.id).descriptor.identityKey,
authorization: 'account',
preferredTransports,
}))
return
}
const link = typeof p.connectionId === 'string' ? this.links.get(p.connectionId) : undefined
if (!link || (peer.id !== link.host && peer.id !== link.client)) throw new ApiError('CONNECTION_NOT_FOUND')
const otherId = peer.id === link.host ? link.client : link.host
const other = this.peers.get(otherId)
if (!other) throw new ApiError('CONNECTION_FAILED')
if (frame.type === 'connect.accepted' || frame.type === 'connect.rejected') {
if (peer.id !== link.host || link.stage !== 'pending') throw new ApiError('INVALID_MESSAGE')
if (frame.type === 'connect.rejected') this.links.delete(link.id)
else link.stage = 'accepted'
this.send(other, frame)
return
}
if (p.targetDeviceId !== otherId || link.stage === 'pending') throw new ApiError('CONNECTION_NOT_FOUND')
if (frame.type.startsWith('signal.')) {
if (link.stage !== 'ready' && link.preferredTransports.some((t) => t !== 'relay')) {
this.send(other, frame)
}
return
}
if (frame.type === 'transport.selected') {
if (peer.id !== link.client) return
if (TRANSPORT_NAMES.includes(p.transport)) {
this.send(other, frame)
}
return
}
if (frame.type === 'secure.handshake') {
if (p.step === 1 && peer.id === link.client && link.stage === 'accepted') link.stage = 'handshake'
else if (p.step === 2 && peer.id === link.host && link.stage === 'handshake') link.stage = 'ready'
else throw new ApiError('INVALID_MESSAGE')
this.send(other, frame)
return
}
if (frame.type === 'relay') {
const counter = p.counter
if (link.stage !== 'ready' || typeof counter !== 'number' || counter <= (link.counters.get(peer.id) ?? -1)) {
throw new ApiError('INVALID_MESSAGE')
}
link.counters.set(peer.id, counter)
this.send(other, frame)
return
}
throw new ApiError('INVALID_MESSAGE')
}
close() {
if (this.closed) return
this.closed = true
clearInterval(this.heartbeat)
this.peers.clear()
this.links.clear()
for (const ws of this.sockets) {
ws.close(SERVER_SHUTDOWN_CLOSE_CODE, SERVER_SHUTDOWN_CLOSE_REASON)
}
this.sockets.clear()
}
}
// ---------------------------------------------------------------------------
// 6. HTTP Server Factory
// ---------------------------------------------------------------------------
async function readJsonBody(req) {
const chunks = []
let len = 0
for await (const c of req) {
len += c.length
if (len > 32768) throw new ApiError('FRAME_TOO_LARGE', 413)
chunks.push(c)
}
try {
return JSON.parse(Buffer.concat(chunks).toString('utf8'))
} catch {
throw new ApiError('INVALID_MESSAGE', 400)
}
}
function sendJson(res, status, value) {
res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store' })
res.end(JSON.stringify(value))
}
export function createSelfHostedRelayServer(config) {
const account = String(config.account || '').trim()
const password = String(config.password || '')
if (!account || password.length < 12) {
throw new Error('DSH_SERVER_ACCOUNT and DSH_SERVER_PASSWORD (at least 12 characters) are required.')
}
const publicUrl = config.publicUrl || `http://localhost:${config.port || 8080}`
const dataFile = config.dataFile || join(__dirname, 'data', 'state.json')
const store = new RelayStore(dataFile, account, password)
const sessions = new Map()
const stunUrls = (config.stunUrls || '').split(',').map((s) => s.trim()).filter(Boolean)
function extractToken(req) {
const auth = req.headers.authorization
if (auth) return auth.startsWith('Bearer ') ? auth.slice(7) : ''
const m = /(?:^|;\s*)dsh_session=([A-Za-z0-9_-]+)/.exec(req.headers.cookie || '')
return m ? m[1] : ''
}
function requireAccountAuth(req) {
const key = sha256Hex(extractToken(req))
const exp = sessions.get(key) || 0
if (exp <= Date.now()) {
sessions.delete(key)
throw new ApiError('ACCOUNT_AUTH_REQUIRED', 401)
}
}
function requireDeviceAuth(req) {
const auth = req.headers.authorization || ''
if (!auth.startsWith('Bearer ')) throw new ApiError('AUTH_REQUIRED', 401)
return store.authenticate(auth.slice(7))
}
const profile = () => ({ account: store.account, profile: { displayName: store.account }, isAdmin: false })
const server = createServer({ requestTimeout: 15000, headersTimeout: 10000 }, (req, res) => {
void route(req, res).catch((err) => {
if (res.headersSent || res.destroyed) return
const e = err instanceof ApiError ? err : new ApiError('INTERNAL_ERROR', 500)
sendJson(res, e.status, {
error: {
code: e.code,
message: e.code,
requestId: randomUUID(),
retryable: e.status >= 500,
},
})
})
})
const gateway = new RelayGateway(server, store, {
heartbeatIntervalMs: config.heartbeatIntervalMs,
heartbeatDisconnectMs: config.heartbeatDisconnectMs,
})
function toDescriptor(saved) {
return {
...saved.descriptor,
membershipId: `account:${sha256Hex(store.account).slice(0, 16)}`,
online: gateway.peers.has(saved.descriptor.deviceId),
lastSeenAt: saved.lastSeenAt,
}
}
async function route(req, res) {
const parsedUrl = new URL(req.url || '/', 'http://localhost')
const path = parsedUrl.pathname
const method = req.method
if (method === 'GET' && (path === '/health' || path === '/healthz' || path === '/ready')) {
sendJson(res, 200, { status: 'ok', onlinePeers: gateway.peers.size })
return
}
if (method === 'GET' && path === '/') {
const hosts = store.list().filter((d) => d.descriptor.role === 'host').map(toDescriptor)
const html = `<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><title>DSH Self-Hosted Relay</title>
<style>body{font-family:system-ui,sans-serif;max-width:720px;margin:40px auto;padding:0 20px;background:#faf8f5;color:#1e293b}
.card{background:#fff;border:1px solid #e2e8f0;border-radius:12px;padding:20px;margin:16px 0}
.badge{display:inline-block;padding:2px 10px;border-radius:99px;font-size:12px;font-weight:600}
.on{background:#dcfce7;color:#166534}.off{background:#f1f5f9;color:#64748b}</style></head><body>
<h1>DSH Remote 自建中继服务器</h1>
<div class="card"><p><b>状态:</b>运行中 | <b>账号:</b>${store.account} | <b>公网/访问地址:</b>${publicUrl}</p>
<p>在手机 <b>dsh-go</b> 中选择「自建服务器」,填写本服务器地址与邮箱密码登录即可连接。</p></div>
<div class="card"><h3>已注册主机 (${hosts.length})</h3>${
hosts.length === 0
? '<p>暂无主机注册,请在 Antigravity 插件中填写本服务器地址并登录。</p>'
: hosts.map((h) => `<p><span class="badge ${h.online ? 'on' : 'off'}">${h.online ? '在线' : '离线'}</span> <b>${h.name}</b> (${h.platform}) — <code>${h.deviceId}</code></p>`).join('')
}</div></body></html>`
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
res.end(html)
return
}
if (method === 'POST' && path === '/api/v1/auth/login') {
const creds = await readJsonBody(req)
const email = String(creds?.email || '').trim()
const pass = String(creds?.password || '')
if (!email || !pass || !constEqual(email, store.account) || !constEqual(pass, password)) {
throw new ApiError('AUTH_INVALID', 401)
}
const value = randomSecret()
const expiresAt = Date.now() + 8 * 3600_000
sessions.set(sha256Hex(value), expiresAt)
res.setHeader('Set-Cookie', `dsh_session=${value}; Path=/; HttpOnly; SameSite=Lax; Max-Age=${8 * 3600}`)
sendJson(res, 200, { ...profile(), token: value, expiresAt })
return
}
if (method === 'GET' && path === '/api/v1/auth/me') {
requireAccountAuth(req)
sendJson(res, 200, profile())
return
}
if (method === 'POST' && path === '/api/v1/auth/logout') {
sessions.delete(sha256Hex(extractToken(req)))
res.setHeader('Set-Cookie', 'dsh_session=; Path=/; HttpOnly; Max-Age=0')
sendJson(res, 200, { status: 'ok' })
return
}
if (method === 'GET' && path === '/api/v1/account/devices') {
requireAccountAuth(req)
sendJson(res, 200, { items: store.list().map(toDescriptor), serverUrl: publicUrl, transport: 'webrtc' })
return
}
if (method === 'POST' && (path === '/api/v1/devices/register' || path === '/api/v1/devices/register-owned-role')) {
const source = path.endsWith('register-owned-role') ? requireDeviceAuth(req) : undefined
if (!source) requireAccountAuth(req)
const payload = await readJsonBody(req)
const device = payload?.device
if (!device) throw new ApiError('INVALID_MESSAGE', 400)
if (source && (source.descriptor.deviceId === device.deviceId || source.descriptor.role === device.role)) {
throw new ApiError('INVALID_MESSAGE', 409)
}
sendJson(res, 200, store.register(device))
return
}
if (method === 'POST' && path === '/api/v1/auth/refresh') {
const data = await readJsonBody(req)
sendJson(res, 200, store.refresh(String(data?.deviceId || ''), String(data?.refreshToken || '')))
return
}
if (method === 'DELETE' && path === '/api/v1/devices/self') {
store.revoke(requireDeviceAuth(req).descriptor.deviceId)
sendJson(res, 200, { status: 'revoked' })
return
}
if (method === 'GET' && path === '/api/v1/me') {
sendJson(res, 200, toDescriptor(requireDeviceAuth(req)))
return
}
if (method === 'GET' && path === '/api/v1/turn/credentials') {
const source = requireDeviceAuth(req)
const connectionId = parsedUrl.searchParams.get('connection_id') || ''
if (!connectionId || !gateway.authorizeIce(source.descriptor.deviceId, connectionId)) {
throw new ApiError('CONNECTION_NOT_FOUND', 404)
}
sendJson(res, 200, { iceServers: stunUrls.length > 0 ? [{ urls: stunUrls }] : [] })
return
}
if (method === 'GET' && path === '/api/v1/devices') {
const source = requireDeviceAuth(req)
if (source.descriptor.role !== 'client') throw new ApiError('MEMBERSHIP_REQUIRED', 403)
const items = store.list()
.filter((d) => d.descriptor.role === 'host')
.map((d) => {
const { identityKey: _ignored, ...rest } = toDescriptor(d)
return rest
})
sendJson(res, 200, { items, nextCursor: null })
return
}
const renameMatch = /^\/api\/v1\/devices\/([^/]+)\/name$/.exec(path)
if (method === 'POST' && renameMatch) {
requireAccountAuth(req)
const payload = await readJsonBody(req)
store.rename(decodeURIComponent(renameMatch[1]), payload?.name)
sendJson(res, 200, toDescriptor(store.get(decodeURIComponent(renameMatch[1]))))
return
}
const deviceMatch = /^\/api\/v1\/devices\/([^/]+)(\/presence)?$/.exec(path)
if (method === 'GET' && deviceMatch) {
const source = requireDeviceAuth(req)
const target = store.get(decodeURIComponent(deviceMatch[1]))
if (source.descriptor.role === target.descriptor.role) throw new ApiError('MEMBERSHIP_REQUIRED', 403)
const desc = toDescriptor(target)
sendJson(
res,
200,
deviceMatch[2] ? { deviceId: desc.deviceId, online: desc.online, lastSeenAt: desc.lastSeenAt } : desc
)
return
}
throw new ApiError('METHOD_NOT_FOUND', 404)
}
return {
server,
store,
gateway,
close: async () => {
gateway.close()
server.closeAllConnections?.()
await new Promise((resolveClose) => server.close(() => resolveClose()))
},
}
}
// ---------------------------------------------------------------------------
// 7. CLI Entry Point
// ---------------------------------------------------------------------------
const isMain = process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)
if (isMain) {
loadEnvFile(join(__dirname, '.env'))
const host = process.env.DSH_SERVER_HOST || '0.0.0.0'
const port = Number(process.env.PORT || process.env.DSH_SERVER_PORT || 8080)
const account = process.env.DSH_SERVER_ACCOUNT || 'admin@local.dsh'
const password = process.env.DSH_SERVER_PASSWORD || 'Antigravity2026!'
const publicUrl = process.env.DSH_SERVER_PUBLIC_URL
|| process.env.RENDER_EXTERNAL_URL
|| (process.env.SPACE_HOST ? `https://${process.env.SPACE_HOST}` : `http://127.0.0.1:${port}`)
const dataFile = process.env.DSH_SERVER_DATA_FILE
? resolve(__dirname, process.env.DSH_SERVER_DATA_FILE)
: (process.env.SPACE_ID ? '/tmp/dsh-state.json' : join(__dirname, 'data', 'state.json'))
const instance = createSelfHostedRelayServer({
account,
password,
publicUrl,
dataFile,
port,
stunUrls: process.env.DSH_SERVER_STUN_URLS || '',
})
instance.server.listen(port, host, () => {
console.log('====================================================')
console.log(' DSH Remote Self-Hosted Relay Server is RUNNING')
console.log(` Listen : http://${host}:${port}`)
console.log(` Account : ${account}`)
console.log(` Data : ${dataFile}`)
console.log('====================================================')
})
const shutdown = () => {
void instance.close().finally(() => process.exit(0))
}
process.on('SIGINT', shutdown)
process.on('SIGTERM', shutdown)
}
|