I've familiar with HTTP_PROXY
and defining a DefaultTransport
to proxy HTTP requests. But I can't find anything about how to do the same for TCP. Is this possible? Or do I have to rely on on the proxy itself to forward the packet?
This is possible but not with an HTTP Proxy. You want a SOCKS proxy. Check out the https://godoc.org/golang.org/x/net/proxy package that provides a SOCKS5 Dialer.
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
}