Install the socks package
masterUse the following command to download and install the h12.io/socks package into your Go module:
go get -u "h12.io/socks"repository·master·Indexed 19 days ago
https://github.com/h12w/socksA Go package providing support for SOCKS4, SOCKS4A, and SOCKS5 proxy protocols. It allows developers to route TCP and HTTP traffic through a proxy by generating compatible dialing functions via socks.Dial and DialSocksProxy.
Use the following command to download and install the h12.io/socks package into your Go module:
go get -u "h12.io/socks"To use a SOCKS proxy that requires user/password authentication, include the credentials in the connection string using the user:password@ format before the host address.
dialSocksProxy := socks.Dial("socks5://user:password@127.0.0.1:1080?timeout=5s")This example demonstrates how to initialize an http.Client using a SOCKS5 proxy via socks.Dial and perform a GET request.
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"h12.io/socks"
)
func main() {
dialSocksProxy := socks.Dial("socks5://127.0.0.1:1080?timeout=5s")
tr := &http.Transport{Dial: dialSocksProxy}
httpClient := &http.Client{Transport: tr}
resp, err := httpClient.Get("http://www.google.com")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Fatal(resp.StatusCode)
}
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(buf))
}The socks.Dial function takes a SOCKS proxy connection string and returns a TCP dialing function. This returned function can be used to establish TCP connections through the proxy or assigned to the Dial field of an http.Transport to route HTTP traffic through the proxy.
Supported protocols in the connection string include socks4, socks4a, and socks5.
dialSocksProxy := socks.Dial("socks5://127.0.0.1:1080?timeout=5s")
tr := &http.Transport{Dial: dialSocksProxy}
httpClient := &http.Client{Transport: tr}This example demonstrates how to integrate the socks package into a standard Go http.Client to route requests through a SOCKS5 proxy.
package main
import (
"fmt"
"h12.io/socks"
"io/ioutil"
"net/http"
)
func main() {
dialSocksProxy := socks.Dial("socks5://127.0.0.1:1080?timeout=5s")
tr := &http.Transport{Dial: dialSocksProxy}
httpClient := &http.Client{Transport: tr}
bodyText, err := TestHttpsGet(httpClient, "https://h12.io/about")
if err != nil {
fmt.Println(err.Error())
}
fmt.Print(bodyText)
}
func TestHttpsGet(c *http.Client, url string) (bodyText string, err error) {
resp, err := c.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
bodyText = string(body)
return
}Use DialSocksProxy(socksType int, proxy string) to generate a dialing function when you want to specify the protocol version and proxy address explicitly without using a URI string.
Parameters:
socksType: An integer constant representing the protocol (SOCKS4, SOCKS4A, or SOCKS5).proxy: The proxy address in the format host:port (e.g., "127.0.0.1:1080").// Example using SOCKS5
dialSocksProxy := socks.DialSocksProxy(socks.SOCKS5, "127.0.0.1:1080")Use Dial(proxyURI string) to generate a dialing function compatible with http.Transport.Dial. The proxyURI must be a URI string specifying the protocol, optional credentials, proxy address, and an optional timeout.
Supported protocols in the URI prefix:
socks5://socks4://socks4a://Example URI format: socks5://user:password@127.0.0.1:1080?timeout=5s.
dialSocksProxy := socks.Dial("socks5://127.0.0.1:1080?timeout=5s")
tr := &http.Transport{Dial: dialSocksProxy}
httpClient := &http.Client{Transport: tr}The following constants define the supported SOCKS protocol versions for use with DialSocksProxy:
SOCKS4SOCKS4ASOCKS5const (
SOCKS4 = iota
SOCKS4A
SOCKS5
)