sing-box/outbound/shadowsocks.go

198 lines
6.4 KiB
Go
Raw Normal View History

2022-07-01 11:34:02 +00:00
package outbound
2022-06-30 13:27:56 +00:00
import (
"context"
"net"
"github.com/sagernet/sing-box/adapter"
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"
2022-07-08 15:03:57 +00:00
"github.com/sagernet/sing-shadowsocks"
"github.com/sagernet/sing-shadowsocks/shadowimpl"
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"
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
)
2022-07-01 11:34:02 +00:00
var _ adapter.Outbound = (*Shadowsocks)(nil)
2022-06-30 13:27:56 +00:00
2022-07-01 11:34:02 +00:00
type Shadowsocks struct {
myOutboundAdapter
2022-07-29 16:29:22 +00:00
dialer N.Dialer
method shadowsocks.Method
serverAddr M.Socksaddr
plugin sip003.Plugin
2022-08-11 02:36:28 +00:00
uot bool
2023-03-15 05:34:02 +00:00
uotVersion int
2022-07-29 16:29:22 +00:00
multiplexDialer N.Dialer
2022-06-30 13:27:56 +00:00
}
2022-07-29 16:29:22 +00:00
func NewShadowsocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksOutboundOptions) (*Shadowsocks, error) {
2023-02-21 06:53:00 +00:00
method, err := shadowimpl.FetchMethod(options.Method, options.Password, router.TimeFunc())
2022-07-07 13:47:21 +00:00
if err != nil {
return nil, err
}
2022-07-29 16:29:22 +00:00
outbound := &Shadowsocks{
myOutboundAdapter: myOutboundAdapter{
2022-07-21 13:03:41 +00:00
protocol: C.TypeShadowsocks,
2022-07-23 01:15:47 +00:00
network: options.Network.Build(),
router: router,
2022-07-01 11:34:02 +00:00
logger: logger,
tag: tag,
},
2022-09-03 04:55:10 +00:00
dialer: dialer.New(router, options.DialerOptions),
2022-07-29 16:29:22 +00:00
method: method,
serverAddr: options.ServerOptions.Build(),
2022-08-11 02:36:28 +00:00
uot: options.UoT,
2022-07-29 16:29:22 +00:00
}
if options.Plugin != "" {
outbound.plugin, err = sip003.CreatePlugin(options.Plugin, options.PluginOptions, router, outbound.dialer, outbound.serverAddr)
if err != nil {
return nil, err
}
}
2022-08-11 02:36:28 +00:00
if !options.UoT {
outbound.multiplexDialer, err = mux.NewClientWithOptions(ctx, (*shadowsocksDialer)(outbound), common.PtrValueOrDefault(options.MultiplexOptions))
if err != nil {
return nil, err
}
2022-08-03 13:51:34 +00:00
}
2023-03-15 05:34:02 +00:00
switch options.UoTVersion {
case uot.LegacyVersion:
outbound.uotVersion = uot.LegacyVersion
case 0, uot.Version:
outbound.uotVersion = uot.Version
default:
return nil, E.New("unknown udp over tcp protocol version ", options.UoTVersion)
}
2022-07-29 16:29:22 +00:00
return outbound, nil
2022-06-30 13:27:56 +00:00
}
2022-07-07 13:47:21 +00:00
func (h *Shadowsocks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
2022-08-12 09:55:52 +00:00
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
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:
2022-08-11 02:36:28 +00:00
if h.uot {
h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
2023-03-15 05:34:02 +00:00
var uotDestination M.Socksaddr
if h.uotVersion == uot.Version {
uotDestination.Fqdn = uot.MagicAddress
} else {
uotDestination.Fqdn = uot.LegacyMagicAddress
}
tcpConn, err := (*shadowsocksDialer)(h).DialContext(ctx, N.NetworkTCP, uotDestination)
2022-08-11 02:36:28 +00:00
if err != nil {
return nil, err
}
2023-03-15 05:34:02 +00:00
if h.uotVersion == uot.Version {
return uot.NewLazyConn(tcpConn, uot.Request{IsConnect: true, Destination: destination}), nil
} else {
return uot.NewConn(tcpConn, false, destination), nil
}
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).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
}
func (h *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
2022-08-12 09:55:52 +00:00
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
2022-08-04 01:11:39 +00:00
if h.multiplexDialer == nil {
2022-08-11 02:36:28 +00:00
if h.uot {
h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
2023-03-15 05:34:02 +00:00
var uotDestination M.Socksaddr
if h.uotVersion == uot.Version {
uotDestination.Fqdn = uot.MagicAddress
} else {
uotDestination.Fqdn = uot.LegacyMagicAddress
}
tcpConn, err := (*shadowsocksDialer)(h).DialContext(ctx, N.NetworkTCP, uotDestination)
2022-08-11 02:36:28 +00:00
if err != nil {
return nil, err
}
2023-03-15 05:34:02 +00:00
if h.uotVersion == uot.Version {
return uot.NewLazyConn(tcpConn, uot.Request{Destination: destination}), nil
} else {
return uot.NewConn(tcpConn, false, destination), nil
}
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
}
func (h *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2023-02-26 15:08:20 +00:00
return NewConnection(ctx, h, conn, metadata)
2022-07-29 16:29:22 +00:00
}
func (h *Shadowsocks) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
return NewPacketConnection(ctx, h, conn, metadata)
}
2022-07-30 01:57:02 +00:00
func (h *Shadowsocks) Close() error {
return common.Close(h.multiplexDialer)
}
2022-07-29 16:29:22 +00:00
var _ N.Dialer = (*shadowsocksDialer)(nil)
type shadowsocksDialer Shadowsocks
func (h *shadowsocksDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
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
}
2022-07-07 13:47:21 +00:00
return &bufio.BindPacketConn{PacketConn: h.method.DialPacketConn(outConn), Addr: 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) {
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
}