Home > Article > Backend Development > Let’s talk about how to set up a proxy in Golang
Golang is a fast, efficient, and concurrent programming language. It is one of the most popular programming languages at present. When doing network programming, you may need to set up a proxy to access restricted websites. In this article, we will learn how to set up proxy in Golang.
A proxy is an intermediary server used to connect clients and target servers. When a client establishes a connection with a proxy server, the proxy server sends a request to the target server on behalf of the client. The target server returns the response to the proxy server, which in turn returns the response to the client. In this way, clients can gain access to resources that would otherwise be inaccessible.
Common proxies include HTTP proxy and SOCKS proxy. In Golang, we can configure the proxy by setting environment variables. Specifically, we can set the HTTP_PROXY and HTTPS_PROXY environment variables or the ALL_PROXY environment variable (at which point the proxy will apply to all protocols). If your agent requires authentication, you will also need to set the agent's username and password.
Here are the steps to set up a proxy in Golang:
Step 1: Set HTTP proxy
Set the HTTP proxy in the terminal, for example:
export HTTP_PROXY=http://proxy.example.com:8080
Assume that the proxy server is "proxy.example.com" and the port is "8080".
Set the HTTP proxy in the Golang program, for example:
package main import ( "fmt" "net/http" "os" ) func main() { client := &http.Client{} req, err := http.NewRequest("GET", "http://example.com", nil) if err != nil { fmt.Println("Error creating request:", err) os.Exit(1) } req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36") resp, err := client.Do(req) if err != nil { fmt.Println("Error executing request:", err) os.Exit(1) } fmt.Println("Response status:", resp.Status) }
The above program will send a GET request to "http://example.com" and print the response status. This program will use the proxy specified by the HTTP_PROXY environment variable. You can change the proxy's settings by setting the HTTP_PROXY environment variable.
Step 2: Set up HTTPS proxy
Similarly, you can configure the HTTPS proxy by setting the HTTPS_PROXY environment variable.
export HTTPS_PROXY=https://proxy.example.com:8080
Using HTTPS proxy in Golang program:
package main import ( "crypto/tls" "fmt" "net/http" "os" ) func main() { tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } client := &http.Client{Transport: tr} req, err := http.NewRequest("GET", "https://example.com", nil) if err != nil { fmt.Println("Error creating request:", err) os.Exit(1) } req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36") resp, err := client.Do(req) if err != nil { fmt.Println("Error executing request:", err) os.Exit(1) } fmt.Println("Response status:", resp.Status) }
The above program will send a GET request to "https://example.com" and print the response status. This program will use the proxy specified by the HTTPS_PROXY environment variable. You can change the proxy's settings by setting the HTTPS_PROXY environment variable. Note that for HTTPS requests we need to create a Transport object and set InsecureSkipVerify to true for the TLS client configuration so that we can verify the certificate ourselves.
Step 3: Set up SOCKS proxy
You can also use a SOCKS proxy. SOCKS proxy is not based on HTTP and HTTPS protocols, but on SOCKS protocol. Set the SOCKS proxy in the terminal:
export ALL_PROXY=socks5://proxy.example.com:1080
Set the SOCKS proxy in the Golang program:
package main import ( "fmt" "golang.org/x/net/proxy" "net/http" "os" ) func main() { dialer, err := proxy.SOCKS5("tcp", "proxy.example.com:1080", nil, proxy.Direct) if err != nil { fmt.Println("Error setting up SOCKS5 proxy:", err) os.Exit(1) } transport := &http.Transport{Dial: dialer.Dial} client := &http.Client{Transport: transport} req, err := http.NewRequest("GET", "http://example.com", nil) if err != nil { fmt.Println("Error creating request:", err) os.Exit(1) } req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36") resp, err := client.Do(req) if err != nil { fmt.Println("Error executing request:", err) os.Exit(1) } fmt.Println("Response status:", resp.Status) }
The above program will send a GET request to "http://example.com" and print the response status . This program will use the SOCKS proxy specified by the ALL_PROXY environment variable. You can configure proxies for all protocols by setting the ALL_PROXY environment variable.
Summary
You can configure HTTP, HTTPS and SOCKS proxies in Golang by setting the HTTP_PROXY, HTTPS_PROXY and ALL_PROXY environment variables. This makes Golang more flexible when doing network programming compared to other programming languages.
The above is the detailed content of Let’s talk about how to set up a proxy in Golang. For more information, please follow other related articles on the PHP Chinese website!