sanbo1200 commited on
Commit
84d57da
·
verified ·
1 Parent(s): 6b0f6e7

Delete internal

Browse files
internal/duckgo/request.go DELETED
@@ -1,247 +0,0 @@
1
- package duckgo
2
-
3
- import (
4
- "aurora/httpclient"
5
- duckgotypes "aurora/typings/duckgo"
6
- officialtypes "aurora/typings/official"
7
- "bufio"
8
- "bytes"
9
- "encoding/json"
10
- "errors"
11
- "io"
12
- "net/http"
13
- "strings"
14
- "sync"
15
- "time"
16
-
17
- "github.com/gin-gonic/gin"
18
- )
19
-
20
- var (
21
- Token *XqdgToken
22
- UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
23
- )
24
-
25
- type XqdgToken struct {
26
- Token string `json:"token"`
27
- M sync.Mutex `json:"-"`
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{
60
- Token: "",
61
- M: sync.Mutex{},
62
- }
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
-
112
- func postStatus(client httpclient.AuroraHttpClient, proxyUrl string) (*http.Response, error) {
113
- if proxyUrl != "" {
114
- client.SetProxy(proxyUrl)
115
- }
116
- header := createHeader()
117
- header.Set("accept", "*/*")
118
- header.Set("x-vqd-accept", "1")
119
- response, err := client.Request(httpclient.GET, "https://duckduckgo.com/duckchat/v1/status", header, nil, nil)
120
- if err != nil {
121
- return nil, err
122
- }
123
- return response, nil
124
- }
125
-
126
- func POSTconversation(client httpclient.AuroraHttpClient, request duckgotypes.ApiRequest, token string, proxyUrl string) (*http.Response, error) {
127
- if proxyUrl != "" {
128
- client.SetProxy(proxyUrl)
129
- }
130
- body_json, err := json.Marshal(request)
131
- if err != nil {
132
- return &http.Response{}, err
133
- }
134
- header := createHeader()
135
- header.Set("accept", "text/event-stream")
136
- header.Set("x-vqd-4", token)
137
- response, err := client.Request(httpclient.POST, "https://duckduckgo.com/duckchat/v1/chat", header, nil, bytes.NewBuffer(body_json))
138
- if err != nil {
139
- return nil, err
140
- }
141
- return response, nil
142
- }
143
-
144
- func Handle_request_error(c *gin.Context, response *http.Response) bool {
145
- if response.StatusCode != 200 {
146
- // Try read response body as JSON
147
- var error_response map[string]interface{}
148
- err := json.NewDecoder(response.Body).Decode(&error_response)
149
- if err != nil {
150
- // Read response body
151
- body, _ := io.ReadAll(response.Body)
152
- c.JSON(response.StatusCode, gin.H{"error": gin.H{
153
- "message": "Unknown error",
154
- "type": "internal_server_error",
155
- "param": nil,
156
- "code": "500",
157
- "details": string(body),
158
- }})
159
- return true
160
- }
161
- c.JSON(response.StatusCode, gin.H{"error": gin.H{
162
- "message": error_response["detail"],
163
- "type": response.Status,
164
- "param": nil,
165
- "code": "error",
166
- }})
167
- return true
168
- }
169
- return false
170
- }
171
-
172
- func createHeader() httpclient.AuroraHeaders {
173
- header := make(httpclient.AuroraHeaders)
174
- header.Set("accept-language", "zh-CN,zh;q=0.9")
175
- header.Set("content-type", "application/json")
176
- header.Set("origin", "https://duckduckgo.com")
177
- header.Set("referer", "https://duckduckgo.com/")
178
- header.Set("sec-ch-ua", `"Chromium";v="120", "Google Chrome";v="120", "Not-A.Brand";v="99"`)
179
- header.Set("sec-ch-ua-mobile", "?0")
180
- header.Set("sec-ch-ua-platform", `"Windows"`)
181
- header.Set("sec-fetch-dest", "empty")
182
- header.Set("sec-fetch-mode", "cors")
183
- header.Set("sec-fetch-site", "same-origin")
184
- header.Set("user-agent", UA)
185
- return header
186
- }
187
-
188
- func Handler(c *gin.Context, response *http.Response, oldRequest duckgotypes.ApiRequest, stream bool) string {
189
- reader := bufio.NewReader(response.Body)
190
- if stream {
191
- // Response content type is text/event-stream
192
- c.Header("Content-Type", "text/event-stream")
193
- } else {
194
- // Response content type is application/json
195
- c.Header("Content-Type", "application/json")
196
- }
197
-
198
- var previousText strings.Builder
199
- for {
200
- line, err := reader.ReadString('\n')
201
- if err != nil {
202
- if err == io.EOF {
203
- break
204
- }
205
- return ""
206
- }
207
- if len(line) < 6 {
208
- continue
209
- }
210
- line = line[6:]
211
- if !strings.HasPrefix(line, "[DONE]") {
212
- var originalResponse duckgotypes.ApiResponse
213
- err = json.Unmarshal([]byte(line), &originalResponse)
214
- if err != nil {
215
- continue
216
- }
217
- if originalResponse.Action != "success" {
218
- c.JSON(500, gin.H{"error": "Error"})
219
- return ""
220
- }
221
- responseString := ""
222
- if originalResponse.Message != "" {
223
- previousText.WriteString(originalResponse.Message)
224
- translatedResponse := officialtypes.NewChatCompletionChunkWithModel(originalResponse.Message, originalResponse.Model)
225
- responseString = "data: " + translatedResponse.String() + "\n\n"
226
- }
227
-
228
- if responseString == "" {
229
- continue
230
- }
231
-
232
- if stream {
233
- _, err = c.Writer.WriteString(responseString)
234
- if err != nil {
235
- return ""
236
- }
237
- c.Writer.Flush()
238
- }
239
- } else {
240
- if stream {
241
- final_line := officialtypes.StopChunkWithModel("stop", oldRequest.Model)
242
- c.Writer.WriteString("data: " + final_line.String() + "\n\n")
243
- }
244
- }
245
- }
246
- return previousText.String()
247
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
internal/proxys/proxys.go DELETED
@@ -1,35 +0,0 @@
1
- package proxys
2
-
3
- import "sync"
4
-
5
- type IProxy struct {
6
- ips []string
7
- lock sync.Mutex
8
- }
9
-
10
- func NewIProxyIP(ips []string) IProxy {
11
- return IProxy{
12
- ips: ips,
13
- }
14
- }
15
-
16
- func (p *IProxy) GetIPS() int {
17
- return len(p.ips)
18
- }
19
-
20
- func (p *IProxy) GetProxyIP() string {
21
- if p == nil {
22
- return ""
23
- }
24
-
25
- p.lock.Lock()
26
- defer p.lock.Unlock()
27
-
28
- if len(p.ips) == 0 {
29
- return ""
30
- }
31
-
32
- proxyIp := p.ips[0]
33
- p.ips = append(p.ips[1:], proxyIp)
34
- return proxyIp
35
- }