-
Notifications
You must be signed in to change notification settings - Fork 68
/
direct.go
164 lines (145 loc) · 4.05 KB
/
direct.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package mallory
import (
"errors"
"io"
"net"
"net/http"
"time"
)
var (
ErrShouldProxy = errors.New("should proxy")
)
type closeWriter interface {
CloseWrite() error
}
// Direct fetcher
type Direct struct {
Tr *http.Transport
}
// Create and initialize
func NewDirect(shouldProxyTimeout time.Duration) *Direct {
if shouldProxyTimeout == 0 {
shouldProxyTimeout = 200 * time.Millisecond
}
tr := http.DefaultTransport.(*http.Transport)
tr.Dial = (&net.Dialer{
Timeout: shouldProxyTimeout,
}).Dial
return &Direct{Tr: tr}
}
// Data flow:
// 1. Receive request R1 from client
// 2. Re-post request R1 to remote server(the one client want to connect)
// 3. Receive response P1 from remote server
// 4. Send response P1 to client
func (self *Direct) ServeHTTP(w http.ResponseWriter, r *http.Request) (err error) {
if r.Method == "CONNECT" {
L.Println("this function can not handle CONNECT method")
http.Error(w, r.Method, http.StatusMethodNotAllowed)
return
}
start := time.Now()
// Client.Do is different from DefaultTransport.RoundTrip ...
// Client.Do will change some part of request as a new request of the server.
// The underlying RoundTrip never changes anything of the request.
resp, err := self.Tr.RoundTrip(r)
if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
L.Printf("RoundTrip: %s, reproxy...\n", err.Error())
err = ErrShouldProxy
return
}
L.Printf("RoundTrip: %s\n", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
// please prepare header first and write them
CopyHeader(w, resp)
w.WriteHeader(resp.StatusCode)
n, err := io.Copy(w, resp.Body)
if err != nil {
L.Printf("Copy: %s\n", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
d := BeautifyDuration(time.Since(start))
ndtos := BeautifySize(n)
L.Printf("RESPONSE %s %s in %s <-%s\n", r.URL.Host, resp.Status, d, ndtos)
return
}
// Data flow:
// 1. Receive CONNECT request from the client
// 2. Dial the remote server(the one client want to conenct)
// 3. Send 200 OK to client if the connection is established
// 4. Exchange data between client and server
func (self *Direct) Connect(w http.ResponseWriter, r *http.Request) (err error) {
if r.Method != "CONNECT" {
L.Println("this function can only handle CONNECT method")
http.Error(w, r.Method, http.StatusMethodNotAllowed)
return
}
start := time.Now()
// Use Hijacker to get the underlying connection
hij, ok := w.(http.Hijacker)
if !ok {
s := "Server does not support Hijacker"
L.Println(s)
http.Error(w, s, http.StatusInternalServerError)
return
}
// connect the remote client directly
dst, err := self.Tr.Dial("tcp", r.URL.Host)
if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
L.Printf("RoundTrip: %s, reproxy...\n", err.Error())
err = ErrShouldProxy
return
}
L.Printf("Dial: %s\n", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer dst.Close()
src, _, err := hij.Hijack()
if err != nil {
L.Printf("Hijack: %s\n", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer src.Close()
// Once connected successfully, return OK
src.Write([]byte("HTTP/1.1 200 OK\r\n\r\n"))
// Proxy is no need to know anything, just exchange data between the client
// the the remote server.
copyAndWait := func(dst, src net.Conn, c chan int64) {
n, err := io.Copy(dst, src)
if err != nil {
L.Printf("Copy: %s\n", err.Error())
// FIXME: how to report error to dst ?
}
if tcpConn, ok := dst.(closeWriter); ok {
tcpConn.CloseWrite()
}
c <- n
}
// client to remote
stod := make(chan int64)
go copyAndWait(dst, src, stod)
// remote to client
dtos := make(chan int64)
go copyAndWait(src, dst, dtos)
var nstod, ndtos int64
for i := 0; i < 2; {
select {
case nstod = <-stod:
i++
case ndtos = <-dtos:
i++
}
}
d := BeautifyDuration(time.Since(start))
L.Printf("CLOSE %s after %s ->%s <-%s\n",
r.URL.Host, d, BeautifySize(nstod), BeautifySize(ndtos))
return
}