-
Notifications
You must be signed in to change notification settings - Fork 10
/
easyss_test.go
370 lines (319 loc) · 8.96 KB
/
easyss_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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package easyss
import (
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"sync"
"testing"
"time"
"github.com/coocood/freecache"
"github.com/oschwald/geoip2-golang"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"golang.org/x/net/proxy"
)
const (
CACert = `-----BEGIN CERTIFICATE-----
MIIB1DCCAXqgAwIBAgIULPyfQyUssIDTxsGLzQJx6w5rb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DyRDp1hCq2toD1wCEjCXOJx5BaF1n8Wo4GHMIGEMA4GA1Ud
DwEB/wQEAwIFoDATBgNVHSUEDDAKBggrBgEFBQcDATAMBgNVHRMBAf8EAjAAMB0G
A1UdDgQWBBSnOwxAIjDEYPp88Ooed4HWL9r8bTAfBgNVHSMEGDAWgBTympPTCYV1
FDqDFf5lYBaEyid8hDAPBgNVHREECDAGhwR/AAABMAoGCCqGSM49BAMCA0gAMEUC
IGphTgfgOgRRGIqDX/ByZx33QUh6P+nRSPyMPLV24nDGAiEAl0I45LBWXopCzDLD
ftJgQof/GMr8+pMLn0UM/Xv5xec=
-----END CERTIFICATE-----
`
ServerKey = `-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIO8NZoPnbSD3NL9PmL9zMz/OUvFuwYVGzzicctyWbf57oAoGCCqGSM49
AwEHoUQDQgAE2gbiDfDZg5kjJkqV2zadqc6iI07cr3HIBBk3cU27VhdmL6qsJkGP
3zw8kQ6dYQqtraA9cAhIwlziceQWhdZ/Fg==
-----END EC PRIVATE KEY-----
`
)
const (
Server = "127.0.0.1"
Password = "test-pass"
ServerPort = 9999
LocalPort = 4567
Method = "aes-256-gcm"
)
const (
TestHTTPSURL = "https://cloud.tencent.com"
TestHTTPURL = "http://cloud.tencent.com"
)
const (
CloseWriteServerPort = "8888"
)
func TestEasyss(t *testing.T) {
suite.Run(t, new(EasyssSuite))
}
type EasyssSuite struct {
suite.Suite
certPath string
keyPath string
caPath string
tempDir string
ss *Easyss
server *EasyServer
}
func (es *EasyssSuite) SetupTest() {
tempDir := es.T().TempDir()
certPath := filepath.Join(tempDir, "cert.pem")
err := os.WriteFile(certPath, []byte(ServerCert), os.ModePerm)
es.Require().Nil(err)
keyPath := filepath.Join(tempDir, "key.pem")
err = os.WriteFile(keyPath, []byte(ServerKey), os.ModePerm)
es.Require().Nil(err)
caPath := filepath.Join(tempDir, "ca.pem")
err = os.WriteFile(caPath, []byte(CACert), os.ModePerm)
es.Require().Nil(err)
es.caPath = caPath
es.tempDir = tempDir
es.certPath = certPath
es.keyPath = keyPath
}
func (es *EasyssSuite) BeforeTest(suiteName, testName string) {
serverConfig := &ServerConfig{
Server: Server,
ServerPort: ServerPort,
Password: Password,
CertPath: es.certPath,
KeyPath: es.keyPath,
}
serverConfig.SetDefaultValue()
server, err := NewServer(serverConfig)
es.Nil(err)
server.disableValidateAddr = true
es.server = server
config := &Config{
Server: Server,
ServerPort: ServerPort,
LocalPort: LocalPort,
Password: Password,
Method: Method,
ProxyRule: "proxy",
CAPath: es.caPath,
}
config.SetDefaultValue()
switch testName {
case "TestDisableTLS":
serverConfig.DisableTLS = true
config.DisableTLS = true
case "TestHTTPTunnelOutbound":
serverConfig.EnableHTTPInbound = true
config.OutboundProto = "https"
config.ServerPort = ServerPort + 1000
}
go es.server.Start()
time.Sleep(time.Second)
ss, err := New(config)
es.Nilf(err, "New Easyss failed")
ss.disableValidateAddr = true
es.ss = ss
go es.ss.LocalSocks5()
time.Sleep(time.Second)
go es.ss.LocalHttp()
time.Sleep(time.Second)
}
func (es *EasyssSuite) TearDownTest() {
es.ss.Close()
es.server.Close()
}
func (es *EasyssSuite) TestEasySuit() {
client := &http.Client{
Transport: &http.Transport{
Proxy: func(*http.Request) (*url.URL, error) {
return url.Parse(es.ss.Socks5ProxyAddr()) // test socks5 proxy
},
TLSHandshakeTimeout: 5 * time.Second,
ForceAttemptHTTP2: true,
MaxConnsPerHost: 1,
},
}
body, err := clientGet(client, TestHTTPSURL)
es.Require().Nilf(err, "http get %s failed:%s", TestHTTPSURL, err)
es.Greater(len(body), 1000)
client2 := &http.Client{
Transport: &http.Transport{
Proxy: func(*http.Request) (*url.URL, error) {
return url.Parse(fmt.Sprintf("http://%s", es.ss.LocalHttpAddr())) // test http proxy
},
TLSHandshakeTimeout: 5 * time.Second,
ForceAttemptHTTP2: true,
MaxConnsPerHost: 1,
},
}
body2, err2 := clientGet(client2, TestHTTPSURL)
es.Require().Nilf(err2, "http get %s failed", TestHTTPSURL)
es.Greater(len(body2), 1000)
body3, err3 := clientGet(client2, TestHTTPURL)
es.Require().Nilf(err3, "http get %s failed", TestHTTPURL)
es.Greater(len(body3), 1000)
}
func (es *EasyssSuite) TestCloseWrite() {
go closeWriteServer()
time.Sleep(time.Second)
msg := "hello"
ret := closeWriteClient(msg)
es.Equal(msg, ret)
}
func (es *EasyssSuite) TestDisableTLS() {
es.TestEasySuit()
}
func (es *EasyssSuite) TestHTTPTunnelOutbound() {
es.TestEasySuit()
}
func clientGet(client *http.Client, url string) (body []byte, err error) {
resp, err := client.Get(url)
if err != nil {
return nil, err
}
if resp.Body != nil {
defer resp.Body.Close()
}
return io.ReadAll(resp.Body)
}
func closeWriteServer() {
lis, err := net.Listen("tcp", ":"+CloseWriteServerPort)
if err != nil {
panic(err)
}
for {
conn, err := lis.Accept()
if err != nil {
fmt.Println("accept:", err.Error())
break
}
go handleConn(conn)
}
}
func handleConn(conn net.Conn) {
defer conn.Close()
for {
b := make([]byte, 1024)
nr, err := conn.Read(b)
if err != nil {
fmt.Println("read:", err.Error())
if nr > 0 {
fmt.Println("read err, bug got:", b[:nr])
}
return
}
fmt.Println("read:", string(b[:nr]))
time.Sleep(6 * time.Second)
if _, err := conn.Write(b[:nr]); err != nil {
fmt.Println("write to remote:", err.Error())
}
}
}
func closeWriteClient(msg string) string {
dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:"+strconv.FormatInt(LocalPort, 10), nil, proxy.Direct)
if err != nil {
panic("create socks5 dialer:" + err.Error())
}
conn, err := dialer.Dial("tcp", "127.0.0.1:"+CloseWriteServerPort)
if err != nil {
panic("dial:" + err.Error())
}
defer conn.Close()
if _, err := conn.Write([]byte(msg)); err != nil {
panic("write greeting message to remote:" + err.Error())
}
if err := conn.(*net.TCPConn).CloseWrite(); err != nil {
panic("close write:" + err.Error())
}
ret := make([]byte, 1024)
nr, err := conn.Read(ret)
if err != nil {
fmt.Println("read from remote:", err.Error())
if nr > 0 {
fmt.Println("read from remote:", string(ret[:nr]))
}
return ""
}
return string(ret[:nr])
}
func getEasyssForBench(config *Config) *Easyss {
ss := &Easyss{
config: config,
stat: &Statistics{},
dnsCache: freecache.NewCache(DefaultDNSCacheSize),
directDNSCache: freecache.NewCache(DefaultDNSCacheSize),
closing: make(chan struct{}, 1),
mu: &sync.RWMutex{},
}
proxyRule := ParseProxyRuleFromString(config.ProxyRule)
if proxyRule == ProxyRuleUnknown {
panic("unknown proxy rule:" + config.ProxyRule)
}
ss.proxyRule = proxyRule
db, err := geoip2.FromBytes(geoIPCNPrivate)
if err != nil {
panic(err)
}
ss.geoIPDB = db
ss.geoSiteDirect = NewGeoSite(geoSiteDirect)
ss.geoSiteBlock = NewGeoSite(geoSiteBlock)
if err := ss.loadCustomIPDomains(); err != nil {
panic(err)
}
return ss
}
func TestSubDomains(t *testing.T) {
domain := "as1.m.hao123.com"
subs := subDomains(domain)
assert.Equal(t, []string{"m.hao123.com", "hao123.com"}, subs)
}
func BenchmarkEasyss_MatchHostRule_Block(b *testing.B) {
host := "googleads.g.doubleclick.net"
config := &Config{ProxyRule: "auto_block"}
config.SetDefaultValue()
ss := getEasyssForBench(config)
assert.Equal(b, HostRuleBlock, ss.MatchHostRule(host))
for i := 0; i < b.N; i++ {
ss.MatchHostRule(host)
}
}
func BenchmarkEasyss_MatchHostRule_Direct(b *testing.B) {
host := "baidu.com"
config := &Config{ProxyRule: "auto_block"}
config.SetDefaultValue()
ss := getEasyssForBench(config)
assert.Equal(b, HostRuleDirect, ss.MatchHostRule(host))
for i := 0; i < b.N; i++ {
ss.MatchHostRule(host)
}
}
func BenchmarkEasyss_MatchHostRule_Proxy(b *testing.B) {
host := "google.com"
config := &Config{ProxyRule: "auto_block"}
config.SetDefaultValue()
ss := getEasyssForBench(config)
assert.Equal(b, HostRuleProxy, ss.MatchHostRule(host))
for i := 0; i < b.N; i++ {
ss.MatchHostRule(host)
}
}