tls.go 4.5 KB

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