Enhance dynamic model support and optimize user agent handling#266
Merged
Conversation
- 新增util包的RandomUserAgent函数,生成主流桌面浏览器随机UA - 替换所有硬编码userAgent为defaultUserAgent()调用 - 为客户端状态加入UserAgent字段并初始化随机UA - 重构prooftoken模块,适配新的随机UA和POW逻辑
There was a problem hiding this comment.
Pull request overview
This PR updates ChatGPT client request handling to support dynamic per-client User-Agent selection, modernizes proof-of-work (PoW) token generation, and improves streaming/TTS parsing and model propagation.
Changes:
- Added a randomized desktop User-Agent generator and began using it (or a per-client override) across request paths.
- Reworked Sentinel PoW token generation into a requirements-token builder plus a challenge-based proof-token solver.
- Improved request/model propagation in handlers and added tests for TTS patch-stream parsing and response conversion.
Reviewed changes
Copilot reviewed 12 out of 14 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
util/useragent.go |
Adds randomized desktop User-Agent generation helper. |
util/useragent_test.go |
Adds tests to validate UA generation behavior. |
internal/prooftoken/prooftoken.go |
Refactors PoW token generation/solving logic and constants. |
internal/chatgpt/request.go |
Switches many request paths to use dynamic UA, updates PoW calls, improves TTS stream parsing, and model fallback behavior. |
internal/chatgpt/request_test.go |
Updates handler tests and adds TTS patch-stream parsing tests. |
internal/chatgpt/files.go |
Uses the new default UA behavior for upload requests. |
internal/chatgpt/client_state.go |
Adds per-client UserAgent to state and initializes it randomly. |
internal/chatgpt/artifact_delivery.go |
Uses the new default UA behavior for artifact downloads. |
initialize/handlers.go |
Updates TTS handler flow to use client-order conversation posting and state. |
conversion/response/chatgpt/convert.go |
Uses request model consistently; safer text-part extraction. |
conversion/response/chatgpt/convert_test.go |
Adds conversion tests for model propagation and source-marker behavior. |
go.mod |
Removes funcaptcha dependency; adds fake-useragent. |
go.sum |
Removes funcaptcha checksums. |
.gitignore |
Adds .atomcode and CLAUDE.md ignores; normalizes .gocache. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+60
to
+83
| var ( | ||
| uaRand *rand.Rand | ||
| uaRandOnce sync.Once | ||
| ) | ||
|
|
||
| func initUARand() { | ||
| uaRandOnce.Do(func() { | ||
| uaRand = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
| }) | ||
| } | ||
|
|
||
| // RandomUserAgent 返回一个随机的主流桌面浏览器 User-Agent | ||
| func RandomUserAgent() string { | ||
| initUARand() | ||
| spec := userAgentSpecs[uaRand.Intn(len(userAgentSpecs))] | ||
|
|
||
| version := spec.MinVersion | ||
| if spec.MaxVersion > spec.MinVersion { | ||
| version += uaRand.Intn(spec.MaxVersion - spec.MinVersion + 1) | ||
| } | ||
|
|
||
| // 部分模板里有两个 %d(如 Edge、Fx),用同一个 version 填充 | ||
| return fmt.Sprintf(spec.Template, version, version) | ||
| } |
Comment on lines
+11
to
+18
| ua := RandomUserAgent() | ||
| if strings.TrimSpace(ua) == "" { | ||
| t.Fatalf("empty UA at iter %d", i) | ||
| } | ||
| if !strings.HasPrefix(ua, "Mozilla/5.0") { | ||
| t.Errorf("UA does not start with Mozilla/5.0: %s", ua) | ||
| } | ||
| seen[ua] = true |
Comment on lines
+24
to
28
| maxRequirementsIter = 500_000 | ||
| maxProofIter = 100_000 | ||
|
|
||
| powFallback = "gAAAAABwQ8Lk5FbGpA2NcR9dShT6gYjU7VxZ4D" | ||
| ) |
Comment on lines
+164
to
+177
| // Python: h[:diff_len] <= target | ||
| // diff_len 是字符数(6),target 是字节(3)。Python bytes cmp 按短的逐字节比较。 | ||
| // 这里保持等价:取 min(len(target), len(sum)) 字节比较。 | ||
| n2 := diffLen | ||
| if n2 > len(sum) { | ||
| n2 = len(sum) | ||
| } | ||
| cmpLen := n2 | ||
| if cmpLen > len(target) { | ||
| cmpLen = len(target) | ||
| } | ||
| if bytes.Compare(sum[:cmpLen], target[:cmpLen]) <= 0 { | ||
| return string(b64buf), true | ||
| } |
Comment on lines
23
to
31
| @@ -24,6 +26,7 @@ func NewChatClientState() *ChatClientState { | |||
| SessionID: uuid.NewString(), | |||
| StartTime: time.Now(), | |||
| ParentMessageID: "client-created-root", | |||
| UserAgent: browser.Random(), | |||
| } | |||
| } | |||
Comment on lines
+2617
to
+2621
| ua := defaultUserAgent() | ||
| if state != nil && state.UserAgent != "" { | ||
| ua = state.UserAgent | ||
| } | ||
| header.Set("user-agent", ua) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.