Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

12.4 KB
Raw Blame
import dns from 'node:dns';
import { createServer as createHttpServer } from 'node:http';
import { afterEach, describe, expect, it, vi } from 'vitest';

import { createAnthropicCompatProxy } from './server.js';
import { listenOnAvailableLoopbackPort } from './test-loopback-server.js';
import { startSocks5Stub } from './test-socks5-stub.js';
import type { ProxyHandle } from './types.js';

/**
 * 出站代理链路的 server 级验证。代理路径用保证不可解析的 `.invalid` 假域 ——
 * 经代理转发时压根不需要解析上游域名(http 走绝对形式、https 由代理端拨号),
 * 因此「请求打到了 mini 代理」本身就证明代理路径生效;直连回退则使用本机测试
 * 上游,确定性验证 fail-open 后请求确实抵达直连目标。
 */
describe('anthropic-compat-proxy outbound proxy wiring', () => {
  let proxy: ProxyHandle | null = null;
  const cleanups: Array<() => Promise<void> | void> = [];

  afterEach(async () => {
    if (proxy) { await proxy.dispose(); proxy = null; }
    while (cleanups.length) await cleanups.pop()!();
  });

  it('forwards http upstreams via absolute-form request to the outbound proxy', async () => {
    const seen: Array<{ url: string; host?: string; proxyAuth?: string }> = [];
    const miniProxy = createHttpServer((req, res) => {
      seen.push({
        url: req.url ?? '',
        host: req.headers.host,
        proxyAuth: req.headers['proxy-authorization'] as string | undefined,
      });
      res.writeHead(200, { 'content-type': 'application/json' });
      res.end(JSON.stringify({ via: 'outbound-proxy' }));
    });
    const miniProxyPort = await listenOnAvailableLoopbackPort(miniProxy);
    cleanups.push(() => new Promise<void>((r) => miniProxy.close(() => r())));

    proxy = await createAnthropicCompatProxy({
      upstream: 'http://upstream.invalid:8080/v1',
      transformRequest: [],
      resolveOutboundProxy: () => `http://user:[email protected]:${miniProxyPort}`,
    });

    const res = await fetch(`${proxy.url}/messages`, {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify({ model: 'x', messages: [] }),
    });
    expect(res.status).toBe(200);
    expect(await res.json()).toEqual({ via: 'outbound-proxy' });

    expect(seen).toHaveLength(1);
    // 绝对形式 URL 指向真实上游;Host 头按 RFC 9110 带非默认端口;凭证进 Proxy-Authorization。
    expect(seen[0].url).toBe('http://upstream.invalid:8080/v1/messages');
    expect(seen[0].host).toBe('upstream.invalid:8080');
    expect(seen[0].proxyAuth).toBe(`Basic ${Buffer.from('user:secret').toString('base64')}`);
  });

  it('routes https upstreams through a CONNECT tunnel to the outbound proxy', async () => {
    const connects: string[] = [];
    const miniProxy = createHttpServer();
    miniProxy.on('connect', (req, clientSocket) => {
      connects.push(req.url ?? '');
      // 拒绝隧道:断言只关心「CONNECT 打到了代理」;成功隧道路径由
      // outbound-proxy.test.ts 的 TunnelingHttpsAgent 用例覆盖。
      clientSocket.end('HTTP/1.1 502 Bad Gateway\r\n\r\n');
    });
    const miniProxyPort = await listenOnAvailableLoopbackPort(miniProxy);
    cleanups.push(() => new Promise<void>((r) => miniProxy.close(() => r())));

    proxy = await createAnthropicCompatProxy({
      upstream: 'https://upstream.invalid',
      transformRequest: [],
      resolveOutboundProxy: () => `http://127.0.0.1:${miniProxyPort}`,
    });

    const res = await fetch(`${proxy.url}/v1/messages`, {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify({ model: 'x', messages: [] }),
    });
    expect(res.status).toBe(502);
    const body = await res.json() as { error: { message: string } };
    expect(body.error.message).toContain('upstream unreachable');
    expect(body.error.message).toContain('CONNECT');
    expect(connects).toEqual(['upstream.invalid:443']);
  });

  it('classifies a closed loopback HTTP CONNECT proxy port as upstream_loopback_refused (#4100)', async () => {
    // 先拿一个刚释放的回环端口:代理进程 / SSH 隧道没起来的形态。
    const placeholder = createHttpServer();
    const closedPort = await listenOnAvailableLoopbackPort(placeholder);
    await new Promise<void>((r) => placeholder.close(() => r()));

    proxy = await createAnthropicCompatProxy({
      upstream: 'https://upstream.invalid',
      transformRequest: [],
      resolveOutboundProxy: () => `http://127.0.0.1:${closedPort}`,
    });

    const res = await fetch(`${proxy.url}/v1/messages`, {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify({ model: 'x', messages: [] }),
    });
    expect(res.status).toBe(502);
    const body = await res.json() as { error: { code?: string; message: string } };
    expect(body.error.code).toBe('upstream_loopback_refused');
    expect(body.error.message).toContain(`outbound proxy at 127.0.0.1:${closedPort}`);
    expect(body.error.message).toContain('nothing is listening');
  });

  it('classifies a closed loopback SOCKS5 proxy port as upstream_loopback_refused (#4100)', async () => {
    const placeholder = createHttpServer();
    const closedPort = await listenOnAvailableLoopbackPort(placeholder);
    await new Promise<void>((r) => placeholder.close(() => r()));

    proxy = await createAnthropicCompatProxy({
      upstream: 'http://upstream.invalid:8080/v1',
      transformRequest: [],
      resolveOutboundProxy: () => `socks5://127.0.0.1:${closedPort}`,
    });

    const res = await fetch(`${proxy.url}/messages`, {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify({ model: 'x', messages: [] }),
    });
    expect(res.status).toBe(502);
    const body = await res.json() as { error: { code?: string; message: string } };
    expect(body.error.code).toBe('upstream_loopback_refused');
    expect(body.error.message).toContain(`outbound proxy at 127.0.0.1:${closedPort}`);
  });

  it('tunnels upstreams through SOCKS5 and hands the domain to the proxy unresolved', async () => {
    const seen: Array<{ url: string; host?: string }> = [];
    const upstream = createHttpServer((req, res) => {
      seen.push({ url: req.url ?? '', host: req.headers.host });
      res.writeHead(200, { 'content-type': 'application/json' });
      res.end(JSON.stringify({ via: 'socks5' }));
    });
    const upstreamPort = await listenOnAvailableLoopbackPort(upstream);
    cleanups.push(() => new Promise<void>((r) => upstream.close(() => r())));
    const stub = await startSocks5Stub({ tunnelToPort: upstreamPort });
    cleanups.push(() => stub.close());

    proxy = await createAnthropicCompatProxy({
      // 上游用保证不可解析的 .invalid 假域:请求能成功本身就证明域名没有在本地解析,
      // 而是原样交给了代理(本 feature 修的正是 getaddrinfo ENOTFOUND 那条链路)。
      upstream: 'http://upstream.invalid:8080/v1',
      transformRequest: [],
      resolveOutboundProxy: () => `socks5://127.0.0.1:${stub.port}`,
    });

    const res = await fetch(`${proxy.url}/messages`, {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify({ model: 'x', messages: [] }),
    });
    expect(res.status).toBe(200);
    expect(await res.json()).toEqual({ via: 'socks5' });

    expect(stub.requests).toEqual([{ atyp: 0x03, host: 'upstream.invalid', port: 8080 }]);
    // L4 隧道:请求仍是 origin-form,Host 与**直连**时逐字节一致(转发层设的
    // `host: target.hostname`),没有 HTTP 代理那套绝对形式 + Host 重写。
    expect(seen).toEqual([{ url: '/v1/messages', host: 'upstream.invalid' }]);
  });

  it('encodes IPv6 literal upstreams as ATYP=0x04 through the whole forward path', async () => {
    // 回归:parseUpstream 保留 WHATWG hostname 的方括号,并一路传到 agent 的
    // options.host。桩直接拒绝 CONNECT(0x05),用例只关心送出去的目标编码。
    const stub = await startSocks5Stub({ replyCode: 0x05 });
    cleanups.push(() => stub.close());

    proxy = await createAnthropicCompatProxy({
      upstream: 'http://[2001:db8::1]:8080',
      transformRequest: [],
      resolveOutboundProxy: () => `socks5://127.0.0.1:${stub.port}`,
    });

    const res = await fetch(`${proxy.url}/v1/messages`, {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify({ model: 'x', messages: [] }),
    });
    expect(res.status).toBe(502);
    expect(stub.requests).toEqual([{ atyp: 0x04, host: '2001:db8:0:0:0:0:0:1', port: 8080 }]);
  });

  it('never consults the resolver for loopback upstreams', async () => {
    const upstream = createHttpServer((_req, res) => {
      res.writeHead(200, { 'content-type': 'application/json' });
      res.end(JSON.stringify({ direct: true }));
    });
    const upstreamPort = await listenOnAvailableLoopbackPort(upstream);
    cleanups.push(() => new Promise<void>((r) => upstream.close(() => r())));

    const resolver = vi.fn(() => 'http://127.0.0.1:1');
    proxy = await createAnthropicCompatProxy({
      upstream: `http://127.0.0.1:${upstreamPort}`,
      transformRequest: [],
      resolveOutboundProxy: resolver,
    });

    const res = await fetch(`${proxy.url}/v1/messages`, {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify({ model: 'x', messages: [] }),
    });
    expect(res.status).toBe(200);
    expect(resolver).not.toHaveBeenCalled();
  });

  it('falls back to direct connection when the resolver throws or returns unsupported urls', async () => {
    // 本用例是文件里唯一真的对 .invalid 假域做**直连**(前面的用例域名都原样交给
    // 代理桩,不走本地解析)。macOS 上 NXDOMAIN 即时返回,但 Windows 的解析器会
    // 带着 DNS 搜索后缀逐个重查,轻松拖过测试超时。桩掉 .invalid 的 lookup 让它
    // 立即 ENOTFOUND —— 仍走真实 net.connect 失败路径,只是把结果变成确定性的;
    // 其余域名(本文件只有 IP 字面量,压根不进 lookup)原样放行。
    const realLookup = dns.lookup;
    vi.spyOn(dns, 'lookup').mockImplementation(((hostname: string, options: unknown, callback?: unknown) => {
      const cb = (typeof options === 'function' ? options : callback) as (err: NodeJS.ErrnoException | null) => void;
      if (!hostname.endsWith('.invalid')) {
        return (realLookup as (...args: unknown[]) => unknown)(hostname, options, callback);
      }
      const err: NodeJS.ErrnoException = Object.assign(
        new Error(`getaddrinfo ENOTFOUND ${hostname}`),
        { code: 'ENOTFOUND', syscall: 'getaddrinfo', hostname },
      );
      queueMicrotask(() => cb(err));
    }) as typeof dns.lookup);
    cleanups.push(() => { vi.restoreAllMocks(); });

    const warns: string[] = [];
    proxy = await createAnthropicCompatProxy({
      upstream: 'http://upstream.invalid:8080',
      transformRequest: [],
      resolveOutboundProxy: () => { throw new Error('resolver boom'); },
      logger: { warn: (msg) => { warns.push(msg); } },
    });

    // 直连假域必然失败,但必须是上游连接的 502,而非 resolver 异常炸掉请求链路。
    const res = await fetch(`${proxy.url}/v1/messages`, {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify({ model: 'x', messages: [] }),
    });
    expect(res.status).toBe(502);
    expect((await res.json() as { error: { message: string } }).error.message).toContain('upstream unreachable');
    expect(warns.some((m) => m.includes('outbound proxy resolver threw'))).toBe(true);

    await proxy.dispose();

    const warns2: string[] = [];
    proxy = await createAnthropicCompatProxy({
      upstream: 'http://upstream.invalid:8080',
      transformRequest: [],
      // socks4 仍不支持(无认证、无 IPv6),按不支持的形态回落直连。
      resolveOutboundProxy: () => 'socks4://127.0.0.1:1080',
      logger: { warn: (msg) => { warns2.push(msg); } },
    });
    const res2 = await fetch(`${proxy.url}/v1/messages`, {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify({ model: 'x', messages: [] }),
    });
    expect(res2.status).toBe(502);
    expect((await res2.json() as { error: { message: string } }).error.message).toContain('upstream unreachable');
    expect(warns2.some((m) => m.includes('unsupported outbound proxy url'))).toBe(true);
  });
});