-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxhttp_padding.go
More file actions
271 lines (242 loc) · 6.89 KB
/
Copy pathxhttp_padding.go
File metadata and controls
271 lines (242 loc) · 6.89 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package singproxy
import (
"crypto/rand"
"encoding/base64"
"fmt"
"math"
"net/http"
"net/url"
"strings"
"golang.org/x/net/http2/hpack"
)
func xhttpBase64URLEncode(data []byte) string {
return base64.RawURLEncoding.EncodeToString(data)
}
type xhttpPaddingMethod string
const (
xhttpPaddingMethodRepeatX xhttpPaddingMethod = "repeat-x"
xhttpPaddingMethodTokenish xhttpPaddingMethod = "tokenish"
)
const xhttpCharsetBase62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
const xhttpAvgHuffmanBytesPerCharBase62 = 0.8
const xhttpValidationTolerance = 2
type xhttpPaddingPlacement struct {
Placement string
Key string
Header string
RawURL string
}
type xhttpPaddingConfig struct {
Length int
Placement xhttpPaddingPlacement
Method xhttpPaddingMethod
}
func xhttpRandStringFromCharset(n int, charset string) (string, bool) {
if n <= 0 || len(charset) == 0 {
return "", false
}
m := len(charset)
limit := byte(256 - (256 % m))
result := make([]byte, n)
i := 0
buf := make([]byte, 256)
for i < n {
if _, err := rand.Read(buf); err != nil {
return "", false
}
for _, rb := range buf {
if rb >= limit {
continue
}
result[i] = charset[int(rb)%m]
i++
if i == n {
break
}
}
}
return string(result), true
}
func xhttpAbsInt(x int) int {
if x < 0 {
return -x
}
return x
}
func xhttpGenerateTokenishPaddingBase62(targetHuffmanBytes int) string {
n := int(math.Ceil(float64(targetHuffmanBytes) / xhttpAvgHuffmanBytesPerCharBase62))
if n < 1 {
n = 1
}
randBase62Str, ok := xhttpRandStringFromCharset(n, xhttpCharsetBase62)
if !ok {
return ""
}
const maxIter = 150
adjustChar := byte('X')
for iter := 0; iter < maxIter; iter++ {
currentLength := int(hpack.HuffmanEncodeLength(randBase62Str))
diff := currentLength - targetHuffmanBytes
if xhttpAbsInt(diff) <= xhttpValidationTolerance {
return randBase62Str
}
if diff < 0 {
randBase62Str += string(adjustChar)
if adjustChar == 'X' {
adjustChar = 'Z'
} else {
adjustChar = 'X'
}
} else {
if len(randBase62Str) <= 1 {
return randBase62Str
}
randBase62Str = randBase62Str[:len(randBase62Str)-1]
}
}
return randBase62Str
}
func xhttpGeneratePadding(method xhttpPaddingMethod, length int) string {
if length <= 0 {
return ""
}
switch method {
case xhttpPaddingMethodRepeatX:
return strings.Repeat("X", length)
case xhttpPaddingMethodTokenish:
paddingValue := xhttpGenerateTokenishPaddingBase62(length)
if paddingValue == "" {
return strings.Repeat("X", length)
}
return paddingValue
default:
return strings.Repeat("X", length)
}
}
func (c *xhttpConfig) buildXPaddingConfig(reqURL string, length int) xhttpPaddingConfig {
config := xhttpPaddingConfig{Length: length}
if c.XPaddingObfsMode {
config.Placement = xhttpPaddingPlacement{
Placement: c.XPaddingPlacement,
Key: c.XPaddingKey,
Header: c.XPaddingHeader,
RawURL: reqURL,
}
config.Method = xhttpPaddingMethod(c.XPaddingMethod)
} else {
config.Placement = xhttpPaddingPlacement{
Placement: xhttpPlacementQueryInHeader,
Key: "x_padding",
Header: "Referer",
RawURL: reqURL,
}
}
return config
}
func (c *xhttpConfig) applyXPaddingToHeader(h http.Header, config xhttpPaddingConfig) {
if h == nil {
return
}
paddingValue := xhttpGeneratePadding(config.Method, config.Length)
switch p := config.Placement; p.Placement {
case xhttpPlacementHeader:
h.Set(p.Header, paddingValue)
case xhttpPlacementQueryInHeader:
u, err := url.Parse(p.RawURL)
if err != nil || u == nil {
return
}
u.RawQuery = p.Key + "=" + paddingValue
h.Set(p.Header, u.String())
}
}
func (c *xhttpConfig) applyXPaddingToRequest(reqHeaders http.Header, reqURL *url.URL, config xhttpPaddingConfig) {
placement := config.Placement.Placement
if placement == xhttpPlacementHeader || placement == xhttpPlacementQueryInHeader {
c.applyXPaddingToHeader(reqHeaders, config)
return
}
paddingValue := xhttpGeneratePadding(config.Method, config.Length)
if placement == xhttpPlacementQuery {
q := reqURL.Query()
q.Set(config.Placement.Key, paddingValue)
reqURL.RawQuery = q.Encode()
}
}
func (c *xhttpConfig) fillStreamRequestHeaders(reqHeaders http.Header, reqURL *url.URL, sessionID string, hasBody bool) error {
c.applyMasqueradedHeaders(reqHeaders, "fetch")
xPaddingBytes, err := c.GetNormalizedXPaddingBytes()
if err != nil {
return err
}
c.applyXPaddingToRequest(reqHeaders, reqURL, c.buildXPaddingConfig(reqURL.String(), xPaddingBytes.Rand()))
c.applyMetaToRequest(reqHeaders, reqURL, sessionID, "")
if hasBody && !c.NoGRPCHeader {
reqHeaders.Set("Content-Type", "application/grpc")
}
return nil
}
func (c *xhttpConfig) fillDownloadRequestHeaders(reqHeaders http.Header, reqURL *url.URL, sessionID string) error {
return c.fillStreamRequestHeaders(reqHeaders, reqURL, sessionID, false)
}
func (c *xhttpConfig) fillPacketRequestHeaders(reqHeaders http.Header, reqURL *url.URL, sessionId string, seqStr string, data []byte) error {
dataPlacement := c.GetNormalizedUplinkDataPlacement()
if dataPlacement == xhttpPlacementBody || dataPlacement == xhttpPlacementAuto {
c.applyMasqueradedHeaders(reqHeaders, "fetch")
} else {
c.applyMasqueradedHeaders(reqHeaders, "fetch")
if dataPlacement == xhttpPlacementHeader {
uplinkChunkSize, err := c.GetNormalizedUplinkChunkSize()
if err != nil {
return err
}
key := c.UplinkDataKey
encodedData := xhttpBase64URLEncode(data)
for i := 0; len(encodedData) > 0; i++ {
chunkSize := uplinkChunkSize.Rand()
if len(encodedData) < chunkSize {
chunkSize = len(encodedData)
}
reqHeaders.Set(fmt.Sprintf("%s-%d", key, i), encodedData[:chunkSize])
encodedData = encodedData[chunkSize:]
}
}
}
xPaddingBytes, err := c.GetNormalizedXPaddingBytes()
if err != nil {
return err
}
c.applyXPaddingToRequest(reqHeaders, reqURL, c.buildXPaddingConfig(reqURL.String(), xPaddingBytes.Rand()))
c.applyMetaToRequest(reqHeaders, reqURL, sessionId, seqStr)
return nil
}
func (c *xhttpConfig) applyMetaToRequest(reqHeaders http.Header, reqURL *url.URL, sessionId string, seqStr string) {
sessionPlacement := c.GetNormalizedSessionPlacement()
seqPlacement := c.GetNormalizedSeqPlacement()
sessionKey := c.GetNormalizedSessionKey()
seqKey := c.GetNormalizedSeqKey()
if sessionId != "" {
switch sessionPlacement {
case xhttpPlacementPath:
reqURL.Path = xhttpAppendToPath(reqURL.Path, sessionId)
case xhttpPlacementQuery:
q := reqURL.Query()
q.Set(sessionKey, sessionId)
reqURL.RawQuery = q.Encode()
case xhttpPlacementHeader:
reqHeaders.Set(sessionKey, sessionId)
}
}
if seqStr != "" {
switch seqPlacement {
case xhttpPlacementPath:
reqURL.Path = xhttpAppendToPath(reqURL.Path, seqStr)
case xhttpPlacementQuery:
q := reqURL.Query()
q.Set(seqKey, seqStr)
reqURL.RawQuery = q.Encode()
case xhttpPlacementHeader:
reqHeaders.Set(seqKey, seqStr)
}
}
}