Table of Contents
Question content
edit
Solution
Home Backend Development Golang Go proxy fails when learning routes

Go proxy fails when learning routes

Feb 14, 2024 pm 05:10 PM
go language

了解路由时 Go 代理失败

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)
}
Copy after login

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)
}
Copy after login

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)
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use reflection to access private fields and methods in golang How to use reflection to access private fields and methods in golang May 03, 2024 pm 12:15 PM

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

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Tips for dynamically creating new functions in golang functions

The difference between performance testing and unit testing in Go language The difference between performance testing and unit testing in Go language May 08, 2024 pm 03:09 PM

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? What pitfalls should we pay attention to when designing distributed systems with Golang technology? May 07, 2024 pm 12:39 PM

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

Golang technology libraries and tools used in machine learning Golang technology libraries and tools used in machine learning May 08, 2024 pm 09:42 PM

Golang technology libraries and tools used in machine learning

The role of Golang technology in mobile IoT development The role of Golang technology in mobile IoT development May 09, 2024 pm 03:51 PM

The role of Golang technology in mobile IoT development

Can golang variable parameters be used for function return values? Can golang variable parameters be used for function return values? Apr 29, 2024 am 11:33 AM

Can golang variable parameters be used for function return values?

The evolution of golang function naming convention The evolution of golang function naming convention May 01, 2024 pm 03:24 PM

The evolution of golang function naming convention

See all articles