inbound.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //go:build go1.18
  2. package shadowsocks_2022
  3. import (
  4. "context"
  5. "github.com/sagernet/sing-shadowsocks"
  6. "github.com/sagernet/sing-shadowsocks/shadowaead_2022"
  7. C "github.com/sagernet/sing/common"
  8. B "github.com/sagernet/sing/common/buf"
  9. "github.com/sagernet/sing/common/bufio"
  10. E "github.com/sagernet/sing/common/exceptions"
  11. M "github.com/sagernet/sing/common/metadata"
  12. N "github.com/sagernet/sing/common/network"
  13. "github.com/xtls/xray-core/common"
  14. "github.com/xtls/xray-core/common/buf"
  15. "github.com/xtls/xray-core/common/log"
  16. "github.com/xtls/xray-core/common/net"
  17. "github.com/xtls/xray-core/common/protocol"
  18. "github.com/xtls/xray-core/common/session"
  19. "github.com/xtls/xray-core/features/routing"
  20. "github.com/xtls/xray-core/transport/internet/stat"
  21. )
  22. func init() {
  23. common.Must(common.RegisterConfig((*ServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  24. return NewServer(ctx, config.(*ServerConfig))
  25. }))
  26. }
  27. type Inbound struct {
  28. networks []net.Network
  29. service shadowsocks.Service
  30. email string
  31. level int
  32. }
  33. func NewServer(ctx context.Context, config *ServerConfig) (*Inbound, error) {
  34. networks := config.Network
  35. if len(networks) == 0 {
  36. networks = []net.Network{
  37. net.Network_TCP,
  38. net.Network_UDP,
  39. }
  40. }
  41. inbound := &Inbound{
  42. networks: networks,
  43. email: config.Email,
  44. level: int(config.Level),
  45. }
  46. if !C.Contains(shadowaead_2022.List, config.Method) {
  47. return nil, newError("unsupported method ", config.Method)
  48. }
  49. service, err := shadowaead_2022.NewServiceWithPassword(config.Method, config.Key, 500, inbound)
  50. if err != nil {
  51. return nil, newError("create service").Base(err)
  52. }
  53. inbound.service = service
  54. return inbound, nil
  55. }
  56. func (i *Inbound) Network() []net.Network {
  57. return i.networks
  58. }
  59. func (i *Inbound) Process(ctx context.Context, network net.Network, connection stat.Connection, dispatcher routing.Dispatcher) error {
  60. inbound := session.InboundFromContext(ctx)
  61. var metadata M.Metadata
  62. if inbound.Source.IsValid() {
  63. metadata.Source = M.ParseSocksaddr(inbound.Source.NetAddr())
  64. }
  65. ctx = session.ContextWithDispatcher(ctx, dispatcher)
  66. if network == net.Network_TCP {
  67. return returnError(i.service.NewConnection(ctx, connection, metadata))
  68. } else {
  69. reader := buf.NewReader(connection)
  70. pc := &natPacketConn{connection}
  71. for {
  72. mb, err := reader.ReadMultiBuffer()
  73. if err != nil {
  74. buf.ReleaseMulti(mb)
  75. return returnError(err)
  76. }
  77. for _, buffer := range mb {
  78. packet := B.As(buffer.Bytes()).ToOwned()
  79. err = i.service.NewPacket(ctx, pc, packet, metadata)
  80. if err != nil {
  81. packet.Release()
  82. buf.ReleaseMulti(mb)
  83. return err
  84. }
  85. buffer.Release()
  86. }
  87. }
  88. }
  89. }
  90. func (i *Inbound) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
  91. inbound := session.InboundFromContext(ctx)
  92. inbound.User = &protocol.MemoryUser{
  93. Email: i.email,
  94. Level: uint32(i.level),
  95. }
  96. ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
  97. From: metadata.Source,
  98. To: metadata.Destination,
  99. Status: log.AccessAccepted,
  100. Email: i.email,
  101. })
  102. newError("tunnelling request to tcp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
  103. dispatcher := session.DispatcherFromContext(ctx)
  104. link, err := dispatcher.Dispatch(ctx, toDestination(metadata.Destination, net.Network_TCP))
  105. if err != nil {
  106. return err
  107. }
  108. outConn := &pipeConnWrapper{
  109. &buf.BufferedReader{Reader: link.Reader},
  110. link.Writer,
  111. conn,
  112. }
  113. return bufio.CopyConn(ctx, conn, outConn)
  114. }
  115. func (i *Inbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
  116. inbound := session.InboundFromContext(ctx)
  117. inbound.User = &protocol.MemoryUser{
  118. Email: i.email,
  119. Level: uint32(i.level),
  120. }
  121. ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
  122. From: metadata.Source,
  123. To: metadata.Destination,
  124. Status: log.AccessAccepted,
  125. Email: i.email,
  126. })
  127. newError("tunnelling request to udp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
  128. dispatcher := session.DispatcherFromContext(ctx)
  129. destination := toDestination(metadata.Destination, net.Network_UDP)
  130. link, err := dispatcher.Dispatch(ctx, destination)
  131. if err != nil {
  132. return err
  133. }
  134. outConn := &packetConnWrapper{
  135. Reader: link.Reader,
  136. Writer: link.Writer,
  137. Dest: destination,
  138. }
  139. return bufio.CopyPacketConn(ctx, conn, outConn)
  140. }
  141. func (i *Inbound) HandleError(err error) {
  142. if E.IsClosed(err) {
  143. return
  144. }
  145. newError(err).AtWarning().WriteToLog()
  146. }
  147. type natPacketConn struct {
  148. net.Conn
  149. }
  150. func (c *natPacketConn) ReadPacket(buffer *B.Buffer) (addr M.Socksaddr, err error) {
  151. _, err = buffer.ReadFrom(c)
  152. return
  153. }
  154. func (c *natPacketConn) WritePacket(buffer *B.Buffer, addr M.Socksaddr) error {
  155. _, err := buffer.WriteTo(c)
  156. return err
  157. }