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
/**
* #4812 public-only guard: the SOCKS5 listener Chromium gets as its only proxy.
* Loopback echo servers stand in for destinations; the guard's reason hook
* counts one fixture port as public, so an allowed connection can complete
* without the internet while every other loopback port stays refused.
*
* Value: protects=the guard refuses non-public destinations before any byte reaches them, replies "not allowed", records the refusal, connects only to the address it checked (no second lookup, so DNS rebinding cannot swap it), falls back across checked addresses, and sends the checked address, not the name, to a SOCKS5 upstream;
* fails_when=the guard connects to the name instead of the checked address, resolves twice, stops recording refusals, or hands a SOCKS5 upstream the host name;
* why_new=socks-bridge.test.ts covers the authenticated bridge only, and the guard is new;
* seam=the reason hook allows one fixture port and a stub resolver supplies DNS answers; no_claim=Chromium's own traffic through the guard is public-only-e2e.test.ts
*/
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
import * as net from 'node:net';
import { SocksClient } from 'socks';
import { startAddressGuard } from '../src/address-guard';
import { addressRefusalReason, refusalsSince } from '../src/public-only';
import type { BridgeHandle } from '../src/socks-bridge';
interface Fixture { port: number; hits: number; close: () => Promise<void> }
async function echoServer(): Promise<Fixture> {
const fixture = { port: 0, hits: 0, close: async () => {} };
const server = net.createServer((sock) => { fixture.hits++; sock.on('data', (d) => sock.write(d)); sock.on('error', () => {}); });
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
fixture.port = (server.address() as net.AddressInfo).port;
fixture.close = () => new Promise<void>((resolve) => server.close(() => resolve()));
return fixture;
}
/** A no-auth SOCKS5 upstream that records each CONNECT destination and relays it. */
async function recordingUpstream(): Promise<{ port: number; destinations: string[]; close: () => Promise<void> }> {
const destinations: string[] = [];
const server = net.createServer((sock) => {
sock.on('error', () => {});
sock.once('data', () => {
sock.write(Buffer.from([0x05, 0x00]));
sock.once('data', (req: Buffer) => {
const atyp = req[3];
const host = atyp === 0x01 ? [...req.subarray(4, 8)].join('.') : req.subarray(5, 5 + req[4]).toString();
const port = req.readUInt16BE(req.length - 2);
destinations.push(`${atyp === 0x01 ? 'ip' : 'name'}:${host}:${port}`);
const out = net.connect({ host: atyp === 0x01 ? host : '127.0.0.1', port }, () => {
sock.write(Buffer.from([0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0]));
sock.pipe(out); out.pipe(sock);
});
out.on('error', () => sock.destroy());
});
});
});
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
return { port: (server.address() as net.AddressInfo).port, destinations, close: () => new Promise<void>((resolve) => server.close(() => resolve())) };
}
async function through(guard: BridgeHandle, host: string, port: number): Promise<string> {
const { socket } = await SocksClient.createConnection({ proxy: { host: '127.0.0.1', port: guard.port, type: 5 }, command: 'connect', destination: { host, port }, timeout: 5000 });
socket.write('ping');
const reply = await new Promise<string>((resolve) => socket.once('data', (d) => resolve(d.toString())));
socket.destroy();
return reply;
}
let publicFixture: Fixture;
let privateFixture: Fixture;
const lookups: string[] = [];
const answers: Record<string, string[]> = {};
const opts = () => ({
resolver: async (host: string) => { lookups.push(host); if (!answers[host]) throw new Error('ENOTFOUND'); return answers[host]; },
reason: (address: string, port: number) => (port === publicFixture.port && address === '127.0.0.1' ? null : addressRefusalReason(address)),
});
beforeAll(async () => { publicFixture = await echoServer(); privateFixture = await echoServer(); });
afterAll(async () => { await publicFixture.close(); await privateFixture.close(); });
describe('startAddressGuard', () => {
test('relays an allowed destination and refuses a non-public one before it is reached', async () => {
const guard = await startAddressGuard(opts());
try {
expect(await through(guard, '127.0.0.1', publicFixture.port)).toBe('ping');
const since = Date.now();
await expect(through(guard, '127.0.0.1', privateFixture.port)).rejects.toThrow(/not allowed|ruleset|Socks5 proxy rejected/i);
await expect(through(guard, 'localhost', privateFixture.port)).rejects.toThrow();
expect(privateFixture.hits).toBe(0);
expect(refusalsSince(since).map(r => r.target)).toEqual([`127.0.0.1:${privateFixture.port}`, `localhost:${privateFixture.port}`]);
} finally { await guard.close(); }
});
test('connects to the checked address with one lookup, so a changed DNS answer cannot redirect it', async () => {
const guard = await startAddressGuard(opts());
try {
answers['pinned.test'] = ['127.0.0.1'];
lookups.length = 0;
expect(await through(guard, 'pinned.test', publicFixture.port)).toBe('ping');
expect(lookups).toEqual(['pinned.test']);
answers['mixed.test'] = ['127.0.0.1', '10.0.0.5'];
await expect(through(guard, 'mixed.test', publicFixture.port)).rejects.toThrow();
await expect(through(guard, 'nxdomain.test', publicFixture.port)).rejects.toThrow();
} finally { await guard.close(); }
});
test('tries each checked address in turn', async () => {
const guard = await startAddressGuard({ ...opts(), reason: () => null, connectTimeoutMs: 2000 });
try {
answers['fallback.test'] = ['127.0.0.2', '127.0.0.1'];
const closed = await echoServer();
const port = closed.port;
await closed.close();
answers['dead.test'] = ['127.0.0.1'];
await expect(through(guard, 'dead.test', port)).rejects.toThrow();
expect(await through(guard, 'fallback.test', publicFixture.port)).toBe('ping');
} finally { await guard.close(); }
});
test('a SOCKS5 upstream receives the checked address, never the name', async () => {
const upstream = await recordingUpstream();
const guard = await startAddressGuard({ ...opts(), upstream: { host: '127.0.0.1', port: upstream.port } });
try {
answers['chained.test'] = ['127.0.0.1'];
expect(await through(guard, 'chained.test', publicFixture.port)).toBe('ping');
expect(upstream.destinations).toEqual([`ip:127.0.0.1:${publicFixture.port}`]);
await expect(through(guard, '127.0.0.1', privateFixture.port)).rejects.toThrow();
expect(upstream.destinations).toHaveLength(1);
} finally { await guard.close(); await upstream.close(); }
});
});