已经看过很多个人发帖子问了。原因无他,不想开tun模式的话,clash不接管chatgpt的websocket。
以下代码保存为bat执行既可
@echo off
setlocal EnableExtensions
chcp 65001 >nul
title Codex WebSocket 代理配置
if /I "%~1"=="--help" goto :help
if /I "%~1"=="--remove" goto :remove
if not "%~1"=="" (
set "CODEX_PROXY_PORT=%~1"
goto :validate_port
)
echo.
echo ========================================
echo Codex WebSocket 代理配置工具
echo ========================================
echo.
echo [1] 配置或更新代理
echo [2] 移除本工具写入的代理项
echo [Q] 退出
echo.
choice /C 12Q /N /M "请选择 [1/2/Q]: "
if errorlevel 3 goto :end
if errorlevel 2 goto :remove
set "CODEX_PROXY_PORT=7897"
set "CODEX_PROXY_INPUT="
set /P "CODEX_PROXY_INPUT=请输入 Clash mixed-port(直接回车使用 7897): "
if defined CODEX_PROXY_INPUT set "CODEX_PROXY_PORT=%CODEX_PROXY_INPUT%"
:validate_port
powershell.exe -NoProfile -Command "$p=0; if ([int]::TryParse($env:CODEX_PROXY_PORT,[ref]$p) -and $p -ge 1 -and $p -le 65535) { exit 0 } else { exit 1 }"
if errorlevel 1 (
echo.
echo [错误] 端口必须是 1 到 65535 之间的数字。
goto :finish
)
echo.
echo 正在更新 %%USERPROFILE%%\.codex\.env ...
powershell.exe -NoProfile -Command ^
"$ErrorActionPreference='Stop';" ^
"$codexDir=Join-Path $env:USERPROFILE '.codex';" ^
"$envFile=Join-Path $codexDir '.env';" ^
"New-Item -ItemType Directory -Path $codexDir -Force | Out-Null;" ^
"if (Test-Path -LiteralPath $envFile) { $stamp=Get-Date -Format 'yyyyMMdd-HHmmss'; Copy-Item -LiteralPath $envFile -Destination ($envFile+'.backup-'+$stamp) };" ^
"$lines=if (Test-Path -LiteralPath $envFile) { @(Get-Content -LiteralPath $envFile) } else { @() };" ^
"$lines=@($lines | Where-Object { $_ -notmatch '(?i)^(HTTP_PROXY|HTTPS_PROXY|ALL_PROXY|NO_PROXY)=' });" ^
"$port=$env:CODEX_PROXY_PORT;" ^
"$lines+='HTTP_PROXY=http://127.0.0.1:'+$port;" ^
"$lines+='HTTPS_PROXY=http://127.0.0.1:'+$port;" ^
"$lines+='ALL_PROXY=socks5://127.0.0.1:'+$port;" ^
"$lines+='NO_PROXY=localhost,127.0.0.1,::1';" ^
"$utf8=New-Object System.Text.UTF8Encoding($false);" ^
"[System.IO.File]::WriteAllLines($envFile,[string[]]$lines,$utf8)"
if errorlevel 1 (
echo.
echo [失败] 无法写入配置。请检查权限后重试。
goto :finish
)
echo.
echo [成功] 已写入以下配置:
echo HTTP_PROXY=http://127.0.0.1:%CODEX_PROXY_PORT%
echo HTTPS_PROXY=http://127.0.0.1:%CODEX_PROXY_PORT%
echo ALL_PROXY=socks5://127.0.0.1:%CODEX_PROXY_PORT%
echo NO_PROXY=localhost,127.0.0.1,::1
echo.
powershell.exe -NoProfile -Command "$c=New-Object System.Net.Sockets.TcpClient; try { $r=$c.BeginConnect('127.0.0.1',[int]$env:CODEX_PROXY_PORT,$null,$null); if (-not $r.AsyncWaitHandle.WaitOne(1000)) { exit 1 }; $c.EndConnect($r); exit 0 } catch { exit 1 } finally { $c.Dispose() }"
if errorlevel 1 (
echo [提醒] 当前未检测到 127.0.0.1:%CODEX_PROXY_PORT% 正在监听。
echo 请启动 Clash,并确认 mixed-port 与此端口一致。
) else (
echo [检查] Clash 代理端口正在监听。
)
echo.
echo 请从系统托盘完全退出 Codex,然后重新启动。
echo 保持原来的 openai provider,不要添加 openai_http。
goto :finish
:remove
echo.
echo 正在移除 Codex 专用代理项...
powershell.exe -NoProfile -Command ^
"$ErrorActionPreference='Stop';" ^
"$envFile=Join-Path (Join-Path $env:USERPROFILE '.codex') '.env';" ^
"if (-not (Test-Path -LiteralPath $envFile)) { exit 0 };" ^
"$stamp=Get-Date -Format 'yyyyMMdd-HHmmss';" ^
"Copy-Item -LiteralPath $envFile -Destination ($envFile+'.backup-'+$stamp);" ^
"$lines=@(Get-Content -LiteralPath $envFile | Where-Object { $_ -notmatch '(?i)^(HTTP_PROXY|HTTPS_PROXY|ALL_PROXY|NO_PROXY)=' });" ^
"$utf8=New-Object System.Text.UTF8Encoding($false);" ^
"[System.IO.File]::WriteAllLines($envFile,[string[]]$lines,$utf8)"
if errorlevel 1 (
echo [失败] 无法修改配置。请检查权限后重试。
) else (
echo [成功] 已移除四个代理项,其他 .env 内容保持不变。
echo 请完全退出并重新启动 Codex。
)
goto :finish
:help
echo 用法:
echo setup-codex-proxy.bat 打开交互菜单
echo setup-codex-proxy.bat 7897 直接配置指定端口
echo setup-codex-proxy.bat --remove 移除代理项
goto :end
:finish
echo.
if "%~1"=="" pause
:end
endlocal
如果你不想这么做。请在你的系统盘/users/你的用户名/.codex/下添加.env文件。
内容,当然,请改为自己的clash的端口
HTTP_PROXY=http://127.0.0.1:7897
HTTPS_PROXY=http://127.0.0.1:7897
ALL_PROXY=socks5://127.0.0.1:7897
NO_PROXY=localhost,127.0.0.1,::1