tls.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package obfs
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "io"
  6. "math/rand"
  7. "net"
  8. "time"
  9. B "github.com/sagernet/sing/common/buf"
  10. "github.com/sagernet/sing/common/random"
  11. )
  12. func init() {
  13. random.InitializeSeed()
  14. }
  15. const (
  16. chunkSize = 1 << 14 // 2 ** 14 == 16 * 1024
  17. )
  18. // TLSObfs is shadowsocks tls simple-obfs implementation
  19. type TLSObfs struct {
  20. net.Conn
  21. server string
  22. remain int
  23. firstRequest bool
  24. firstResponse bool
  25. }
  26. func (to *TLSObfs) read(b []byte, discardN int) (int, error) {
  27. buf := B.Get(discardN)
  28. _, err := io.ReadFull(to.Conn, buf)
  29. B.Put(buf)
  30. if err != nil {
  31. return 0, err
  32. }
  33. sizeBuf := make([]byte, 2)
  34. _, err = io.ReadFull(to.Conn, sizeBuf)
  35. if err != nil {
  36. return 0, nil
  37. }
  38. length := int(binary.BigEndian.Uint16(sizeBuf))
  39. if length > len(b) {
  40. n, err := to.Conn.Read(b)
  41. if err != nil {
  42. return n, err
  43. }
  44. to.remain = length - n
  45. return n, nil
  46. }
  47. return io.ReadFull(to.Conn, b[:length])
  48. }
  49. func (to *TLSObfs) Read(b []byte) (int, error) {
  50. if to.remain > 0 {
  51. length := to.remain
  52. if length > len(b) {
  53. length = len(b)
  54. }
  55. n, err := io.ReadFull(to.Conn, b[:length])
  56. to.remain -= n
  57. return n, err
  58. }
  59. if to.firstResponse {
  60. // type + ver + lensize + 91 = 96
  61. // type + ver + lensize + 1 = 6
  62. // type + ver = 3
  63. to.firstResponse = false
  64. return to.read(b, 105)
  65. }
  66. // type + ver = 3
  67. return to.read(b, 3)
  68. }
  69. func (to *TLSObfs) Write(b []byte) (int, error) {
  70. length := len(b)
  71. for i := 0; i < length; i += chunkSize {
  72. end := i + chunkSize
  73. if end > length {
  74. end = length
  75. }
  76. n, err := to.write(b[i:end])
  77. if err != nil {
  78. return n, err
  79. }
  80. }
  81. return length, nil
  82. }
  83. func (to *TLSObfs) write(b []byte) (int, error) {
  84. if to.firstRequest {
  85. helloMsg := makeClientHelloMsg(b, to.server)
  86. _, err := to.Conn.Write(helloMsg)
  87. to.firstRequest = false
  88. return len(b), err
  89. }
  90. buf := B.NewSize(5 + len(b))
  91. defer buf.Release()
  92. buf.Write([]byte{0x17, 0x03, 0x03})
  93. binary.Write(buf, binary.BigEndian, uint16(len(b)))
  94. buf.Write(b)
  95. _, err := to.Conn.Write(buf.Bytes())
  96. return len(b), err
  97. }
  98. func (to *TLSObfs) Upstream() any {
  99. return to.Conn
  100. }
  101. // NewTLSObfs return a SimpleObfs
  102. func NewTLSObfs(conn net.Conn, server string) net.Conn {
  103. return &TLSObfs{
  104. Conn: conn,
  105. server: server,
  106. firstRequest: true,
  107. firstResponse: true,
  108. }
  109. }
  110. func makeClientHelloMsg(data []byte, server string) []byte {
  111. random := make([]byte, 28)
  112. sessionID := make([]byte, 32)
  113. rand.Read(random)
  114. rand.Read(sessionID)
  115. buf := &bytes.Buffer{}
  116. // handshake, TLS 1.0 version, length
  117. buf.WriteByte(22)
  118. buf.Write([]byte{0x03, 0x01})
  119. length := uint16(212 + len(data) + len(server))
  120. buf.WriteByte(byte(length >> 8))
  121. buf.WriteByte(byte(length & 0xff))
  122. // clientHello, length, TLS 1.2 version
  123. buf.WriteByte(1)
  124. buf.WriteByte(0)
  125. binary.Write(buf, binary.BigEndian, uint16(208+len(data)+len(server)))
  126. buf.Write([]byte{0x03, 0x03})
  127. // random with timestamp, sid len, sid
  128. binary.Write(buf, binary.BigEndian, uint32(time.Now().Unix()))
  129. buf.Write(random)
  130. buf.WriteByte(32)
  131. buf.Write(sessionID)
  132. // cipher suites
  133. buf.Write([]byte{0x00, 0x38})
  134. buf.Write([]byte{
  135. 0xc0, 0x2c, 0xc0, 0x30, 0x00, 0x9f, 0xcc, 0xa9, 0xcc, 0xa8, 0xcc, 0xaa, 0xc0, 0x2b, 0xc0, 0x2f,
  136. 0x00, 0x9e, 0xc0, 0x24, 0xc0, 0x28, 0x00, 0x6b, 0xc0, 0x23, 0xc0, 0x27, 0x00, 0x67, 0xc0, 0x0a,
  137. 0xc0, 0x14, 0x00, 0x39, 0xc0, 0x09, 0xc0, 0x13, 0x00, 0x33, 0x00, 0x9d, 0x00, 0x9c, 0x00, 0x3d,
  138. 0x00, 0x3c, 0x00, 0x35, 0x00, 0x2f, 0x00, 0xff,
  139. })
  140. // compression
  141. buf.Write([]byte{0x01, 0x00})
  142. // extension length
  143. binary.Write(buf, binary.BigEndian, uint16(79+len(data)+len(server)))
  144. // session ticket
  145. buf.Write([]byte{0x00, 0x23})
  146. binary.Write(buf, binary.BigEndian, uint16(len(data)))
  147. buf.Write(data)
  148. // server name
  149. buf.Write([]byte{0x00, 0x00})
  150. binary.Write(buf, binary.BigEndian, uint16(len(server)+5))
  151. binary.Write(buf, binary.BigEndian, uint16(len(server)+3))
  152. buf.WriteByte(0)
  153. binary.Write(buf, binary.BigEndian, uint16(len(server)))
  154. buf.Write([]byte(server))
  155. // ec_point
  156. buf.Write([]byte{0x00, 0x0b, 0x00, 0x04, 0x03, 0x01, 0x00, 0x02})
  157. // groups
  158. buf.Write([]byte{0x00, 0x0a, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x19, 0x00, 0x18})
  159. // signature
  160. buf.Write([]byte{
  161. 0x00, 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x01, 0x06, 0x02, 0x06, 0x03, 0x05,
  162. 0x01, 0x05, 0x02, 0x05, 0x03, 0x04, 0x01, 0x04, 0x02, 0x04, 0x03, 0x03, 0x01,
  163. 0x03, 0x02, 0x03, 0x03, 0x02, 0x01, 0x02, 0x02, 0x02, 0x03,
  164. })
  165. // encrypt then mac
  166. buf.Write([]byte{0x00, 0x16, 0x00, 0x00})
  167. // extended master secret
  168. buf.Write([]byte{0x00, 0x17, 0x00, 0x00})
  169. return buf.Bytes()
  170. }