I have a ssh tunnel to my server (via port: 9998). I want my http GET/POST requests to be routed through this port in Go. In java I would specify the DsocksProxyHost and DsocksProxyPort. I am looking for a similar option in Go. Thank you for the help in advance.
Using the information provided in the above comments, here is a working example on how to tunnel HTTP requests through a SOCKS proxy:
package main
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"time"
"golang.org/x/net/proxy"
)
func main() {
url := "https://example.com"
socksAddress := "localhost:9998"
socks, err := proxy.SOCKS5("tcp", socksAddress, nil, &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
})
if err != nil {
panic(err)
}
client := &http.Client{
Transport: &http.Transport{
Dial: socks.Dial,
TLSHandshakeTimeout: 10 * time.Second,
},
}
res, err := client.Get(url)
if err != nil {
panic(err)
}
content, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
panic(err)
}
fmt.Printf("%s", string(content))
}