Repository navigation
Expand file tree
/
Copy pathconn_test.go
More file actions
295 lines (248 loc) · 6.41 KB
/
Copy pathconn_test.go
File metadata and controls
295 lines (248 loc) · 6.41 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package enproxy
import (
"bufio"
"bytes"
"crypto/tls"
"io"
"net"
"net/http"
"sync"
"testing"
"time"
"github.com/getlantern/keyman"
"github.com/getlantern/testify/assert"
. "github.com/getlantern/waitforserver"
)
const (
TEXT = "Hello byte counting world"
HR = "----------------------------"
)
var (
pk *keyman.PrivateKey
cert *keyman.Certificate
)
var (
proxyAddr = ""
httpAddr = ""
httpsAddr = ""
bytesReceived = int64(0)
bytesSent = int64(0)
destsReceived = make(map[string]bool)
destsSent = make(map[string]bool)
statMutex sync.Mutex
)
func TestPlainTextStreaming(t *testing.T) {
doTestPlainText(false, t)
}
func TestPlainTextBuffered(t *testing.T) {
doTestPlainText(true, t)
}
func TestTLSStreaming(t *testing.T) {
doTestTLS(false, t)
}
func TestTLSBuffered(t *testing.T) {
doTestTLS(true, t)
}
func TestBadStreaming(t *testing.T) {
doTestBad(false, t)
}
func TestBadBuffered(t *testing.T) {
doTestBad(true, t)
}
func doTestPlainText(buffered bool, t *testing.T) {
startServers(t)
conn, err := prepareConn(httpAddr, buffered, false, t)
if err != nil {
t.Fatalf("Unable to prepareConn: %s", err)
}
defer conn.Close()
doRequests(conn, t)
assert.Equal(t, 226, bytesReceived, "Wrong number of bytes received")
assert.Equal(t, 284, bytesSent, "Wrong number of bytes sent")
assert.True(t, destsSent[httpAddr], "http address wasn't recorded as sent destination")
assert.True(t, destsReceived[httpAddr], "http address wasn't recorded as received destination")
}
func doTestTLS(buffered bool, t *testing.T) {
startServers(t)
conn, err := prepareConn(httpsAddr, buffered, false, t)
if err != nil {
t.Fatalf("Unable to prepareConn: %s", err)
}
tlsConn := tls.Client(conn, &tls.Config{
ServerName: "localhost",
RootCAs: cert.PoolContainingCert(),
})
defer tlsConn.Close()
err = tlsConn.Handshake()
if err != nil {
t.Fatalf("Unable to handshake: %s", err)
}
doRequests(tlsConn, t)
assert.True(t, destsSent[httpsAddr], "https address wasn't recorded as sent destination")
assert.True(t, destsReceived[httpsAddr], "https address wasn't recorded as received destination")
}
func doTestBad(buffered bool, t *testing.T) {
startServers(t)
conn, err := prepareConn(httpAddr, buffered, true, t)
if err == nil {
defer conn.Close()
t.Error("Bad conn should have returned error on Connect()")
}
}
func prepareConn(addr string, buffered bool, fail bool, t *testing.T) (conn *Conn, err error) {
conn = &Conn{
Addr: addr,
Config: &Config{
DialProxy: func(addr string) (net.Conn, error) {
proto := "tcp"
if fail {
proto = "fakebad"
}
return net.Dial(proto, proxyAddr)
},
NewRequest: func(host string, method string, body io.Reader) (req *http.Request, err error) {
if host == "" {
host = proxyAddr
}
return http.NewRequest(method, "http://"+host, body)
},
BufferRequests: buffered,
},
}
err = conn.Connect()
return
}
func doRequests(conn net.Conn, t *testing.T) {
// Single request/response pair
req := makeRequest(conn, t)
readResponse(conn, req, t)
// Consecutive request/response pairs
req = makeRequest(conn, t)
readResponse(conn, req, t)
}
func makeRequest(conn net.Conn, t *testing.T) *http.Request {
req, err := http.NewRequest("GET", "http://www.google.com/humans.txt", nil)
if err != nil {
t.Fatalf("Unable to create request: %s", err)
}
req.Header.Set("Proxy-Connection", "keep-alive")
go func() {
err = req.Write(conn)
if err != nil {
t.Fatalf("Unable to write request: %s", err)
}
}()
return req
}
func readResponse(conn net.Conn, req *http.Request, t *testing.T) {
buffIn := bufio.NewReader(conn)
resp, err := http.ReadResponse(buffIn, req)
if err != nil {
t.Fatalf("Unable to read response: %s", err)
}
buff := bytes.NewBuffer(nil)
_, err = io.Copy(buff, resp.Body)
if err != nil {
t.Fatalf("Unable to read response body: %s", err)
}
text := string(buff.Bytes())
assert.Contains(t, text, TEXT, "Wrong text returned from server")
}
func startServers(t *testing.T) {
startHttpServer(t)
startHttpsServer(t)
startProxy(t)
}
func startProxy(t *testing.T) {
if proxyAddr != "" {
statMutex.Lock()
bytesReceived = 0
bytesSent = 0
destsReceived = make(map[string]bool)
destsReceived = make(map[string]bool)
statMutex.Unlock()
return
}
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Proxy unable to listen: %v", err)
}
proxyAddr = l.Addr().String()
go func() {
proxy := &Proxy{
OnBytesReceived: func(clientIp string, destAddr string, req *http.Request, bytes int64) {
statMutex.Lock()
bytesReceived += bytes
destsReceived[destAddr] = true
statMutex.Unlock()
},
OnBytesSent: func(clientIp string, destAddr string, req *http.Request, bytes int64) {
statMutex.Lock()
bytesSent += bytes
destsSent[destAddr] = true
statMutex.Unlock()
},
}
err := proxy.Serve(l)
if err != nil {
t.Fatalf("Proxy unable to serve: %s", err)
}
}()
if err := WaitForServer("tcp", proxyAddr, 1*time.Second); err != nil {
t.Fatal(err)
}
}
func startHttpServer(t *testing.T) {
if httpAddr != "" {
return
}
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("HTTP unable to listen: %v", err)
}
httpAddr = l.Addr().String()
doStartServer(t, l)
}
func startHttpsServer(t *testing.T) {
if httpsAddr != "" {
return
}
var err error
pk, err = keyman.GeneratePK(2048)
if err != nil {
t.Fatalf("Unable to generate key: %s", err)
}
// Generate self-signed certificate
cert, err = pk.TLSCertificateFor("tlsdialer", "localhost", time.Now().Add(1*time.Hour), true, nil)
if err != nil {
t.Fatalf("Unable to generate cert: %s", err)
}
keypair, err := tls.X509KeyPair(cert.PEMEncoded(), pk.PEMEncoded())
if err != nil {
t.Fatalf("Unable to generate x509 key pair: %s", err)
}
l, err := tls.Listen("tcp", "localhost:0", &tls.Config{
Certificates: []tls.Certificate{keypair},
})
if err != nil {
t.Fatalf("HTTP unable to listen: %v", err)
}
httpsAddr = l.Addr().String()
doStartServer(t, l)
}
func doStartServer(t *testing.T, l net.Listener) {
go func() {
httpServer := &http.Server{
Handler: http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
resp.Write([]byte(TEXT))
}),
}
err := httpServer.Serve(l)
if err != nil {
t.Fatalf("Unable to start http server: %s", err)
}
}()
if err := WaitForServer("tcp", l.Addr().String(), 1*time.Second); err != nil {
t.Fatal(err)
}
}