Skip to content

Commit

Permalink
Update UoT protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Mar 15, 2023
1 parent f1e35ad commit 65e149a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 34 deletions.
12 changes: 10 additions & 2 deletions app/proxyman/outbound/uot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ import (
)

func (h *Handler) getUoTConnection(ctx context.Context, dest net.Destination) (stat.Connection, error) {
if !dest.Address.Family().IsDomain() || dest.Address.Domain() != uot.UOTMagicAddress {
if !dest.Address.Family().IsDomain() {
return nil, os.ErrInvalid
}
var uotVersion int
if dest.Address.Domain() == uot.MagicAddress {
uotVersion = uot.Version
} else if dest.Address.Domain() == uot.LegacyMagicAddress {
uotVersion = uot.LegacyVersion
} else {
return nil, os.ErrInvalid
}
packetConn, err := internet.ListenSystemPacket(ctx, &net.UDPAddr{IP: net.AnyIP.IP(), Port: 0}, h.streamSettings.SocketSettings)
if err != nil {
return nil, newError("unable to listen socket").Base(err)
}
conn := uot.NewServerConn(packetConn)
conn := uot.NewServerConn(packetConn, uotVersion)
return h.getStatCouterConnection(conn), nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/pires/go-proxyproto v0.6.2
github.com/quic-go/quic-go v0.33.0
github.com/refraction-networking/utls v1.2.3-0.20230308205431-4f1df6c200db
github.com/sagernet/sing v0.1.8
github.com/sagernet/sing v0.1.9-0.20230315063014-2731df16725b
github.com/sagernet/sing-shadowsocks v0.1.1
github.com/sagernet/wireguard-go v0.0.0-20221116151939-c99467f53f2c
github.com/seiflotfy/cuckoofilter v0.0.0-20220411075957-e3b120b3f5fb
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ github.com/refraction-networking/utls v1.2.3-0.20230308205431-4f1df6c200db/go.mo
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3 h1:f/FNXud6gA3MNr8meMVVGxhp+QBTqY91tM8HjEuMjGg=
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3/go.mod h1:HgjTstvQsPGkxUsCd2KWxErBblirPizecHcpD3ffK+s=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/sagernet/sing v0.1.8 h1:6DKo2FkSHn0nUcjO7bAext/ai7y7pCusK/+fScBJ5Jk=
github.com/sagernet/sing v0.1.8/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk=
github.com/sagernet/sing v0.1.9-0.20230315063014-2731df16725b h1:1iKGftQ59+shDSx2RaLaxXJcMK/B+IU9WqUPwyBW+E0=
github.com/sagernet/sing v0.1.9-0.20230315063014-2731df16725b/go.mod h1:9uHswk2hITw8leDbiLS/xn0t9nzBcbePxzm9PJhwdlw=
github.com/sagernet/sing-shadowsocks v0.1.1 h1:uFK2rlVeD/b1xhDwSMbUI2goWc6fOKxp+ZeKHZq6C9Q=
github.com/sagernet/sing-shadowsocks v0.1.1/go.mod h1:f3mHTy5shnVM9l8UocMlJgC/1G/zdj5FuEuVXhDinGU=
github.com/sagernet/wireguard-go v0.0.0-20221116151939-c99467f53f2c h1:vK2wyt9aWYHHvNLWniwijBu/n4pySypiKRhN32u/JGo=
Expand Down
18 changes: 10 additions & 8 deletions infra/conf/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,15 @@ func buildShadowsocks2022(v *ShadowsocksServerConfig) (proto.Message, error) {
}

type ShadowsocksServerTarget struct {
Address *Address `json:"address"`
Port uint16 `json:"port"`
Cipher string `json:"method"`
Password string `json:"password"`
Email string `json:"email"`
Level byte `json:"level"`
IVCheck bool `json:"ivCheck"`
UoT bool `json:"uot"`
Address *Address `json:"address"`
Port uint16 `json:"port"`
Cipher string `json:"method"`
Password string `json:"password"`
Email string `json:"email"`
Level byte `json:"level"`
IVCheck bool `json:"ivCheck"`
UoT bool `json:"uot"`
UoTVersion int `json:"uotVersion"`
}

type ShadowsocksClientConfig struct {
Expand Down Expand Up @@ -193,6 +194,7 @@ func (v *ShadowsocksClientConfig) Build() (proto.Message, error) {
config.Method = server.Cipher
config.Key = server.Password
config.UdpOverTcp = server.UoT
config.UdpOverTcpVersion = uint32(server.UoTVersion)
return config, nil
}
}
Expand Down
41 changes: 26 additions & 15 deletions proxy/shadowsocks_2022/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions proxy/shadowsocks_2022/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ message ClientConfig {
string method = 3;
string key = 4;
bool udp_over_tcp = 5;
uint32 udp_over_tcp_version = 6;
}
26 changes: 20 additions & 6 deletions proxy/shadowsocks_2022/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ func init() {
}

type Outbound struct {
ctx context.Context
server net.Destination
method shadowsocks.Method
uot bool
ctx context.Context
server net.Destination
method shadowsocks.Method
uot bool
uotVersion int
}

func NewClient(ctx context.Context, config *ClientConfig) (*Outbound, error) {
Expand All @@ -57,6 +58,14 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Outbound, error) {
} else {
return nil, newError("unknown method ", config.Method)
}
switch config.UdpOverTcpVersion {
case uot.LegacyVersion:
o.uotVersion = uot.LegacyVersion
case 0, uot.Version:
o.uotVersion = 2
default:
return nil, newError("unknown udp over tcp protocol version ", config.UdpOverTcpVersion)
}
return o, nil
}

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

if o.uot {
serverConn := o.method.DialEarlyConn(connection, M.Socksaddr{Fqdn: uot.UOTMagicAddress})
return returnError(bufio.CopyPacketConn(ctx, packetConn, uot.NewClientConn(serverConn)))
var uConn *uot.Conn
if o.uotVersion == uot.Version {
uConn = uot.NewLazyConn(o.method.DialEarlyConn(connection, M.Socksaddr{Fqdn: uot.MagicAddress}), uot.Request{Destination: toSocksaddr(destination)})
} else {
uConn = uot.NewConn(o.method.DialEarlyConn(connection, M.Socksaddr{Fqdn: uot.LegacyMagicAddress}), false, toSocksaddr(destination))
}
return returnError(bufio.CopyPacketConn(ctx, packetConn, uConn))
} else {
serverConn := o.method.DialPacketConn(connection)
return returnError(bufio.CopyPacketConn(ctx, packetConn, serverConn))
Expand Down

0 comments on commit 65e149a

Please sign in to comment.