HTTP 代理 GIA 中转教程

通过 CN2 GIA 服务器中转第三方 HTTP/SOCKS 代理,实现加密接入 + 线路优化。使用 sing-box + Docker 一键部署,Surge 客户端直连使用。

一、为什么需要 GIA 中转

从第三方购买的 HTTP/SOCKS 代理(住宅代理、ISP 代理、数据中心代理)通常有两个问题:

  1. 不加密:HTTP 代理用明文传输认证信息和请求内容,在网络链路上容易被审查和干扰
  2. 直连线路差:从国内直连海外代理服务器,走的是普通国际出口,延迟高、丢包多、晚高峰卡顿

解决方案是在中间加一台 CN2 GIA 线路的服务器做中转:

你的设备
  → SS2022 加密隧道(走 CN2 GIA 优质线路)
    → GIA 中转服务器(sing-box)
      → 第三方 HTTP/SOCKS 代理(明文,但已在海外内网)
        → 目标网站

这样你获得了:

  • 加密传输:设备到 GIA 服务器之间走 SS2022 或 Trojan 加密协议,彻底解决明文问题
  • 线路优化:CN2 GIA 是电信最优质的国际出口,延迟低、稳定、不惧晚高峰
  • 客户端友好:在 Surge/Clash 里只需配置一条 SS2022 代理线,不需要处理 HTTP 代理的认证格式
  • 多代理统一入口:一台 GIA 服务器可以同时中转几十个不同的上游代理,每个代理对应一个独立用户

二、你需要准备什么

1. 一台 CN2 GIA 服务器

推荐选择美西(洛杉矶、圣何塞)机房的 CN2 GIA VPS。常见提供商:

提供商 特点 参考价格
BandwagonHost (搬瓦工) 老牌 CN2 GIA,稳定 $50-170/年
DMIT 高端 CN2 GIA / CMIN2 $40-70/季
GreenCloud 性价比,不定期补货 $30-60/年
关键指标:CN2 GIA 回程(从服务器到国内走 CN2),至少 500G-1T 月流量,Linux 系统(Ubuntu/Debian)。

2. 第三方 HTTP/SOCKS 代理

从代理提供商购买的代理,通常会给你这些信息:

协议:HTTP 或 SOCKS5
地址:proxy.example.com
端口:10001
用户名:your_username
密码:your_password

常见的住宅/ISP 代理提供商:Decodo (原 Smartproxy)、Bright Data、SOAX、IPRoyal 等。大多数提供 HTTP 协议的代理端口。

3. 本地工具

  • SSH 客户端(连接服务器)
  • Surge / Clash 等代理客户端

三、架构概览

                    ┌─────────────────────────────────────┐
                    │         GIA 中转服务器                │
                    │         (sing-box in Docker)         │
                    │                                     │
  SS2022 :8443 ────►│  inbound ──► route ──► outbound     │
  Trojan :8444 ────►│                                     │
                    │  用户 proxy-1  →  http://proxy:10001 │
                    │  用户 proxy-2  →  http://proxy:10002 │
                    │  用户 proxy-3  →  socks://socks:1080 │
                    │  用户 direct   →  direct (GIA 直连)  │
                    └─────────────────────────────────────┘

sing-box 的核心设计:

  • inbound(入站):监听 SS2022 和 Trojan 端口,接受客户端连接。每个用户有独立的密钥
  • outbound(出站):定义上游代理连接。每个 HTTP/SOCKS 代理是一个 outbound
  • route(路由):按用户名(auth_user)将流量分发到对应的 outbound

一个用户名 = 一条代理线路。你在 Surge 里切换不同的代理线,本质上是用不同的用户密钥连接 sing-box,sing-box 根据用户名路由到不同的上游代理。


四、部署步骤

4.1 安装 Docker

SSH 登录 GIA 服务器后:

# Ubuntu/Debian
curl -fsSL https://get.docker.com | sh
systemctl enable docker

验证安装:

docker --version
docker compose version

4.2 创建项目目录

mkdir -p /opt/relay-server/config
cd /opt/relay-server

4.3 生成密钥

SS2022 需要一个 服务端主密钥 和每个用户各一个 用户密钥。Trojan 需要每个用户各一个密码。

