macOS 使用 Privoxy 将 SOCKS5 转 HTTP 代理(完整步骤)

总结摘要
本文记录在 macOS 上通过 Homebrew 安装 Privoxy,并将本地 SOCKS5 代理转为 HTTP/HTTPS 代理的完整配置过程,包含验证命令与常见问题排查。

macOS 使用 Privoxy 将 SOCKS5 代理转换为 HTTP 代理

很多代理工具(如 Clash、V2Ray、Shadowsocks)提供的是 SOCKS5 代理。 但有些开发工具(例如某些 CLI、构建工具、老软件)只支持 HTTP/HTTPS 代理。

这时可以使用 Privoxy 作为中间层,将 SOCKS5 代理转换为 HTTP 代理。

一、整体架构

代理请求流程如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
应用程序(只支持 HTTP Proxy)
        |
        v
HTTP Proxy
Privoxy (127.0.0.1:7890)
        |
        v
SOCKS5 Proxy
127.0.0.1:13659
        |
        v
代理客户端(Clash / V2Ray / Shadowsocks)
        |
        v
互联网

简单理解:HTTP Proxy -> Privoxy -> SOCKS5 Proxy

二、安装 Privoxy

macOS 推荐使用 Homebrew:

1
brew install privoxy

安装后配置文件通常位于:

1
/opt/homebrew/etc/privoxy/config

三、修改 Privoxy 配置

打开配置文件:

1
vim /opt/homebrew/etc/privoxy/config

在文件末尾添加:

1
2
listen-address 127.0.0.1:7890
forward-socks5 / 127.0.0.1:13659 .

配置说明

listen-address 127.0.0.1:7890

  • Privoxy 在本地 7890 端口提供 HTTP 代理服务。

forward-socks5 / 127.0.0.1:13659 .

  • /:匹配所有请求。
  • 127.0.0.1:13659:上游 SOCKS5 代理地址。
  • .:不指定额外 DNS 服务器。

四、启动 Privoxy

1
brew services start privoxy

常用管理命令:

1
2
3
brew services stop privoxy
brew services restart privoxy
brew services list | grep privoxy

五、配置终端代理(可选)

临时生效:

1
2
export HTTP_PROXY="http://127.0.0.1:7890"
export HTTPS_PROXY="http://127.0.0.1:7890"

长期生效(写入 ~/.zshrc):

1
2
3
echo 'export HTTP_PROXY="http://127.0.0.1:7890"' >> ~/.zshrc
echo 'export HTTPS_PROXY="http://127.0.0.1:7890"' >> ~/.zshrc
source ~/.zshrc

六、测试代理是否生效

1
curl -x http://127.0.0.1:7890 ipinfo.io

如果返回的 IP 为代理出口 IP,说明配置成功。

七、常见使用场景

Git 代理:

1
2
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890

Docker 代理(~/.docker/config.json):

1
2
3
4
5
6
7
8
{
  "proxies": {
    "default": {
      "httpProxy": "http://127.0.0.1:7890",
      "httpsProxy": "http://127.0.0.1:7890"
    }
  }
}

pip 代理:

1
pip install -i https://pypi.org/simple package_name --proxy http://127.0.0.1:7890

八、常见问题

  1. 端口冲突

如果 7890 被占用,可改为其他端口,例如:

1
listen-address 127.0.0.1:8888

并同步更新环境变量与工具配置。

  1. 查看端口占用
1
lsof -i :7890

九、总结

通过 Privoxy 可以把 SOCKS5 代理转换为 HTTP 代理,解决“工具只支持 HTTP 代理”的兼容问题。

落地步骤很简单:

  1. 安装 Privoxy。
  2. 修改 config 增加 listen-addressforward-socks5
  3. 启动服务并验证。
  4. 按需配置系统或工具级 HTTP 代理。