shadowsocks_relay.go 4.0 KB

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