Skip to content

Commit f57ec13

Browse files
committed
Update UoT protocol
1 parent f1e35ad commit f57ec13

File tree

7 files changed

+66
-38
lines changed

7 files changed

+66
-38
lines changed

app/proxyman/outbound/uot.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,21 @@ import (
1111
)
1212

1313
func (h *Handler) getUoTConnection(ctx context.Context, dest net.Destination) (stat.Connection, error) {
14-
if !dest.Address.Family().IsDomain() || dest.Address.Domain() != uot.UOTMagicAddress {
14+
if !dest.Address.Family().IsDomain() {
15+
return nil, os.ErrInvalid
16+
}
17+
var uotVersion int
18+
if dest.Address.Domain() == uot.MagicAddress {
19+
uotVersion = uot.Version
20+
} else if dest.Address.Domain() == uot.LegacyMagicAddress {
21+
uotVersion = uot.LegacyVersion
22+
} else {
1523
return nil, os.ErrInvalid
1624
}
1725
packetConn, err := internet.ListenSystemPacket(ctx, &net.UDPAddr{IP: net.AnyIP.IP(), Port: 0}, h.streamSettings.SocketSettings)
1826
if err != nil {
1927
return nil, newError("unable to listen socket").Base(err)
2028
}
21-
conn := uot.NewServerConn(packetConn)
29+
conn := uot.NewServerConn(packetConn, uotVersion)
2230
return h.getStatCouterConnection(conn), nil
2331
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/pires/go-proxyproto v0.6.2
1414
github.com/quic-go/quic-go v0.33.0
1515
github.com/refraction-networking/utls v1.2.3-0.20230308205431-4f1df6c200db
16-
github.com/sagernet/sing v0.1.8
16+
github.com/sagernet/sing v0.2.0
1717
github.com/sagernet/sing-shadowsocks v0.1.1
1818
github.com/sagernet/wireguard-go v0.0.0-20221116151939-c99467f53f2c
1919
github.com/seiflotfy/cuckoofilter v0.0.0-20220411075957-e3b120b3f5fb

go.sum

+4-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,10 @@ github.com/refraction-networking/utls v1.2.3-0.20230308205431-4f1df6c200db/go.mo
141141
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3 h1:f/FNXud6gA3MNr8meMVVGxhp+QBTqY91tM8HjEuMjGg=
142142
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3/go.mod h1:HgjTstvQsPGkxUsCd2KWxErBblirPizecHcpD3ffK+s=
143143
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
144-
github.com/sagernet/sing v0.1.8 h1:6DKo2FkSHn0nUcjO7bAext/ai7y7pCusK/+fScBJ5Jk=
145-
github.com/sagernet/sing v0.1.8/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk=
144+
github.com/sagernet/sing v0.1.9-0.20230315063014-2731df16725b h1:1iKGftQ59+shDSx2RaLaxXJcMK/B+IU9WqUPwyBW+E0=
145+
github.com/sagernet/sing v0.1.9-0.20230315063014-2731df16725b/go.mod h1:9uHswk2hITw8leDbiLS/xn0t9nzBcbePxzm9PJhwdlw=
146+
github.com/sagernet/sing v0.2.0 h1:iyc4TaeXG5XYXixl48zSDDTw46C9NOEAVFq6ZE0dA2k=
147+
github.com/sagernet/sing v0.2.0/go.mod h1:9uHswk2hITw8leDbiLS/xn0t9nzBcbePxzm9PJhwdlw=
146148
github.com/sagernet/sing-shadowsocks v0.1.1 h1:uFK2rlVeD/b1xhDwSMbUI2goWc6fOKxp+ZeKHZq6C9Q=
147149
github.com/sagernet/sing-shadowsocks v0.1.1/go.mod h1:f3mHTy5shnVM9l8UocMlJgC/1G/zdj5FuEuVXhDinGU=
148150
github.com/sagernet/wireguard-go v0.0.0-20221116151939-c99467f53f2c h1:vK2wyt9aWYHHvNLWniwijBu/n4pySypiKRhN32u/JGo=

infra/conf/shadowsocks.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,15 @@ func buildShadowsocks2022(v *ShadowsocksServerConfig) (proto.Message, error) {
155155
}
156156

157157
type ShadowsocksServerTarget struct {
158-
Address *Address `json:"address"`
159-
Port uint16 `json:"port"`
160-
Cipher string `json:"method"`
161-
Password string `json:"password"`
162-
Email string `json:"email"`
163-
Level byte `json:"level"`
164-
IVCheck bool `json:"ivCheck"`
165-
UoT bool `json:"uot"`
158+
Address *Address `json:"address"`
159+
Port uint16 `json:"port"`
160+
Cipher string `json:"method"`
161+
Password string `json:"password"`
162+
Email string `json:"email"`
163+
Level byte `json:"level"`
164+
IVCheck bool `json:"ivCheck"`
165+
UoT bool `json:"uot"`
166+
UoTVersion int `json:"uotVersion"`
166167
}
167168

168169
type ShadowsocksClientConfig struct {
@@ -193,6 +194,7 @@ func (v *ShadowsocksClientConfig) Build() (proto.Message, error) {
193194
config.Method = server.Cipher
194195
config.Key = server.Password
195196
config.UdpOverTcp = server.UoT
197+
config.UdpOverTcpVersion = uint32(server.UoTVersion)
196198
return config, nil
197199
}
198200
}

proxy/shadowsocks_2022/config.pb.go

+26-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proxy/shadowsocks_2022/config.proto

+1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ message ClientConfig {
5151
string method = 3;
5252
string key = 4;
5353
bool udp_over_tcp = 5;
54+
uint32 udp_over_tcp_version = 6;
5455
}

proxy/shadowsocks_2022/outbound.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
C "github.com/sagernet/sing/common"
1212
B "github.com/sagernet/sing/common/buf"
1313
"github.com/sagernet/sing/common/bufio"
14-
M "github.com/sagernet/sing/common/metadata"
1514
N "github.com/sagernet/sing/common/network"
1615
"github.com/sagernet/sing/common/uot"
1716
"github.com/xtls/xray-core/common"
@@ -29,10 +28,10 @@ func init() {
2928
}
3029

3130
type Outbound struct {
32-
ctx context.Context
33-
server net.Destination
34-
method shadowsocks.Method
35-
uot bool
31+
ctx context.Context
32+
server net.Destination
33+
method shadowsocks.Method
34+
uotClient *uot.Client
3635
}
3736

3837
func NewClient(ctx context.Context, config *ClientConfig) (*Outbound, error) {
@@ -43,7 +42,6 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Outbound, error) {
4342
Port: net.Port(config.Port),
4443
Network: net.Network_TCP,
4544
},
46-
uot: config.UdpOverTcp,
4745
}
4846
if C.Contains(shadowaead_2022.List, config.Method) {
4947
if config.Key == "" {
@@ -57,6 +55,9 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Outbound, error) {
5755
} else {
5856
return nil, newError("unknown method ", config.Method)
5957
}
58+
if config.UdpOverTcp {
59+
o.uotClient = &uot.Client{Version: uint8(config.UdpOverTcpVersion)}
60+
}
6061
return o, nil
6162
}
6263

@@ -77,7 +78,7 @@ func (o *Outbound) Process(ctx context.Context, link *transport.Link, dialer int
7778
newError("tunneling request to ", destination, " via ", o.server.NetAddr()).WriteToLog(session.ExportIDToError(ctx))
7879

7980
serverDestination := o.server
80-
if o.uot {
81+
if o.uotClient != nil {
8182
serverDestination.Network = net.Network_TCP
8283
} else {
8384
serverDestination.Network = network
@@ -149,9 +150,12 @@ func (o *Outbound) Process(ctx context.Context, link *transport.Link, dialer int
149150
}
150151
}
151152

152-
if o.uot {
153-
serverConn := o.method.DialEarlyConn(connection, M.Socksaddr{Fqdn: uot.UOTMagicAddress})
154-
return returnError(bufio.CopyPacketConn(ctx, packetConn, uot.NewClientConn(serverConn)))
153+
if o.uotClient != nil {
154+
uConn, err := o.uotClient.DialEarlyConn(o.method.DialEarlyConn(connection, uot.RequestDestination(o.uotClient.Version)), false, toSocksaddr(destination))
155+
if err != nil {
156+
return err
157+
}
158+
return returnError(bufio.CopyPacketConn(ctx, packetConn, uConn))
155159
} else {
156160
serverConn := o.method.DialPacketConn(connection)
157161
return returnError(bufio.CopyPacketConn(ctx, packetConn, serverConn))

0 commit comments

Comments
 (0)