Skip to content

Commit 0604983

Browse files
committed
refactor(util/useragent): 限定UA模板仅为Edge Windows一族
移除其他浏览器的UA模板,仅保留Edge Windows版本,匹配硬编码的sec-ch-ua头以通过Cloudflare校验,同时保留版本随机化保证指纹多样性
1 parent 0264bdd commit 0604983

2 files changed

Lines changed: 11 additions & 34 deletions

File tree

internal/chatgpt/request_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,11 @@ func TestCreateBaseHeaderMatchesWebClientShape(t *testing.T) {
409409
if first["oai-language"] != "zh-CN" {
410410
t.Fatalf("oai-language = %q, want zh-CN", first["oai-language"])
411411
}
412-
if !strings.Contains(first["user-agent"], "Edg/147.0.0.0") {
413-
t.Fatalf("user-agent = %q, want Edge 147 shape", first["user-agent"])
412+
// UA must be the Edge variant to stay consistent with the hardcoded
413+
// sec-ch-ua = "Microsoft Edge";v="146". Version is randomized.
414+
ua := first["user-agent"]
415+
if !strings.Contains(ua, "Edg/") {
416+
t.Fatalf("user-agent = %q, want Edge variant to match sec-ch-ua=Microsoft Edge 146", ua)
414417
}
415418
if first["oai-device-id"] == "" || first["oai-device-id"] != second["oai-device-id"] {
416419
t.Fatalf("oai-device-id should be stable across headers: first=%q second=%q", first["oai-device-id"], second["oai-device-id"])

util/useragent.go

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,19 @@ type userAgentSpec struct {
1616
Family string
1717
}
1818

19-
// 模板覆盖 Chrome / Edge / Firefox / Safari 四大主流桌面浏览器,
20-
// 版本号在 [MinVersion, MaxVersion] 闭区间内随机。
19+
// 模板限定为 Edge(Windows)一族。
20+
//
21+
// 为什么不能用其他浏览器:internal/chatgpt 的 createBaseHeaderForState 同时
22+
// 写死了 sec-ch-ua = "Microsoft Edge";v="146",如果 user-agent 跟 sec-ch-ua
23+
// 不一致,Cloudflare/ChatGPT 一眼就能看出是脚本客户端。版本号在
24+
// [MinVersion, MaxVersion] 闭区间内随机,仍然保留一定的指纹多样性。
2125
var userAgentSpecs = []userAgentSpec{
22-
{
23-
Template: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%d.0.0.0 Safari/537.36",
24-
MinVersion: 120,
25-
MaxVersion: 147,
26-
Family: "Chrome-Win",
27-
},
28-
{
29-
Template: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%d.0.0.0 Safari/537.36",
30-
MinVersion: 120,
31-
MaxVersion: 147,
32-
Family: "Chrome-Mac",
33-
},
3426
{
3527
Template: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%d.0.0.0 Safari/537.36 Edg/%d.0.0.0",
3628
MinVersion: 120,
3729
MaxVersion: 147,
3830
Family: "Edge-Win",
3931
},
40-
{
41-
Template: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:%d.0) Gecko/20100101 Firefox/%d.0",
42-
MinVersion: 120,
43-
MaxVersion: 132,
44-
Family: "Firefox-Win",
45-
},
46-
{
47-
Template: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:%d.0) Gecko/20100101 Firefox/%d.0",
48-
MinVersion: 120,
49-
MaxVersion: 132,
50-
Family: "Firefox-Mac",
51-
},
52-
{
53-
Template: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/%d.0 Safari/605.1.15",
54-
MinVersion: 17,
55-
MaxVersion: 18,
56-
Family: "Safari-Mac",
57-
},
5832
}
5933

6034
var (

0 commit comments

Comments
 (0)