inbound.go 4.8 KB

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