Skip to content

Commit 65e149a

Browse files
committed
Update UoT protocol
1 parent f1e35ad commit 65e149a

File tree

7 files changed

+70
-34
lines changed

7 files changed

+70
-34
lines changed

app/proxyman/outbound/uot.go

Lines changed: 10 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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.1.9-0.20230315063014-2731df16725b
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ 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=
146146
github.com/sagernet/sing-shadowsocks v0.1.1 h1:uFK2rlVeD/b1xhDwSMbUI2goWc6fOKxp+ZeKHZq6C9Q=
147147
github.com/sagernet/sing-shadowsocks v0.1.1/go.mod h1:f3mHTy5shnVM9l8UocMlJgC/1G/zdj5FuEuVXhDinGU=
148148
github.com/sagernet/wireguard-go v0.0.0-20221116151939-c99467f53f2c h1:vK2wyt9aWYHHvNLWniwijBu/n4pySypiKRhN32u/JGo=

infra/conf/shadowsocks.go

Lines changed: 10 additions & 8 deletions
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

Lines changed: 26 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proxy/shadowsocks_2022/config.proto

Lines changed: 1 addition & 0 deletions
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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ func init() {
2929
}
3030

3131
type Outbound struct {
32-
ctx context.Context
33-
server net.Destination
34-
method shadowsocks.Method
35-
uot bool
32+
ctx context.Context
33+
server net.Destination
34+
method shadowsocks.Method
35+
uot bool
36+
uotVersion int
3637
}
3738

3839
func NewClient(ctx context.Context, config *ClientConfig) (*Outbound, error) {
@@ -57,6 +58,14 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Outbound, error) {
5758
} else {
5859
return nil, newError("unknown method ", config.Method)
5960
}
61+
switch config.UdpOverTcpVersion {
62+
case uot.LegacyVersion:
63+
o.uotVersion = uot.LegacyVersion
64+
case 0, uot.Version:
65+
o.uotVersion = 2
66+
default:
67+
return nil, newError("unknown udp over tcp protocol version ", config.UdpOverTcpVersion)
68+
}
6069
return o, nil
6170
}
6271

@@ -150,8 +159,13 @@ func (o *Outbound) Process(ctx context.Context, link *transport.Link, dialer int
150159
}
151160

152161
if o.uot {
153-
serverConn := o.method.DialEarlyConn(connection, M.Socksaddr{Fqdn: uot.UOTMagicAddress})
154-
return returnError(bufio.CopyPacketConn(ctx, packetConn, uot.NewClientConn(serverConn)))
162+
var uConn *uot.Conn
163+
if o.uotVersion == uot.Version {
164+
uConn = uot.NewLazyConn(o.method.DialEarlyConn(connection, M.Socksaddr{Fqdn: uot.MagicAddress}), uot.Request{Destination: toSocksaddr(destination)})
165+
} else {
166+
uConn = uot.NewConn(o.method.DialEarlyConn(connection, M.Socksaddr{Fqdn: uot.LegacyMagicAddress}), false, toSocksaddr(destination))
167+
}
168+
return returnError(bufio.CopyPacketConn(ctx, packetConn, uConn))
155169
} else {
156170
serverConn := o.method.DialPacketConn(connection)
157171
return returnError(bufio.CopyPacketConn(ctx, packetConn, serverConn))

0 commit comments

Comments
 (0)