Repository navigation
Expand file tree
/
Copy pathxray-core.patch
More file actions
450 lines (443 loc) · 14.5 KB
/
Copy pathxray-core.patch
File metadata and controls
450 lines (443 loc) · 14.5 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
diff -Naur a/core/domain2ip.go b/core/domain2ip.go
--- a/core/domain2ip.go 1970-01-01 00:00:00.000000000 +0000
+++ b/core/domain2ip.go 2026-02-23 16:54:12.830250167 +0000
@@ -0,0 +1,40 @@
+package core
+
+import (
+ "sync"
+)
+
+// DomainToIPs is a map of domain names to IP addresses
+// Note: there may be multiple IP addresses for a given domain
+var DomainToIPs = map[string][]string{}
+var domainMutex sync.RWMutex
+
+// GetDomainIPs returns IP addresses for a given domain
+func GetDomainIPs(domain string) ([]string, bool) {
+ domainMutex.RLock()
+ defer domainMutex.RUnlock()
+ ips, ok := DomainToIPs[domain]
+ return ips, ok
+}
+
+// SetDomainIPs sets IP addresses for a given domain
+func SetDomainIPs(domain string, ips []string) {
+ domainMutex.Lock()
+ defer domainMutex.Unlock()
+ DomainToIPs[domain] = ips
+}
+
+// DropDomainIPs drops specific IP address for a given domain from the cache
+func DropDomainIPs(domain string, ip string) {
+ domainMutex.Lock()
+ defer domainMutex.Unlock()
+ for i, existingIP := range DomainToIPs[domain] {
+ if existingIP == ip {
+ DomainToIPs[domain] = append(DomainToIPs[domain][:i], DomainToIPs[domain][i+1:]...)
+ if len(DomainToIPs[domain]) == 0 {
+ delete(DomainToIPs, domain)
+ }
+ break
+ }
+ }
+}
diff -Naur a/envoy/xray.go b/envoy/xray.go
--- a/envoy/xray.go 1970-01-01 00:00:00.000000000 +0000
+++ b/envoy/xray.go 2026-04-27 11:54:21.346086194 +0000
@@ -0,0 +1,267 @@
+package xray
+
+// 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 xray 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
+// independently. Unfortunately you can't start them all and tell xray to
+// use the one that works... but that's what Envoy is good at.
+
+import (
+ "fmt"
+ "strings"
+ "sync"
+
+ core "github.com/xtls/xray-core/core"
+ _ "github.com/xtls/xray-core/main/distro/all"
+)
+
+const jsonV5 = "json"
+
+var xrxhServers sync.Map
+
+// getInbound
+//
+// @param port - port to listen for SOCKS5 connections
+func getInbound(clientPort int) string {
+ return fmt.Sprintf(`
+ {
+ "port": %d,
+ "protocol": "socks",
+ "sniffing": {
+ "enabled": true,
+ "destOverride": ["http", "tls"]
+ },
+ "settings": {}
+ }`, clientPort)
+}
+
+func getXrxhConfig(clientPort int, serverAddress, serverPort, xhttpPath, id string, allowInsecure bool, serverName string, hostName string, xhttpMode string, xhttpVersion string) string {
+ if xhttpVersion == "h2" {
+ return fmt.Sprintf(`
+ {
+ "log": {
+ "access": "none",
+ "loglevel": "warning"
+ },
+ "inbounds": [ %s ],
+ "outbounds": [
+ {
+ "protocol": "vless",
+ "settings": {
+ "vnext": [
+ {
+ "address": "%s",
+ "port": %s,
+ "users": [
+ {
+ "id": "%s",
+ "encryption": "none"
+ }
+ ]
+ }
+ ]
+ },
+ "streamSettings": {
+ "network": "xhttp",
+ "xhttpSettings": {
+ "host": "%s",
+ "path": "%s",
+ "mode": "%s"
+ },
+ "security": "tls",
+ "tlsSettings": {
+ "serverName": "%s",
+ "allowInsecure": %t,
+ "alpn": [ "h2", "http/1.1"]
+ }
+ }
+ }
+ ]
+ }`, getInbound(clientPort), serverAddress, serverPort, id, hostName, xhttpPath, xhttpMode, serverName, allowInsecure)
+ } else if xhttpVersion == "h3" {
+ return fmt.Sprintf(`
+ {
+ "log": {
+ "access": "none",
+ "loglevel": "warning"
+ },
+ "inbounds": [ %s ],
+ "outbounds": [
+ {
+ "protocol": "vless",
+ "settings": {
+ "vnext": [
+ {
+ "address": "%s",
+ "port": %s,
+ "users": [
+ {
+ "id": "%s",
+ "encryption": "none"
+ }
+ ]
+ }
+ ]
+ },
+ "streamSettings": {
+ "network": "xhttp",
+ "xhttpSettings": {
+ "host": "%s",
+ "path": "%s",
+ "mode": "%s",
+ "#xmux": { //Use default values. To customize: remove the preceding # (hash). Note: cannot exceed Nginx's maximum (upper limit) value.
+ "maxConcurrency": 128, //Nginx default limit 128. https://nginx.org/en/docs/http/ngx_http_v3_module.html#http3_max_concurrent_streams
+ "hMaxRequestTimes": 1000, //Nginx default limit 1000. https://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_requests
+ "hMaxReusableSecs": 3600 //Nginx default limit 3600s(1h). https://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_time
+ },
+ "#downloadSettings": { //For H2 download: remove the preceding # (hash) to use downloadSettings; change the mode above and in server.json to stream-up.
+ "address": "%s",
+ "port": %s,
+ "network": "xhttp",
+ "xhttpSettings": {
+ "host": "%s",
+ "path": "%s", //Fill in your path (same as above)
+ "#xmux": { //Use default values. To customize: remove the preceding # (hash). Note: cannot exceed Nginx's maximum (upper limit) value.
+ "maxConcurrency": 128, //Nginx default limit 128. https://nginx.org/en/docs/http/ngx_http_v3_module.html#http3_max_concurrent_streams
+ "hMaxRequestTimes": 1000, //Nginx default limit 1000. https://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_requests
+ "hMaxReusableSecs": 3600 //Nginx default limit 3600s(1h). https://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_time
+ }
+ },
+ "security": "tls",
+ "tlsSettings": {
+ "serverName": "%s",
+ "allowInsecure": %t,
+ "alpn": ["h2", "http/1.1"]
+ }
+ }
+ },
+ "security": "tls",
+ "tlsSettings": {
+ "serverName": "%s",
+ "allowInsecure": %t,
+ "alpn": [
+ "h3" //If performance is far below expectations and the log shows "QUIC flood detected", refer to: https://github.com/XTLS/Xray-core/discussions/4197
+ ]
+ }
+ }
+ }
+ ]
+ }`, getInbound(clientPort), serverAddress, serverPort, id, hostName, xhttpPath, xhttpMode, serverAddress, serverPort, hostName, xhttpPath, serverName, allowInsecure, serverName, allowInsecure)
+ } else {
+ return ""
+ }
+}
+
+func startServer(jsonConfig string) (*core.Instance, error) {
+ // fmt.Printf("DEBUG: Starting Xray with JSON config:\n%s\n", jsonConfig)
+ reader := strings.NewReader(jsonConfig)
+
+ config, err := core.LoadConfig(jsonV5, reader)
+ if err != nil {
+ fmt.Printf("error reading config: %s\n", err)
+ return nil, err
+ }
+
+ server, err := core.New(config)
+ if err != nil {
+ fmt.Printf("error creating server: %s\n", err)
+ return nil, err
+ }
+
+ if err := server.Start(); err != nil {
+ fmt.Printf("failed to start %s\n", err)
+
+ _ = server.Close()
+
+ return nil, err
+ }
+
+ return server, nil
+}
+
+// XrxhConfigOptional holds optional configuration for Xrxh connections
+type XrxhConfigOptional struct {
+ AllowInsecure bool
+ ServerName string
+ Hostname string
+ XhttpMode string
+ XhttpVersion string
+}
+
+// StartXrxh - start Xray with XHTTP transport via HTTP/2 or HTTP/1.1
+//
+// @param clientPort - client SOCKS port routed to the XHTTP server
+//
+// @param serverAddress - IP or hostname of the server
+//
+// @param serverPort - port of the XHTTP server (probably 443)
+//
+// @param xhttpPath - path to the XHTTP endpoint on the server
+//
+// @param id - UUID used to authenticate with the server
+//
+// @param config - optional configuration with certain defaults
+//
+// @returns error, if transport could not be started, or `nil` on success.
+func StartXrxh(clientPort int, serverAddress, serverPort, xhttpPath, id string, config ...XrxhConfigOptional) error {
+ // Set defaults for optional config
+ allowInsecure := false
+ serverName := serverAddress
+ hostName := serverAddress
+ // Defaults to HTTP/2
+ xhttpMode := "stream-one"
+ xhttpVersion := "h2"
+
+ // Override with provided config if any
+ if len(config) > 0 {
+ cfg := config[0]
+ allowInsecure = cfg.AllowInsecure
+ if cfg.ServerName != "" {
+ serverName = cfg.ServerName
+ }
+ if cfg.Hostname != "" {
+ hostName = cfg.Hostname
+ }
+ if cfg.XhttpMode != "" {
+ // Can be either stream-one (for h2) or packet-up (for h3)
+ xhttpMode = cfg.XhttpMode
+ }
+ if cfg.XhttpVersion != "" {
+ // Can be either h2 or h3
+ xhttpVersion = cfg.XhttpVersion
+ }
+ }
+
+ server, err := startServer(getXrxhConfig(clientPort, serverAddress, serverPort, xhttpPath, id, allowInsecure, serverName, hostName, xhttpMode, xhttpVersion))
+ if err != nil {
+ return err
+ }
+
+ xrxhServers.Store(clientPort, server)
+ return nil
+}
+
+func StopXrxhByPort(clientPort int) {
+ if val, ok := xrxhServers.LoadAndDelete(clientPort); ok {
+ _ = val.(*core.Instance).Close()
+ }
+}
+
+func StopXrxh() {
+ xrxhServers.Range(func(key, value any) bool {
+ xrxhServers.Delete(key)
+ _ = value.(*core.Instance).Close()
+ return true
+ })
+}
diff -Naur a/proxy/wireguard/tun.go b/proxy/wireguard/tun.go
--- a/proxy/wireguard/tun.go 2025-10-14 20:23:35.482996098 +0000
+++ b/proxy/wireguard/tun.go 2026-02-23 16:54:12.830250167 +0000
@@ -194,7 +194,7 @@
Timeout: 15 * time.Second,
})
- handler(xnet.UDPDestination(xnet.IPAddress(id.LocalAddress.AsSlice()), xnet.Port(id.LocalPort)), gonet.NewUDPConn(&wq, ep))
+ handler(xnet.UDPDestination(xnet.IPAddress(id.LocalAddress.AsSlice()), xnet.Port(id.LocalPort)), gonet.NewUDPConn(stack, &wq, ep))
}(r)
})
stack.SetTransportProtocolHandler(udp.ProtocolNumber, udpForwarder.HandlePacket)
diff -Naur a/transport/internet/splithttp/dialer.go b/transport/internet/splithttp/dialer.go
--- a/transport/internet/splithttp/dialer.go 2025-10-14 20:23:35.490996211 +0000
+++ b/transport/internet/splithttp/dialer.go 2026-02-23 16:54:12.830250167 +0000
@@ -21,6 +21,7 @@
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/signal/done"
"github.com/xtls/xray-core/common/uuid"
+ core "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/browser_dialer"
"github.com/xtls/xray-core/transport/internet/reality"
@@ -117,6 +118,9 @@
return nil, err
}
+ // Log the actual remote address we connected to
+ errors.LogInfo(ctxInner, "Successfully connected to remote address: ", conn.RemoteAddr().String())
+
if realityConfig != nil {
return reality.UClient(conn, realityConfig, ctxInner, dest)
}
@@ -161,8 +165,8 @@
transport = &http3.Transport{
QUICConfig: quicConfig,
TLSClientConfig: gotlsConfig,
- Dial: func(ctx context.Context, addr string, tlsCfg *gotls.Config, cfg *quic.Config) (*quic.Conn, error) {
- conn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
+ Dial: func(ctxH3 context.Context, addr string, tlsCfg *gotls.Config, cfg *quic.Config) (*quic.Conn, error) {
+ conn, err := internet.DialSystem(ctxH3, dest, streamSettings.SocketSettings)
if err != nil {
return nil, err
}
@@ -195,7 +199,10 @@
}
}
- return quic.DialEarly(ctx, udpConn, udpAddr, tlsCfg, cfg)
+ // Log the UDP address we're dialing for HTTP/3
+ errors.LogInfo(ctxH3, "HTTP/3 QUIC dialing to UDP address: ", udpAddr.String())
+
+ return quic.DialEarly(ctxH3, udpConn, udpAddr, tlsCfg, cfg)
},
}
} else if httpVersion == "2" {
@@ -253,6 +260,23 @@
dest.Network = net.Network_UDP
}
+ // Only try to resolve using GetDomainIPs if the address is a domain, not an IP
+ var originalDomain string
+ if dest.Address.Family().IsDomain() {
+ originalDomain = dest.Address.Domain()
+ if ips, ok := core.GetDomainIPs(originalDomain); ok {
+ resolvedIP := ips[0]
+ dest.Address = net.ParseAddress(resolvedIP)
+ errors.LogInfo(ctx, "Resolved domain ", originalDomain, " to IP ", resolvedIP, " from cache")
+ } else {
+ errors.LogInfo(ctx, "Domain ", originalDomain, " not found in cache, will use system DNS")
+ }
+ } else {
+ originalDomain = dest.Address.String()
+ }
+
+ errors.LogInfo(ctx, "Connecting to IP: ", dest.Address.String(), " (original domain: ", originalDomain, ")")
+
transportConfiguration := streamSettings.ProtocolSettings.(*Config)
var requestURL url.URL
@@ -269,7 +293,7 @@
requestURL.Host = realityConfig.ServerName
}
if requestURL.Host == "" {
- requestURL.Host = dest.Address.String()
+ requestURL.Host = originalDomain
}
sessionIdUuid := uuid.New()
@@ -311,6 +335,24 @@
if httpVersion2 == "3" {
dest2.Network = net.Network_UDP
}
+
+ // Only try to resolve using GetDomainIPs if the address is a domain, not an IP
+ var originalDomain string
+ if dest2.Address.Family().IsDomain() {
+ originalDomain = dest2.Address.Domain()
+ if ips, ok := core.GetDomainIPs(originalDomain); ok {
+ resolvedIP := ips[0]
+ dest2.Address = net.ParseAddress(resolvedIP)
+ errors.LogInfo(ctx, "DownloadSettings: Resolved domain ", originalDomain, " to IP ", resolvedIP, " from cache")
+ } else {
+ errors.LogInfo(ctx, "DownloadSettings: Domain ", originalDomain, " not found in cache, will use system DNS")
+ }
+ } else {
+ originalDomain = dest2.Address.String()
+ }
+
+ errors.LogInfo(ctx, "DownloadSettings: Connecting to IP: ", dest2.Address.String(), " (original domain: ", originalDomain, ")")
+
if tlsConfig2 != nil || realityConfig2 != nil {
requestURL2.Scheme = "https"
} else {
@@ -325,7 +367,7 @@
requestURL2.Host = realityConfig2.ServerName
}
if requestURL2.Host == "" {
- requestURL2.Host = dest2.Address.String()
+ requestURL2.Host = originalDomain
}
requestURL2.Path = config2.GetNormalizedPath() + sessionIdUuid.String()
requestURL2.RawQuery = config2.GetNormalizedQuery()
diff -Naur a/transport/internet/websocket/dialer.go b/transport/internet/websocket/dialer.go
--- a/transport/internet/websocket/dialer.go 2025-10-14 20:23:35.490996211 +0000
+++ b/transport/internet/websocket/dialer.go 2026-02-23 16:54:12.830250167 +0000
@@ -53,7 +53,7 @@
},
ReadBufferSize: 4 * 1024,
WriteBufferSize: 4 * 1024,
- HandshakeTimeout: time.Second * 8,
+ HandshakeTimeout: time.Second * 15,
}
protocol := "ws"