sanbo
commited on
Commit
·
88c12dd
1
Parent(s):
d86a84e
update sth. at 2024-12-17 23:06:16
Browse files- README.md +1 -0
- internal/duckgo/request.go +65 -10
README.md
CHANGED
@@ -56,6 +56,7 @@ curl --location 'https://sanbo1200-duck2api.hf.space/hf/v1/chat/completions' \
|
|
56 |
"messages": [{"role":"system","content":"你是一个辅助机器人"},{"role": "user", "content": "你的知识库最后什么日期"}],
|
57 |
"stream": true
|
58 |
}'
|
|
|
59 |
```
|
60 |
|
61 |
### 国内反代地址(maybe)
|
|
|
56 |
"messages": [{"role":"system","content":"你是一个辅助机器人"},{"role": "user", "content": "你的知识库最后什么日期"}],
|
57 |
"stream": true
|
58 |
}'
|
59 |
+
|
60 |
```
|
61 |
|
62 |
### 国内反代地址(maybe)
|
internal/duckgo/request.go
CHANGED
@@ -28,6 +28,32 @@ type XqdgToken struct {
|
|
28 |
ExpireAt time.Time `json:"expire"`
|
29 |
}
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
func InitXVQD(client httpclient.AuroraHttpClient, proxyUrl string) (string, error) {
|
32 |
if Token == nil {
|
33 |
Token = &XqdgToken{
|
@@ -37,20 +63,49 @@ func InitXVQD(client httpclient.AuroraHttpClient, proxyUrl string) (string, erro
|
|
37 |
}
|
38 |
Token.M.Lock()
|
39 |
defer Token.M.Unlock()
|
|
|
|
|
40 |
if Token.Token == "" || Token.ExpireAt.Before(time.Now()) {
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
}
|
50 |
-
|
51 |
-
|
|
|
52 |
}
|
53 |
|
|
|
54 |
return Token.Token, nil
|
55 |
}
|
56 |
|
|
|
28 |
ExpireAt time.Time `json:"expire"`
|
29 |
}
|
30 |
|
31 |
+
// func InitXVQD(client httpclient.AuroraHttpClient, proxyUrl string) (string, error) {
|
32 |
+
// if Token == nil {
|
33 |
+
// Token = &XqdgToken{
|
34 |
+
// Token: "",
|
35 |
+
// M: sync.Mutex{},
|
36 |
+
// }
|
37 |
+
// }
|
38 |
+
// Token.M.Lock()
|
39 |
+
// defer Token.M.Unlock()
|
40 |
+
// if Token.Token == "" || Token.ExpireAt.Before(time.Now()) {
|
41 |
+
// status, err := postStatus(client, proxyUrl)
|
42 |
+
// if err != nil {
|
43 |
+
// return "", err
|
44 |
+
// }
|
45 |
+
// defer status.Body.Close()
|
46 |
+
// token := status.Header.Get("x-vqd-4")
|
47 |
+
// if token == "" {
|
48 |
+
// return "", errors.New("no x-vqd-4 token")
|
49 |
+
// }
|
50 |
+
// Token.Token = token
|
51 |
+
// Token.ExpireAt = time.Now().Add(time.Minute * 3)
|
52 |
+
// }
|
53 |
+
//
|
54 |
+
// return Token.Token, nil
|
55 |
+
// }
|
56 |
+
|
57 |
func InitXVQD(client httpclient.AuroraHttpClient, proxyUrl string) (string, error) {
|
58 |
if Token == nil {
|
59 |
Token = &XqdgToken{
|
|
|
63 |
}
|
64 |
Token.M.Lock()
|
65 |
defer Token.M.Unlock()
|
66 |
+
|
67 |
+
// 如果 token 已经失效或为空,尝试重新获取
|
68 |
if Token.Token == "" || Token.ExpireAt.Before(time.Now()) {
|
69 |
+
const maxRetries = 3 // 设置最大重试次数
|
70 |
+
const retryInterval = 2 * time.Second // 设置每次重试间隔
|
71 |
+
|
72 |
+
for retries := 0; retries < maxRetries; retries++ {
|
73 |
+
// 发送请求
|
74 |
+
status, err := postStatus(client, proxyUrl)
|
75 |
+
if err != nil {
|
76 |
+
// 如果是非网络类错误,直接返回
|
77 |
+
if retries == maxRetries-1 {
|
78 |
+
return "", err
|
79 |
+
}
|
80 |
+
// 网络错误,等待重试
|
81 |
+
time.Sleep(retryInterval)
|
82 |
+
continue
|
83 |
+
}
|
84 |
+
|
85 |
+
// 获取 x-vqd-4 token
|
86 |
+
defer status.Body.Close()
|
87 |
+
token := status.Header.Get("x-vqd-4")
|
88 |
+
if token == "" {
|
89 |
+
// 如果没有获取到 token,判断是否是最后一次重试
|
90 |
+
if retries == maxRetries-1 {
|
91 |
+
return "", errors.New("no x-vqd-4 token after retries")
|
92 |
+
}
|
93 |
+
// 没有获取到 token,等待重试
|
94 |
+
time.Sleep(retryInterval)
|
95 |
+
continue
|
96 |
+
}
|
97 |
+
|
98 |
+
// 成功获取到 token
|
99 |
+
Token.Token = token
|
100 |
+
Token.ExpireAt = time.Now().Add(time.Minute * 3)
|
101 |
+
return Token.Token, nil
|
102 |
}
|
103 |
+
|
104 |
+
// 重试完仍未成功,返回错误
|
105 |
+
return "", errors.New("failed to get x-vqd-4 token after retries")
|
106 |
}
|
107 |
|
108 |
+
// 如果 token 已存在且有效,直接返回
|
109 |
return Token.Token, nil
|
110 |
}
|
111 |
|