shadowsocks.go 3.6 KB

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