conn.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package tf
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/binary"
  6. "math/rand"
  7. "net"
  8. "strings"
  9. "time"
  10. N "github.com/sagernet/sing/common/network"
  11. "golang.org/x/net/publicsuffix"
  12. )
  13. type Conn struct {
  14. net.Conn
  15. tcpConn *net.TCPConn
  16. ctx context.Context
  17. firstPacketWritten bool
  18. splitRecord bool
  19. fallbackDelay time.Duration
  20. }
  21. func NewConn(conn net.Conn, ctx context.Context, splitRecord bool, fallbackDelay time.Duration) *Conn {
  22. tcpConn, _ := N.UnwrapReader(conn).(*net.TCPConn)
  23. return &Conn{
  24. Conn: conn,
  25. tcpConn: tcpConn,
  26. ctx: ctx,
  27. splitRecord: splitRecord,
  28. fallbackDelay: fallbackDelay,
  29. }
  30. }
  31. func (c *Conn) Write(b []byte) (n int, err error) {
  32. if !c.firstPacketWritten {
  33. defer func() {
  34. c.firstPacketWritten = true
  35. }()
  36. serverName := indexTLSServerName(b)
  37. if serverName != nil {
  38. if !c.splitRecord {
  39. if c.tcpConn != nil {
  40. err = c.tcpConn.SetNoDelay(true)
  41. if err != nil {
  42. return
  43. }
  44. }
  45. }
  46. splits := strings.Split(serverName.ServerName, ".")
  47. currentIndex := serverName.Index
  48. if publicSuffix := publicsuffix.List.PublicSuffix(serverName.ServerName); publicSuffix != "" {
  49. splits = splits[:len(splits)-strings.Count(serverName.ServerName, ".")]
  50. }
  51. if len(splits) > 1 && splits[0] == "..." {
  52. currentIndex += len(splits[0]) + 1
  53. splits = splits[1:]
  54. }
  55. var splitIndexes []int
  56. for i, split := range splits {
  57. splitAt := rand.Intn(len(split))
  58. splitIndexes = append(splitIndexes, currentIndex+splitAt)
  59. currentIndex += len(split)
  60. if i != len(splits)-1 {
  61. currentIndex++
  62. }
  63. }
  64. var buffer bytes.Buffer
  65. for i := 0; i <= len(splitIndexes); i++ {
  66. var payload []byte
  67. if i == 0 {
  68. payload = b[:splitIndexes[i]]
  69. if c.splitRecord {
  70. payload = payload[recordLayerHeaderLen:]
  71. }
  72. } else if i == len(splitIndexes) {
  73. payload = b[splitIndexes[i-1]:]
  74. } else {
  75. payload = b[splitIndexes[i-1]:splitIndexes[i]]
  76. }
  77. if c.splitRecord {
  78. payloadLen := uint16(len(payload))
  79. buffer.Write(b[:3])
  80. binary.Write(&buffer, binary.BigEndian, payloadLen)
  81. buffer.Write(payload)
  82. } else if c.tcpConn != nil && i != len(splitIndexes) {
  83. err = writeAndWaitAck(c.ctx, c.tcpConn, payload, c.fallbackDelay)
  84. if err != nil {
  85. return
  86. }
  87. } else {
  88. _, err = c.Conn.Write(payload)
  89. if err != nil {
  90. return
  91. }
  92. }
  93. }
  94. if c.splitRecord {
  95. _, err = c.Conn.Write(buffer.Bytes())
  96. if err != nil {
  97. return
  98. }
  99. } else {
  100. if c.tcpConn != nil {
  101. err = c.tcpConn.SetNoDelay(false)
  102. if err != nil {
  103. return
  104. }
  105. }
  106. }
  107. return len(b), nil
  108. }
  109. }
  110. return c.Conn.Write(b)
  111. }
  112. func (c *Conn) ReaderReplaceable() bool {
  113. return true
  114. }
  115. func (c *Conn) WriterReplaceable() bool {
  116. return c.firstPacketWritten
  117. }
  118. func (c *Conn) Upstream() any {
  119. return c.Conn
  120. }