shadowsocks_multi.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package inbound
  2. import (
  3. "context"
  4. "net"
  5. "github.com/sagernet/sing-box/adapter"
  6. C "github.com/sagernet/sing-box/constant"
  7. "github.com/sagernet/sing-box/log"
  8. "github.com/sagernet/sing-box/option"
  9. "github.com/sagernet/sing-shadowsocks"
  10. "github.com/sagernet/sing-shadowsocks/shadowaead_2022"
  11. "github.com/sagernet/sing/common"
  12. "github.com/sagernet/sing/common/buf"
  13. F "github.com/sagernet/sing/common/format"
  14. N "github.com/sagernet/sing/common/network"
  15. )
  16. var _ adapter.Inbound = (*ShadowsocksMulti)(nil)
  17. type ShadowsocksMulti struct {
  18. myInboundAdapter
  19. service *shadowaead_2022.MultiService[int]
  20. users []option.ShadowsocksUser
  21. }
  22. func newShadowsocksMulti(ctx context.Context, router adapter.Router, logger log.Logger, tag string, options option.ShadowsocksInboundOptions) (*ShadowsocksMulti, error) {
  23. inbound := &ShadowsocksMulti{
  24. myInboundAdapter: myInboundAdapter{
  25. protocol: C.TypeShadowsocks,
  26. network: options.Network.Build(),
  27. ctx: ctx,
  28. router: router,
  29. logger: logger,
  30. tag: tag,
  31. listenOptions: options.ListenOptions,
  32. },
  33. users: options.Users,
  34. }
  35. inbound.connHandler = inbound
  36. inbound.packetHandler = inbound
  37. var udpTimeout int64
  38. if options.UDPTimeout != 0 {
  39. udpTimeout = options.UDPTimeout
  40. } else {
  41. udpTimeout = 300
  42. }
  43. service, err := shadowaead_2022.NewMultiServiceWithPassword[int](
  44. options.Method,
  45. options.Password,
  46. udpTimeout,
  47. adapter.NewUpstreamContextHandler(inbound.newConnection, inbound.newPacketConnection, inbound),
  48. )
  49. if err != nil {
  50. return nil, err
  51. }
  52. err = service.UpdateUsersWithPasswords(common.MapIndexed(options.Users, func(index int, user option.ShadowsocksUser) int {
  53. return index
  54. }), common.Map(options.Users, func(user option.ShadowsocksUser) string {
  55. return user.Password
  56. }))
  57. if err != nil {
  58. return nil, err
  59. }
  60. inbound.service = service
  61. inbound.packetUpstream = service
  62. return inbound, err
  63. }
  64. func (h *ShadowsocksMulti) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  65. return h.service.NewConnection(adapter.ContextWithMetadata(log.ContextWithID(ctx), metadata), conn, adapter.UpstreamMetadata(metadata))
  66. }
  67. func (h *ShadowsocksMulti) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata adapter.InboundContext) error {
  68. return h.service.NewPacket(adapter.ContextWithMetadata(log.ContextWithID(ctx), metadata), conn, buffer, adapter.UpstreamMetadata(metadata))
  69. }
  70. func (h *ShadowsocksMulti) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  71. userCtx := ctx.(*shadowsocks.UserContext[int])
  72. user := h.users[userCtx.User].Name
  73. if user == "" {
  74. user = F.ToString(userCtx.User)
  75. }
  76. h.logger.WithContext(ctx).Info("[", user, "] inbound connection to ", metadata.Destination)
  77. return h.router.RouteConnection(ctx, conn, metadata)
  78. }
  79. func (h *ShadowsocksMulti) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  80. userCtx := ctx.(*shadowsocks.UserContext[int])
  81. user := h.users[userCtx.User].Name
  82. if user == "" {
  83. user = F.ToString(userCtx.User)
  84. }
  85. ctx = log.ContextWithID(ctx)
  86. h.logger.WithContext(ctx).Info("[", user, "] inbound packet connection from ", metadata.Source)
  87. h.logger.WithContext(ctx).Info("[", user, "] inbound packet connection to ", metadata.Destination)
  88. return h.router.RoutePacketConnection(ctx, conn, metadata)
  89. }