inbound_relay.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package shadowsocks_2022
  2. import (
  3. "context"
  4. "strconv"
  5. "strings"
  6. "github.com/sagernet/sing-shadowsocks/shadowaead_2022"
  7. C "github.com/sagernet/sing/common"
  8. A "github.com/sagernet/sing/common/auth"
  9. B "github.com/sagernet/sing/common/buf"
  10. "github.com/sagernet/sing/common/bufio"
  11. E "github.com/sagernet/sing/common/exceptions"
  12. M "github.com/sagernet/sing/common/metadata"
  13. N "github.com/sagernet/sing/common/network"
  14. "github.com/xtls/xray-core/common"
  15. "github.com/xtls/xray-core/common/buf"
  16. "github.com/xtls/xray-core/common/errors"
  17. "github.com/xtls/xray-core/common/log"
  18. "github.com/xtls/xray-core/common/net"
  19. "github.com/xtls/xray-core/common/protocol"
  20. "github.com/xtls/xray-core/common/session"
  21. "github.com/xtls/xray-core/common/singbridge"
  22. "github.com/xtls/xray-core/common/uuid"
  23. "github.com/xtls/xray-core/features/routing"
  24. "github.com/xtls/xray-core/transport/internet/stat"
  25. )
  26. func init() {
  27. common.Must(common.RegisterConfig((*RelayServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  28. return NewRelayServer(ctx, config.(*RelayServerConfig))
  29. }))
  30. }
  31. type RelayInbound struct {
  32. networks []net.Network
  33. destinations []*RelayDestination
  34. service *shadowaead_2022.RelayService[int]
  35. }
  36. func NewRelayServer(ctx context.Context, config *RelayServerConfig) (*RelayInbound, error) {
  37. networks := config.Network
  38. if len(networks) == 0 {
  39. networks = []net.Network{
  40. net.Network_TCP,
  41. net.Network_UDP,
  42. }
  43. }
  44. inbound := &RelayInbound{
  45. networks: networks,
  46. destinations: config.Destinations,
  47. }
  48. if !C.Contains(shadowaead_2022.List, config.Method) || !strings.Contains(config.Method, "aes") {
  49. return nil, errors.New("unsupported method ", config.Method)
  50. }
  51. service, err := shadowaead_2022.NewRelayServiceWithPassword[int](config.Method, config.Key, 500, inbound)
  52. if err != nil {
  53. return nil, errors.New("create service").Base(err)
  54. }
  55. for i, destination := range config.Destinations {
  56. if destination.Email == "" {
  57. u := uuid.New()
  58. destination.Email = "unnamed-destination-" + strconv.Itoa(i) + "-" + u.String()
  59. }
  60. }
  61. err = service.UpdateUsersWithPasswords(
  62. C.MapIndexed(config.Destinations, func(index int, it *RelayDestination) int { return index }),
  63. C.Map(config.Destinations, func(it *RelayDestination) string { return it.Key }),
  64. C.Map(config.Destinations, func(it *RelayDestination) M.Socksaddr {
  65. return singbridge.ToSocksaddr(net.Destination{
  66. Address: it.Address.AsAddress(),
  67. Port: net.Port(it.Port),
  68. })
  69. }),
  70. )
  71. if err != nil {
  72. return nil, errors.New("create service").Base(err)
  73. }
  74. inbound.service = service
  75. return inbound, nil
  76. }
  77. func (i *RelayInbound) Network() []net.Network {
  78. return i.networks
  79. }
  80. func (i *RelayInbound) Process(ctx context.Context, network net.Network, connection stat.Connection, dispatcher routing.Dispatcher) error {
  81. inbound := session.InboundFromContext(ctx)
  82. inbound.Name = "shadowsocks-2022-relay"
  83. inbound.CanSpliceCopy = 3
  84. var metadata M.Metadata
  85. if inbound.Source.IsValid() {
  86. metadata.Source = M.ParseSocksaddr(inbound.Source.NetAddr())
  87. }
  88. ctx = session.ContextWithDispatcher(ctx, dispatcher)
  89. if network == net.Network_TCP {
  90. return singbridge.ReturnError(i.service.NewConnection(ctx, connection, metadata))
  91. } else {
  92. reader := buf.NewReader(connection)
  93. pc := &natPacketConn{connection}
  94. for {
  95. mb, err := reader.ReadMultiBuffer()
  96. if err != nil {
  97. buf.ReleaseMulti(mb)
  98. return singbridge.ReturnError(err)
  99. }
  100. for _, buffer := range mb {
  101. packet := B.As(buffer.Bytes()).ToOwned()
  102. buffer.Release()
  103. err = i.service.NewPacket(ctx, pc, packet, metadata)
  104. if err != nil {
  105. packet.Release()
  106. buf.ReleaseMulti(mb)
  107. return err
  108. }
  109. }
  110. }
  111. }
  112. }
  113. func (i *RelayInbound) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
  114. inbound := session.InboundFromContext(ctx)
  115. userInt, _ := A.UserFromContext[int](ctx)
  116. user := i.destinations[userInt]
  117. inbound.User = &protocol.MemoryUser{
  118. Email: user.Email,
  119. Level: uint32(user.Level),
  120. }
  121. ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
  122. From: metadata.Source,
  123. To: metadata.Destination,
  124. Status: log.AccessAccepted,
  125. Email: user.Email,
  126. })
  127. errors.LogInfo(ctx, "tunnelling request to tcp:", metadata.Destination)
  128. dispatcher := session.DispatcherFromContext(ctx)
  129. link, err := dispatcher.Dispatch(ctx, singbridge.ToDestination(metadata.Destination, net.Network_TCP))
  130. if err != nil {
  131. return err
  132. }
  133. return singbridge.CopyConn(ctx, nil, link, conn)
  134. }
  135. func (i *RelayInbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
  136. inbound := session.InboundFromContext(ctx)
  137. userInt, _ := A.UserFromContext[int](ctx)
  138. user := i.destinations[userInt]
  139. inbound.User = &protocol.MemoryUser{
  140. Email: user.Email,
  141. Level: uint32(user.Level),
  142. }
  143. ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
  144. From: metadata.Source,
  145. To: metadata.Destination,
  146. Status: log.AccessAccepted,
  147. Email: user.Email,
  148. })
  149. errors.LogInfo(ctx, "tunnelling request to udp:", metadata.Destination)
  150. dispatcher := session.DispatcherFromContext(ctx)
  151. destination := singbridge.ToDestination(metadata.Destination, net.Network_UDP)
  152. link, err := dispatcher.Dispatch(ctx, destination)
  153. if err != nil {
  154. return err
  155. }
  156. outConn := &singbridge.PacketConnWrapper{
  157. Reader: link.Reader,
  158. Writer: link.Writer,
  159. Dest: destination,
  160. }
  161. return bufio.CopyPacketConn(ctx, conn, outConn)
  162. }
  163. func (i *RelayInbound) NewError(ctx context.Context, err error) {
  164. if E.IsClosed(err) {
  165. return
  166. }
  167. errors.LogWarning(ctx, err.Error())
  168. }