-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_test.go
91 lines (79 loc) · 2.08 KB
/
proxy_test.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
package tcpwebsocketproxy
import (
"io"
"net"
"testing"
"time"
)
func TestLoopback(t *testing.T) {
// Start an echo server running on localhost:4000
ln, err := net.Listen("tcp", "127.0.0.1:4000")
if err != nil {
t.Fatalf("[SERVER] Failed to start echo server: %v", err)
}
t.Logf("[SERVER] Running echo server on 127.0.0.1:4000")
go func() {
server_conn, err := ln.Accept()
if err != nil {
t.Fatalf("[SERVER] Failed to accept connection: %v", err)
}
defer server_conn.Close()
t.Logf("[SERVER] Accepted connection from %v", server_conn.RemoteAddr())
buf := make([]byte, 256)
for {
n, err := server_conn.Read(buf)
if err != nil {
if err == io.EOF {
break
}
t.Fatalf("[SERVER] Read failed: %v", err)
}
if n > 0 {
t.Logf("[SERVER] Read %d bytes", n)
n, err := server_conn.Write(buf[0:n])
if err != nil {
t.Fatalf("[SERVER] Write failed: %v", err)
}
t.Logf("[SERVER] Wrote %d bytes", n)
}
}
t.Logf("[SERVER] Connection closed")
}()
// Proxy localhost:3000 to localhost:4000 over websockets
go ProxyWebsocket("/proxy", "localhost:4000")
go ProxyPort(3000, "ws://localhost:8080/proxy")
// Connect to the the proxied server
var client_conn net.Conn
start := time.Now()
for {
client_conn, err = net.Dial("tcp", "127.0.0.1:3000")
if err == nil {
break
}
if time.Since(start) > 50*time.Millisecond {
t.Fatalf("[CLIENT] Timed out trying to connect")
} else if err != nil {
t.Logf("[CLIENT] Failed to connect to proxied port %v", err)
}
}
defer client_conn.Close()
t.Logf("[CLIENT] Connected to echo server")
// Send data to the server
test := func(s string) {
n, err := client_conn.Write([]byte(s))
if err != nil {
t.Fatalf("[CLIENT] Failed to write: %v", err)
}
t.Logf("[CLIENT] Wrote %v bytes", n)
buf := make([]byte, 256)
n, err = client_conn.Read(buf)
if err != nil {
t.Fatalf("[CLIENT] Failed to read: %v", err)
}
t.Logf("[CLIENT] Read %d bytes", n)
if string(buf[0:n]) != s {
t.Fatalf("[CLIENT] Expected %v but got %v", s, string(buf))
}
}
test("Hello world\n")
}