sing-box/outbound/shadowsocks.go

189 lines
6.0 KiB
Go
Raw Normal View History

2022-07-01 19:34:02 +08:00
package outbound
2022-06-30 21:27:56 +08:00
import (
"context"
"net"
"github.com/sagernet/sing-box/adapter"
2022-07-07 21:47:21 +08:00
"github.com/sagernet/sing-box/common/dialer"
2022-07-30 00:29:22 +08:00
"github.com/sagernet/sing-box/common/mux"
2022-06-30 21:27:56 +08:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
2022-07-02 14:07:50 +08:00
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/transport/sip003"
2023-04-28 11:35:57 +08:00
"github.com/sagernet/sing-shadowsocks2"
2022-07-30 00:29:22 +08:00
"github.com/sagernet/sing/common"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing/common/bufio"
2022-07-30 00:29:22 +08:00
E "github.com/sagernet/sing/common/exceptions"
2022-07-08 23:03:57 +08:00
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
2022-08-11 10:36:28 +08:00
"github.com/sagernet/sing/common/uot"
2022-06-30 21:27:56 +08:00
)
2022-07-01 19:34:02 +08:00
var _ adapter.Outbound = (*Shadowsocks)(nil)
2022-06-30 21:27:56 +08:00
2022-07-01 19:34:02 +08:00
type Shadowsocks struct {
myOutboundAdapter
2022-07-30 00:29:22 +08:00
dialer N.Dialer
method shadowsocks.Method
serverAddr M.Socksaddr
plugin sip003.Plugin
2023-03-17 12:24:29 +08:00
uotClient *uot.Client
2023-04-20 11:16:22 +08:00
multiplexDialer *mux.Client
2022-06-30 21:27:56 +08:00
}
2022-07-30 00:29:22 +08:00
func NewShadowsocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksOutboundOptions) (*Shadowsocks, error) {
2023-04-28 11:35:57 +08:00
method, err := shadowsocks.CreateMethod(ctx, options.Method, shadowsocks.MethodOptions{
Password: options.Password,
})
2022-07-07 21:47:21 +08:00
if err != nil {
return nil, err
}
2023-08-08 16:14:03 +08:00
outboundDialer, err := dialer.New(router, options.DialerOptions)
if err != nil {
return nil, err
}
2022-07-30 00:29:22 +08:00
outbound := &Shadowsocks{
myOutboundAdapter: myOutboundAdapter{
2023-06-13 22:38:05 +08:00
protocol: C.TypeShadowsocks,
network: options.Network.Build(),
router: router,
logger: logger,
tag: tag,
dependencies: withDialerDependency(options.DialerOptions),
2022-07-01 19:34:02 +08:00
},
2023-08-08 16:14:03 +08:00
dialer: outboundDialer,
2022-07-30 00:29:22 +08:00
method: method,
serverAddr: options.ServerOptions.Build(),
}
if options.Plugin != "" {
outbound.plugin, err = sip003.CreatePlugin(options.Plugin, options.PluginOptions, router, outbound.dialer, outbound.serverAddr)
if err != nil {
return nil, err
}
}
2023-03-17 12:24:29 +08:00
uotOptions := common.PtrValueOrDefault(options.UDPOverTCPOptions)
if !uotOptions.Enabled {
2023-04-20 11:16:22 +08:00
outbound.multiplexDialer, err = mux.NewClientWithOptions((*shadowsocksDialer)(outbound), common.PtrValueOrDefault(options.MultiplexOptions))
2022-08-11 10:36:28 +08:00
if err != nil {
return nil, err
}
2022-08-03 21:51:34 +08:00
}
2023-03-17 12:24:29 +08:00
if uotOptions.Enabled {
outbound.uotClient = &uot.Client{
Dialer: (*shadowsocksDialer)(outbound),
Version: uotOptions.Version,
}
2023-03-15 13:34:02 +08:00
}
2022-07-30 00:29:22 +08:00
return outbound, nil
2022-06-30 21:27:56 +08:00
}
2022-07-07 21:47:21 +08:00
func (h *Shadowsocks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
2022-08-12 17:55:52 +08:00
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
2022-08-04 09:11:39 +08: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 12:24:29 +08: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 10:36:28 +08:00
}
2022-08-04 09:11:39 +08: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-30 00:29:22 +08:00
}
func (h *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
2022-08-12 17:55:52 +08:00
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
2022-08-04 09:11:39 +08:00
if h.multiplexDialer == nil {
2023-03-17 12:24:29 +08:00
if h.uotClient != nil {
2022-08-11 10:36:28 +08:00
h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
2023-03-17 12:24:29 +08:00
return h.uotClient.ListenPacket(ctx, destination)
} else {
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
2022-08-11 10:36:28 +08:00
}
2022-08-04 09:11:39 +08: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-30 00:29:22 +08:00
}
func (h *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2023-02-26 23:08:20 +08:00
return NewConnection(ctx, h, conn, metadata)
2022-07-30 00:29:22 +08:00
}
func (h *Shadowsocks) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
return NewPacketConnection(ctx, h, conn, metadata)
}
2023-07-23 14:42:19 +08:00
func (h *Shadowsocks) InterfaceUpdated() {
2023-04-20 11:16:22 +08:00
if h.multiplexDialer != nil {
h.multiplexDialer.Reset()
}
2023-07-23 14:42:19 +08:00
return
2023-04-20 11:16:22 +08:00
}
2022-07-30 09:57:02 +08:00
func (h *Shadowsocks) Close() error {
2023-04-20 11:16:22 +08:00
return common.Close(common.PtrOrNil(h.multiplexDialer))
2022-07-30 09:57:02 +08:00
}
2022-07-30 00:29:22 +08:00
var _ N.Dialer = (*shadowsocksDialer)(nil)
type shadowsocksDialer Shadowsocks
func (h *shadowsocksDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
2023-03-29 10:29:52 +08:00
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
2022-07-30 00:29:22 +08: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 21:27:56 +08:00
if err != nil {
return nil, err
}
2022-07-07 21:47:21 +08:00
return h.method.DialEarlyConn(outConn, destination), nil
2022-07-30 00:29:22 +08:00
case N.NetworkUDP:
outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr)
2022-06-30 21:27:56 +08:00
if err != nil {
return nil, err
}
2023-04-14 19:19:27 +08:00
return bufio.NewBindPacketConn(h.method.DialPacketConn(outConn), destination), nil
2022-06-30 21:27:56 +08:00
default:
2022-07-30 00:29:22 +08:00
return nil, E.Extend(N.ErrUnknownNetwork, network)
2022-06-30 21:27:56 +08:00
}
}
2022-07-30 00:29:22 +08:00
func (h *shadowsocksDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
2023-03-29 10:29:52 +08:00
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
2022-07-30 00:29:22 +08:00
outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr)
2022-06-30 21:27:56 +08:00
if err != nil {
return nil, err
}
2022-07-10 14:22:28 +08:00
return h.method.DialPacketConn(outConn), nil
2022-06-30 21:27:56 +08:00
}