# SS2022 主密钥(16 bytes base64,对应 aes-128-gcm)
echo "Server Key: $(openssl rand -base64 16)"

# 为每个代理生成用户密钥
echo "User proxy-1 SS Key: $(openssl rand -base64 16)"
echo "User proxy-2 SS Key: $(openssl rand -base64 16)"
echo "User proxy-3 SS Key: $(openssl rand -base64 16)"

# 为每个用户生成 Trojan 密码
echo "User proxy-1 Trojan: $(openssl rand -hex 16)"
echo "User proxy-2 Trojan: $(openssl rand -hex 16)"
echo "User proxy-3 Trojan: $(openssl rand -hex 16)"
把输出全部记下来,后面配置要用。

4.4 生成 TLS 证书(Trojan 用)

Trojan 协议需要 TLS 证书。自签证书即可(客户端配 skip-cert-verify=true):

openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 \
  -keyout config/key.pem -out config/cert.pem \
  -days 3650 -nodes -subj "/CN=relay-proxy"

4.5 编写 sing-box 配置

创建 config/config.json

{
  "log": {
    "level": "info",
    "timestamp": true
  },
  "dns": {
    "servers": [
      {"tag": "dns-google", "address": "8.8.8.8"},
      {"tag": "dns-cf", "address": "1.1.1.1"}
    ]
  },
  "inbounds": [
    {
      "type": "shadowsocks",
      "tag": "ss-in",
      "listen": "::",
      "listen_port": 8443,
      "method": "2022-blake3-aes-128-gcm",
      "password": "<SS_SERVER_KEY>",
      "users": [
        {"name": "proxy-1", "password": "<SS_KEY_PROXY1>"},
        {"name": "proxy-2", "password": "<SS_KEY_PROXY2>"},
        {"name": "proxy-3", "password": "<SS_KEY_PROXY3>"},
        {"name": "direct",  "password": "<SS_KEY_DIRECT>"}
      ]
    },
    {
      "type": "trojan",
      "tag": "trojan-in",
      "listen": "::",
      "listen_port": 8444,
      "users": [
        {"name": "proxy-1", "password": "<TJ_PROXY1>"},
        {"name": "proxy-2", "password": "<TJ_PROXY2>"},
        {"name": "proxy-3", "password": "<TJ_PROXY3>"},
        {"name": "direct",  "password": "<TJ_DIRECT>"}
      ],
      "tls": {
        "enabled": true,
        "certificate_path": "/etc/sing-box/cert.pem",
        "key_path": "/etc/sing-box/key.pem"
      }
    }
  ],
  "outbounds": [
    {
      "type": "http",
      "tag": "upstream-1",
      "server": "proxy.example.com",
      "server_port": 10001,
      "username": "your_username",
      "password": "your_password"
    },
    {
      "type": "http",
      "tag": "upstream-2",
      "server": "proxy.example.com",
      "server_port": 10002,
      "username": "your_username",
      "password": "your_password"
    },
    {
      "type": "socks",
      "tag": "upstream-3",
      "server": "socks.example.com",
      "server_port": 1080,
      "username": "your_username",
      "password": "your_password"
    },
    {"type": "direct", "tag": "direct"},
    {"type": "block", "tag": "block"}
  ],
  "route": {
    "rules": [
      {"auth_user": ["proxy-1"], "outbound": "upstream-1"},
      {"auth_user": ["proxy-2"], "outbound": "upstream-2"},
      {"auth_user": ["proxy-3"], "outbound": "upstream-3"},
      {"auth_user": ["direct"],  "outbound": "direct"}
    ],
    "final": "direct"
  }
}

关键配置解释:

字段 含义
inbounds[].password SS2022 服务端主密钥,所有用户共享
inbounds[].users[].name 用户名,路由规则通过它分发流量
inbounds[].users[].password 用户独立密钥,客户端需要用 主密钥:用户密钥 格式连接
outbounds[].type: "http" 上游是 HTTP 代理
outbounds[].type: "socks" 上游是 SOCKS5 代理
route.rules[].auth_user 按用户名匹配,将该用户的流量路由到指定 outbound
route.final 未匹配任何规则的流量走 direct(直连)
<SS_SERVER_KEY> 等占位符替换为 4.3 步生成的实际密钥。上游代理的地址、端口、认证信息替换为你购买的代理信息。

