shadowsocks.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package inbound
  2. import (
  3. "context"
  4. "net"
  5. "time"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/common/mux"
  8. "github.com/sagernet/sing-box/common/uot"
  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. M "github.com/sagernet/sing/common/metadata"
  19. N "github.com/sagernet/sing/common/network"
  20. "github.com/sagernet/sing/common/ntp"
  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. var (
  35. _ adapter.Inbound = (*Shadowsocks)(nil)
  36. _ adapter.TCPInjectableInbound = (*Shadowsocks)(nil)
  37. )
  38. type Shadowsocks struct {
  39. myInboundAdapter
  40. service shadowsocks.Service
  41. }
  42. func newShadowsocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksInboundOptions) (*Shadowsocks, error) {
  43. inbound := &Shadowsocks{
  44. myInboundAdapter: myInboundAdapter{
  45. protocol: C.TypeShadowsocks,
  46. network: options.Network.Build(),
  47. ctx: ctx,
  48. router: uot.NewRouter(router, logger),
  49. logger: logger,
  50. tag: tag,
  51. listenOptions: options.ListenOptions,
  52. },
  53. }
  54. inbound.connHandler = inbound
  55. inbound.packetHandler = inbound
  56. var err error
  57. inbound.router, err = mux.NewRouterWithOptions(inbound.router, logger, common.PtrValueOrDefault(options.Multiplex))
  58. if err != nil {
  59. return nil, err
  60. }
  61. var udpTimeout time.Duration
  62. if options.UDPTimeout != 0 {
  63. udpTimeout = time.Duration(options.UDPTimeout)
  64. } else {
  65. udpTimeout = C.UDPTimeout
  66. }
  67. switch {
  68. case options.Method == shadowsocks.MethodNone:
  69. inbound.service = shadowsocks.NewNoneService(int64(udpTimeout.Seconds()), adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newConnection, inbound.newPacketConnection, inbound))
  70. case common.Contains(shadowaead.List, options.Method):
  71. inbound.service, err = shadowaead.NewService(options.Method, nil, options.Password, int64(udpTimeout.Seconds()), adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newConnection, inbound.newPacketConnection, inbound))
  72. case common.Contains(shadowaead_2022.List, options.Method):
  73. inbound.service, err = shadowaead_2022.NewServiceWithPassword(options.Method, options.Password, int64(udpTimeout.Seconds()), adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newConnection, inbound.newPacketConnection, inbound), ntp.TimeFuncFromContext(ctx))
  74. default:
  75. err = E.New("unsupported method: ", options.Method)
  76. }
  77. inbound.packetUpstream = inbound.service
  78. return inbound, err
  79. }
  80. func (h *Shadowsocks) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
  81. err := h.service.NewConnection(ctx, conn, adapter.UpstreamMetadata(metadata))
  82. N.CloseOnHandshakeFailure(conn, onClose, err)
  83. if err != nil {
  84. h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
  85. }
  86. }
  87. func (h *Shadowsocks) NewPacketEx(buffer *buf.Buffer, source M.Socksaddr) {
  88. err := h.service.NewPacket(h.ctx, h.packetConn(), buffer, M.Metadata{Source: source})
  89. if err != nil {
  90. h.logger.Error(E.Cause(err, "process packet from ", source))
  91. }
  92. }
  93. func (h *Shadowsocks) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  94. h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  95. return h.router.RouteConnection(ctx, conn, h.createMetadata(conn, metadata))
  96. }
  97. func (h *Shadowsocks) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  98. ctx = log.ContextWithNewID(ctx)
  99. h.logger.InfoContext(ctx, "inbound packet connection from ", metadata.Source)
  100. h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  101. return h.router.RoutePacketConnection(ctx, conn, h.createPacketMetadata(conn, metadata))
  102. }