MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / defaultMakeSocksAgent

Function defaultMakeSocksAgent

packages/agent-core/src/utils/proxy.ts:219–253  ·  view source on GitHub ↗
({ proxy, noProxy })

Source from the content-addressed store, hash-verified

217 new EnvHttpProxyAgent({ httpProxy, httpsProxy, noProxy });
218
219const defaultMakeSocksAgent: ProxyAgentFactories['makeSocksAgent'] = ({ proxy, noProxy }) => {
220 // undici has no SOCKS support, so we drive a custom connector: tunnel the
221 // destination through the SOCKS proxy with the `socks` client, then hand the
222 // established socket back to undici's connector — which performs the TLS
223 // upgrade for https targets (reusing undici's ALPN/servername handling).
224 const directConnect = buildConnector({});
225 const bypass = makeNoProxyMatcher(noProxy);
226 const connect: typeof directConnect = (options, callback) => {
227 if (bypass(options.hostname, options.port)) {
228 directConnect(options, callback);
229 return;
230 }
231 void (async () => {
232 try {
233 const isTls = options.protocol === 'https:';
234 const port = Number(options.port) || (isTls ? 443 : 80);
235 const { socket } = await SocksClient.createConnection({
236 proxy: { host: proxy.host, port: proxy.port, type: proxy.type, userId: proxy.userId, password: proxy.password },
237 command: 'connect',
238 destination: { host: options.hostname, port },
239 });
240 if (isTls) {
241 // Upgrade the SOCKS socket to TLS via undici's own connector.
242 directConnect({ ...options, httpSocket: socket } as Parameters<typeof directConnect>[0], callback);
243 } else {
244 socket.setNoDelay(true);
245 callback(null, socket);
246 }
247 } catch (error) {
248 callback(error instanceof Error ? error : new Error(String(error)), null);
249 }
250 })();
251 };
252 return new Agent({ connect });
253};
254
255/**
256 * Build an undici dispatcher that routes outbound `fetch` through the

Callers

nothing calls this directly

Calls 1

makeNoProxyMatcherFunction · 0.85

Tested by

no test coverage detected