arblant
BlueKnight-Panel-Marvel / railway / lib / ws-node.mjs
ws-node.mjs246 lines · 8.1 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 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
// Minimal RFC 6455 WebSocket server for Node hosts.
//
// Cloudflare answers a proxy upgrade with `new Response(null, {status: 101,
// webSocket})`, which is a workerd-only extension: Node's Response rejects 101
// outright and its http server never performs the handshake. Without this the
// VLESS/Trojan tunnel is dead on every non-Cloudflare deployment.
//
// Only what the proxy path needs: binary messages, fragmentation, ping/pong and
// close. No extensions, no permessage-deflate, no subprotocol negotiation.
import { createHash } from 'node:crypto';
import { EventEmitter } from 'node:events';

const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
const MAX_MESSAGE_BYTES = 64 * 1024 * 1024; // guard against a hostile length header

export function acceptKey(clientKey) {
  return createHash('sha1').update(clientKey + GUID).digest('base64');
}

function toBuffer(data) {
  if (Buffer.isBuffer(data)) return data;
  if (typeof data === 'string') return Buffer.from(data);
  if (ArrayBuffer.isView(data)) return Buffer.from(data.buffer, data.byteOffset, data.byteLength);
  return Buffer.from(data);
}

/** Encode one server->client frame. Server frames are never masked. */
export function encodeFrame(payload, opcode = 0x2) {
  const data = toBuffer(payload);
  const len = data.length;
  let header;
  if (len < 126) {
    header = Buffer.alloc(2);
    header[1] = len;
  } else if (len < 65536) {
    header = Buffer.alloc(4);
    header[1] = 126;
    header.writeUInt16BE(len, 2);
  } else {
    header = Buffer.alloc(10);
    header[1] = 127;
    header.writeBigUInt64BE(BigInt(len), 2);
  }
  header[0] = 0x80 | opcode; // FIN + opcode
  return Buffer.concat([header, data]);
}

/**
 * Incremental frame decoder. TCP gives arbitrary chunk boundaries, so every
 * field has to survive being split mid-header.
 */
export class FrameParser {
  constructor(handlers) {
    this.buf = Buffer.alloc(0);
    this.handlers = handlers; // { onMessage, onClose, onPing, onPong, onError }
    this.fragments = [];
    this.fragmentOpcode = null;
  }

  push(chunk) {
    this.buf = this.buf.length === 0 ? chunk : Buffer.concat([this.buf, chunk]);
    // A single TCP read can carry many frames; drain until one is incomplete.
    while (this.step()) { /* keep draining */ }
  }

  step() {
    const b = this.buf;
    if (b.length < 2) return false;

    const fin = (b[0] & 0x80) !== 0;
    const opcode = b[0] & 0x0f;
    const masked = (b[1] & 0x80) !== 0;
    let len = b[1] & 0x7f;
    let offset = 2;

    if (len === 126) {
      if (b.length < offset + 2) return false;
      len = b.readUInt16BE(offset);
      offset += 2;
    } else if (len === 127) {
      if (b.length < offset + 8) return false;
      const big = b.readBigUInt64BE(offset);
      if (big > BigInt(MAX_MESSAGE_BYTES)) {
        this.handlers.onError?.(new Error('WebSocket frame exceeds size limit'));
        return false;
      }
      len = Number(big);
      offset += 8;
    }

    let mask = null;
    if (masked) {
      if (b.length < offset + 4) return false;
      mask = b.subarray(offset, offset + 4);
      offset += 4;
    }

    if (b.length < offset + len) return false; // frame not fully arrived yet

    const payload = Buffer.from(b.subarray(offset, offset + len)); // copy: buf gets reused
    if (mask) {
      for (let i = 0; i < payload.length; i++) payload[i] ^= mask[i & 3];
    }
    this.buf = b.subarray(offset + len);

    // Control frames may be interleaved between fragments of a data message.
    if (opcode === 0x8) { this.handlers.onClose?.(payload); return false; }
    if (opcode === 0x9) { this.handlers.onPing?.(payload); return true; }
    if (opcode === 0xa) { this.handlers.onPong?.(payload); return true; }

    if (opcode === 0x0) {
      if (this.fragmentOpcode === null) {
        this.handlers.onError?.(new Error('Continuation frame with nothing to continue'));
        return false;
      }
      this.fragments.push(payload);
    } else if (!fin) {
      this.fragmentOpcode = opcode;
      this.fragments = [payload];
      return true;
    } else {
      this.handlers.onMessage?.(payload, opcode);
      return true;
    }

    if (fin) {
      const full = Buffer.concat(this.fragments);
      const op = this.fragmentOpcode;
      this.fragments = [];
      this.fragmentOpcode = null;
      this.handlers.onMessage?.(full, op);
    }
    return true;
  }
}

