-
Notifications
You must be signed in to change notification settings - Fork 6
/
conn_test.go
175 lines (149 loc) · 5.09 KB
/
conn_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
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
package viaproxy_test
import (
"bytes"
"io"
"io/ioutil"
"log"
"net"
"testing"
"github.com/inkel/viaproxy"
)
type conn struct {
net.Conn
data io.Reader
}
func (c *conn) Read(b []byte) (n int, err error) { return c.data.Read(b) }
func (c *conn) LocalAddr() net.Addr { return &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 9876} }
func (c *conn) RemoteAddr() net.Addr { return &net.TCPAddr{IP: net.ParseIP("10.0.1.2"), Port: 1234} }
func testConn(data []byte) net.Conn { return &conn{data: bytes.NewReader(data)} }
type testAddr string
func (a testAddr) Network() string { return "tcp" }
func (a testAddr) String() string { return string(a) }
func equalAddr(a, b net.Addr) bool {
return (a == nil && b == nil) || a.Network() == b.Network() && a.String() == b.String()
}
func TestWrap(t *testing.T) {
cases := []struct {
line, data []byte
remoteIP string
remotePort int
proxy net.Addr
err bool
}{
{[]byte("PROXY TCP4 192.168.1.20 10.0.0.1 5678 1234\r\nfoo\r\nbar\r\n"), []byte("foo\r\nbar\r\n"), "192.168.1.20", 5678, &net.TCPAddr{IP: net.IPv4(10, 0, 0, 1), Port: 1234}, false},
{[]byte("PROXY TCP6 fe80::aede:48ff:fe00:1122 ::1 5678 1234\r\nfoo\r\nbar\r\n"), []byte("foo\r\nbar\r\n"), "fe80::aede:48ff:fe00:1122", 5678, &net.TCPAddr{IP: net.ParseIP("::1"), Port: 1234}, false},
{[]byte("PROXY UNKNOWN\r\nfoo\r\nbar\r\n"), []byte("foo\r\nbar\r\n"), "10.0.1.2", 1234, nil, false},
// Invalid proxy protocol lines
{[]byte("GET / HTTP/1.0\r\n"), nil, "", -1, nil, true},
{[]byte("PROXY TCP5\r\n"), nil, "", -1, nil, true},
{[]byte("PROXY TCP4 192.168.X.20 10.0.0.1 5678 1234\r\n"), nil, "", -1, nil, true},
{[]byte("PROXY TCP4 192.168.1.20 10.X.0.1 5678 1234\r\n"), nil, "", -1, nil, true},
{[]byte("PROXY TCP4 192.168.1.20 10.0.0.1 567X 1234\r\n"), nil, "", -1, nil, true},
{[]byte("PROXY TCP4 192.168.1.20 10.0.0.1 5678 123X\r\n"), nil, "", -1, nil, true},
{[]byte("PROXY TCP4 192.168.1.20 10.0.0.1 5678\r\nfoo\r\nbar\r\n"), nil, "", -1, nil, true},
{[]byte("PROXY TCP4 192.168.1.20 10.0.0.1 5678 1234"), nil, "", -1, nil, true},
{[]byte("PROXY TCP4 192.168.1.20 10.0.0.1 5678 1234\r"), nil, "", -1, nil, true},
{[]byte("PROXY TCP4 192.168.1.20 10.0.0.1"), nil, "", -1, nil, true},
{[]byte("PROXY TCP4 192.168.1.20"), nil, "", -1, nil, true},
{[]byte("PROXY TCP 192.168.1.20 10.0.0.1 5678 1234\r\n"), nil, "", -1, nil, true},
{[]byte("PROXY TCP\r\n192.168.1.20 10.0.0.1 5678 1234\r\n"), nil, "", -1, nil, true},
{[]byte("PROXY TCP4"), nil, "", -1, nil, true},
{[]byte(""), nil, "", -1, nil, true},
}
for _, c := range cases {
t.Run(string(c.line), func(t *testing.T) {
cn, err := viaproxy.Wrap(testConn(c.line))
if c.err && err == nil {
t.Fatal("expecting error, got nil")
}
if !c.err && err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cn == nil && c.err && err != nil {
// no need to continue processing
return
}
var remote net.Addr = &net.TCPAddr{IP: net.ParseIP(c.remoteIP), Port: c.remotePort}
if !equalAddr(remote, cn.RemoteAddr()) {
t.Errorf("expecting RemoteAddr() %v, got %v", remote, cn.RemoteAddr())
}
data, err := ioutil.ReadAll(cn)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(c.data, data) {
t.Errorf("expecting data %q, got %q", c.data, data)
}
if !equalAddr(cn.ProxyAddr(), c.proxy) {
t.Errorf("expecting ProxyAddr() %v, got %v", c.proxy, cn.ProxyAddr())
}
})
}
}
func ExampleListener_Accept() {
// Listen on TCP port 8080 for connections coming from a proxy that sends
// the Proxy Protocol header.
l, err := viaproxy.Listen("tcp", ":8080")
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
// Wait for a connection.
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
// The connection should be safe to be converted to a *viaproxy.Conn
// structure.
cn := conn.(*viaproxy.Conn)
log.Printf("remote address is: %v", cn.RemoteAddr())
log.Printf("local address is: %v", cn.LocalAddr())
log.Printf("proxy address is: %v", cn.ProxyAddr())
cn.Close()
}
}
func ExampleListener_AcceptFromProxy() {
// Listen on TCP port 8080 for connections coming from a proxy that sends
// the Proxy Protocol header.
l, err := viaproxy.Listen("tcp", ":8080")
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
// Wait for a connection.
cn, err := l.AcceptFromProxy()
if err != nil {
log.Fatal(err)
}
log.Printf("remote address is: %v", cn.RemoteAddr())
log.Printf("local address is: %v", cn.LocalAddr())
log.Printf("proxy address is: %v", cn.ProxyAddr())
cn.Close()
}
}
func ExampleWrap() {
// Listen on TCP port 8080 for connections coming from a proxy that sends
// the Proxy Protocol header.
l, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
// Wait for a connection.
cn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
pcn, err := viaproxy.Wrap(cn)
if err != nil {
log.Fatal(err)
}
log.Printf("remote address is: %v", pcn.RemoteAddr())
log.Printf("local address is: %v", pcn.LocalAddr())
log.Printf("proxy address is: %v", pcn.ProxyAddr())
pcn.Close()
}
}