-
Notifications
You must be signed in to change notification settings - Fork 71
/
handle_test.go
137 lines (111 loc) · 2.99 KB
/
handle_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
package socks5
import (
"bytes"
"io"
"log"
"net"
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/things-go/go-socks5/bufferpool"
"github.com/things-go/go-socks5/statute"
)
type MockConn struct {
buf bytes.Buffer
}
func (m *MockConn) Write(b []byte) (int, error) {
return m.buf.Write(b)
}
func (m *MockConn) RemoteAddr() net.Addr {
return &net.TCPAddr{IP: []byte{127, 0, 0, 1}, Port: 65432}
}
func TestRequest_Connect(t *testing.T) {
// Create a local listener
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
go func() {
conn, err := l.Accept()
require.NoError(t, err)
defer conn.Close()
buf := make([]byte, 4)
_, err = io.ReadAtLeast(conn, buf, 4)
require.NoError(t, err)
require.Equal(t, []byte("ping"), buf)
conn.Write([]byte("pong")) //nolint: errcheck
}()
lAddr := l.Addr().(*net.TCPAddr)
// Make proxy server
proxySrv := &Server{
rules: NewPermitAll(),
resolver: DNSResolver{},
logger: NewLogger(log.New(os.Stdout, "socks5: ", log.LstdFlags)),
bufferPool: bufferpool.NewPool(32 * 1024),
}
// Create the connect request
buf := bytes.NewBuffer([]byte{
statute.VersionSocks5, statute.CommandConnect, 0,
statute.ATYPIPv4, 127, 0, 0, 1, byte(lAddr.Port >> 8), byte(lAddr.Port),
})
// Send a ping
buf.WriteString("ping")
// Handle the request
rsp := new(MockConn)
req, err := ParseRequest(buf)
require.NoError(t, err)
err = proxySrv.handleRequest(rsp, req)
require.NoError(t, err)
// Verify response
out := rsp.buf.Bytes()
expected := []byte{
statute.VersionSocks5, statute.RepSuccess, 0,
statute.ATYPIPv4, 127, 0, 0, 1, 0, 0,
'p', 'o', 'n', 'g',
}
// Ignore the port for both
out[8] = 0
out[9] = 0
require.Equal(t, expected, out)
}
func TestRequest_Connect_RuleFail(t *testing.T) {
// Create a local listener
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
go func() {
conn, err := l.Accept()
require.NoError(t, err)
defer conn.Close()
buf := make([]byte, 4)
_, err = io.ReadAtLeast(conn, buf, 4)
require.NoError(t, err)
require.Equal(t, []byte("ping"), buf)
conn.Write([]byte("pong")) //nolint: errcheck
}()
lAddr := l.Addr().(*net.TCPAddr)
// Make server
s := &Server{
rules: NewPermitNone(),
resolver: DNSResolver{},
logger: NewLogger(log.New(os.Stdout, "socks5: ", log.LstdFlags)),
bufferPool: bufferpool.NewPool(32 * 1024),
}
// Create the connect request
buf := bytes.NewBuffer([]byte{
statute.VersionSocks5, statute.CommandConnect, 0,
statute.ATYPIPv4, 127, 0, 0, 1, byte(lAddr.Port >> 8), byte(lAddr.Port),
})
// Send a ping
buf.WriteString("ping")
// Handle the request
rsp := new(MockConn)
req, err := ParseRequest(buf)
require.NoError(t, err)
err = s.handleRequest(rsp, req)
require.Contains(t, err.Error(), "blocked by rules")
// Verify response
out := rsp.buf.Bytes()
expected := []byte{
statute.VersionSocks5, statute.RepRuleFailure, 0,
statute.ATYPIPv4, 0, 0, 0, 0, 0, 0,
}
require.Equal(t, expected, out)
}