CONNECT 方法的支持
通过 CONNECT 方法可以建立 Web 隧道,支持https等。
用 CONNECT建立 Web 隧道的过程如下:
- 客户端发送CONNECT 请求给代理服务器;
- 代理接收CONNECT请求后建立一条客户端到代理之间的TCP 连接;
- 代理建立远程服务器到代理之间的TCP连接,并返回含HTTP 200响应码的报文给客户端;
- 之后,两条TCP连接直接进行数据交换。
代码如下:
var HTTP_200 = []byte("HTTP/1.1 200 Connection Established\r\n\r\n")
//HttpsHandler handles any connection which need connect method.
func (proxy *ProxyServer) HttpsHandler(rw http.ResponseWriter, req *http.Request) {
log.Printf("%v tried to connect to %v", proxy.User, req.RequestURI)
hj, _ := rw.(http.Hijacker)
Client, _, err := hj.Hijack()
if err != nil {
log.Printf("%v failed to get Tcp connection of \n", proxy.User, req.RequestURI)
return
}
defer Client.Close()
Remote, err := net.Dial("tcp", req.URL.Host)
if err != nil {
log.Printf("%v failed to connect %v\n", proxy.User, req.RequestURI)
return
}
defer Remote.Close()
Client.Write(HTTP_200)
go io.Copy(Remote, Client)
io.Copy(Client, Remote)
}