github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/inbound/shadowsocks.go (about)

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