4.6 编写 Docker Compose

创建 /opt/relay-server/docker-compose.yml

services:
  sing-box:
    image: ghcr.io/sagernet/sing-box:latest
    container_name: relay-proxy
    restart: unless-stopped
    ports:
      - "8443:8443/tcp"
      - "8443:8443/udp"
      - "8444:8444/tcp"
      - "8444:8444/udp"
    volumes:
      - ./config:/etc/sing-box:ro
    command: ["run", "-c", "/etc/sing-box/config.json"]

4.7 验证配置并启动

# 验证配置语法
docker run --rm -v ./config:/etc/sing-box:ro \
  ghcr.io/sagernet/sing-box:latest check -c /etc/sing-box/config.json

# 启动服务
docker compose up -d

# 查看日志确认运行正常
docker logs relay-proxy

正常启动后日志应显示 inbound 监听信息,没有 error。


五、客户端配置(Surge)

SS2022 代理线(推荐)

Proxy-1 = ss, <GIA_SERVER_IP>, 8443, encrypt-method=2022-blake3-aes-128-gcm, password=<SS_SERVER_KEY>:<SS_KEY_PROXY1>, udp-relay=true
Proxy-2 = ss, <GIA_SERVER_IP>, 8443, encrypt-method=2022-blake3-aes-128-gcm, password=<SS_SERVER_KEY>:<SS_KEY_PROXY2>, udp-relay=true
Proxy-3 = ss, <GIA_SERVER_IP>, 8443, encrypt-method=2022-blake3-aes-128-gcm, password=<SS_SERVER_KEY>:<SS_KEY_PROXY3>, udp-relay=true
GIA直连 = ss, <GIA_SERVER_IP>, 8443, encrypt-method=2022-blake3-aes-128-gcm, password=<SS_SERVER_KEY>:<SS_KEY_DIRECT>, udp-relay=true

Trojan 代理线(备用)

Proxy-1-TJ = trojan, <GIA_SERVER_IP>, 8444, password=<TJ_PROXY1>, skip-cert-verify=true
Proxy-2-TJ = trojan, <GIA_SERVER_IP>, 8444, password=<TJ_PROXY2>, skip-cert-verify=true

Clash Meta 格式

proxies:
  - name: Proxy-1
    type: ss
    server: <GIA_SERVER_IP>
    port: 8443
    cipher: 2022-blake3-aes-128-gcm
    password: "<SS_SERVER_KEY>:<SS_KEY_PROXY1>"
    udp: true

  - name: Proxy-2
    type: ss
    server: <GIA_SERVER_IP>
    port: 8443
    cipher: 2022-blake3-aes-128-gcm
    password: "<SS_SERVER_KEY>:<SS_KEY_PROXY2>"
    udp: true

注意事项:

  • SS2022 的 password 格式是 服务端主密钥:用户密钥,冒号分隔,缺一不可
  • Trojan 使用自签证书,客户端必须开启 skip-cert-verify
  • 所有代理线都连同一个 GIA 服务器 IP 和端口,靠不同的密钥区分用户/路由

六、添加新代理

当你购买了新的 HTTP/SOCKS 代理后,添加流程如下:

1. 生成新用户密钥

echo "SS Key: $(openssl rand -base64 16)"
echo "Trojan: $(openssl rand -hex 16)"

2. 编辑 config.json

在三个位置添加:

// inbounds → shadowsocks → users 末尾
{"name": "proxy-new", "password": "<新生成的SS密钥>"}

// inbounds → trojan → users 末尾
{"name": "proxy-new", "password": "<新生成的Trojan密码>"}

// outbounds 中添加上游代理
{
  "type": "http",
  "tag": "upstream-new",
  "server": "new-proxy.example.com",
  "server_port": 10099,
  "username": "new_user",
  "password": "new_pass"
}

// route → rules 中添加路由规则
{"auth_user": ["proxy-new"], "outbound": "upstream-new"}

3. 验证并重启

# 验证
docker run --rm -v ./config:/etc/sing-box:ro \
  ghcr.io/sagernet/sing-box:latest check -c /etc/sing-box/config.json

