Go proxy fails when learning routes
Feb 14, 2024 pm 05:10 PMphp editor Strawberry understands the importance of routing when solving the problem of Go proxy failure. Routing is a core concept in network communications, which determines how data packets should be delivered from source address to destination address. When using Go language for proxying, it is very important to configure routing correctly. By deeply understanding the principles of routing and related configurations, we can effectively solve the problem of Go proxy failure and ensure the stability and reliability of network communication. In this article, we will introduce the working principle of routing and common configuration methods to help everyone better understand and apply routing technology.
Question content
I have a simple go proxy like this. I want to proxy requests through it and modify the responses of some websites. The sites run via tls, but my proxy is just a local server.
func main() { target, _ := url.parse("https://www.google.com") proxy := httputil.newsinglehostreverseproxy(target) proxy.modifyresponse = rewritebody http.handle("/", proxy) http.listenandserve(":80", proxy) }
Result: 404 error, as shown below:
As I understand it, the proxy server initiates the request and closes the request, then returns a modified response. I'm not sure it will fail here. Am I missing something to forward the headers to where this request fails?
edit
I have the routing working properly. Initially, I was interested in modifying the response, but saw no changes other than seeing the magical
header.
func modifyResponse() func(*http.Response) error { return func(resp *http.Response) error { resp.Header.Set("X-Proxy", "Magical") b, _ := ioutil.ReadAll(resp.Body) b = bytes.Replace(b, []byte("About"), []byte("Modified String Test"), -1) // replace html body := ioutil.NopCloser(bytes.NewReader(b)) resp.Body = body resp.ContentLength = int64(len(b)) resp.Header.Set("Content-Length", strconv.Itoa(len(b))) resp.Body.Close() return nil } } func main() { target, _ := url.Parse("https://www.google.com") proxy := httputil.NewSingleHostReverseProxy(target) director := proxy.Director proxy.Director = func(r *http.Request) { director(r) r.Host = r.URL.Hostname() } proxy.ModifyResponse = modifyResponse() http.Handle("/", proxy) http.ListenAndServe(":80", proxy) }
Solution
The key issue is mentioned in the document, but it is not clear from the document how to deal with it accurately:
newsinglehostreverseproxy does not rewrite the host header. rewrite Host header, use reverseproxy directly with a custom director policy.
https://www.php.cn/link/747e32ab0fea7fbd2ad9ec03daa3f840
You are not using reverseproxy
directly. You can still use newsinglehostreverseproxy
and adjust the director
function like this:
func main() { target, _ := url.Parse("https://www.google.com") proxy := httputil.NewSingleHostReverseProxy(target) director := proxy.Director proxy.Director = func(r *http.Request) { director(r) r.Host = r.URL.Hostname() // Adjust Host } http.Handle("/", proxy) http.ListenAndServe(":80", proxy) }
The above is the detailed content of Go proxy fails when learning routes. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use reflection to access private fields and methods in golang

Tips for dynamically creating new functions in golang functions

The difference between performance testing and unit testing in Go language

What pitfalls should we pay attention to when designing distributed systems with Golang technology?

Golang technology libraries and tools used in machine learning

The role of Golang technology in mobile IoT development

Can golang variable parameters be used for function return values?

The evolution of golang function naming convention
