shadowsocks_relay.go 3.6 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 ShadowsocksRelay struct {
  18. myInboundAdapter
  19. service *shadowaead_2022.RelayService[int]
  20. destinations []option.ShadowsocksDestination
  21. }
  22. func newShadowsocksRelay(ctx context.Context, router adapter.Router, logger log.Logger, tag string, options option.ShadowsocksInboundOptions) (*ShadowsocksRelay, error) {
  23. inbound := &ShadowsocksRelay{
  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. destinations: options.Destinations,
  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.NewRelayServiceWithPassword[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.Destinations, func(index int, user option.ShadowsocksDestination) int {
  53. return index
  54. }), common.Map(options.Destinations, func(user option.ShadowsocksDestination) string {
  55. return user.Password
  56. }), common.Map(options.Destinations, option.ShadowsocksDestination.Build))
  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 *ShadowsocksRelay) 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 *ShadowsocksRelay) 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 *ShadowsocksRelay) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  71. userCtx := ctx.(*shadowsocks.UserContext[int])
  72. destination := h.destinations[userCtx.User].Name
  73. if destination == "" {
  74. destination = F.ToString(userCtx.User)
  75. }
  76. h.logger.WithContext(ctx).Info("[", destination, "] inbound connection to ", metadata.Destination)
  77. return h.router.RouteConnection(ctx, conn, metadata)
  78. }
  79. func (h *ShadowsocksRelay) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  80. userCtx := ctx.(*shadowsocks.UserContext[int])
  81. destination := h.destinations[userCtx.User].Name
  82. if destination == "" {
  83. destination = F.ToString(userCtx.User)
  84. }
  85. ctx = log.ContextWithID(ctx)
  86. h.logger.WithContext(ctx).Info("[", destination, "] inbound packet connection from ", metadata.Source)
  87. h.logger.WithContext(ctx).Info("[", destination, "] inbound packet connection to ", metadata.Destination)
  88. return h.router.RoutePacketConnection(ctx, conn, metadata)
  89. }