go - 向代理发送 TCP 数据包

标签 go tcp

我熟悉 HTTP_PROXY 并定义了一个 DefaultTransport 来代理 HTTP 请求。但是我找不到任何关于如何为 TCP 做同样的事情。这可能吗?还是必须依靠代理本身来转发数据包?

最佳答案

这是可能的,但不能使用 HTTP 代理。你想要一个 SOCKS 代理。查看https://godoc.org/golang.org/x/net/proxy提供 SOCKS5 拨号器的软件包。

package main

import (
    "fmt"
    "os"

    "golang.org/x/net/proxy"
)

var (
    proxy_addr  = "my.socks.proxy.local:8088"
    remote_addr = "chat.freenode.net:6697"
)

func main() {
    dialer, err := proxy.SOCKS5("tcp", proxy_addr, nil, proxy.Direct)
    if err != nil {
        fmt.Fprintln(os.Stderr, "proxy connection error:", err)
        os.Exit(1)
    }
    conn, err := dialer.Dial("tcp", remote_addr)
    if err != nil {
        fmt.Fprintln(os.Stderr, "remote connection error:", err)
        os.Exit(1)
    }
    defer conn.Close()

    // communicate with remote addr here
}

关于go - 向代理发送 TCP 数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40749222/

相关文章:

json - 如何最好地使用 Go 使用 FCM/GCM 发送复杂数据

Golang gorilla /session

go - 调用 Google Apps 脚本

python - Twisted > 如何读取比 TCP 帧长度更长的 TCP 消息,例如来自窗口 TCP 客户端的 1380 字节

C#:简单的TCP服务器问题

启动时的 MySQL 问题

java - 使用 asynctask 进行网络连接

firebase - FCM HTTP v1 : how to get access token using Go?

http - 如何限制用 golang 编写的 Web 服务器允许特定地址而不是模式?

linux - 在哪个本地接口(interface)上接收到连接或数据包?