This repository has been archived by the owner on Mar 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathtcp_test.go
187 lines (151 loc) · 4.4 KB
/
tcp_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
package v2ray_simple_test
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"testing"
"github.com/e1732a364fed/v2ray_simple"
"github.com/e1732a364fed/v2ray_simple/netLayer"
"github.com/e1732a364fed/v2ray_simple/proxy"
"github.com/e1732a364fed/v2ray_simple/utils"
_ "github.com/e1732a364fed/v2ray_simple/advLayer/grpcSimple"
_ "github.com/e1732a364fed/v2ray_simple/advLayer/ws"
_ "github.com/e1732a364fed/v2ray_simple/proxy/dokodemo"
_ "github.com/e1732a364fed/v2ray_simple/proxy/shadowsocks"
_ "github.com/e1732a364fed/v2ray_simple/proxy/simplesocks"
_ "github.com/e1732a364fed/v2ray_simple/proxy/socks5http"
_ "github.com/e1732a364fed/v2ray_simple/proxy/trojan"
_ "github.com/e1732a364fed/v2ray_simple/proxy/vless"
_ "github.com/e1732a364fed/v2ray_simple/proxy/vmess"
)
func TestTCP_vless(t *testing.T) {
testTCP(t, "vless", 0, "tcp", false)
}
func TestTCP_trojan(t *testing.T) {
testTCP(t, "trojan", 0, "tcp", false)
}
func TestTCP_trojan_mux(t *testing.T) {
testTCP(t, "trojan", 0, "tcp", true)
}
// tcp测试我们直接使用http请求来测试
func testTCP(t *testing.T, protocol string, version int, network string, innermux bool) {
utils.LogLevel = utils.Log_debug
utils.InitLog("")
var testClientConfFormatStr = `
[[listen]]
protocol = "http"
host = "127.0.0.1"
port = %s
[[dial]]
protocol = "%s"
uuid = "a684455c-b14f-11ea-bf0d-42010aaa0003"
host = "127.0.0.1"
port = %s
version = %d
insecure = true
network = "%s"
`
if innermux {
testClientConfFormatStr += "use_mux = true"
}
const testServerConfFormatStr = `
[[listen]]
protocol = "%s"
uuid = "a684455c-b14f-11ea-bf0d-42010aaa0003"
host = "127.0.0.1"
port = %s
version = %d
insecure = true
cert = "cert.pem"
key = "cert.key"
network = "%s"
[[dial]]
protocol = "direct"
`
clientListenPort := netLayer.RandPortStr(true, false)
clientDialPort := netLayer.RandPortStr(true, false)
testClientConfStr := fmt.Sprintf(testClientConfFormatStr, clientListenPort, protocol, clientDialPort, version, network)
testServerConfStr := fmt.Sprintf(testServerConfFormatStr, protocol, clientDialPort, version, network)
clientConf, err := proxy.LoadStandardConfFromTomlStr(testClientConfStr)
if err != nil {
t.Log(err)
t.FailNow()
}
serverConf, err := proxy.LoadStandardConfFromTomlStr(testServerConfStr)
if err != nil {
t.Log(err)
t.FailNow()
}
//先建立服务端监听和客户端监听,最后自定义dns查询 并导向 客户端的 dokodemo监听端口
//http in
clientEndInServer, err := proxy.NewServer(clientConf.Listen[0])
if err != nil {
t.Log("can not create clientEndInServer: ", err)
t.FailNow()
}
// vless out
clientEndOutClient, err := proxy.NewClient(clientConf.Dial[0])
if err != nil {
t.Log("can not create clientEndOutClient: ", err)
t.FailNow()
}
//vless in
serverEndInServer, err := proxy.NewServer(serverConf.Listen[0])
if err != nil {
t.Log("can not create serverEndInServer: ", err)
t.FailNow()
}
// direct out
serverEndOutClient, err := proxy.NewClient(serverConf.Dial[0])
if err != nil {
t.Log("can not create serverEndOutClient: ", err)
t.FailNow()
}
c1 := v2ray_simple.ListenSer(clientEndInServer, clientEndOutClient, nil, nil)
c2 := v2ray_simple.ListenSer(serverEndInServer, serverEndOutClient, nil, nil)
if c1 != nil {
defer c1.Close()
}
if c2 != nil {
defer c2.Close()
}
proxyurl := "http://127.0.0.1:" + clientListenPort
url_proxy, e2 := url.Parse(proxyurl)
if e2 != nil {
fmt.Println("proxyurl given was wrong,", proxyurl, e2)
return
}
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(url_proxy),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
tryGetHttp(t, client, "http://captive.apple.com")
tryGetHttp(t, client, "http://www.msftconnecttest.com/connecttest.txt")
//联通性测试 可参考 https://imldy.cn/posts/99d42f85/
// 用这种 captive 测试 不容易遇到 网站无法在 某些地区 如 github action 所在的地区 访问 or卡顿等情况.
}
func tryGetHttp(t *testing.T, client *http.Client, path string) {
t.Log("start dial", path)
resp, err := client.Get(path)
if err != nil {
t.Log("get http failed", err)
t.FailNow()
}
t.Log("Got response, start read")
bs, err := io.ReadAll(resp.Body)
if err != nil {
t.Log("get http failed read", err)
t.FailNow()
}
resp.Body.Close()
t.Log("got len", len(bs))
if len(bs) > 5 {
t.Log("first 5:", string(bs[:5]))
} else {
t.Log("all:", bs)
}
}