Blogger Information
Blog 12
fans 0
comment 0
visits 27681
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
go实现一个反向代理
小生我怕怕啊的博客
Original
496 people have browsed it
package mainimport (    "bufio"    "log"    "net/http"    "net/url")var (    proxy_addr = "http://127.0.0.1:2003"    port       = "2002")func handler(w http.ResponseWriter, r *http.Request) {    //step 1 解析代理地址,并更改请求体的协议和主机    proxy, err := url.Parse(proxy_addr)    r.URL.Scheme = proxy.Scheme    r.URL.Host = proxy.Host    //step 2 请求下游    transport := http.DefaultTransport    resp, err := transport.RoundTrip(r)    if err != nil {        log.Print(err)        return    }    //step 3 把下游请求内容返回给上游    for k, vv := range resp.Header {        for _, v := range vv {            w.Header().Add(k, v)        }    }    defer resp.Body.Close()    bufio.NewReader(resp.Body).WriteTo(w)}func main() {    http.HandleFunc("/", handler)    log.Println("Start serving on port " + port)    err := http.ListenAndServe(":"+port, nil)    if err != nil {        log.Fatal(err)    }}
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post