tls1.2_ticket_auth.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package obfs
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "encoding/binary"
  6. "math/rand"
  7. "net"
  8. "strings"
  9. "time"
  10. "github.com/Dreamacro/clash/common/pool"
  11. "github.com/Dreamacro/clash/transport/ssr/tools"
  12. )
  13. func init() {
  14. register("tls1.2_ticket_auth", newTLS12Ticket, 5)
  15. register("tls1.2_ticket_fastauth", newTLS12Ticket, 5)
  16. }
  17. type tls12Ticket struct {
  18. *Base
  19. *authData
  20. }
  21. func newTLS12Ticket(b *Base) Obfs {
  22. r := &tls12Ticket{Base: b, authData: &authData{}}
  23. rand.Read(r.clientID[:])
  24. return r
  25. }
  26. type tls12TicketConn struct {
  27. net.Conn
  28. *tls12Ticket
  29. handshakeStatus int
  30. decoded bytes.Buffer
  31. underDecoded bytes.Buffer
  32. sendBuf bytes.Buffer
  33. }
  34. func (t *tls12Ticket) StreamConn(c net.Conn) net.Conn {
  35. return &tls12TicketConn{Conn: c, tls12Ticket: t}
  36. }
  37. func (c *tls12TicketConn) Read(b []byte) (int, error) {
  38. if c.decoded.Len() > 0 {
  39. return c.decoded.Read(b)
  40. }
  41. buf := pool.Get(pool.RelayBufferSize)
  42. defer pool.Put(buf)
  43. n, err := c.Conn.Read(buf)
  44. if err != nil {
  45. return 0, err
  46. }
  47. if c.handshakeStatus == 8 {
  48. c.underDecoded.Write(buf[:n])
  49. for c.underDecoded.Len() > 5 {
  50. if !bytes.Equal(c.underDecoded.Bytes()[:3], []byte{0x17, 3, 3}) {
  51. c.underDecoded.Reset()
  52. return 0, errTLS12TicketAuthIncorrectMagicNumber
  53. }
  54. size := int(binary.BigEndian.Uint16(c.underDecoded.Bytes()[3:5]))
  55. if c.underDecoded.Len() < 5+size {
  56. break
  57. }
  58. c.underDecoded.Next(5)
  59. c.decoded.Write(c.underDecoded.Next(size))
  60. }
  61. n, _ = c.decoded.Read(b)
  62. return n, nil
  63. }
  64. if n < 11+32+1+32 {
  65. return 0, errTLS12TicketAuthTooShortData
  66. }
  67. if !hmac.Equal(buf[33:43], c.hmacSHA1(buf[11:33])[:10]) || !hmac.Equal(buf[n-10:n], c.hmacSHA1(buf[:n-10])[:10]) {
  68. return 0, errTLS12TicketAuthHMACError
  69. }
  70. c.Write(nil)
  71. return 0, nil
  72. }
  73. func (c *tls12TicketConn) Write(b []byte) (int, error) {
  74. length := len(b)
  75. if c.handshakeStatus == 8 {
  76. buf := pool.GetBuffer()
  77. defer pool.PutBuffer(buf)
  78. for len(b) > 2048 {
  79. size := rand.Intn(4096) + 100
  80. if len(b) < size {
  81. size = len(b)
  82. }
  83. packData(buf, b[:size])
  84. b = b[size:]
  85. }
  86. if len(b) > 0 {
  87. packData(buf, b)
  88. }
  89. _, err := c.Conn.Write(buf.Bytes())
  90. if err != nil {
  91. return 0, err
  92. }
  93. return length, nil
  94. }
  95. if len(b) > 0 {
  96. packData(&c.sendBuf, b)
  97. }
  98. if c.handshakeStatus == 0 {
  99. c.handshakeStatus = 1
  100. data := pool.GetBuffer()
  101. defer pool.PutBuffer(data)
  102. data.Write([]byte{3, 3})
  103. c.packAuthData(data)
  104. data.WriteByte(0x20)
  105. data.Write(c.clientID[:])
  106. data.Write([]byte{0x00, 0x1c, 0xc0, 0x2b, 0xc0, 0x2f, 0xcc, 0xa9, 0xcc, 0xa8, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x0a, 0xc0, 0x14, 0xc0, 0x09, 0xc0, 0x13, 0x00, 0x9c, 0x00, 0x35, 0x00, 0x2f, 0x00, 0x0a})
  107. data.Write([]byte{0x1, 0x0})
  108. ext := pool.GetBuffer()
  109. defer pool.PutBuffer(ext)
  110. host := c.getHost()
  111. ext.Write([]byte{0xff, 0x01, 0x00, 0x01, 0x00})
  112. packSNIData(ext, host)
  113. ext.Write([]byte{0, 0x17, 0, 0})
  114. c.packTicketBuf(ext, host)
  115. ext.Write([]byte{0x00, 0x0d, 0x00, 0x16, 0x00, 0x14, 0x06, 0x01, 0x06, 0x03, 0x05, 0x01, 0x05, 0x03, 0x04, 0x01, 0x04, 0x03, 0x03, 0x01, 0x03, 0x03, 0x02, 0x01, 0x02, 0x03})
  116. ext.Write([]byte{0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00})
  117. ext.Write([]byte{0x00, 0x12, 0x00, 0x00})
  118. ext.Write([]byte{0x75, 0x50, 0x00, 0x00})
  119. ext.Write([]byte{0x00, 0x0b, 0x00, 0x02, 0x01, 0x00})
  120. ext.Write([]byte{0x00, 0x0a, 0x00, 0x06, 0x00, 0x04, 0x00, 0x17, 0x00, 0x18})
  121. binary.Write(data, binary.BigEndian, uint16(ext.Len()))
  122. data.ReadFrom(ext)
  123. ret := pool.GetBuffer()
  124. defer pool.PutBuffer(ret)
  125. ret.Write([]byte{0x16, 3, 1})
  126. binary.Write(ret, binary.BigEndian, uint16(data.Len()+4))
  127. ret.Write([]byte{1, 0})
  128. binary.Write(ret, binary.BigEndian, uint16(data.Len()))
  129. ret.ReadFrom(data)
  130. _, err := c.Conn.Write(ret.Bytes())
  131. if err != nil {
  132. return 0, err
  133. }
  134. return length, nil
  135. } else if c.handshakeStatus == 1 && len(b) == 0 {
  136. buf := pool.GetBuffer()
  137. defer pool.PutBuffer(buf)
  138. buf.Write([]byte{0x14, 3, 3, 0, 1, 1, 0x16, 3, 3, 0, 0x20})
  139. tools.AppendRandBytes(buf, 22)
  140. buf.Write(c.hmacSHA1(buf.Bytes())[:10])
  141. buf.ReadFrom(&c.sendBuf)
  142. c.handshakeStatus = 8
  143. _, err := c.Conn.Write(buf.Bytes())
  144. return 0, err
  145. }
  146. return length, nil
  147. }
  148. func packData(buf *bytes.Buffer, data []byte) {
  149. buf.Write([]byte{0x17, 3, 3})
  150. binary.Write(buf, binary.BigEndian, uint16(len(data)))
  151. buf.Write(data)
  152. }
  153. func (t *tls12Ticket) packAuthData(buf *bytes.Buffer) {
  154. binary.Write(buf, binary.BigEndian, uint32(time.Now().Unix()))
  155. tools.AppendRandBytes(buf, 18)
  156. buf.Write(t.hmacSHA1(buf.Bytes()[buf.Len()-22:])[:10])
  157. }
  158. func packSNIData(buf *bytes.Buffer, u string) {
  159. len := uint16(len(u))
  160. buf.Write([]byte{0, 0})
  161. binary.Write(buf, binary.BigEndian, len+5)
  162. binary.Write(buf, binary.BigEndian, len+3)
  163. buf.WriteByte(0)
  164. binary.Write(buf, binary.BigEndian, len)
  165. buf.WriteString(u)
  166. }
  167. func (c *tls12TicketConn) packTicketBuf(buf *bytes.Buffer, u string) {
  168. length := 16 * (rand.Intn(17) + 8)
  169. buf.Write([]byte{0, 0x23})
  170. binary.Write(buf, binary.BigEndian, uint16(length))
  171. tools.AppendRandBytes(buf, length)
  172. }
  173. func (t *tls12Ticket) hmacSHA1(data []byte) []byte {
  174. key := pool.Get(len(t.Key) + 32)
  175. defer pool.Put(key)
  176. copy(key, t.Key)
  177. copy(key[len(t.Key):], t.clientID[:])
  178. sha1Data := tools.HmacSHA1(key, data)
  179. return sha1Data[:10]
  180. }
  181. func (t *tls12Ticket) getHost() string {
  182. host := t.Param
  183. if len(host) == 0 {
  184. host = t.Host
  185. }
  186. if len(host) > 0 && host[len(host)-1] >= '0' && host[len(host)-1] <= '9' {
  187. host = ""
  188. }
  189. hosts := strings.Split(host, ",")
  190. host = hosts[rand.Intn(len(hosts))]
  191. return host
  192. }