github.com/sagernet/sing-box@v1.2.7/inbound/shadowsocks.go (about)

     1  package inbound
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"os"
     7  
     8  	"github.com/sagernet/sing-box/adapter"
     9  	C "github.com/sagernet/sing-box/constant"
    10  	"github.com/sagernet/sing-box/log"
    11  	"github.com/sagernet/sing-box/option"
    12  	"github.com/sagernet/sing-shadowsocks"
    13  	"github.com/sagernet/sing-shadowsocks/shadowaead"
    14  	"github.com/sagernet/sing-shadowsocks/shadowaead_2022"
    15  	"github.com/sagernet/sing/common"
    16  	"github.com/sagernet/sing/common/buf"
    17  	E "github.com/sagernet/sing/common/exceptions"
    18  	N "github.com/sagernet/sing/common/network"
    19  )
    20  
    21  func NewShadowsocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksInboundOptions) (adapter.Inbound, error) {
    22  	if len(options.Users) > 0 && len(options.Destinations) > 0 {
    23  		return nil, E.New("users and destinations options must not be combined")
    24  	}
    25  	if len(options.Users) > 0 {
    26  		return newShadowsocksMulti(ctx, router, logger, tag, options)
    27  	} else if len(options.Destinations) > 0 {
    28  		return newShadowsocksRelay(ctx, router, logger, tag, options)
    29  	} else {
    30  		return newShadowsocks(ctx, router, logger, tag, options)
    31  	}
    32  }
    33  
    34  var (
    35  	_ adapter.Inbound           = (*Shadowsocks)(nil)
    36  	_ adapter.InjectableInbound = (*Shadowsocks)(nil)
    37  )
    38  
    39  type Shadowsocks struct {
    40  	myInboundAdapter
    41  	service shadowsocks.Service
    42  }
    43  
    44  func newShadowsocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksInboundOptions) (*Shadowsocks, error) {
    45  	inbound := &Shadowsocks{
    46  		myInboundAdapter: myInboundAdapter{
    47  			protocol:      C.TypeShadowsocks,
    48  			network:       options.Network.Build(),
    49  			ctx:           ctx,
    50  			router:        router,
    51  			logger:        logger,
    52  			tag:           tag,
    53  			listenOptions: options.ListenOptions,
    54  		},
    55  	}
    56  	inbound.connHandler = inbound
    57  	inbound.packetHandler = inbound
    58  	var udpTimeout int64
    59  	if options.UDPTimeout != 0 {
    60  		udpTimeout = options.UDPTimeout
    61  	} else {
    62  		udpTimeout = int64(C.UDPTimeout.Seconds())
    63  	}
    64  	var err error
    65  	switch {
    66  	case options.Method == shadowsocks.MethodNone:
    67  		inbound.service = shadowsocks.NewNoneService(options.UDPTimeout, inbound.upstreamContextHandler())
    68  	case common.Contains(shadowaead.List, options.Method):
    69  		inbound.service, err = shadowaead.NewService(options.Method, nil, options.Password, udpTimeout, inbound.upstreamContextHandler())
    70  	case common.Contains(shadowaead_2022.List, options.Method):
    71  		inbound.service, err = shadowaead_2022.NewServiceWithPassword(options.Method, options.Password, udpTimeout, inbound.upstreamContextHandler(), router.TimeFunc())
    72  	default:
    73  		err = E.New("unsupported method: ", options.Method)
    74  	}
    75  	inbound.packetUpstream = inbound.service
    76  	return inbound, err
    77  }
    78  
    79  func (h *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
    80  	return h.service.NewConnection(adapter.WithContext(log.ContextWithNewID(ctx), &metadata), conn, adapter.UpstreamMetadata(metadata))
    81  }
    82  
    83  func (h *Shadowsocks) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata adapter.InboundContext) error {
    84  	return h.service.NewPacket(adapter.WithContext(ctx, &metadata), conn, buffer, adapter.UpstreamMetadata(metadata))
    85  }
    86  
    87  func (h *Shadowsocks) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
    88  	return os.ErrInvalid
    89  }