Skip to content

Commit 2e942e0

Browse files
authored
Fix Trojan XTLS
1 parent decb012 commit 2e942e0

File tree

4 files changed

+48
-29
lines changed

4 files changed

+48
-29
lines changed

infra/conf/trojan.go

+19
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ func (c *TrojanClientConfig) Build() (proto.Message, error) {
5252
Password: rec.Password,
5353
Flow: rec.Flow,
5454
}
55+
56+
switch account.Flow {
57+
case "", "xtls-rprx-origin", "xtls-rprx-origin-udp443", "xtls-rprx-direct", "xtls-rprx-direct-udp443":
58+
case "xtls-rprx-splice", "xtls-rprx-splice-udp443":
59+
if runtime.GOOS != "linux" && runtime.GOOS != "android" {
60+
return nil, newError(`Trojan servers: "` + account.Flow + `" only support linux in this version`)
61+
}
62+
default:
63+
return nil, newError(`Trojan servers: "flow" doesn't support "` + account.Flow + `" in this version`)
64+
}
65+
5566
trojan := &protocol.ServerEndpoint{
5667
Address: rec.Address.Build(),
5768
Port: uint32(rec.Port),
@@ -107,6 +118,14 @@ func (c *TrojanServerConfig) Build() (proto.Message, error) {
107118
Flow: rawUser.Flow,
108119
}
109120

121+
switch account.Flow {
122+
case "", "xtls-rprx-origin", "xtls-rprx-direct":
123+
case "xtls-rprx-splice":
124+
return nil, newError(`Trojan clients: inbound doesn't support "xtls-rprx-splice" in this version, please use "xtls-rprx-direct" instead`)
125+
default:
126+
return nil, newError(`Trojan clients: "flow" doesn't support "` + account.Flow + `" in this version`)
127+
}
128+
110129
user.Email = rawUser.Email
111130
user.Level = uint32(rawUser.Level)
112131
user.Account = serial.ToTypedMessage(account)

proxy/trojan/client.go

+20-19
Original file line numberDiff line numberDiff line change
@@ -79,62 +79,63 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
7979

8080
defer conn.Close()
8181

82+
iConn := conn
83+
statConn, ok := iConn.(*internet.StatCouterConnection)
84+
if ok {
85+
iConn = statConn.Connection
86+
}
87+
8288
user := server.PickUser()
8389
account, ok := user.Account.(*MemoryAccount)
8490
if !ok {
8591
return newError("user account is not valid")
8692
}
8793

88-
iConn := conn
89-
statConn, ok := iConn.(*internet.StatCouterConnection)
90-
if ok {
91-
iConn = statConn.Connection
94+
connWriter := &ConnWriter{
95+
Flow: account.Flow,
9296
}
9397

9498
var rawConn syscall.RawConn
9599
var sctx context.Context
96100

97-
connWriter := &ConnWriter{}
98101
allowUDP443 := false
99-
switch account.Flow {
102+
switch connWriter.Flow {
100103
case XRO + "-udp443", XRD + "-udp443", XRS + "-udp443":
101104
allowUDP443 = true
102-
account.Flow = account.Flow[:16]
105+
connWriter.Flow = connWriter.Flow[:16]
103106
fallthrough
104107
case XRO, XRD, XRS:
105108
if destination.Address.Family().IsDomain() && destination.Address.Domain() == muxCoolAddress {
106-
return newError(account.Flow + " doesn't support Mux").AtWarning()
109+
return newError(connWriter.Flow + " doesn't support Mux").AtWarning()
107110
}
108111
if destination.Network == net.Network_UDP {
109112
if !allowUDP443 && destination.Port == 443 {
110-
return newError(account.Flow + " stopped UDP/443").AtInfo()
113+
return newError(connWriter.Flow + " stopped UDP/443").AtInfo()
111114
}
115+
connWriter.Flow = ""
112116
} else { // enable XTLS only if making TCP request
113117
if xtlsConn, ok := iConn.(*xtls.Conn); ok {
114118
xtlsConn.RPRX = true
115-
xtlsConn.SHOW = trojanXTLSShow
119+
xtlsConn.SHOW = xtls_show
116120
xtlsConn.MARK = "XTLS"
117-
if account.Flow == XRS {
121+
if connWriter.Flow == XRS {
118122
sctx = ctx
119-
account.Flow = XRD
123+
connWriter.Flow = XRD
120124
}
121-
if account.Flow == XRD {
125+
if connWriter.Flow == XRD {
122126
xtlsConn.DirectMode = true
123127
if sc, ok := xtlsConn.Connection.(syscall.Conn); ok {
124128
rawConn, _ = sc.SyscallConn()
125129
}
126130
}
127-
connWriter.Flow = account.Flow
128131
} else {
129-
return newError(`failed to use ` + account.Flow + `, maybe "security" is not "xtls"`).AtWarning()
132+
return newError(`failed to use ` + connWriter.Flow + `, maybe "security" is not "xtls"`).AtWarning()
130133
}
131134
}
132-
case "":
135+
default:
133136
if _, ok := iConn.(*xtls.Conn); ok {
134137
panic(`To avoid misunderstanding, you must fill in Trojan "flow" when using XTLS.`)
135138
}
136-
default:
137-
return newError("unsupported flow " + account.Flow).AtWarning()
138139
}
139140

140141
sessionPolicy := c.policyManager.ForLevel(user.Level)
@@ -211,6 +212,6 @@ func init() {
211212

212213
xtlsShow := platform.NewEnvFlag("xray.trojan.xtls.show").GetValue(func() string { return defaultFlagValue })
213214
if xtlsShow == "true" {
214-
trojanXTLSShow = true
215+
xtls_show = true
215216
}
216217
}

proxy/trojan/protocol.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828
protocol.AddressFamilyByte(0x03, net.AddressFamilyDomain),
2929
)
3030

31-
trojanXTLSShow = false
31+
xtls_show = false
3232
)
3333

3434
const (
@@ -90,10 +90,10 @@ func (c *ConnWriter) writeHeader() error {
9090
command := commandTCP
9191
if c.Target.Network == net.Network_UDP {
9292
command = commandUDP
93-
} else if c.Flow == XRO {
94-
command = commandXRO
9593
} else if c.Flow == XRD {
9694
command = commandXRD
95+
} else if c.Flow == XRO {
96+
command = commandXRO
9797
}
9898

9999
if _, err := buffer.Write(c.Account.Key); err != nil {
@@ -211,10 +211,10 @@ func (c *ConnReader) ParseHeader() error {
211211
network := net.Network_TCP
212212
if command[0] == commandUDP {
213213
network = net.Network_UDP
214-
} else if command[0] == commandXRO {
215-
c.Flow = XRO
216214
} else if command[0] == commandXRD {
217215
c.Flow = XRD
216+
} else if command[0] == commandXRO {
217+
c.Flow = XRO
218218
}
219219

220220
addr, port, err := addrParser.ReadAddressPort(nil, c.Reader)

proxy/trojan/server.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func init() {
3838

3939
xtlsShow := platform.NewEnvFlag("xray.trojan.xtls.show").GetValue(func() string { return defaultFlagValue })
4040
if xtlsShow == "true" {
41-
trojanXTLSShow = true
41+
xtls_show = true
4242
}
4343
}
4444

@@ -219,7 +219,8 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn internet
219219
}
220220
if xtlsConn, ok := iConn.(*xtls.Conn); ok {
221221
xtlsConn.RPRX = true
222-
xtlsConn.SHOW = trojanXTLSShow
222+
xtlsConn.SHOW = xtls_show
223+
xtlsConn.MARK = "XTLS"
223224
if clientReader.Flow == XRD {
224225
xtlsConn.DirectMode = true
225226
if sc, ok := xtlsConn.Connection.(syscall.Conn); ok {
@@ -230,11 +231,9 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn internet
230231
return newError(`failed to use ` + clientReader.Flow + `, maybe "security" is not "xtls"`).AtWarning()
231232
}
232233
} else {
233-
return newError("unable to use ", clientReader.Flow).AtWarning()
234+
return newError(account.Password + " is not able to use " + clientReader.Flow).AtWarning()
234235
}
235236
case "":
236-
default:
237-
return newError("unsupported flow " + account.Flow).AtWarning()
238237
}
239238

240239
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{

0 commit comments

Comments
 (0)