# 重启生效
docker restart relay-proxy

4. 添加 Surge 代理线

新代理 = ss, <GIA_SERVER_IP>, 8443, encrypt-method=2022-blake3-aes-128-gcm, password=<SS_SERVER_KEY>:<新用户密钥>, udp-relay=true

七、进阶:中转 VLESS/Reality 等加密代理

同样的架构不仅能中转 HTTP/SOCKS 代理,也能中转 VLESS、Shadowsocks 等加密协议的代理。只需把 outbound 类型改为对应协议:

{
  "type": "vless",
  "tag": "vless-jp",
  "server": "vless-server.example.com",
  "server_port": 443,
  "uuid": "your-uuid-here",
  "flow": "xtls-rprx-vision",
  "tls": {
    "enabled": true,
    "server_name": "www.example.com",
    "utls": {"enabled": true, "fingerprint": "chrome"},
    "reality": {
      "enabled": true,
      "public_key": "your-public-key",
      "short_id": "your-short-id"
    }
  }
}

这种场景的好处是:即使上游 VLESS 节点本身线路不好,通过 GIA 服务器中转后,你到 GIA 服务器这一段走 CN2 GIA 优质线路,整体体验大幅提升。


八、运维与排错

常用命令

# 查看实时日志
docker logs -f relay-proxy

# 重启服务
docker restart relay-proxy

# 停止服务
docker compose down

# 更新 sing-box 版本
docker compose pull && docker compose up -d

远程更新配置

从本地机器推送配置到服务器:

scp config.json root@<GIA_SERVER_IP>:/opt/relay-server/config/config.json
ssh root@<GIA_SERVER_IP> "docker restart relay-proxy"

常见问题

Q: Surge 测速显示超时,但实际浏览正常?

部分 HTTP 代理不支持 CONNECT 方法连接 80 端口(Surge 默认测速 URL 是 HTTP),但正常的 HTTPS 浏览没问题。可以忽略测速结果,或将 Surge 的测速 URL 改为 HTTPS:

proxy-test-url = https://cp.cloudflare.com/generate_204

Q: 连接上游代理报错 connection refused

  1. 确认上游代理的地址和端口是否正确
  2. 确认 GIA 服务器能访问上游代理:curl -x http://user:pass@proxy:port https://httpbin.org/ip
  3. 检查是否有防火墙限制

Q: 如何确认流量确实走了上游代理?

访问 IP 检测网站(如 https://ipinfo.io),返回的 IP 应该是上游代理的出口 IP,而不是 GIA 服务器的 IP。如果显示的是 GIA 服务器 IP,说明路由规则可能没有正确匹配。

Q: SS2022 和 Trojan 选哪个?

推荐 SS2022。SS2022(2022-blake3-aes-128-gcm)是目前性能最好的加密代理协议之一,基于 AEAD 2022 规范,支持 UDP,延迟低。Trojan 作为备用方案,在 SS2022 被干扰时切换使用。


九、安全注意事项

  1. 密钥管理:SS2022 密钥和 Trojan 密码等同于你的代理账号密码,不要泄露
  2. 上游代理凭证:第三方代理的用户名密码存储在 GIA 服务器的 config.json 中,确保服务器安全(SSH 密钥登录、禁用密码登录、防火墙)
  3. 端口暴露:只开放必要端口(8443、8444),不要暴露 Docker API 或其他管理端口
  4. 定期更新docker compose pull 保持 sing-box 版本最新,修复已知漏洞

十、总结

整体流程回顾:

1. 买一台 CN2 GIA 服务器
2. 装 Docker + sing-box
3. 配置 inbound(SS2022 + Trojan 监听)
4. 配置 outbound(上游 HTTP/SOCKS 代理)
5. 配置 route(用户名 → 上游代理的映射)
6. 在 Surge/Clash 中添加 SS2022 代理线
7. 完成。所有流量:设备 → 加密 → GIA → 上游代理 → 目标

这套方案的核心价值在于 解耦——你可以随时更换上游代理提供商,而客户端配置完全不需要改动。GIA 服务器作为统一入口,把各种协议、各种提供商的代理都转化为标准的 SS2022 加密隧道,对客户端完全透明。