sing-box/protocol/shadowsocks/outbound.go

180 lines
5.8 KiB
Go
Raw Permalink Normal View History

2024-11-01 16:39:02 +00:00
package shadowsocks
2022-06-30 13:27:56 +00:00
import (
"context"
"net"
"github.com/sagernet/sing-box/adapter"
2024-11-01 16:39:02 +00:00
"github.com/sagernet/sing-box/adapter/outbound"
2022-07-07 13:47:21 +00:00
"github.com/sagernet/sing-box/common/dialer"
2022-07-29 16:29:22 +00:00
"github.com/sagernet/sing-box/common/mux"
2022-06-30 13:27:56 +00:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
2022-07-02 06:07:50 +00:00
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/transport/sip003"
2023-04-28 03:35:57 +00:00
"github.com/sagernet/sing-shadowsocks2"
2022-07-29 16:29:22 +00:00
"github.com/sagernet/sing/common"
2022-07-08 15:03:57 +00:00
"github.com/sagernet/sing/common/bufio"
2022-07-29 16:29:22 +00:00
E "github.com/sagernet/sing/common/exceptions"
2024-11-01 16:39:02 +00:00
"github.com/sagernet/sing/common/logger"
2022-07-08 15:03:57 +00:00
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
2022-08-11 02:36:28 +00:00
"github.com/sagernet/sing/common/uot"
2022-06-30 13:27:56 +00:00
)
2024-11-01 16:39:02 +00:00
func RegisterOutbound(registry *outbound.Registry) {
outbound.Register[option.ShadowsocksOutboundOptions](registry, C.TypeShadowsocks, NewOutbound)
}
2022-06-30 13:27:56 +00:00
2024-11-01 16:39:02 +00:00
type Outbound struct {
outbound.Adapter
logger logger.ContextLogger
2022-07-29 16:29:22 +00:00
dialer N.Dialer
method shadowsocks.Method
serverAddr M.Socksaddr
plugin sip003.Plugin
2023-03-17 04:24:29 +00:00
uotClient *uot.Client
2023-04-20 03:16:22 +00:00
multiplexDialer *mux.Client
2022-06-30 13:27:56 +00:00
}
2024-11-01 16:39:02 +00:00
func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksOutboundOptions) (adapter.Outbound, error) {
2023-04-28 03:35:57 +00:00
method, err := shadowsocks.CreateMethod(ctx, options.Method, shadowsocks.MethodOptions{
Password: options.Password,
})
2022-07-07 13:47:21 +00:00
if err != nil {
return nil, err
}
outboundDialer, err := dialer.New(ctx, options.DialerOptions)
2023-08-08 08:14:03 +00:00
if err != nil {
return nil, err
}
2024-11-01 16:39:02 +00:00
outbound := &Outbound{
Adapter: outbound.NewAdapterWithDialerOptions(C.TypeShadowsocks, options.Network.Build(), tag, options.DialerOptions),
logger: logger,
2023-08-08 08:14:03 +00:00
dialer: outboundDialer,
2022-07-29 16:29:22 +00:00
method: method,
serverAddr: options.ServerOptions.Build(),
}
if options.Plugin != "" {
2023-08-29 05:43:42 +00:00
outbound.plugin, err = sip003.CreatePlugin(ctx, options.Plugin, options.PluginOptions, router, outbound.dialer, outbound.serverAddr)
if err != nil {
return nil, err
}
}
uotOptions := common.PtrValueOrDefault(options.UDPOverTCP)
2023-03-17 04:24:29 +00:00
if !uotOptions.Enabled {
outbound.multiplexDialer, err = mux.NewClientWithOptions((*shadowsocksDialer)(outbound), logger, common.PtrValueOrDefault(options.Multiplex))
2022-08-11 02:36:28 +00:00
if err != nil {
return nil, err
}
2022-08-03 13:51:34 +00:00
}
2023-03-17 04:24:29 +00:00
if uotOptions.Enabled {
outbound.uotClient = &uot.Client{
Dialer: (*shadowsocksDialer)(outbound),
Version: uotOptions.Version,
}
2023-03-15 05:34:02 +00:00
}
2022-07-29 16:29:22 +00:00
return outbound, nil
2022-06-30 13:27:56 +00:00
}
2024-11-01 16:39:02 +00:00
func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
2024-10-26 23:45:15 +00:00
ctx, metadata := adapter.ExtendContext(ctx)
2024-11-01 16:39:02 +00:00
metadata.Outbound = h.Tag()
2022-08-12 09:55:52 +00:00
metadata.Destination = destination
2022-08-04 01:11:39 +00:00
if h.multiplexDialer == nil {
switch N.NetworkName(network) {
case N.NetworkTCP:
h.logger.InfoContext(ctx, "outbound connection to ", destination)
case N.NetworkUDP:
2023-03-17 04:24:29 +00:00
if h.uotClient != nil {
h.logger.InfoContext(ctx, "outbound UoT connect packet connection to ", destination)
return h.uotClient.DialContext(ctx, network, destination)
} else {
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
2022-08-11 02:36:28 +00:00
}
2022-08-04 01:11:39 +00:00
}
return (*shadowsocksDialer)(h).DialContext(ctx, network, destination)
} else {
switch N.NetworkName(network) {
case N.NetworkTCP:
h.logger.InfoContext(ctx, "outbound multiplex connection to ", destination)
case N.NetworkUDP:
h.logger.InfoContext(ctx, "outbound multiplex packet connection to ", destination)
}
return h.multiplexDialer.DialContext(ctx, network, destination)
}
2022-07-29 16:29:22 +00:00
}
2024-11-01 16:39:02 +00:00
func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
2024-10-26 23:45:15 +00:00
ctx, metadata := adapter.ExtendContext(ctx)
2024-11-01 16:39:02 +00:00
metadata.Outbound = h.Tag()
2022-08-12 09:55:52 +00:00
metadata.Destination = destination
2022-08-04 01:11:39 +00:00
if h.multiplexDialer == nil {
2023-03-17 04:24:29 +00:00
if h.uotClient != nil {
2022-08-11 02:36:28 +00:00
h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
2023-03-17 04:24:29 +00:00
return h.uotClient.ListenPacket(ctx, destination)
} else {
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
2022-08-11 02:36:28 +00:00
}
2022-08-04 01:11:39 +00:00
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
return (*shadowsocksDialer)(h).ListenPacket(ctx, destination)
} else {
h.logger.InfoContext(ctx, "outbound multiplex packet connection to ", destination)
return h.multiplexDialer.ListenPacket(ctx, destination)
}
2022-07-29 16:29:22 +00:00
}
2024-11-01 16:39:02 +00:00
func (h *Outbound) InterfaceUpdated() {
2023-04-20 03:16:22 +00:00
if h.multiplexDialer != nil {
h.multiplexDialer.Reset()
}
2023-07-23 06:42:19 +00:00
return
2023-04-20 03:16:22 +00:00
}
2024-11-01 16:39:02 +00:00
func (h *Outbound) Close() error {
2023-04-20 03:16:22 +00:00
return common.Close(common.PtrOrNil(h.multiplexDialer))
2022-07-30 01:57:02 +00:00
}
2022-07-29 16:29:22 +00:00
var _ N.Dialer = (*shadowsocksDialer)(nil)
2024-11-01 16:39:02 +00:00
type shadowsocksDialer Outbound
2022-07-29 16:29:22 +00:00
func (h *shadowsocksDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
2024-10-26 23:45:15 +00:00
ctx, metadata := adapter.ExtendContext(ctx)
2024-11-01 16:39:02 +00:00
metadata.Outbound = h.Tag()
2023-03-29 02:29:52 +00:00
metadata.Destination = destination
2022-07-29 16:29:22 +00:00
switch N.NetworkName(network) {
case N.NetworkTCP:
var outConn net.Conn
var err error
if h.plugin != nil {
outConn, err = h.plugin.DialContext(ctx)
} else {
outConn, err = h.dialer.DialContext(ctx, N.NetworkTCP, h.serverAddr)
}
2022-06-30 13:27:56 +00:00
if err != nil {
return nil, err
}
2022-07-07 13:47:21 +00:00
return h.method.DialEarlyConn(outConn, destination), nil
2022-07-29 16:29:22 +00:00
case N.NetworkUDP:
outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr)
2022-06-30 13:27:56 +00:00
if err != nil {
return nil, err
}
2023-04-14 11:19:27 +00:00
return bufio.NewBindPacketConn(h.method.DialPacketConn(outConn), destination), nil
2022-06-30 13:27:56 +00:00
default:
2022-07-29 16:29:22 +00:00
return nil, E.Extend(N.ErrUnknownNetwork, network)
2022-06-30 13:27:56 +00:00
}
}
2022-07-29 16:29:22 +00:00
func (h *shadowsocksDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
2024-10-26 23:45:15 +00:00
ctx, metadata := adapter.ExtendContext(ctx)
2024-11-01 16:39:02 +00:00
metadata.Outbound = h.Tag()
2023-03-29 02:29:52 +00:00
metadata.Destination = destination
2022-07-29 16:29:22 +00:00
outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr)
2022-06-30 13:27:56 +00:00
if err != nil {
return nil, err
}
2022-07-10 06:22:28 +00:00
return h.method.DialPacketConn(outConn), nil
2022-06-30 13:27:56 +00:00
}