创建一个Go语言的Socks5客户端。

huangapple go评论74阅读模式
英文:

Creating a go socks5 client

问题

所以我正在查看net/proxy文档,但是没有任何关于如何使用其方法的示例。我正在研究如何使用socks5。函数的样子如下:

func SOCKS5(network, addr string, auth *Auth, forward Dialer) (Dialer, error)

现在一切都有点明白,除了forward这个类型为Dialer的参数,而函数本身返回一个Dialer。其他的都很清楚,network, addr, auth,只有forward让我困惑。我该如何设置我的客户端来使用socks5 Dialer?

英文:

So I'm looking at the net/proxy docs and there is no examples at all of how to use any of its methods. I'm looking into using socks5. This is the how the function looks:

func SOCKS5(network, addr string, auth *Auth, forward Dialer) (Dialer, error)

Now everything kinda makes sense except I'm confused about forward which is a type Dialer and the function itself returns a Dialer. Everything else makes sense network, add, auth just forward is throwing me off. How would I set my client up to use the socks5 Dialer?

答案1

得分: 17

以下是如何在Go中设置Socks5客户端的代码:

dialSocksProxy, err := proxy.SOCKS5("tcp", "proxy_ip", nil, proxy.Direct)
if err != nil {
    fmt.Println("连接代理服务器时出错:", err)
}
tr := &http.Transport{Dial: dialSocksProxy.Dial}

// 创建客户端
myClient := &http.Client{
    Transport: tr,
}

希望对你有帮助!

英文:

So I was able to find the answer to my question anyone interested how to set up a socks5 client in go here it is:

dialSocksProxy, err := proxy.SOCKS5("tcp", "proxy_ip", nil, proxy.Direct)
if err != nil {
	fmt.Println("Error connecting to proxy:", err)
}
tr := &http.Transport{Dial: dialSocksProxy.Dial}

// Create client
myClient := &http.Client{
	Transport: tr,
}

答案2

得分: 8

最近的Go版本还支持通过HTTP_PROXY环境变量使用SOCKS5代理。你可以像往常一样编写你的http.Client代码,然后在运行时设置环境变量,例如:

HTTP_PROXY="socks5://127.0.0.1:1080/" ./myGoHttpClient
英文:

Recent versions of Go also have SOCKS5 proxy support via the HTTP_PROXY environment variable. You would write your http.Client code as usual, then just set the environment variable at runtime, for example:

HTTP_PROXY="socks5://127.0.0.1:1080/" ./myGoHttpClient

答案3

得分: 1

如果你需要带有身份验证的socks5客户端代理,你可以使用类似以下的代码:

auth := proxy.Auth{
    User:     "YOUR_PROXY_LOGIN",
    Password: "YOUR_PROXY_PASSWORD",
}

dialer, err := proxy.SOCKS5("tcp", "PROXY_IP", &auth, proxy.Direct)
if err != nil {
    fmt.Fprintln(os.Stderr, "无法连接到代理服务器:", err)
}

tr := &http.Transport{Dial: dialer.Dial}
myClient := &http.Client{
    Transport: tr,
}

这段代码使用了proxy包来设置带有身份验证的socks5代理。你需要将YOUR_PROXY_LOGIN替换为你的代理登录用户名,将YOUR_PROXY_PASSWORD替换为你的代理登录密码,将PROXY_IP替换为代理服务器的IP地址。然后,你可以使用myClient来发送HTTP请求,它会通过代理服务器进行连接。

英文:

If you need socks5 client proxy with auth, you may use something like this:

auth := proxy.Auth{
	User:     "YOUR_PROXY_LOGIN",
	Password: "YOUR_PROXY_PASSWORD",
}

dialer, err := proxy.SOCKS5("tcp", "PROXY_IP", &auth, proxy.Direct)
if err != nil {
	fmt.Fprintln(os.Stderr, "can't connect to the proxy:", err)
}

tr := &http.Transport{Dial: dialer.Dial}
myClient := &http.Client{
	Transport: tr,
}

huangapple
  • 本文由 发表于 2015年11月8日 01:24:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/33585587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定