tls.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package tls
  2. import (
  3. "crypto/rand"
  4. "crypto/tls"
  5. "math/big"
  6. utls "github.com/refraction-networking/utls"
  7. "github.com/xtls/xray-core/common/buf"
  8. "github.com/xtls/xray-core/common/net"
  9. )
  10. //go:generate go run github.com/xtls/xray-core/common/errors/errorgen
  11. type Interface interface {
  12. net.Conn
  13. Handshake() error
  14. VerifyHostname(host string) error
  15. NegotiatedProtocol() (name string, mutual bool)
  16. }
  17. var _ buf.Writer = (*Conn)(nil)
  18. type Conn struct {
  19. *tls.Conn
  20. }
  21. func (c *Conn) WriteMultiBuffer(mb buf.MultiBuffer) error {
  22. mb = buf.Compact(mb)
  23. mb, err := buf.WriteMultiBuffer(c, mb)
  24. buf.ReleaseMulti(mb)
  25. return err
  26. }
  27. func (c *Conn) HandshakeAddress() net.Address {
  28. if err := c.Handshake(); err != nil {
  29. return nil
  30. }
  31. state := c.ConnectionState()
  32. if state.ServerName == "" {
  33. return nil
  34. }
  35. return net.ParseAddress(state.ServerName)
  36. }
  37. func (c *Conn) NegotiatedProtocol() (name string, mutual bool) {
  38. state := c.ConnectionState()
  39. return state.NegotiatedProtocol, state.NegotiatedProtocolIsMutual
  40. }
  41. // Client initiates a TLS client handshake on the given connection.
  42. func Client(c net.Conn, config *tls.Config) net.Conn {
  43. tlsConn := tls.Client(c, config)
  44. return &Conn{Conn: tlsConn}
  45. }
  46. // Server initiates a TLS server handshake on the given connection.
  47. func Server(c net.Conn, config *tls.Config) net.Conn {
  48. tlsConn := tls.Server(c, config)
  49. return &Conn{Conn: tlsConn}
  50. }
  51. type UConn struct {
  52. *utls.UConn
  53. }
  54. func (c *UConn) HandshakeAddress() net.Address {
  55. if err := c.Handshake(); err != nil {
  56. return nil
  57. }
  58. state := c.ConnectionState()
  59. if state.ServerName == "" {
  60. return nil
  61. }
  62. return net.ParseAddress(state.ServerName)
  63. }
  64. // WebsocketHandshake basically calls UConn.Handshake inside it but it will only send
  65. // http/1.1 in its ALPN.
  66. func (c *UConn) WebsocketHandshake() error {
  67. // Build the handshake state. This will apply every variable of the TLS of the
  68. // fingerprint in the UConn
  69. if err := c.BuildHandshakeState(); err != nil {
  70. return err
  71. }
  72. // Iterate over extensions and check for utls.ALPNExtension
  73. hasALPNExtension := false
  74. for _, extension := range c.Extensions {
  75. if alpn, ok := extension.(*utls.ALPNExtension); ok {
  76. hasALPNExtension = true
  77. alpn.AlpnProtocols = []string{"http/1.1"}
  78. break
  79. }
  80. }
  81. if !hasALPNExtension { // Append extension if doesn't exists
  82. c.Extensions = append(c.Extensions, &utls.ALPNExtension{AlpnProtocols: []string{"http/1.1"}})
  83. }
  84. // Rebuild the client hello and do the handshake
  85. if err := c.BuildHandshakeState(); err != nil {
  86. return err
  87. }
  88. return c.Handshake()
  89. }
  90. func (c *UConn) NegotiatedProtocol() (name string, mutual bool) {
  91. state := c.ConnectionState()
  92. return state.NegotiatedProtocol, state.NegotiatedProtocolIsMutual
  93. }
  94. func UClient(c net.Conn, config *tls.Config, fingerprint *utls.ClientHelloID) net.Conn {
  95. utlsConn := utls.UClient(c, copyConfig(config), *fingerprint)
  96. return &UConn{UConn: utlsConn}
  97. }
  98. func copyConfig(c *tls.Config) *utls.Config {
  99. return &utls.Config{
  100. RootCAs: c.RootCAs,
  101. ServerName: c.ServerName,
  102. InsecureSkipVerify: c.InsecureSkipVerify,
  103. VerifyPeerCertificate: c.VerifyPeerCertificate,
  104. }
  105. }
  106. func init() {
  107. bigInt, _ := rand.Int(rand.Reader, big.NewInt(int64(len(ModernFingerprints))))
  108. stopAt := int(bigInt.Int64())
  109. i := 0
  110. for _, v := range ModernFingerprints {
  111. if i == stopAt {
  112. PresetFingerprints["random"] = v
  113. break
  114. }
  115. i++
  116. }
  117. weights := utls.DefaultWeights
  118. weights.TLSVersMax_Set_VersionTLS13 = 1
  119. weights.FirstKeyShare_Set_CurveP256 = 0
  120. randomized := utls.HelloRandomized
  121. randomized.Seed, _ = utls.NewPRNGSeed()
  122. randomized.Weights = &weights
  123. PresetFingerprints["randomized"] = &randomized
  124. }
  125. func GetFingerprint(name string) (fingerprint *utls.ClientHelloID) {
  126. if name == "" {
  127. return
  128. }
  129. if fingerprint = PresetFingerprints[name]; fingerprint != nil {
  130. return
  131. }
  132. if fingerprint = ModernFingerprints[name]; fingerprint != nil {
  133. return
  134. }
  135. if fingerprint = OtherFingerprints[name]; fingerprint != nil {
  136. return
  137. }
  138. return
  139. }
  140. var PresetFingerprints = map[string]*utls.ClientHelloID{
  141. // Recommended preset options in GUI clients
  142. "chrome": &utls.HelloChrome_Auto,
  143. "firefox": &utls.HelloFirefox_Auto,
  144. "safari": &utls.HelloSafari_Auto,
  145. "ios": &utls.HelloIOS_Auto,
  146. "android": &utls.HelloAndroid_11_OkHttp,
  147. "edge": &utls.HelloEdge_Auto,
  148. "360": &utls.Hello360_Auto,
  149. "qq": &utls.HelloQQ_Auto,
  150. "random": nil,
  151. "randomized": nil,
  152. }
  153. var ModernFingerprints = map[string]*utls.ClientHelloID{
  154. // One of these will be chosen as `random` at startup
  155. "hellofirefox_99": &utls.HelloFirefox_99,
  156. "hellofirefox_102": &utls.HelloFirefox_102,
  157. "hellofirefox_105": &utls.HelloFirefox_105,
  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_13": &utls.HelloIOS_13,
  165. "helloios_14": &utls.HelloIOS_14,
  166. "helloedge_85": &utls.HelloEdge_85,
  167. "helloedge_106": &utls.HelloEdge_106,
  168. "hellosafari_16_0": &utls.HelloSafari_16_0,
  169. "hello360_11_0": &utls.Hello360_11_0,
  170. "helloqq_11_1": &utls.HelloQQ_11_1,
  171. }
  172. var OtherFingerprints = map[string]*utls.ClientHelloID{
  173. // Golang, randomized, auto, and fingerprints that are too old
  174. "hellogolang": &utls.HelloGolang,
  175. "hellorandomized": &utls.HelloRandomized,
  176. "hellorandomizedalpn": &utls.HelloRandomizedALPN,
  177. "hellorandomizednoalpn": &utls.HelloRandomizedNoALPN,
  178. "hellofirefox_auto": &utls.HelloFirefox_Auto,
  179. "hellofirefox_55": &utls.HelloFirefox_55,
  180. "hellofirefox_56": &utls.HelloFirefox_56,
  181. "hellofirefox_63": &utls.HelloFirefox_63,
  182. "hellofirefox_65": &utls.HelloFirefox_65,
  183. "hellochrome_auto": &utls.HelloChrome_Auto,
  184. "hellochrome_58": &utls.HelloChrome_58,
  185. "hellochrome_62": &utls.HelloChrome_62,
  186. "hellochrome_70": &utls.HelloChrome_70,
  187. "hellochrome_72": &utls.HelloChrome_72,
  188. "helloios_auto": &utls.HelloIOS_Auto,
  189. "helloios_11_1": &utls.HelloIOS_11_1,
  190. "helloios_12_1": &utls.HelloIOS_12_1,
  191. "helloandroid_11_okhttp": &utls.HelloAndroid_11_OkHttp,
  192. "helloedge_auto": &utls.HelloEdge_Auto,
  193. "hellosafari_auto": &utls.HelloSafari_Auto,
  194. "hello360_auto": &utls.Hello360_Auto,
  195. "hello360_7_5": &utls.Hello360_7_5,
  196. "helloqq_auto": &utls.HelloQQ_Auto,
  197. }