inbound_multi.go 5.2 KB

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