tls.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. KeyLogWriter: c.KeyLogWriter,
  105. }
  106. }
  107. func init() {
  108. bigInt, _ := rand.Int(rand.Reader, big.NewInt(int64(len(ModernFingerprints))))
  109. stopAt := int(bigInt.Int64())
  110. i := 0
  111. for _, v := range ModernFingerprints {
  112. if i == stopAt {
  113. PresetFingerprints["random"] = v
  114. break
  115. }
  116. i++
  117. }
  118. weights := utls.DefaultWeights
  119. weights.TLSVersMax_Set_VersionTLS13 = 1
  120. weights.FirstKeyShare_Set_CurveP256 = 0
  121. randomized := utls.HelloRandomized
  122. randomized.Seed, _ = utls.NewPRNGSeed()
  123. randomized.Weights = &weights
  124. PresetFingerprints["randomized"] = &randomized
  125. }
  126. func GetFingerprint(name string) (fingerprint *utls.ClientHelloID) {
  127. if name == "" {
  128. return
  129. }
  130. if fingerprint = PresetFingerprints[name]; fingerprint != nil {
  131. return
  132. }
  133. if fingerprint = ModernFingerprints[name]; fingerprint != nil {
  134. return
  135. }
  136. if fingerprint = OtherFingerprints[name]; fingerprint != nil {
  137. return
  138. }
  139. return
  140. }
  141. var PresetFingerprints = map[string]*utls.ClientHelloID{
  142. // Recommended preset options in GUI clients
  143. "chrome": &utls.HelloChrome_Auto,
  144. "firefox": &utls.HelloFirefox_Auto,
  145. "safari": &utls.HelloSafari_Auto,
  146. "ios": &utls.HelloIOS_Auto,
  147. "android": &utls.HelloAndroid_11_OkHttp,
  148. "edge": &utls.HelloEdge_Auto,
  149. "360": &utls.Hello360_Auto,
  150. "qq": &utls.HelloQQ_Auto,
  151. "random": nil,
  152. "randomized": nil,
  153. }
  154. var ModernFingerprints = map[string]*utls.ClientHelloID{
  155. // One of these will be chosen as `random` at startup
  156. "hellofirefox_99": &utls.HelloFirefox_99,
  157. "hellofirefox_102": &utls.HelloFirefox_102,
  158. "hellofirefox_105": &utls.HelloFirefox_105,
  159. "hellochrome_83": &utls.HelloChrome_83,
  160. "hellochrome_87": &utls.HelloChrome_87,
  161. "hellochrome_96": &utls.HelloChrome_96,
  162. "hellochrome_100": &utls.HelloChrome_100,
  163. "hellochrome_102": &utls.HelloChrome_102,
  164. "hellochrome_106_shuffle": &utls.HelloChrome_106_Shuffle,
  165. "helloios_13": &utls.HelloIOS_13,
  166. "helloios_14": &utls.HelloIOS_14,
  167. "helloedge_85": &utls.HelloEdge_85,
  168. "helloedge_106": &utls.HelloEdge_106,
  169. "hellosafari_16_0": &utls.HelloSafari_16_0,
  170. "hello360_11_0": &utls.Hello360_11_0,
  171. "helloqq_11_1": &utls.HelloQQ_11_1,
  172. }
  173. var OtherFingerprints = map[string]*utls.ClientHelloID{
  174. // Golang, randomized, auto, and fingerprints that are too old
  175. "hellogolang": &utls.HelloGolang,
  176. "hellorandomized": &utls.HelloRandomized,
  177. "hellorandomizedalpn": &utls.HelloRandomizedALPN,
  178. "hellorandomizednoalpn": &utls.HelloRandomizedNoALPN,
  179. "hellofirefox_auto": &utls.HelloFirefox_Auto,
  180. "hellofirefox_55": &utls.HelloFirefox_55,
  181. "hellofirefox_56": &utls.HelloFirefox_56,
  182. "hellofirefox_63": &utls.HelloFirefox_63,
  183. "hellofirefox_65": &utls.HelloFirefox_65,
  184. "hellochrome_auto": &utls.HelloChrome_Auto,
  185. "hellochrome_58": &utls.HelloChrome_58,
  186. "hellochrome_62": &utls.HelloChrome_62,
  187. "hellochrome_70": &utls.HelloChrome_70,
  188. "hellochrome_72": &utls.HelloChrome_72,
  189. "helloios_auto": &utls.HelloIOS_Auto,
  190. "helloios_11_1": &utls.HelloIOS_11_1,
  191. "helloios_12_1": &utls.HelloIOS_12_1,
  192. "helloandroid_11_okhttp": &utls.HelloAndroid_11_OkHttp,
  193. "helloedge_auto": &utls.HelloEdge_Auto,
  194. "hellosafari_auto": &utls.HelloSafari_Auto,
  195. "hello360_auto": &utls.Hello360_Auto,
  196. "hello360_7_5": &utls.Hello360_7_5,
  197. "helloqq_auto": &utls.HelloQQ_Auto,
  198. }