Repository navigation
Expand file tree
/
Copy pathproxy_part2_test.go
More file actions
115 lines (98 loc) · 2.83 KB
/
Copy pathproxy_part2_test.go
File metadata and controls
115 lines (98 loc) · 2.83 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
package proxy
import (
"errors"
"net"
"testing"
"gnalloy.org/gnalloy/buffer"
"gnalloy.org/gnalloy/channel"
)
func TestSOCKS4ClientRejectsHandshakeFailure(t *testing.T) {
sink := &proxyCaptureSink{}
errorsSeen := &proxyErrorCapture{}
ch := channel.NewLocalChannel(1, buffer.NewHeapAllocator(), sink)
client, err := NewSOCKS4Client("127.0.0.1:443", "")
if err != nil {
t.Fatal(err)
}
if err := ch.Pipeline().AddLast("socks4", client); err != nil {
t.Fatal(err)
}
if err := ch.Pipeline().AddLast("errors", errorsSeen); err != nil {
t.Fatal(err)
}
ch.Pipeline().FireChannelActive()
ch.Pipeline().FireChannelRead(byteBufWithBytes([]byte{0x00, 0x5b, 0x00, 0x00, 127, 0, 0, 1}))
if len(errorsSeen.errors) != 1 || !errors.Is(errorsSeen.errors[0], ErrHandshakeFailed) {
t.Fatalf("errors=%v, want ErrHandshakeFailed", errorsSeen.errors)
}
sink.release()
}
func TestParseHAProxyV1AndV2(t *testing.T) {
v1 := []byte("PROXY TCP4 192.0.2.1 198.51.100.1 12345 443\r\nGET / HTTP/1.1\r\n")
info, consumed, err := ParseHAProxyHeader(v1)
if err != nil {
t.Fatal(err)
}
if info.Version != 1 || info.Protocol != "TCP4" || !info.SourceIP.Equal(net.ParseIP("192.0.2.1")) || info.SourcePort != 12345 {
t.Fatalf("info=%+v", info)
}
if consumed != len("PROXY TCP4 192.0.2.1 198.51.100.1 12345 443\r\n") {
t.Fatalf("consumed=%d", consumed)
}
v2 := append([]byte(nil), haproxyV2Signature...)
v2 = append(v2, 0x21, 0x11, 0x00, 0x0c)
v2 = append(v2, []byte{192, 0, 2, 1, 198, 51, 100, 1, 0x30, 0x39, 0x01, 0xbb}...)
info, consumed, err = ParseHAProxyHeader(v2)
if err != nil {
t.Fatal(err)
}
if info.Version != 2 || info.Protocol != "TCP4" || !info.DestIP.Equal(net.ParseIP("198.51.100.1")) || info.DestPort != 443 {
t.Fatalf("info=%+v", info)
}
if consumed != len(v2) {
t.Fatalf("consumed=%d", consumed)
}
}
type proxyCaptureSink struct {
writes []any
flushes int
}
func (s *proxyCaptureSink) Write(msg any) error {
s.writes = append(s.writes, msg)
return nil
}
func (s *proxyCaptureSink) Flush() error {
s.flushes++
return nil
}
func (s *proxyCaptureSink) Close() error {
return nil
}
func (s *proxyCaptureSink) release() {
for _, msg := range s.writes {
if releasable, ok := msg.(interface{ Release() }); ok {
releasable.Release()
}
}
}
type proxyEventCapture struct {
events []any
reads []any
}
func (h *proxyEventCapture) UserEventTriggered(_ *channel.HandlerContext, event any) {
h.events = append(h.events, event)
}
func (h *proxyEventCapture) ChannelRead(_ *channel.HandlerContext, msg any) {
h.reads = append(h.reads, msg)
}
type proxyErrorCapture struct {
errors []error
}
func (h *proxyErrorCapture) ExceptionCaught(_ *channel.HandlerContext, err error) {
h.errors = append(h.errors, err)
}
func byteBufWithBytes(src []byte) buffer.ByteBuf {
buf := buffer.NewHeapBuffer(len(src))
_, _ = buf.WriteBytes(src)
return buf
}