outbound.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package shadowsocks_2022
  2. import (
  3. "context"
  4. "time"
  5. shadowsocks "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. N "github.com/sagernet/sing/common/network"
  11. "github.com/sagernet/sing/common/uot"
  12. "github.com/xtls/xray-core/common"
  13. "github.com/xtls/xray-core/common/buf"
  14. "github.com/xtls/xray-core/common/net"
  15. "github.com/xtls/xray-core/common/session"
  16. "github.com/xtls/xray-core/common/singbridge"
  17. "github.com/xtls/xray-core/transport"
  18. "github.com/xtls/xray-core/transport/internet"
  19. )
  20. func init() {
  21. common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  22. return NewClient(ctx, config.(*ClientConfig))
  23. }))
  24. }
  25. type Outbound struct {
  26. ctx context.Context
  27. server net.Destination
  28. method shadowsocks.Method
  29. uotClient *uot.Client
  30. }
  31. func NewClient(ctx context.Context, config *ClientConfig) (*Outbound, error) {
  32. o := &Outbound{
  33. ctx: ctx,
  34. server: net.Destination{
  35. Address: config.Address.AsAddress(),
  36. Port: net.Port(config.Port),
  37. Network: net.Network_TCP,
  38. },
  39. }
  40. if C.Contains(shadowaead_2022.List, config.Method) {
  41. if config.Key == "" {
  42. return nil, newError("missing psk")
  43. }
  44. method, err := shadowaead_2022.NewWithPassword(config.Method, config.Key, nil)
  45. if err != nil {
  46. return nil, newError("create method").Base(err)
  47. }
  48. o.method = method
  49. } else {
  50. return nil, newError("unknown method ", config.Method)
  51. }
  52. if config.UdpOverTcp {
  53. o.uotClient = &uot.Client{Version: uint8(config.UdpOverTcpVersion)}
  54. }
  55. return o, nil
  56. }
  57. func (o *Outbound) Process(ctx context.Context, link *transport.Link, dialer internet.Dialer) error {
  58. var inboundConn net.Conn
  59. inbound := session.InboundFromContext(ctx)
  60. if inbound != nil {
  61. inboundConn = inbound.Conn
  62. }
  63. outbounds := session.OutboundsFromContext(ctx)
  64. ob := outbounds[len(outbounds) - 1]
  65. if !ob.Target.IsValid() {
  66. return newError("target not specified")
  67. }
  68. ob.Name = "shadowsocks-2022"
  69. ob.CanSpliceCopy = 3
  70. destination := ob.Target
  71. network := destination.Network
  72. newError("tunneling request to ", destination, " via ", o.server.NetAddr()).WriteToLog(session.ExportIDToError(ctx))
  73. serverDestination := o.server
  74. if o.uotClient != nil {
  75. serverDestination.Network = net.Network_TCP
  76. } else {
  77. serverDestination.Network = network
  78. }
  79. connection, err := dialer.Dial(ctx, serverDestination)
  80. if err != nil {
  81. return newError("failed to connect to server").Base(err)
  82. }
  83. if session.TimeoutOnlyFromContext(ctx) {
  84. ctx, _ = context.WithCancel(context.Background())
  85. }
  86. if network == net.Network_TCP {
  87. serverConn := o.method.DialEarlyConn(connection, singbridge.ToSocksaddr(destination))
  88. var handshake bool
  89. if timeoutReader, isTimeoutReader := link.Reader.(buf.TimeoutReader); isTimeoutReader {
  90. mb, err := timeoutReader.ReadMultiBufferTimeout(time.Millisecond * 100)
  91. if err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
  92. return newError("read payload").Base(err)
  93. }
  94. payload := B.New()
  95. for {
  96. payload.Reset()
  97. nb, n := buf.SplitBytes(mb, payload.FreeBytes())
  98. if n > 0 {
  99. payload.Truncate(n)
  100. _, err = serverConn.Write(payload.Bytes())
  101. if err != nil {
  102. payload.Release()
  103. return newError("write payload").Base(err)
  104. }
  105. handshake = true
  106. }
  107. if nb.IsEmpty() {
  108. break
  109. }
  110. mb = nb
  111. }
  112. payload.Release()
  113. }
  114. if !handshake {
  115. _, err = serverConn.Write(nil)
  116. if err != nil {
  117. return newError("client handshake").Base(err)
  118. }
  119. }
  120. return singbridge.CopyConn(ctx, inboundConn, link, serverConn)
  121. } else {
  122. var packetConn N.PacketConn
  123. if pc, isPacketConn := inboundConn.(N.PacketConn); isPacketConn {
  124. packetConn = pc
  125. } else if nc, isNetPacket := inboundConn.(net.PacketConn); isNetPacket {
  126. packetConn = bufio.NewPacketConn(nc)
  127. } else {
  128. packetConn = &singbridge.PacketConnWrapper{
  129. Reader: link.Reader,
  130. Writer: link.Writer,
  131. Conn: inboundConn,
  132. Dest: destination,
  133. }
  134. }
  135. if o.uotClient != nil {
  136. uConn, err := o.uotClient.DialEarlyConn(o.method.DialEarlyConn(connection, uot.RequestDestination(o.uotClient.Version)), false, singbridge.ToSocksaddr(destination))
  137. if err != nil {
  138. return err
  139. }
  140. return singbridge.ReturnError(bufio.CopyPacketConn(ctx, packetConn, uConn))
  141. } else {
  142. serverConn := o.method.DialPacketConn(connection)
  143. return singbridge.ReturnError(bufio.CopyPacketConn(ctx, packetConn, serverConn))
  144. }
  145. }
  146. }