Skip to content

Use Garry Tan's exact Claude Code setup: 23 opinionated tools that serve as CEO, Designer, Eng Manager, Release Manager, Doc Engineer, and QA

TypeScript
2.8 KB
Raw Blame
/**
 * The public-only guard (#4812): a no-auth SOCKS5 listener on 127.0.0.1 that
 * Chromium gets as its only proxy, with no bypass list, when public-only mode
 * is on. Chromium hands SOCKS5 the host name (remote DNS), so every connection
 * arrives here: navigations, redirects, subresources, fetch, WebSocket,
 * workers, downloads, and Playwright's page.request.
 *
 * For each CONNECT the guard checks the destination (public-only.ts
 * checkDestination: the literal and every DNS answer must be public and off
 * this machine's own networks) and then connects to one of the addresses it
 * checked, never to the name, so a DNS answer that changes between the check
 * and the connection (DNS rebinding) cannot redirect it. With a SOCKS5
 * upstream the checked address, not the name, goes to the upstream.
 */

import * as net from 'node:net';
import { SocksClient } from 'socks';
import { buildUpstream, serveSocks5, SocksRefusal, REPLY_NOT_ALLOWED, UPSTREAM_CONNECT_TIMEOUT_MS, type BridgeHandle, type UpstreamConfig } from './socks-bridge';
import { checkDestination, recordRefusal, type CheckOptions } from './public-only';

export interface GuardOptions extends CheckOptions {
  /** Chain allowed connections through this SOCKS5 upstream instead of connecting directly. */
  upstream?: UpstreamConfig;
  port?: number;
  /** Per-address connect timeout. */
  connectTimeoutMs?: number;
}

function connectDirect(host: string, port: number, timeoutMs: number): Promise<net.Socket> {
  return new Promise((resolve, reject) => {
    const socket = net.connect({ host, port });
    const timer = setTimeout(() => { socket.destroy(); reject(new Error('connect timeout')); }, timeoutMs);
    socket.once('connect', () => { clearTimeout(timer); resolve(socket); });
    socket.once('error', (err) => { clearTimeout(timer); reject(err); });
  });
}

export async function startAddressGuard(opts: GuardOptions = {}): Promise<BridgeHandle> {
  const upstream = opts.upstream ? buildUpstream(opts.upstream) : null;
  const timeoutMs = opts.connectTimeoutMs ?? (upstream ? UPSTREAM_CONNECT_TIMEOUT_MS : 10_000);
  return serveSocks5(async ({ host, port }) => {
    const checked = await checkDestination(host, port, opts);
    const target = host.includes(':') ? `[${host}]:${port}` : `${host}:${port}`;
    if ('reason' in checked) {
      recordRefusal(target, checked.reason);
      throw new SocksRefusal(REPLY_NOT_ALLOWED, checked.reason);
    }
    let lastError: unknown;
    for (const address of checked.addresses) {
      try {
        if (!upstream) return await connectDirect(address, port, timeoutMs);
        const result = await SocksClient.createConnection({ proxy: upstream, command: 'connect', destination: { host: address, port }, timeout: timeoutMs });
        return result.socket;
      } catch (err) {
        lastError = err;
      }
    }
    throw lastError;
  }, opts.port ?? 0);
}