-
Notifications
You must be signed in to change notification settings - Fork 107
/
socks5_test.go
119 lines (109 loc) · 2.74 KB
/
socks5_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
package socks
import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"runtime"
"strconv"
"testing"
"time"
socks5 "github.com/h12w/go-socks5"
"github.com/phayes/freeport"
)
var httpTestServer = func() *http.Server {
var err error
httpTestPort, err := freeport.GetFreePort()
if err != nil {
panic(err)
}
s := &http.Server{
Addr: ":" + strconv.Itoa(httpTestPort),
Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("hello"))
}),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
go s.ListenAndServe()
runtime.Gosched()
tcpReady(httpTestPort, 2*time.Second)
return s
}()
func newTestSocksServer(withAuth bool) (port int) {
authenticator := socks5.Authenticator(socks5.NoAuthAuthenticator{})
if withAuth {
authenticator = socks5.UserPassAuthenticator{
Credentials: socks5.StaticCredentials{
"test_user": "test_pass",
},
}
}
conf := &socks5.Config{
Logger: log.New(ioutil.Discard, "", log.LstdFlags),
AuthMethods: []socks5.Authenticator{
authenticator,
},
}
srv, err := socks5.New(conf)
if err != nil {
panic(err)
}
socksTestPort, err := freeport.GetFreePort()
if err != nil {
panic(err)
}
go func() {
if err := srv.ListenAndServe("tcp", "0.0.0.0:"+strconv.Itoa(socksTestPort)); err != nil {
panic(err)
}
}()
runtime.Gosched()
tcpReady(socksTestPort, 2*time.Second)
return socksTestPort
}
func TestSocks5Anonymous(t *testing.T) {
socksTestPort := newTestSocksServer(false)
dialSocksProxy := Dial(fmt.Sprintf("socks5://127.0.0.1:%d?timeout=5s", socksTestPort))
tr := &http.Transport{Dial: dialSocksProxy}
httpClient := &http.Client{Transport: tr}
resp, err := httpClient.Get(fmt.Sprintf("http://localhost" + httpTestServer.Addr))
if err != nil {
panic(err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
if string(respBody) != "hello" {
t.Fatalf("expect response hello but got %s", respBody)
}
}
func TestSocks5Auth(t *testing.T) {
socksTestPort := newTestSocksServer(true)
dialSocksProxy := Dial(fmt.Sprintf("socks5://test_user:[email protected]:%d?timeout=5s", socksTestPort))
tr := &http.Transport{Dial: dialSocksProxy}
httpClient := &http.Client{Transport: tr}
resp, err := httpClient.Get(fmt.Sprintf("http://localhost" + httpTestServer.Addr))
if err != nil {
panic(err)
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
if string(respBody) != "hello" {
t.Fatalf("expect response hello but got %s", respBody)
}
}
func tcpReady(port int, timeout time.Duration) {
conn, err := net.DialTimeout("tcp", "127.0.0.1:"+strconv.Itoa(port), timeout)
if err != nil {
panic(err)
}
conn.Close()
}