Home >Backend Development >Golang >How to Set Up a Go SOCKS5 Client?
Setting Up a Go SOCKS5 Client
The Go programming language provides a comprehensive networking library that includes support for SOCKS5 proxies. However, the documentation for the net/proxy package lacks detailed examples on how to utilize its methods effectively. One of the key methods in this context is SOCKS5, which creates a SOCKS5 dialer.
The SOCKS5 function takes several parameters:
The function itself returns a Dialer type, which can be used to create connections that route through the SOCKS5 proxy. A common use case is to set up an HTTP client that utilizes the SOCKS5 proxy.
Here is a simplified example of how to create a SOCKS5 client in Go:
import ( "fmt" "net" "net/http" "net/http/proxy" ) func main() { // Establish SOCKS5 dialer proxyDialer, err := proxy.SOCKS5("tcp", "proxy_ip", nil, proxy.Direct) if err != nil { fmt.Println("Error connecting to proxy:", err) return } // Create HTTP transport using our SOCKS5 dialer transport := &http.Transport{Dial: proxyDialer.Dial} // Initialize HTTP client with our transport client := &http.Client{Transport: transport} // Make a request through our SOCKS5 proxy req, err := http.NewRequest("GET", "https://example.com", nil) if err != nil { fmt.Println("Error creating request:", err) return } resp, err := client.Do(req) if err != nil { fmt.Println("Error making request:", err) return } defer resp.Body.Close() fmt.Println("Response status:", resp.Status) }
This example assumes that proxy_ip is the IP address or hostname of your SOCKS5 proxy server. By using the SOCKS5 function and routing your HTTP requests through the dialer it creates, you can establish connections that are proxied through the SOCKS5 server.
The above is the detailed content of How to Set Up a Go SOCKS5 Client?. For more information, please follow other related articles on the PHP Chinese website!