inbound_relay.go 5.4 KB

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