inbound.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package shadowsocks_2022
  2. import (
  3. "context"
  4. "github.com/sagernet/sing-shadowsocks"
  5. "github.com/sagernet/sing-shadowsocks/shadowaead_2022"
  6. C "github.com/sagernet/sing/common"
  7. B "github.com/sagernet/sing/common/buf"
  8. "github.com/sagernet/sing/common/bufio"
  9. E "github.com/sagernet/sing/common/exceptions"
  10. M "github.com/sagernet/sing/common/metadata"
  11. N "github.com/sagernet/sing/common/network"
  12. "github.com/xtls/xray-core/common"
  13. "github.com/xtls/xray-core/common/buf"
  14. "github.com/xtls/xray-core/common/log"
  15. "github.com/xtls/xray-core/common/net"
  16. "github.com/xtls/xray-core/common/protocol"
  17. "github.com/xtls/xray-core/common/session"
  18. "github.com/xtls/xray-core/common/singbridge"
  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 singbridge.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 singbridge.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, singbridge.ToDestination(metadata.Destination, net.Network_TCP))
  105. if err != nil {
  106. return err
  107. }
  108. return singbridge.CopyConn(ctx, nil, link, conn)
  109. }
  110. func (i *Inbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
  111. inbound := session.InboundFromContext(ctx)
  112. inbound.User = &protocol.MemoryUser{
  113. Email: i.email,
  114. Level: uint32(i.level),
  115. }
  116. ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
  117. From: metadata.Source,
  118. To: metadata.Destination,
  119. Status: log.AccessAccepted,
  120. Email: i.email,
  121. })
  122. newError("tunnelling request to udp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
  123. dispatcher := session.DispatcherFromContext(ctx)
  124. destination := singbridge.ToDestination(metadata.Destination, net.Network_UDP)
  125. link, err := dispatcher.Dispatch(ctx, destination)
  126. if err != nil {
  127. return err
  128. }
  129. outConn := &packetConnWrapper{
  130. Reader: link.Reader,
  131. Writer: link.Writer,
  132. Dest: destination,
  133. }
  134. return bufio.CopyPacketConn(ctx, conn, outConn)
  135. }
  136. func (i *Inbound) NewError(ctx context.Context, err error) {
  137. if E.IsClosed(err) {
  138. return
  139. }
  140. newError(err).AtWarning().WriteToLog()
  141. }
  142. type natPacketConn struct {
  143. net.Conn
  144. }
  145. func (c *natPacketConn) ReadPacket(buffer *B.Buffer) (addr M.Socksaddr, err error) {
  146. _, err = buffer.ReadFrom(c)
  147. return
  148. }
  149. func (c *natPacketConn) WritePacket(buffer *B.Buffer, addr M.Socksaddr) error {
  150. _, err := buffer.WriteTo(c)
  151. return err
  152. }