icebear0828 Claude Opus 4.6 commited on
Commit
d7d7389
·
1 Parent(s): 059677f

fix: Docker container auto-detect host proxy via host.docker.internal

Browse files

Docker containers can't reach host proxies on 127.0.0.1 (container loopback).
Now probes both 127.0.0.1 and host.docker.internal so Docker users get
automatic proxy detection without any configuration.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Files changed (3) hide show
  1. CHANGELOG.md +1 -0
  2. docker-compose.yml +2 -0
  3. src/tls/curl-binary.ts +19 -9
CHANGELOG.md CHANGED
@@ -36,6 +36,7 @@
36
  - `ClaudeCodeSetup.tsx` 文件名与导出名不一致,重命名为 `AnthropicSetup.tsx`
37
  - Dashboard 模型偏好从硬编码 `gpt-5.2-codex` 改为使用 `codex` 别名
38
  - 构建脚本 `vite build --root web` 兼容性问题,改用 `npm run build:web`
 
39
 
40
  ## [v0.8.0](https://github.com/icebear0828/codex-proxy/releases/tag/v0.8.0) - 2026-02-24
41
 
 
36
  - `ClaudeCodeSetup.tsx` 文件名与导出名不一致,重命名为 `AnthropicSetup.tsx`
37
  - Dashboard 模型偏好从硬编码 `gpt-5.2-codex` 改为使用 `codex` 别名
38
  - 构建脚本 `vite build --root web` 兼容性问题,改用 `npm run build:web`
39
+ - Docker 容器内代理自动检测失败:`detectLocalProxy()` 现在同时探测 `127.0.0.1`(裸机)和 `host.docker.internal`(Docker 容器→宿主机),零配置即生效
40
 
41
  ## [v0.8.0](https://github.com/icebear0828/codex-proxy/releases/tag/v0.8.0) - 2026-02-24
42
 
docker-compose.yml CHANGED
@@ -1,6 +1,8 @@
1
  services:
2
  codex-proxy:
3
  build: .
 
 
4
  ports:
5
  - "${PORT:-8080}:8080"
6
  volumes:
 
1
  services:
2
  codex-proxy:
3
  build: .
4
+ extra_hosts:
5
+ - "host.docker.internal:host-gateway"
6
  ports:
7
  - "${PORT:-8080}:8080"
8
  volumes:
src/tls/curl-binary.ts CHANGED
@@ -187,12 +187,20 @@ const PROXY_PORTS = [
187
  { port: 10808, proto: "socks5" },// v2ray SOCKS5
188
  ];
189
 
 
 
 
 
 
 
 
 
190
  let _proxyUrl: string | null | undefined; // undefined = not yet detected
191
 
192
- /** Probe a TCP port on localhost. Resolves true if a server is listening. */
193
- function probePort(port: number, timeoutMs = 500): Promise<boolean> {
194
  return new Promise((resolve) => {
195
- const sock = createConnection({ host: "127.0.0.1", port }, () => {
196
  sock.destroy();
197
  resolve(true);
198
  });
@@ -203,15 +211,17 @@ function probePort(port: number, timeoutMs = 500): Promise<boolean> {
203
  }
204
 
205
  /**
206
- * Detect a local proxy by probing common ports.
207
  * Called once at startup, result is cached.
208
  */
209
  async function detectLocalProxy(): Promise<string | null> {
210
- for (const { port, proto } of PROXY_PORTS) {
211
- if (await probePort(port)) {
212
- const url = `${proto}://127.0.0.1:${port}`;
213
- console.log(`[Proxy] Auto-detected local proxy: ${url}`);
214
- return url;
 
 
215
  }
216
  }
217
  return null;
 
187
  { port: 10808, proto: "socks5" },// v2ray SOCKS5
188
  ];
189
 
190
+ /**
191
+ * Hosts to probe for proxy detection.
192
+ * 127.0.0.1 — bare-metal / host machine.
193
+ * host.docker.internal — Docker container → host machine
194
+ * (DNS lookup fails on bare-metal → ENOTFOUND → handled by error callback, <5ms).
195
+ */
196
+ const PROXY_HOSTS = ["127.0.0.1", "host.docker.internal"];
197
+
198
  let _proxyUrl: string | null | undefined; // undefined = not yet detected
199
 
200
+ /** Probe a TCP port on the given host. Resolves true if a server is listening. */
201
+ function probePort(host: string, port: number, timeoutMs = 500): Promise<boolean> {
202
  return new Promise((resolve) => {
203
+ const sock = createConnection({ host, port }, () => {
204
  sock.destroy();
205
  resolve(true);
206
  });
 
211
  }
212
 
213
  /**
214
+ * Detect a local proxy by probing common ports on localhost and Docker host.
215
  * Called once at startup, result is cached.
216
  */
217
  async function detectLocalProxy(): Promise<string | null> {
218
+ for (const host of PROXY_HOSTS) {
219
+ for (const { port, proto } of PROXY_PORTS) {
220
+ if (await probePort(host, port)) {
221
+ const url = `${proto}://${host}:${port}`;
222
+ console.log(`[Proxy] Auto-detected local proxy: ${url}`);
223
+ return url;
224
+ }
225
  }
226
  }
227
  return null;