inbound_relay.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. inbound.Name = "shadowsocks-2022-relay"
  82. var metadata M.Metadata
  83. if inbound.Source.IsValid() {
  84. metadata.Source = M.ParseSocksaddr(inbound.Source.NetAddr())
  85. }
  86. ctx = session.ContextWithDispatcher(ctx, dispatcher)
  87. if network == net.Network_TCP {
  88. return singbridge.ReturnError(i.service.NewConnection(ctx, connection, metadata))
  89. } else {
  90. reader := buf.NewReader(connection)
  91. pc := &natPacketConn{connection}
  92. for {
  93. mb, err := reader.ReadMultiBuffer()
  94. if err != nil {
  95. buf.ReleaseMulti(mb)
  96. return singbridge.ReturnError(err)
  97. }
  98. for _, buffer := range mb {
  99. packet := B.As(buffer.Bytes()).ToOwned()
  100. err = i.service.NewPacket(ctx, pc, packet, metadata)
  101. if err != nil {
  102. packet.Release()
  103. buf.ReleaseMulti(mb)
  104. return err
  105. }
  106. buffer.Release()
  107. }
  108. }
  109. }
  110. }
  111. func (i *RelayInbound) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
  112. inbound := session.InboundFromContext(ctx)
  113. userInt, _ := A.UserFromContext[int](ctx)
  114. user := i.destinations[userInt]
  115. inbound.User = &protocol.MemoryUser{
  116. Email: user.Email,
  117. Level: uint32(user.Level),
  118. }
  119. ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
  120. From: metadata.Source,
  121. To: metadata.Destination,
  122. Status: log.AccessAccepted,
  123. Email: user.Email,
  124. })
  125. newError("tunnelling request to tcp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
  126. dispatcher := session.DispatcherFromContext(ctx)
  127. link, err := dispatcher.Dispatch(ctx, singbridge.ToDestination(metadata.Destination, net.Network_TCP))
  128. if err != nil {
  129. return err
  130. }
  131. return singbridge.CopyConn(ctx, nil, link, conn)
  132. }
  133. func (i *RelayInbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
  134. inbound := session.InboundFromContext(ctx)
  135. userInt, _ := A.UserFromContext[int](ctx)
  136. user := i.destinations[userInt]
  137. inbound.User = &protocol.MemoryUser{
  138. Email: user.Email,
  139. Level: uint32(user.Level),
  140. }
  141. ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
  142. From: metadata.Source,
  143. To: metadata.Destination,
  144. Status: log.AccessAccepted,
  145. Email: user.Email,
  146. })
  147. newError("tunnelling request to udp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
  148. dispatcher := session.DispatcherFromContext(ctx)
  149. destination := singbridge.ToDestination(metadata.Destination, net.Network_UDP)
  150. link, err := dispatcher.Dispatch(ctx, destination)
  151. if err != nil {
  152. return err
  153. }
  154. outConn := &singbridge.PacketConnWrapper{
  155. Reader: link.Reader,
  156. Writer: link.Writer,
  157. Dest: destination,
  158. }
  159. return bufio.CopyPacketConn(ctx, conn, outConn)
  160. }
  161. func (i *RelayInbound) NewError(ctx context.Context, err error) {
  162. if E.IsClosed(err) {
  163. return
  164. }
  165. newError(err).AtWarning().WriteToLog()
  166. }