-
Notifications
You must be signed in to change notification settings - Fork 2
/
v2ray-core.patch
303 lines (302 loc) · 7.08 KB
/
v2ray-core.patch
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
envoy/v2ray.go | 294 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 294 insertions(+)
diff --git a/envoy/v2ray.go b/envoy/v2ray.go
new file mode 100644
index 00000000..6a153e8c
--- /dev/null
+++ b/envoy/v2ray.go
@@ -0,0 +1,294 @@
+package v2ray
+
+// copied and modified from main/commands/run.go
+
+// feeding core.LoadConfig with a string reader containing the config
+// JSON seems the simplest way to run v2ray as a library
+//
+// Golang's JSON support seems a little cumbersome for a couple
+// string substitutions in a complex JSON snippet, so we just
+// use fmt.Sprintf to assemble the config
+//
+// The JSON file we build should look similar to the client example config
+// (that will be) documented here:
+// https://gitlab.com/stevenmcdonald/envoy-proxy-examples/v2ray/
+
+// We provide functions for starting and stopping several client services
+// indpenedently. Unfortunately you can't start them all and tell v2ray to
+// use the one that works... but that's what Envoy is good at.
+
+import (
+ "fmt"
+ "os"
+ "os/signal"
+ "strings"
+ "syscall"
+
+ core "github.com/v2fly/v2ray-core/v5"
+ _ "github.com/v2fly/v2ray-core/v5/main/distro/all"
+)
+
+var osWsSignals = make(chan os.Signal, 1)
+var osWechatSignals = make(chan os.Signal, 1)
+var osSrtpSignals = make(chan os.Signal, 1)
+
+// getInbound
+//
+// @param port - port to listen for SOCKS5 connections
+//
+func getInbound(clientPort *string) string {
+ inbound := fmt.Sprintf(`
+ {
+ "port": %s,
+ "protocol": "socks",
+ "sniffing": {
+ "enabled": true,
+ "destOverride": ["http", "tls"]
+ },
+ "settings": {
+ "auth": "noauth"
+ }
+ }`, *clientPort)
+
+ return inbound
+}
+
+func getWsConfig(clientPort, serverAddress, serverWsPort, wsPath, id *string) string {
+
+ inbound := getInbound(clientPort)
+
+ jsonConfig := fmt.Sprintf(`
+ {
+ "log": {
+ "loglevel": "error"
+ },
+ "inbounds": [
+ %s
+ ],
+ "outbounds": [
+ {
+ "protocol": "vmess",
+ "settings": {
+ "vnext": [
+ {
+ "address": "%s",
+ "port": %s,
+ "users": [
+ {
+ "id": "%s",
+ "alterId": 0
+ }
+ ]
+ }
+ ]
+ },
+ "streamSettings": {
+ "network": "ws",
+ "security": "tls",
+ "wsSettings": {
+ "path": "%s"
+ }
+ }
+ }
+ ]
+ }`, inbound, *serverAddress, *serverWsPort, *id, *wsPath)
+
+ return jsonConfig
+}
+
+// getQUICConfig
+//
+// @param clientPort - port to listen on for SOCKS5 connections
+//
+// @param serverAddress - server address to connect to
+//
+// @param serverPort - server port to connect to
+//
+// @oaram type - type of QUIC obfuscation, should be "srtp" or "wechat-video"
+func getQuicConfig(clientPort, serverAddress, serverPort, quicType, id *string) string {
+
+ inbound := getInbound(clientPort)
+
+ jsonConfig := fmt.Sprintf(`
+ {
+ "log": {
+ "loglevel": "error"
+ },
+ "inbounds": [
+ %s
+ ],
+ "outbounds": [
+ {
+ "protocol": "vmess",
+ "settings": {
+ "vnext": [
+ {
+ "address": "%s",
+ "port": %s,
+ "users": [
+ {
+ "id": "%s",
+ "alterId": 0
+ }
+ ]
+ }
+ ]
+ },
+ "streamSettings": {
+ "network": "quic",
+ "quicSettings": {
+ "security": "aes-128-gcm",
+ "header": {
+ "type": "%s"
+ },
+ "key": "0"
+ }
+ }
+ }
+ ]
+ }`, inbound, *serverAddress, *serverPort, *id, *quicType)
+
+ return jsonConfig
+}
+
+// StartWs - start v2ray, websocket transport
+//
+// @param clientPort - client SOCKS port routed to the WS server
+//
+// @param serverAddress - IP or hostname of the server
+//
+// @param serverPort - port of the websocket server (probably 443)
+//
+// @param wsPath - path to the websocket on the server
+//
+// @param id - UUID used to authenticate with the server
+//
+func StartWs(clientPort, serverAddress, serverPort, wsPath, id *string) error {
+ configJson := getWsConfig(clientPort, serverAddress, serverPort, wsPath, id)
+
+ // fmt.Println(configJSON)
+
+ reader := strings.NewReader(configJson)
+ clientConfig, err := core.LoadConfig(core.FormatJSON, reader)
+ if err != nil {
+ fmt.Println("error reading config: %s", err)
+ return err
+ }
+
+ server, err := core.New(clientConfig)
+ if err != nil {
+ fmt.Println("error creating server: %s", err)
+ return err
+ }
+
+ if err := server.Start(); err != nil {
+ fmt.Println("failed to start %s", err)
+ }
+
+ defer server.Close()
+
+ {
+ signal.Notify(osWsSignals, syscall.SIGTERM)
+ <-osWsSignals
+ }
+
+ return nil
+}
+
+func StopWs() {
+ osWsSignals <- syscall.SIGTERM
+}
+
+// StartSrtp - start v2ray, QUIC/SRTP transport
+//
+// @param clientPort - client SOCKS port routed to the WS server
+//
+// @param serverAddress - IP or hostname of the server
+//
+// @param serverPort - port of the websocket server (probably 443)
+//
+// @param id - UUID used to authenticate with the server
+//
+func StartSrtp(clientPort, serverAddress, serverPort, id *string) error {
+ quicType := "srtp"
+ configJson := getQuicConfig(clientPort, serverAddress, serverPort, &quicType, id)
+
+ // fmt.Println(configJson)
+
+ reader := strings.NewReader(configJson)
+ clientConfig, err := core.LoadConfig(core.FormatJSON, reader)
+ if err != nil {
+ fmt.Println("error reading config: %s", err)
+ return err
+ }
+
+ server, err := core.New(clientConfig)
+ if err != nil {
+ fmt.Println("error creating server: %s", err)
+ return err
+ }
+
+ if err := server.Start(); err != nil {
+ fmt.Println("failed to start %s", err)
+ }
+
+ defer server.Close()
+
+ {
+ signal.Notify(osSrtpSignals, syscall.SIGTERM)
+ <-osSrtpSignals
+ }
+
+ return nil
+}
+
+func StopSrtp() {
+ osSrtpSignals <- syscall.SIGTERM
+}
+
+// StartWechat - start v2ray, QUIC/Wechat-video transport
+//
+// @param clientPort - client SOCKS port routed to the WS server
+//
+// @param serverAddress - IP or hostname of the server
+//
+// @param serverPort - port of the websocket server (probably 443)
+//
+// @param id - UUID used to authenticate with the server
+//
+func StartWechat(clientPort, serverAddress, serverPort, id *string) error {
+ quicType := "wechat-video"
+ configJson := getQuicConfig(clientPort, serverAddress, serverPort, &quicType, id)
+
+ // fmt.Println(configJSON)
+
+ reader := strings.NewReader(configJson)
+ clientConfig, err := core.LoadConfig(core.FormatJSON, reader)
+ if err != nil {
+ fmt.Println("error reading config: %s", err)
+ return err
+ }
+
+ server, err := core.New(clientConfig)
+ if err != nil {
+ fmt.Println("error creating server: %s", err)
+ return err
+ }
+
+ if err := server.Start(); err != nil {
+ fmt.Println("failed to start %s", err)
+ }
+
+ defer server.Close()
+
+ {
+ signal.Notify(osWechatSignals, syscall.SIGTERM)
+ <-osWechatSignals
+ }
+
+ return nil
+}
+
+func StopWechat() {
+ osWechatSignals <- syscall.SIGTERM
+}