tls.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package tls
  2. import (
  3. "context"
  4. "crypto/rand"
  5. "crypto/tls"
  6. "math/big"
  7. utls "github.com/refraction-networking/utls"
  8. "github.com/xtls/xray-core/common/buf"
  9. "github.com/xtls/xray-core/common/net"
  10. "github.com/xtls/xray-core/common/session"
  11. )
  12. //go:generate go run github.com/xtls/xray-core/common/errors/errorgen
  13. var _ buf.Writer = (*Conn)(nil)
  14. var XrayRandom *utls.ClientHelloID
  15. type Conn struct {
  16. *tls.Conn
  17. }
  18. func (c *Conn) WriteMultiBuffer(mb buf.MultiBuffer) error {
  19. mb = buf.Compact(mb)
  20. mb, err := buf.WriteMultiBuffer(c, mb)
  21. buf.ReleaseMulti(mb)
  22. return err
  23. }
  24. func (c *Conn) HandshakeAddress() net.Address {
  25. if err := c.Handshake(); err != nil {
  26. return nil
  27. }
  28. state := c.ConnectionState()
  29. if state.ServerName == "" {
  30. return nil
  31. }
  32. return net.ParseAddress(state.ServerName)
  33. }
  34. func (c *Conn) NegotiatedProtocol() (name string, mutual bool) {
  35. state := c.ConnectionState()
  36. return state.NegotiatedProtocol, state.NegotiatedProtocolIsMutual
  37. }
  38. // Client initiates a TLS client handshake on the given connection.
  39. func Client(c net.Conn, config *tls.Config) net.Conn {
  40. tlsConn := tls.Client(c, config)
  41. return &Conn{Conn: tlsConn}
  42. }
  43. // Server initiates a TLS server handshake on the given connection.
  44. func Server(c net.Conn, config *tls.Config) net.Conn {
  45. tlsConn := tls.Server(c, config)
  46. return &Conn{Conn: tlsConn}
  47. }
  48. type UConn struct {
  49. *utls.UConn
  50. }
  51. func (c *UConn) HandshakeAddress() net.Address {
  52. if err := c.Handshake(); err != nil {
  53. return nil
  54. }
  55. state := c.ConnectionState()
  56. if state.ServerName == "" {
  57. return nil
  58. }
  59. return net.ParseAddress(state.ServerName)
  60. }
  61. // WebsocketHandshake basically calls UConn.Handshake inside it but it will only send
  62. // http/1.1 in its ALPN.
  63. func (c *UConn) WebsocketHandshake() error {
  64. // Build the handshake state. This will apply every variable of the TLS of the
  65. // fingerprint in the UConn
  66. if err := c.BuildHandshakeState(); err != nil {
  67. return err
  68. }
  69. // Iterate over extensions and check for utls.ALPNExtension
  70. hasALPNExtension := false
  71. for _, extension := range c.Extensions {
  72. if alpn, ok := extension.(*utls.ALPNExtension); ok {
  73. hasALPNExtension = true
  74. alpn.AlpnProtocols = []string{"http/1.1"}
  75. break
  76. }
  77. }
  78. if !hasALPNExtension { // Append extension if doesn't exists
  79. c.Extensions = append(c.Extensions, &utls.ALPNExtension{AlpnProtocols: []string{"http/1.1"}})
  80. }
  81. // Rebuild the client hello and do the handshake
  82. if err := c.BuildHandshakeState(); err != nil {
  83. return err
  84. }
  85. return c.Handshake()
  86. }
  87. func (c *UConn) NegotiatedProtocol() (name string, mutual bool) {
  88. state := c.ConnectionState()
  89. return state.NegotiatedProtocol, state.NegotiatedProtocolIsMutual
  90. }
  91. func UClient(c net.Conn, config *tls.Config, fingerprint *utls.ClientHelloID) net.Conn {
  92. utlsConn := utls.UClient(c, copyConfig(config), *fingerprint)
  93. return &UConn{UConn: utlsConn}
  94. }
  95. func copyConfig(c *tls.Config) *utls.Config {
  96. return &utls.Config{
  97. RootCAs: c.RootCAs,
  98. ServerName: c.ServerName,
  99. InsecureSkipVerify: c.InsecureSkipVerify,
  100. VerifyPeerCertificate: c.VerifyPeerCertificate,
  101. }
  102. }
  103. func GetFingerprint(ctx context.Context, config string) (*utls.ClientHelloID, bool) {
  104. if XrayRandom == nil {
  105. // lazy init
  106. for k, v := range FingerprintsForRNG {
  107. Fingerprints[k] = v
  108. }
  109. big, err := rand.Int(rand.Reader, big.NewInt(int64(len(FingerprintsForRNG))))
  110. if err != nil {
  111. newError("failed to generate xray random fingerprint").Base(err).WriteToLog(session.ExportIDToError(ctx))
  112. }
  113. var i = int(big.Int64())
  114. count := 0
  115. for k, v := range FingerprintsForRNG {
  116. if count == i {
  117. newError("xray random fingerprint: ", k).WriteToLog(session.ExportIDToError(ctx))
  118. XrayRandom = v
  119. break
  120. }
  121. count++
  122. }
  123. }
  124. if config == "random" {
  125. return XrayRandom, true
  126. }
  127. fingerprint, ok := Fingerprints[config]
  128. return fingerprint, ok
  129. }
  130. var Fingerprints = map[string]*utls.ClientHelloID{
  131. "chrome": &utls.HelloChrome_Auto,
  132. "firefox": &utls.HelloFirefox_Auto,
  133. "safari": &utls.HelloSafari_Auto,
  134. "randomized": &utls.HelloRandomized,
  135. // This is a bit lame, but it seems there is no good way to reflect variables from Golang package
  136. // We don't RNG for go, randomized, or fingerprints that is more than 4 years old
  137. "hellogolang": &utls.HelloGolang,
  138. "hellorandomized": &utls.HelloRandomized,
  139. "hellorandomizedalpn": &utls.HelloRandomizedALPN,
  140. "hellorandomizednoalpn": &utls.HelloRandomizedNoALPN,
  141. "hellofirefox_55": &utls.HelloFirefox_55,
  142. "hellofirefox_56": &utls.HelloFirefox_56,
  143. "hellofirefox_63": &utls.HelloFirefox_63,
  144. "hellofirefox_65": &utls.HelloFirefox_65,
  145. "hellochrome_58": &utls.HelloChrome_58,
  146. "hellochrome_62": &utls.HelloChrome_62,
  147. "hellochrome_70": &utls.HelloChrome_70,
  148. "hellochrome_72": &utls.HelloChrome_72,
  149. "helloios_11_1": &utls.HelloIOS_11_1,
  150. "hello360_7_5": &utls.Hello360_7_5,
  151. }
  152. var FingerprintsForRNG = map[string]*utls.ClientHelloID{
  153. "hellofirefox_auto": &utls.HelloFirefox_Auto,
  154. "hellofirefox_99": &utls.HelloFirefox_99,
  155. "hellofirefox_102": &utls.HelloFirefox_102,
  156. "hellofirefox_105": &utls.HelloFirefox_105,
  157. "hellochrome_auto": &utls.HelloChrome_Auto,
  158. "hellochrome_83": &utls.HelloChrome_83,
  159. "hellochrome_87": &utls.HelloChrome_87,
  160. "hellochrome_96": &utls.HelloChrome_96,
  161. "hellochrome_100": &utls.HelloChrome_100,
  162. "hellochrome_102": &utls.HelloChrome_102,
  163. "hellochrome_106_shuffle": &utls.HelloChrome_106_Shuffle,
  164. "helloios_auto": &utls.HelloIOS_Auto,
  165. "helloios_12_1": &utls.HelloIOS_12_1,
  166. "helloios_13": &utls.HelloIOS_13,
  167. "helloios_14": &utls.HelloIOS_14,
  168. "helloandroid_11_okhttp": &utls.HelloAndroid_11_OkHttp,
  169. "helloedge_auto": &utls.HelloEdge_Auto,
  170. "helloedge_85": &utls.HelloEdge_85,
  171. "helloedge_106": &utls.HelloEdge_106,
  172. "hellosafari_auto": &utls.HelloSafari_Auto,
  173. "hellosafari_16_0": &utls.HelloSafari_16_0,
  174. "hello360_auto": &utls.Hello360_Auto,
  175. "hello360_11_0": &utls.Hello360_11_0,
  176. "helloqq_auto": &utls.HelloQQ_Auto,
  177. "helloqq_11_1": &utls.HelloQQ_11_1,
  178. }
  179. type Interface interface {
  180. net.Conn
  181. Handshake() error
  182. VerifyHostname(host string) error
  183. NegotiatedProtocol() (name string, mutual bool)
  184. }