-
Notifications
You must be signed in to change notification settings - Fork 14
/
chromium.go
198 lines (181 loc) · 5.74 KB
/
chromium.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package mimic
import (
"crypto/rand"
"errors"
"math/big"
"strconv"
"strings"
utls "github.com/refraction-networking/utls"
"github.com/saucesteals/fhttp/http2"
)
var (
ErrUnsupportedVersion = errors.New("mimic: unsupported version")
)
func getMajor(version string) (string, int, error) {
major := strings.SplitN(version, ".", 2)[0]
iMajor, err := strconv.Atoi(major)
return major, iMajor, err
}
func chromiumExtensions(randomize bool) []utls.TLSExtension {
extensions := []utls.TLSExtension{
&utls.UtlsGREASEExtension{},
&utls.SNIExtension{},
&utls.UtlsExtendedMasterSecretExtension{},
&utls.RenegotiationInfoExtension{Renegotiation: utls.RenegotiateOnceAsClient},
&utls.SupportedCurvesExtension{
Curves: []utls.CurveID{
utls.CurveID(utls.GREASE_PLACEHOLDER),
utls.X25519,
utls.CurveP256,
utls.CurveP384,
}},
&utls.SupportedPointsExtension{SupportedPoints: []byte{
0x00, // pointFormatUncompressed
}},
&utls.SessionTicketExtension{},
&utls.ALPNExtension{AlpnProtocols: []string{"h2", "http/1.1"}},
&utls.StatusRequestExtension{},
&utls.SignatureAlgorithmsExtension{SupportedSignatureAlgorithms: []utls.SignatureScheme{
utls.ECDSAWithP256AndSHA256,
utls.PSSWithSHA256,
utls.PKCS1WithSHA256,
utls.ECDSAWithP384AndSHA384,
utls.PSSWithSHA384,
utls.PKCS1WithSHA384,
utls.PSSWithSHA512,
utls.PKCS1WithSHA512,
}},
&utls.SCTExtension{},
&utls.KeyShareExtension{
KeyShares: []utls.KeyShare{
{Group: utls.CurveID(utls.GREASE_PLACEHOLDER), Data: []byte{0}},
{Group: utls.X25519},
}},
&utls.PSKKeyExchangeModesExtension{
Modes: []uint8{
utls.PskModeDHE,
}},
&utls.SupportedVersionsExtension{
Versions: []uint16{
utls.GREASE_PLACEHOLDER,
utls.VersionTLS13,
utls.VersionTLS12,
}},
&utls.UtlsCompressCertExtension{
Algorithms: []utls.CertCompressionAlgo{
utls.CertCompressionBrotli,
},
},
&utls.ApplicationSettingsExtension{SupportedProtocols: []string{"h2"}},
&utls.UtlsGREASEExtension{},
&utls.UtlsPaddingExtension{GetPaddingLen: utls.BoringPaddingStyle},
}
if !randomize {
return extensions
}
// randomize except for first (grease) and last two (grease and padding)
max := big.NewInt(int64(len(extensions) - 3))
for i := 1; i < len(extensions)-2; i++ {
j, err := rand.Int(rand.Reader, max)
if err != nil {
panic(err)
}
j.Add(j, big.NewInt(1))
extensions[i], extensions[j.Int64()] = extensions[j.Int64()], extensions[i]
}
return extensions
}
func chromiumClientHelloSpec(randomExtensions bool) func() *utls.ClientHelloSpec {
return func() *utls.ClientHelloSpec {
return &utls.ClientHelloSpec{
CipherSuites: []uint16{
utls.GREASE_PLACEHOLDER,
utls.TLS_AES_128_GCM_SHA256,
utls.TLS_AES_256_GCM_SHA384,
utls.TLS_CHACHA20_POLY1305_SHA256,
utls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
utls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
utls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
utls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
utls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
utls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
utls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
utls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
utls.TLS_RSA_WITH_AES_128_GCM_SHA256,
utls.TLS_RSA_WITH_AES_256_GCM_SHA384,
utls.TLS_RSA_WITH_AES_128_CBC_SHA,
utls.TLS_RSA_WITH_AES_256_CBC_SHA,
},
CompressionMethods: []byte{
0x00, // compressionNone
},
Extensions: chromiumExtensions(randomExtensions),
}
}
}
func Chromium(brand Brand, version string) (*ClientSpec, error) {
majorVersion, majorVersionNumber, err := getMajor(version)
if err != nil {
return nil, err
}
// legacy
switch {
case majorVersionNumber < 100:
return nil, ErrUnsupportedVersion
case majorVersionNumber < 107:
return &ClientSpec{
version,
clientHintUA(brand, majorVersion, majorVersionNumber),
&HTTP2Options{
PseudoHeaderOrder: []string{":method", ":authority", ":scheme", ":path"},
MaxHeaderListSize: 262144,
Settings: []http2.Setting{
{ID: http2.SettingHeaderTableSize, Val: 65536},
{ID: http2.SettingMaxConcurrentStreams, Val: 1000},
{ID: http2.SettingInitialWindowSize, Val: 6291456},
{ID: http2.SettingMaxHeaderListSize, Val: 100000},
},
InitialWindowSize: 6291456,
HeaderTableSize: 65536,
},
chromiumClientHelloSpec(false),
}, nil
case majorVersionNumber < 110:
return &ClientSpec{
version,
clientHintUA(brand, majorVersion, majorVersionNumber),
&HTTP2Options{
PseudoHeaderOrder: []string{":method", ":authority", ":scheme", ":path"},
MaxHeaderListSize: 262144,
Settings: []http2.Setting{
{ID: http2.SettingHeaderTableSize, Val: 65536},
{ID: http2.SettingEnablePush, Val: 0},
{ID: http2.SettingMaxConcurrentStreams, Val: 1000},
{ID: http2.SettingInitialWindowSize, Val: 6291456},
{ID: http2.SettingMaxHeaderListSize, Val: 262144},
},
InitialWindowSize: 6291456,
HeaderTableSize: 65536,
},
chromiumClientHelloSpec(false),
}, nil
}
return &ClientSpec{
version,
clientHintUA(brand, majorVersion, majorVersionNumber),
&HTTP2Options{
PseudoHeaderOrder: []string{":method", ":authority", ":scheme", ":path"},
MaxHeaderListSize: 262144,
Settings: []http2.Setting{
{ID: http2.SettingHeaderTableSize, Val: 65536},
{ID: http2.SettingEnablePush, Val: 0},
{ID: http2.SettingMaxConcurrentStreams, Val: 1000},
{ID: http2.SettingInitialWindowSize, Val: 6291456},
{ID: http2.SettingMaxHeaderListSize, Val: 262144},
},
InitialWindowSize: 6291456,
HeaderTableSize: 65536,
},
chromiumClientHelloSpec(true),
}, nil
}