Add UoT option to socks outbound too

This commit is contained in:
世界 2022-08-12 17:55:52 +08:00
parent 51bbf93ff2
commit 340fce9f1c
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
6 changed files with 45 additions and 9 deletions

View file

@ -1,6 +1,11 @@
#### 2022/08/12
* Performance improvements
* Add UoT option for [Socks](/configuration/outbound/socks) outbound
#### 2022/08/11 #### 2022/08/11
* Add UoT option for [Shadowsocks](/configuration/outbound/shadowsocks) outbound, UoT support for all inbounds. * Add UoT option for [Shadowsocks](/configuration/outbound/shadowsocks) outbound, UoT support for all inbounds
#### 2022/08/10 #### 2022/08/10

View file

@ -88,7 +88,7 @@ Both is enabled by default.
#### udp_over_tcp #### udp_over_tcp
Enable UDP over TCP protocol. Enable the UDP over TCP protocol.
Conflict with `multiplex`. Conflict with `multiplex`.

View file

@ -15,6 +15,7 @@
"username": "sekai", "username": "sekai",
"password": "admin", "password": "admin",
"network": "udp", "network": "udp",
"udp_over_tcp": false,
"detour": "upstream-out", "detour": "upstream-out",
"bind_interface": "en0", "bind_interface": "en0",
@ -65,6 +66,10 @@ One of `tcp` `udp`.
Both is enabled by default. Both is enabled by default.
#### udp_over_tcp
Enable the UDP over TCP protocol.
### Dial Fields ### Dial Fields
#### detour #### detour

View file

@ -21,6 +21,7 @@ type SocksOutboundOptions struct {
Username string `json:"username,omitempty"` Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"` Password string `json:"password,omitempty"`
Network NetworkList `json:"network,omitempty"` Network NetworkList `json:"network,omitempty"`
UoT bool `json:"udp_over_tcp,omitempty"`
} }
type HTTPOutboundOptions struct { type HTTPOutboundOptions struct {

View file

@ -59,6 +59,9 @@ func NewShadowsocks(ctx context.Context, router adapter.Router, logger log.Conte
} }
func (h *Shadowsocks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { func (h *Shadowsocks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
if h.multiplexDialer == nil { if h.multiplexDialer == nil {
switch N.NetworkName(network) { switch N.NetworkName(network) {
case N.NetworkTCP: case N.NetworkTCP:
@ -90,6 +93,9 @@ func (h *Shadowsocks) DialContext(ctx context.Context, network string, destinati
} }
func (h *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { func (h *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
if h.multiplexDialer == nil { if h.multiplexDialer == nil {
if h.uot { if h.uot {
h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination) h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
@ -127,9 +133,6 @@ var _ N.Dialer = (*shadowsocksDialer)(nil)
type shadowsocksDialer Shadowsocks type shadowsocksDialer Shadowsocks
func (h *shadowsocksDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { func (h *shadowsocksDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
switch N.NetworkName(network) { switch N.NetworkName(network) {
case N.NetworkTCP: case N.NetworkTCP:
outConn, err := h.dialer.DialContext(ctx, N.NetworkTCP, h.serverAddr) outConn, err := h.dialer.DialContext(ctx, N.NetworkTCP, h.serverAddr)
@ -149,9 +152,6 @@ func (h *shadowsocksDialer) DialContext(ctx context.Context, network string, des
} }
func (h *shadowsocksDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { func (h *shadowsocksDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr) outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -12,6 +12,7 @@ import (
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network" N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/uot"
"github.com/sagernet/sing/protocol/socks" "github.com/sagernet/sing/protocol/socks"
) )
@ -20,6 +21,7 @@ var _ adapter.Outbound = (*Socks)(nil)
type Socks struct { type Socks struct {
myOutboundAdapter myOutboundAdapter
client *socks.Client client *socks.Client
uot bool
} }
func NewSocks(router adapter.Router, logger log.ContextLogger, tag string, options option.SocksOutboundOptions) (*Socks, error) { func NewSocks(router adapter.Router, logger log.ContextLogger, tag string, options option.SocksOutboundOptions) (*Socks, error) {
@ -43,6 +45,7 @@ func NewSocks(router adapter.Router, logger log.ContextLogger, tag string, optio
tag: tag, tag: tag,
}, },
socks.NewClient(detour, options.ServerOptions.Build(), version, options.Username, options.Password), socks.NewClient(detour, options.ServerOptions.Build(), version, options.Username, options.Password),
options.UoT,
}, nil }, nil
} }
@ -54,6 +57,17 @@ func (h *Socks) DialContext(ctx context.Context, network string, destination M.S
case N.NetworkTCP: case N.NetworkTCP:
h.logger.InfoContext(ctx, "outbound connection to ", destination) h.logger.InfoContext(ctx, "outbound connection to ", destination)
case N.NetworkUDP: case N.NetworkUDP:
if h.uot {
h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
tcpConn, err := h.client.DialContext(ctx, N.NetworkTCP, M.Socksaddr{
Fqdn: uot.UOTMagicAddress,
Port: destination.Port,
})
if err != nil {
return nil, err
}
return uot.NewClientConn(tcpConn), nil
}
h.logger.InfoContext(ctx, "outbound packet connection to ", destination) h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
default: default:
return nil, E.Extend(N.ErrUnknownNetwork, network) return nil, E.Extend(N.ErrUnknownNetwork, network)
@ -65,6 +79,17 @@ func (h *Socks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.
ctx, metadata := adapter.AppendContext(ctx) ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag metadata.Outbound = h.tag
metadata.Destination = destination metadata.Destination = destination
if h.uot {
h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
tcpConn, err := h.client.DialContext(ctx, N.NetworkTCP, M.Socksaddr{
Fqdn: uot.UOTMagicAddress,
Port: destination.Port,
})
if err != nil {
return nil, err
}
return uot.NewClientConn(tcpConn), nil
}
h.logger.InfoContext(ctx, "outbound packet connection to ", destination) h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
return h.client.ListenPacket(ctx, destination) return h.client.ListenPacket(ctx, destination)
} }