shadowsocks.go 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 {
  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. )
  34. type Shadowsocks struct {
  35. myInboundAdapter
  36. service shadowsocks.Service
  37. }
  38. func newShadowsocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksInboundOptions) (*Shadowsocks, error) {
  39. inbound := &Shadowsocks{
  40. myInboundAdapter: myInboundAdapter{
  41. protocol: C.TypeShadowsocks,
  42. network: options.Network.Build(),
  43. ctx: ctx,
  44. router: router,
  45. logger: logger,
  46. tag: tag,
  47. listenOptions: options.ListenOptions,
  48. },
  49. }
  50. inbound.connHandler = inbound
  51. inbound.packetHandler = inbound
  52. var udpTimeout int64
  53. if options.UDPTimeout != 0 {
  54. udpTimeout = options.UDPTimeout
  55. } else {
  56. udpTimeout = int64(C.UDPTimeout.Seconds())
  57. }
  58. var err error
  59. switch {
  60. case options.Method == shadowsocks.MethodNone:
  61. inbound.service = shadowsocks.NewNoneService(options.UDPTimeout, inbound.upstreamContextHandler())
  62. case common.Contains(shadowaead.List, options.Method):
  63. inbound.service, err = shadowaead.NewService(options.Method, nil, options.Password, udpTimeout, inbound.upstreamContextHandler())
  64. case common.Contains(shadowaead_2022.List, options.Method):
  65. inbound.service, err = shadowaead_2022.NewServiceWithPassword(options.Method, options.Password, udpTimeout, inbound.upstreamContextHandler(), router.TimeFunc())
  66. default:
  67. err = E.New("unsupported method: ", options.Method)
  68. }
  69. inbound.packetUpstream = inbound.service
  70. return inbound, err
  71. }
  72. func (h *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  73. return h.service.NewConnection(adapter.WithContext(log.ContextWithNewID(ctx), &metadata), conn, adapter.UpstreamMetadata(metadata))
  74. }
  75. func (h *Shadowsocks) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata adapter.InboundContext) error {
  76. return h.service.NewPacket(adapter.WithContext(ctx, &metadata), conn, buffer, adapter.UpstreamMetadata(metadata))
  77. }
  78. func (h *Shadowsocks) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  79. return os.ErrInvalid
  80. }