【问题标题】:"EOF" when making request through proxy通过代理发出请求时的“EOF”
【发布时间】:2018-11-30 04:43:46
【问题描述】:

我正在使用 ssh 创建到 EC2 实例的代理连接:

ssh -v -D 8123 ubuntu@some.ip.address

我编写了一个非常简单的 Go 程序来发出 http 请求:

package main

import (
    "io/ioutil"
    "net/http"
    "os"
    "log"
    "fmt"

)

func main(){

    resp, err := http.Get("http://example.com")

    if err != nil {
        log.Println(err)
        os.Exit(1)
    }

    fmt.Println(string(ioutil.ReadAll(resp.Body)))

}

当我正常运行我的程序时,它按预期工作。但是,当我使用 HTTP_PROXY env var(godocexample)来指定一个 http 代理(或者如果我创建一个使用该代理进行传输的自定义 http 客户端)时,我会得到以下信息:

HTTP_PROXY=localhost:8123 ./main 2018/11/29 23:33:27 Get http://example.com: EOF

我建立代理的 SSH 会话的调试给了我:

debug1: Connection to port 8123 forwarding to socks port 0 requested.
debug1: channel 3: new [dynamic-tcpip]
debug1: channel 3: free: dynamic-tcpip, nchannels 4

当我将我的 macbook 的 wifi 适配器配置为使用 localhost:8123 作为 SOCKS 代理时,它似乎可以工作。我面向公众的 IP 地址发生了变化,流量似乎在流动(事实上,我现在正在使用它)。知道我的 Go 程序出现问题的原因吗?

【问题讨论】:

    标签: go proxy network-programming http-proxy


    【解决方案1】:

    socks -D 创建一个 SOCKS 代理。在您的测试中,您设置了一个 HTTP 代理。 SOCKS 代理和 HTTP 代理使用不同的协议,这意味着您的客户端发出的请求不符合代理的预期,因此会失败。

    要在 Go 中使用 SOCKS 代理,请参阅 Creating a go socks5 client

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      相关资源
      最近更新 更多