/**
 * One end of a WebSocketPair. Mirrors the Cloudflare API surface the worker
 * uses: accept/send/close/addEventListener.
 */
class PairSocket extends EventEmitter {
  constructor() {
    super();
    this.readyState = 1; // OPEN
    this.peer = null;
    this.setMaxListeners(0);
  }
  accept() { /* workerd requires this call; nothing to do on Node */ }
  send(data) {
    if (this.readyState !== 1 || !this.peer || this.peer.readyState !== 1) return;
    this.peer.emit('message', { data });
  }
  close(code = 1000, reason = '') {
    if (this.readyState === 3) return;
    this.readyState = 3;
    if (this.peer && this.peer.readyState !== 3) {
      this.peer.readyState = 3;
      this.peer.emit('close', { code, reason });
    }
    this.emit('close', { code, reason });
  }
  addEventListener(type, fn) { this.on(type, fn); }
  removeEventListener(type, fn) { this.off(type, fn); }
}

export class WebSocketPair {
  constructor() {
    const client = new PairSocket();
    const server = new PairSocket();
    client.peer = server;
    server.peer = client;
    this[0] = client;
    this[1] = server;
  }
}

function closeFrame(code, reason) {
  const head = Buffer.alloc(2);
  head.writeUInt16BE(code);
  return encodeFrame(Buffer.concat([head, Buffer.from(String(reason))]), 0x8);
}

/**
 * Complete the handshake on a raw upgrade socket and pump frames between it and
 * `clientWs` (the endpoint the worker handed back in its 101 response).
 */
export function attachSocket(socket, head, clientWs, reqHeaders = {}) {
  const key = reqHeaders['sec-websocket-key'];
  const lines = [
    'HTTP/1.1 101 Switching Protocols',
    'Upgrade: websocket',
    'Connection: Upgrade',
    'Sec-WebSocket-Accept: ' + acceptKey(key)
  ];
  if (reqHeaders['sec-websocket-protocol']) {
    lines.push('Sec-WebSocket-Protocol: ' + reqHeaders['sec-websocket-protocol']);
  }
  socket.write(lines.join('\r\n') + '\r\n\r\n');
  socket.setNoDelay(true);

  let closed = false;
  const shutdown = (code = 1000, reason = '') => {
    if (closed) return;
    closed = true;
    try { socket.write(closeFrame(code, reason)); } catch {}
    try { socket.end(); } catch {}
    try { clientWs.close(code, reason); } catch {}
  };

  const parser = new FrameParser({
    onMessage: (payload) => { try { clientWs.send(payload); } catch {} },
    onPing: (payload) => { try { socket.write(encodeFrame(payload, 0xa)); } catch {} },
    onClose: () => shutdown(1000, 'Client Closed'),
    onError: (err) => shutdown(1002, String(err.message).slice(0, 100))
  });

  // Bytes the http parser had already read past the request headers.
  if (head && head.length) parser.push(head);
  socket.on('data', (chunk) => {
    try { parser.push(chunk); } catch { shutdown(1011, 'Parse Error'); }
  });
  socket.on('error', () => { closed = true; try { clientWs.close(1006, 'Socket Error'); } catch {} });
  socket.on('end', () => shutdown(1000, 'Client Disconnected'));
  socket.on('close', () => { closed = true; try { clientWs.close(1006, 'Socket Closed'); } catch {} });

  // Worker -> client
  clientWs.addEventListener('message', (event) => {
    if (closed) return;
    try { socket.write(encodeFrame(toBuffer(event.data), 0x2)); } catch {}
  });
  clientWs.addEventListener('close', (e) => shutdown(e?.code ?? 1000, e?.reason ?? ''));
}

/** Cloudflare returns status 101 with a `webSocket`; Node's Response forbids both. */
export class WorkerResponse extends Response {
  constructor(body, init = {}) {
    if (init && init.status === 101) {
      super(null, { ...init, status: 200, statusText: undefined });
      Object.defineProperty(this, 'status', { value: 101, enumerable: true });
      Object.defineProperty(this, 'webSocket', { value: init.webSocket, enumerable: true });
    } else {
      super(body, init);
      Object.defineProperty(this, 'webSocket', { value: init?.webSocket ?? null, enumerable: true });
    }
  }
}