-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconnectProxy.go
More file actions
74 lines (61 loc) · 1.63 KB
/
connectProxy.go
File metadata and controls
74 lines (61 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package vproxy
import (
"net/http"
"net"
"context"
)
var resultStatus200 = []byte("HTTP/1.1 200 Connection established\r\n\r\n")
type proxyConnect struct{
*Proxy
}
func host2addr(host, scheme string) string {
rh, rp, _ := net.SplitHostPort(host)
if rp == "" {
rp = "443"
if scheme == "http" {
rp = "80"
}
}
return net.JoinHostPort(rh, rp)
}
func (T *proxyConnect) ServeHTTP(rw http.ResponseWriter, req *http.Request){
var (
remoteConn net.Conn
err error
remoteAddr = host2addr(req.URL.Host, req.URL.Scheme)
ctx = req.Context()
)
var dial func(ctx context.Context, network, address string) (net.Conn, error)
if T.Proxy.DialContext != nil {
dial = T.Proxy.DialContext
}else{
dial = new(net.Dialer).DialContext
}
remoteConn, err = dial(ctx, "tcp", remoteAddr)
if err != nil {
T.Proxy.logf(Error, err.Error())
http.Error(rw, err.Error(), http.StatusBadGateway)
return
}
hj, ok := rw.(http.Hijacker)
if !ok {
remoteConn.Close()
T.Proxy.logf(Error, "代理服务器不支持劫持客户端连接转TCP")
http.Error(rw, "Proxy server doesn't support hijacking", http.StatusInternalServerError)
return
}
conn, _, err := hj.Hijack()
if err != nil {
remoteConn.Close()
T.Proxy.logf(Error, err.Error())
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
conn.Write(resultStatus200)
var bufSize int = defaultDataBufioSize
if T.Proxy.DataBufioSize != 0 {
bufSize = T.Proxy.DataBufioSize
}
go copyDate(remoteConn, conn, bufSize)
go copyDate(conn, remoteConn, bufSize)
}