This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("FMT" "io" "Log" "Net/http" "Net/http/httputil" "Net/url") Func httpuserinfo (w http. Responsewriter, R *http. Request) {ret: = "Your addr is:" + r.remoteaddr + "\ r \ n" ret + = "Request headers:" + FMT. Sprint (r.header) io. WriteString (W, ret)}func main () {targeturl, err: = URL. Parse ("http://127.0.0.1:80") log. PRINTLN ("program start ...") if err! = Nil {log. Println ("Bad URL:", err) Return}httpproxy: = Httputil. Newsinglehostreverseproxy (TargetUrl) http. Handlefunc ("/myinfo/", Httpuserinfo) http. Handle ("/", Httpproxy) log. Println ("Listen HTTP proxy on:8001 ...") http. Listenandserve (": 8001", nil)}
There is a flaw in the above code, that is, after HTTP Proxy HTTP protocol header of the host domain will not change, such as proxy to www.qq.com, when grasping the packet:
get/http/1.1
host:127.0.0.1:8001
user-agent:mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; trident/5.0)
Accept:text/html, Application/xhtml+xml, */*
Accept-encoding:gzip, deflate
Accept-language:zh-cn
x-forwarded-for:192.168.20.155
This is the simplest way to broker a server without checking the host domain. It is also easy to check the host domain's Web site to modify the implementation within the package itself or handle the HTTP header itself.