Go proxy fails when learning routes
php 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 AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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



Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...
