shadowsocks_relay.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/shadowaead_2022"
  11. "github.com/sagernet/sing/common"
  12. "github.com/sagernet/sing/common/auth"
  13. "github.com/sagernet/sing/common/buf"
  14. F "github.com/sagernet/sing/common/format"
  15. N "github.com/sagernet/sing/common/network"
  16. )
  17. var _ adapter.Inbound = (*ShadowsocksMulti)(nil)
  18. type ShadowsocksRelay struct {
  19. myInboundAdapter
  20. service *shadowaead_2022.RelayService[int]
  21. destinations []option.ShadowsocksDestination
  22. }
  23. func newShadowsocksRelay(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksInboundOptions) (*ShadowsocksRelay, error) {
  24. inbound := &ShadowsocksRelay{
  25. myInboundAdapter: myInboundAdapter{
  26. protocol: C.TypeShadowsocks,
  27. network: options.Network.Build(),
  28. ctx: ctx,
  29. router: router,
  30. logger: logger,
  31. tag: tag,
  32. listenOptions: options.ListenOptions,
  33. },
  34. destinations: options.Destinations,
  35. }
  36. inbound.connHandler = inbound
  37. inbound.packetHandler = inbound
  38. var udpTimeout int64
  39. if options.UDPTimeout != 0 {
  40. udpTimeout = options.UDPTimeout
  41. } else {
  42. udpTimeout = int64(C.UDPTimeout.Seconds())
  43. }
  44. service, err := shadowaead_2022.NewRelayServiceWithPassword[int](
  45. options.Method,
  46. options.Password,
  47. udpTimeout,
  48. adapter.NewUpstreamContextHandler(inbound.newConnection, inbound.newPacketConnection, inbound),
  49. )
  50. if err != nil {
  51. return nil, err
  52. }
  53. err = service.UpdateUsersWithPasswords(common.MapIndexed(options.Destinations, func(index int, user option.ShadowsocksDestination) int {
  54. return index
  55. }), common.Map(options.Destinations, func(user option.ShadowsocksDestination) string {
  56. return user.Password
  57. }), common.Map(options.Destinations, option.ShadowsocksDestination.Build))
  58. if err != nil {
  59. return nil, err
  60. }
  61. inbound.service = service
  62. inbound.packetUpstream = service
  63. return inbound, err
  64. }
  65. func (h *ShadowsocksRelay) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  66. return h.service.NewConnection(adapter.WithContext(log.ContextWithNewID(ctx), &metadata), conn, adapter.UpstreamMetadata(metadata))
  67. }
  68. func (h *ShadowsocksRelay) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata adapter.InboundContext) error {
  69. return h.service.NewPacket(adapter.WithContext(ctx, &metadata), conn, buffer, adapter.UpstreamMetadata(metadata))
  70. }
  71. func (h *ShadowsocksRelay) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  72. destinationIndex, loaded := auth.UserFromContext[int](ctx)
  73. if !loaded {
  74. return os.ErrInvalid
  75. }
  76. destination := h.destinations[destinationIndex].Name
  77. if destination == "" {
  78. destination = F.ToString(destinationIndex)
  79. } else {
  80. metadata.User = destination
  81. }
  82. h.logger.InfoContext(ctx, "[", destination, "] inbound connection to ", metadata.Destination)
  83. return h.router.RouteConnection(ctx, conn, metadata)
  84. }
  85. func (h *ShadowsocksRelay) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  86. destinationIndex, loaded := auth.UserFromContext[int](ctx)
  87. if !loaded {
  88. return os.ErrInvalid
  89. }
  90. destination := h.destinations[destinationIndex].Name
  91. if destination == "" {
  92. destination = F.ToString(destinationIndex)
  93. } else {
  94. metadata.User = destination
  95. }
  96. ctx = log.ContextWithNewID(ctx)
  97. h.logger.InfoContext(ctx, "[", destination, "] inbound packet connection from ", metadata.Source)
  98. h.logger.InfoContext(ctx, "[", destination, "] inbound packet connection to ", metadata.Destination)
  99. return h.router.RoutePacketConnection(ctx, conn, metadata)
  100. }