douwu8524
2018-11-30 04:43
浏览 1.3k
已采纳

通过代理请求时的“ EOF”

I'm using ssh to create a proxy connection to an EC2 instance:

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

and I've written a very simple Go program that makes http requests:

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)))

}

When I run my program normally, it works as expected. However, when I use the HTTP_PROXY env var (godoc, example) to specify an http proxy (or if I make a custom http client that uses that proxy for transport), I get the following:

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

and the debug for my SSH session where I've established the proxy gives me:

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

When I configure my macbook's wifi adapter to use localhost:8123 as a SOCKS proxy, it appears to work. My public-facing IP address changes, and traffic seems to flow (in fact, I'm using it now). Any idea why my Go program is having problems?

图片转代码服务由CSDN问答提供 功能建议

我正在使用ssh创建与EC2实例的代理连接: < pre> ssh -v -D 8123 ubuntu@some.ip.address

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

 软件包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( godoc 时,示例)来指定http代理(或者,如果我创建了一个使用该代理进行传输的自定义http客户端),则会得到以下信息: \ n

  HTTP_PROXY = localhost:8123 ./main 2018/11/29 23:33:27获取http://example.com:EOF 
   
 
 <  p>并且在我已建立代理的SSH会话中进行的调试使我得到: 
 
 
  debug1:请求连接到端口8123并转发到socks端口0。
debug1:通道 3:新的[dynamic-tcpip] 
debug1:频道3:免费:dynamic-tcpip,nchannels 4 
   
 
 

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanfa2014 2018-11-30 05:23
    已采纳

    socks -D creates a SOCKS proxy. In your test you set a HTTP proxy. SOCKS proxy and HTTP proxy use different protocols, which means the request your client makes does not fit into what the proxy expects and thus it fails.

    For using a SOCKS proxy with Go see Creating a go socks5 client.

    打赏 评论

相关推荐 更多相似问题