使用 Brook Server 和 Cloudfare Warp 解锁 ChatGPT

Updated at: 2025-01-02

背景

因为 OpenAI 在某些地区不可用,还 ban 掉了大部分 VPS 提供商的 IP。这篇文章的目的找个可用的落地 IP 来访问 ChatGPT。

cloudflare warp

以下在全新的 Ubuntu 22.04 上操作并验证可行

curl https://pkg.cloudflareclient.com/pubkey.gpg | sudo gpg --yes --dearmor --output /usr/share/keyrings/cloudflare-warp-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/cloudflare-warp-archive-keyring.gpg] https://pkg.cloudflareclient.com/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/cloudflare-client.list
apt update
apt install cloudflare-warp -y
echo y | warp-cli registration new
warp-cli mode proxy
warp-cli proxy port 1080
warp-cli connect
sleep 3
warp-cli status

到这里,你应该有了一个socks5, 127.0.0.1:1080

brook

bash <(curl https://bash.ooo/nami.sh)
nami install brook

测试下 warp 创建出来的 socks5

brook testsocks5 -s 127.0.0.1:1080
Testing TCP: query http3.ooo A on 8.8.8.8:53
2023/05/13 09:17:14 Sent NegotiationRequest: 0x5 0x1 []byte{0x0}
2023/05/13 09:17:14 Got NegotiationReply: 0x5 0x0
2023/05/13 09:17:14 Sent Request: 0x5 0x1 0x0 0x1 []byte{0x8, 0x8, 0x8, 0x8} []byte{0x0, 0x35}
2023/05/13 09:17:14 Got Reply: 0x5 0x0 0x0 0x1 []byte{0x7f, 0x0, 0x0, 0x1} []byte{0x0, 0x0}
TCP: OK
Testing UDP: query http3.ooo A on 8.8.8.8:53
2023/05/13 09:17:14 Sent NegotiationRequest: 0x5 0x1 []byte{0x0}
2023/05/13 09:17:14 Got NegotiationReply: 0x5 0x0
2023/05/13 09:17:14 Sent Request: 0x5 0x3 0x0 0x1 []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0} []byte{0x0, 0x0}
2023/05/13 09:17:14 unexpected EOF

可以看出,这个 socks5 只支持 TCP,不能处理 UDP。所以只能让 brook server block 掉 OpenAI 的 UDP,然后将 OpenAI 的 TCP 转发到 Warp 的 socks5.

brook --script /path/to/script.tengo server --listen :9999 -p hello
match_domain := func(m, domain){
    text := import("text")
    ss := text.split(text.to_lower(domain), ".")
    s := ""
    for i := len(ss) - 1; i >= 0; i-- {
        if s == "" {
            s = ss[i]
        } else {
            s = ss[i] + "." + s
        }
        if m[s] {
            return true
        }
    }
    return false
}
match_openai := func(domain){
    m := {
        "ai.com": true,
        "chatgpt.com": true,
        "chat.com": true,
        "oaistatic.com": true,
        "oaiusercontent.com": true,
        "openai.com": true,
        "sora.com": true,
        "openaiapi-site.azureedge.net": true,
        "openaicom-api-bdcpf8c6d2e9atf6.z01.azurefd.net": true,
        "openaicomproductionae4b.blob.core.windows.net": true,
        "production-openaicom-storage.azureedge.net": true,
        "chatgpt.livekit.cloud": true,
        "host.livekit.cloud": true,
        "turn.livekit.cloud": true,
        "o33249.ingest.sentry.io": true,
        "openaicom.imgix.net": true,
        "browser-intake-datadoghq.com": true
    }
    if match_domain(m, domain) {
        return true
    }
    text := import("text")
    if text.contains(domain, "chatgpt-async-webps-prod") {
        return true
    }
    return false
}
f := func() {
    if in_brooklinks {
        return {
            "warp": "brook://socks5?password=&socks5=socks5%3A%2F%2F127.0.0.1%3A1080"
        }
    }
    if in_address {
        m := in_address
        if m.domainaddress {
            brook := import("brook")
            r := brook.splithostport(m.domainaddress)
            if m.network == "udp" && match_openai(r.host) {
                return { block: true }
            }
            if m.network == "tcp" && match_openai(r.host) {
                return { brooklinkkey: "warp" }
            }
        }
    }
}
out := f()