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!