forked from mmatczuk/go-http-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintegration_test.go
264 lines (228 loc) · 5 KB
/
integration_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
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
// Copyright (C) 2017 Michał Matczuk
// Copyright (C) 2022 jlandowner
// Use of this source code is governed by an AGPL-style
// license that can be found in the LICENSE file.
package tunnel_test
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"math/rand"
"net"
"os"
"sync"
"testing"
"time"
tunnel "github.com/jlandowner/go-tcp-tunnel"
"github.com/jlandowner/go-tcp-tunnel/log"
"github.com/jlandowner/go-tcp-tunnel/proto"
)
const (
payloadInitialSize = 512
payloadLen = 10
)
// echoTCP accepts connections and copies back received bytes.
func echoTCP(l net.Listener) {
for {
conn, err := l.Accept()
if err != nil {
return
}
go func() {
io.Copy(conn, conn)
}()
}
}
func makeEcho(t testing.TB) (tcp net.Listener) {
var err error
// TCP echo
tcp, err = net.Listen("tcp", ":0")
if err != nil {
t.Fatal(err)
}
go echoTCP(tcp)
return
}
func makeTunnelServer(t testing.TB) *tunnel.Server {
s, err := tunnel.NewServer(&tunnel.ServerConfig{
Addr: ":0",
AutoSubscribe: true,
TLSConfig: tlsConfig(),
Logger: log.NewStdLogger(),
})
if err != nil {
t.Fatal(err)
}
go s.Start()
return s
}
func makeTunnelClient(t testing.TB, serverAddr string, tcpLocalAddr, tcpAddr net.Addr) *tunnel.Client {
tcpProxy := tunnel.NewMultiTCPProxy(map[string]string{
port(tcpLocalAddr): tcpAddr.String(),
}, log.NewStdLogger())
tunnels := map[string]*proto.Tunnel{
proto.TCP: {
Protocol: proto.TCP,
Addr: tcpLocalAddr.String(),
},
}
c, err := tunnel.NewClient(&tunnel.ClientConfig{
ServerAddr: serverAddr,
TLSClientConfig: tlsConfig(),
Tunnels: tunnels,
Proxy: tunnel.Proxy(tunnel.ProxyFuncs{
TCP: tcpProxy.Proxy,
}),
Logger: log.NewStdLogger(),
})
if err != nil {
t.Fatal(err)
}
go func() {
if err := c.Start(); err != nil {
t.Log(err)
}
}()
return c
}
func TestIntegration(t *testing.T) {
// local services
tcp := makeEcho(t)
defer tcp.Close()
// server
s := makeTunnelServer(t)
defer s.Stop()
tcpLocalAddr := freeAddr()
// client
c := makeTunnelClient(t, s.Addr(),
tcpLocalAddr, tcp.Addr(),
)
// FIXME: replace sleep with client state change watch when ready
time.Sleep(500 * time.Millisecond)
defer c.Stop()
payload := randPayload(payloadInitialSize, payloadLen)
table := []struct {
S []uint
}{
{[]uint{200, 160, 120, 80, 40, 20}},
{[]uint{40, 80, 120, 160, 200}},
{[]uint{0, 0, 0, 0, 0, 0, 0, 0, 0, 200}},
}
var wg sync.WaitGroup
for _, test := range table {
for i, repeat := range test.S {
p := payload[i]
r := repeat
wg.Add(1)
go func() {
testTCP(t, tcpLocalAddr, p, r)
wg.Done()
}()
}
}
wg.Wait()
}
func testTCP(t testing.TB, addr net.Addr, payload []byte, repeat uint) {
conn, err := net.Dial("tcp", addr.String())
if err != nil {
t.Fatal("Dial failed", err)
}
defer conn.Close()
var buf = make([]byte, 10*1024*1024)
var read, write int
for repeat > 0 {
m, err := conn.Write(payload)
if err != nil {
t.Error("Write failed", err)
}
if m != len(payload) {
t.Log("Write mismatch", m, len(payload))
}
write += m
n, err := conn.Read(buf)
if err != nil {
t.Error("Read failed", err)
}
read += n
repeat--
}
for read < write {
t.Log("No yet read everything", "write", write, "read", read)
time.Sleep(50 * time.Millisecond)
n, err := conn.Read(buf)
if err != nil {
t.Error("Read failed", err)
}
read += n
}
if read != write {
t.Fatal("Write read mismatch", read, write)
}
}
//
// helpers
//
// randPayload returns slice of randomly initialised data buffers.
func randPayload(initialSize, n int) [][]byte {
payload := make([][]byte, n)
l := initialSize
for i := 0; i < n; i++ {
payload[i] = randBytes(l)
l *= 2
}
return payload
}
func randBytes(n int) []byte {
b := make([]byte, n)
read, err := rand.Read(b)
if err != nil {
panic(err)
}
if read != n {
panic("read did not fill whole slice")
}
return b
}
func freeAddr() net.Addr {
l, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
defer l.Close()
return l.Addr()
}
func port(addr net.Addr) string {
return fmt.Sprint(addr.(*net.TCPAddr).Port)
}
func tlsConfig() *tls.Config {
cert, err := tls.LoadX509KeyPair("./testdata/selfsigned.crt", "./testdata/selfsigned.key")
if err != nil {
panic(err)
}
f, err := os.Open("./testdata/selfsigned.crt")
if err != nil {
panic(err)
}
ca, err := io.ReadAll(f)
if err != nil {
panic(err)
}
caPool := x509.NewCertPool()
if ok := caPool.AppendCertsFromPEM(ca); !ok {
panic("failed to append cert")
}
c := &tls.Config{
ServerName: "localhost",
Certificates: []tls.Certificate{cert},
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: caPool,
RootCAs: caPool,
SessionTicketsDisabled: true,
MinVersion: tls.VersionTLS12,
CipherSuites: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
PreferServerCipherSuites: true,
NextProtos: []string{"h2"},
}
return c
}