-
Notifications
You must be signed in to change notification settings - Fork 4
/
request_handler.go
552 lines (465 loc) · 16.1 KB
/
request_handler.go
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
)
type NSFWResponse struct {
Porn float64 `json:"porn"`
Sexy float64 `json:"sexy"`
Hentai float64 `json:"hentai"`
Neutral float64 `json:"neutral"`
Drawing float64 `json:"drawing"`
}
type AdvanceConfig struct {
PathList []struct {
Paths []string `json:"paths"`
URL []string `json:"url"`
} `json:"pathlist"`
}
type HTTPResponse struct {
Body []byte
ContentType string
}
var (
contentTypes string
body []byte
isAPI bool
)
func handleRequest(c *gin.Context) {
// 获取请求路径和Referer
path := c.Param("path")
referer := c.Request.Referer()
pathAll := fmt.Sprintf("%s%s", path, c.Param("filepath"))
params := c.Request.URL.RawQuery
urlAll := c.Request.URL.String()
fmt.Println("参数:" + params)
if isBlacklistMode() {
// 检查路径黑名单
if isPathBlacklisted("/" + pathAll) {
if RejectionMethod == "301" {
c.Redirect(http.StatusMovedPermanently, RedirectUrl+pathAll)
} else {
c.JSON(http.StatusForbidden, gin.H{"error": "路径被禁止访问", "hitokoto": hitokoto()})
}
return
}
// 检查Referer黑名单
if isRefererBlacklisted(referer) {
if RejectionMethod == "301" {
c.Redirect(http.StatusMovedPermanently, RedirectUrl+pathAll)
} else {
c.JSON(http.StatusForbidden, gin.H{"error": "路径被禁止访问", "hitokoto": hitokoto()})
}
return
}
} else {
// 检查路径黑名单
if isPathBlacklisted("/" + pathAll) {
if RejectionMethod == "301" {
c.Redirect(http.StatusMovedPermanently, RedirectUrl+pathAll)
} else {
c.JSON(http.StatusForbidden, gin.H{"error": "路径被禁止访问", "hitokoto": hitokoto()})
}
return
}
// 检查路径白名单
if !isPathWhitelisted("/" + pathAll) {
if RejectionMethod == "301" {
c.Redirect(http.StatusMovedPermanently, RedirectUrl+pathAll)
} else {
c.JSON(http.StatusForbidden, gin.H{"error": "路径被禁止访问", "hitokoto": hitokoto()})
}
return
}
// 检查Referer白名单
if !isRefererWhitelisted(referer) {
if RejectionMethod == "301" {
c.Redirect(http.StatusMovedPermanently, RedirectUrl+pathAll)
} else {
c.JSON(http.StatusForbidden, gin.H{"error": "路径被禁止访问", "hitokoto": hitokoto()})
}
return
}
}
// 检查是否存在缓存
if data, err := redisClient.Get(urlAll).Result(); err == nil {
// 缓存存在,直接返回数据
contentType, _ := redisClient.Get(urlAll + ":content-type").Result()
if strings.Contains(contentType, "image") || strings.Contains(contentType, "font") {
c.Header("Cache-Control", "max-age=315360000")
c.Header("Expires", time.Now().Add(315360000*time.Second).Format(http.TimeFormat))
} else {
ttl, _ := redisClient.TTL(urlAll).Result()
c.Header("Cache-Control", fmt.Sprintf("max-age=%d", int(ttl.Seconds())))
c.Header("Expires", time.Now().Add(ttl).Format(http.TimeFormat))
}
c.Data(http.StatusOK, contentType, []byte(data))
return
}
// 检查当前模式是否为代理模式
if proxyMode == "jsd" {
// 调用http请求函数
httpResponse, err := makeJSDRequest(pathAll)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "请求失败", "hitokoto": hitokoto()})
return
}
contentTypes = httpResponse.ContentType
body = httpResponse.Body
} else if proxyMode == "local" {
// 调用http请求函数
httpResponse, err := makeLocalRequest(pathAll)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "请求失败", "hitokoto": hitokoto()})
return
}
contentTypes = httpResponse.ContentType
body = httpResponse.Body
} else if proxyMode == "advance" {
// 调用advance模式处理函数
advanceResponse, err := makeAdvanceRequest(pathAll)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "请求失败", "hitokoto": hitokoto()})
// 打印错误信息
fmt.Println(err)
return
}
contentTypes = advanceResponse.ContentType
body = advanceResponse.Body
} else {
c.JSON(http.StatusNotFound, gin.H{"error": "请求失败", "hitokoto": hitokoto()})
// 打印错误信息
fmt.Println("未知的代理模式")
return
}
if params != "" && isImage(contentTypes) {
isAPI = true
if params == "webp=true" {
fmt.Println("转换webp:", pathAll)
// 转webp
webpData, err := convertImageToWebp(body)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "图片转换失败", "hitokoto": hitokoto()})
return
}
body = webpData
} else {
// 调用图片处理函数
fmt.Println("处理图片:", pathAll, "参数:", params)
imageData, contentType := handleImageRequest(body, params)
if imageData != nil {
body = imageData
contentTypes = contentType
}
}
}
// 检查是否需要进行词库匹配,异步
if !isAPI && (strings.Contains(contentTypes, "text/html") || strings.Contains(contentTypes, "text/plain") || strings.Contains(contentTypes, "application/json")) {
go func() {
if checkKeywords(string(body)) {
// 更新黑名单数据
blacklist.PathList = append(blacklist.PathList, PathItem{Paths: []string{"/" + pathAll}, Reason: "内容包含违规关键词"})
// 将黑名单数据存储到Redis
syncBlacklistToDB()
// 将黑名单数据存储到blacklist.json文件
blacklistData, _ := json.Marshal(blacklist)
os.WriteFile("blacklist.json", blacklistData, 0644)
}
}()
}
// 检查是否需要进行图片 NSFW 检查,异步,当文件类型为png jpg jpeg webp触发
if !isAPI && (strings.Contains(contentTypes, "image/png") || strings.Contains(contentTypes, "image/jpg") || strings.Contains(contentTypes, "image/jpeg") || strings.Contains(contentTypes, "image/webp")) {
fmt.Println("检查图片 NSFW:", pathAll)
go func() {
nsfwResult, err := detectNSFW(body, pathAll)
if err != nil {
fmt.Println("检查图片 NSFW 失败:", err)
return
}
if nsfwResult.NSFW {
// 更新黑名单数据
blacklist.PathList = append(blacklist.PathList, PathItem{Paths: []string{"/" + pathAll}, Reason: "涩图,封禁"})
// 将黑名单数据存储到Redis
syncBlacklistToDB()
// 将黑名单数据存储到blacklist.json文件
blacklistData, _ := json.Marshal(blacklist)
os.WriteFile("blacklist.json", blacklistData, 0644)
}
}()
}
// 异步存储到Redis
go func() {
// 设置缓存时间,通过读取配置项EXPIRES的值来设置缓存时间
expiresTime, err := strconv.Atoi(expiresTimeStr)
if err != nil {
expiresTime = 6
}
cacheTime := time.Duration(expiresTime) * time.Hour
if strings.Contains(urlAll, "@") {
cacheTime = 7 * 24 * time.Hour
}
redisClient.Set(urlAll, string(body), cacheTime)
redisClient.Set(urlAll+":content-type", contentTypes, cacheTime)
}()
// 返回响应内容
c.Data(http.StatusOK, contentTypes, body)
}
func makeJSDRequest(pathAll string) (*HTTPResponse, error) {
url := fmt.Sprintf(jsdelivrPrefix+"%s", pathAll)
fmt.Println("源请求URL:" + url)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("创建请求失败: %v", err)
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.5412")
req.Header.Set("Referer", "https://baidu.com")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("请求失败: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("读取响应失败: %v", err)
}
if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("请求失败,状态码: %d", resp.StatusCode)
} else if resp.StatusCode >= http.StatusBadRequest {
return nil, fmt.Errorf("请求失败,状态码: %d", resp.StatusCode)
}
contentType := resp.Header.Get("Content-Type")
response := &HTTPResponse{
Body: body,
ContentType: contentType,
}
return response, nil
}
func makeLocalRequest(pathAll string) (*HTTPResponse, error) {
firstDir := pathAll[:strings.Index(pathAll, "/")]
pathAll = pathAll[strings.Index(pathAll, "/")+1:]
if firstDir == "gh" {
pack := pathAll[:strings.Index(pathAll[strings.Index(pathAll, "/")+1:], "/")+1]
file := pathAll[strings.Index(pathAll[strings.Index(pathAll, "/")+1:], "/")+1:]
re := regexp.MustCompile(`@([^/]+)`)
match := re.FindStringSubmatch(file)
var version string
if len(match) > 1 {
version = match[1]
file = re.ReplaceAllString(file, "")
} else {
version = "main"
}
// 拼接URL,源:https://raw.githubusercontent.com/%s/%s/%s
url := fmt.Sprintf("%s%s/%s%s", ghrawPrefix, pack, version, file)
fmt.Println("源请求URL:" + url)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("创建请求失败: %v", err)
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.5412")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("请求失败: %v", err)
}
defer resp.Body.Close()
// 读取响应内容 body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("读取响应失败: %v", err)
}
if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("请求失败,状态码: %d", resp.StatusCode)
} else if resp.StatusCode >= http.StatusBadRequest {
return nil, fmt.Errorf("请求失败,状态码: %d", resp.StatusCode)
}
contentType := resp.Header.Get("Content-Type")
response := &HTTPResponse{
Body: body,
ContentType: contentType,
}
return response, nil
} else if firstDir == "npm" {
packWithVersion := pathAll[:strings.Index(pathAll, "/")]
pack := packWithVersion[:strings.Index(packWithVersion, "@")]
version := packWithVersion[strings.Index(packWithVersion, "@")+1:]
file := pathAll[strings.Index(pathAll, "/")+1:]
// 拼接URL,源:https://registry.npmmirror.com/%s/%s/files/dist/%s
url := fmt.Sprintf("%s%s/%s/files/dist/%s", npmPrefix, pack, version, file)
fmt.Println("源请求URL:" + url)
// 创建自定义请求
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("创建请求失败: %v", err)
}
req.Header.Set("User-Agent", "npm/7.20.6 node/v14.17.6 win32 x64")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("请求失败: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("读取响应失败: %v", err)
}
if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("请求失败,状态码: %d", resp.StatusCode)
} else if resp.StatusCode >= http.StatusBadRequest {
return nil, fmt.Errorf("请求失败,状态码: %d", resp.StatusCode)
}
contentType := resp.Header.Get("Content-Type")
response := &HTTPResponse{
Body: body,
ContentType: contentType,
}
return response, nil
}
return nil, fmt.Errorf("请求失败,状态码: %d", 404)
}
func makeAdvanceRequest(pathAll string) (*HTTPResponse, error) {
fmt.Println("请求路径:" + pathAll)
go loadAdvance()
advanceData, err := redisClient.Get("advance.json").Bytes()
if err != nil {
return nil, fmt.Errorf("读取advance.json文件失败: %v", err)
}
var advanceConfig AdvanceConfig
err = json.Unmarshal(advanceData, &advanceConfig)
if err != nil {
return nil, fmt.Errorf("解析advance.json数据失败: %v", err)
}
// 遍历配置列表,查找匹配的路径
for _, config := range advanceConfig.PathList {
for _, pathPattern := range config.Paths {
match, err := regexp.MatchString("^"+pathPattern+"/", pathAll)
if err != nil {
return nil, fmt.Errorf("正则表达式匹配失败: %v", err)
}
if match {
re := regexp.MustCompile("^([^/]+)/")
pathAll = re.ReplaceAllString(pathAll, "")
pathAll = "/" + pathAll
// 并发请求多个URL,返回最快的响应
responses := make(chan *HTTPResponse, len(config.URL))
errors := make(chan error, len(config.URL))
var wg sync.WaitGroup
for _, url := range config.URL {
wg.Add(1)
go func(url string) {
defer wg.Done()
httpResponse, err := makeRequest(url + pathAll)
if err != nil {
errors <- fmt.Errorf("请求失败: %v", err)
} else {
responses <- httpResponse
}
}(url)
}
// 等待所有请求完成
go func() {
wg.Wait()
close(responses)
close(errors)
}()
for {
select {
case <-responses:
// 有请求完成,直接返回
return <-responses, nil
case err := <-errors:
// 请求出错,继续等待其他请求完成
fmt.Println(err)
}
}
}
}
}
return nil, fmt.Errorf("未找到匹配的路径")
}
func makeRequest(url string) (*HTTPResponse, error) {
fmt.Println("源请求URL:" + url)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("创建请求失败: %v", err)
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.5412")
req.Header.Set("Referer", "https://baidu.com")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("请求失败: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("读取响应失败: %v", err)
}
if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("请求失败,状态码: %d", resp.StatusCode)
} else if resp.StatusCode >= http.StatusBadRequest {
return nil, fmt.Errorf("请求失败,状态码: %d", resp.StatusCode)
}
contentType := resp.Header.Get("Content-Type")
response := &HTTPResponse{
Body: body,
ContentType: contentType,
}
return response, nil
}
// 判断是否为图片格式
func isImage(contentType string) bool {
return strings.Contains(contentType, "image")
}
// 读取本地advance.json文件同步到Redis
func loadAdvance() {
advanceData, err := os.ReadFile("advance.json")
if err != nil {
fmt.Println("无法读取advance.json文件:", err)
return
}
redisClient.Set("advance.json", string(advanceData), 0)
}
func getAdvance(c *gin.Context) {
advanceData, err := redisClient.Get("advance.json").Bytes()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "读取advance.json文件失败", "hitokoto": hitokoto()})
return
}
var advanceConfig AdvanceConfig
err = json.Unmarshal(advanceData, &advanceConfig)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "解析advance.json数据失败", "hitokoto": hitokoto()})
return
}
c.JSON(http.StatusOK, gin.H{"data": advanceConfig})
}
func setAdvance(c *gin.Context) {
var advanceConfig AdvanceConfig
if c.Query("key") != apiKey {
c.JSON(http.StatusUnauthorized, gin.H{"error": "无效的API密钥"})
return
}
err := c.ShouldBindJSON(&advanceConfig)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "解析JSON数据失败", "hitokoto": hitokoto()})
return
}
advanceData, err := json.Marshal(advanceConfig)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "解析JSON数据失败", "hitokoto": hitokoto()})
return
}
err = os.WriteFile("advance.json", advanceData, 0644)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "写入advance.json文件失败", "hitokoto": hitokoto()})
return
}
redisClient.Set("advance.json", string(advanceData), 0)
c.JSON(http.StatusOK, gin.H{"data": "设置成功"})
}