-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblacklist.go
129 lines (103 loc) · 2.73 KB
/
blacklist.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/gin-gonic/gin"
)
type Blacklist struct {
PathList []PathItem `json:"pathlist"`
ReferList []ReferItem `json:"referlist"`
}
type PathItem struct {
Paths []string `json:"paths"`
Reason string `json:"reason"`
}
type ReferItem struct {
Refer string `json:"refer"`
Reason string `json:"reason"`
}
func syncBlacklistToDB() {
blacklistData, _ := json.Marshal(blacklist)
redisClient.Set("blacklist", string(blacklistData), 0)
}
func isPathBlacklisted(path string) bool {
for _, item := range blacklist.PathList {
for _, p := range item.Paths {
match, err := regexp.MatchString(p, path)
if err != nil {
fmt.Printf("正则匹配错误:%s", err)
continue
}
if match {
return true
}
}
}
return false
}
func isRefererBlacklisted(referer string) bool {
for _, item := range blacklist.ReferList {
if strings.Contains(referer, item.Refer) {
return true
}
}
return false
}
func getBlacklist(c *gin.Context) {
c.JSON(http.StatusOK, blacklist)
}
func updatePathBlacklist(c *gin.Context) {
if c.Query("key") != apiKey {
c.JSON(http.StatusUnauthorized, gin.H{"error": "无效的API密钥"})
return
}
var pathItem PathItem
if err := c.ShouldBindJSON(&pathItem); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求数据"})
return
}
blacklist.PathList = append(blacklist.PathList, pathItem)
syncBlacklistToDB()
blacklistData, _ := json.Marshal(blacklist)
os.WriteFile("blacklist.json", blacklistData, 0644)
c.JSON(http.StatusOK, gin.H{"message": "路径黑名单已更新"})
}
func updateReferBlacklist(c *gin.Context) {
if c.Query("key") != apiKey {
c.JSON(http.StatusUnauthorized, gin.H{"error": "无效的API密钥"})
return
}
var referItem ReferItem
if err := c.ShouldBindJSON(&referItem); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求数据"})
return
}
blacklist.ReferList = append(blacklist.ReferList, referItem)
syncBlacklistToDB()
blacklistData, _ := json.Marshal(blacklist)
os.WriteFile("blacklist.json", blacklistData, 0644)
c.JSON(http.StatusOK, gin.H{"message": "Referer黑名单已更新"})
}
func loadBlacklist() {
data, err := os.ReadFile("blacklist.json")
if err != nil {
fmt.Println("无法加载黑名单数据:", err)
return
}
if err := json.Unmarshal(data, &blacklist); err != nil {
fmt.Println("无法解析黑名单数据:", err)
return
}
syncBlacklistToDB()
ticker := time.NewTicker(5 * time.Minute)
go func() {
for range ticker.C {
syncBlacklistToDB()
}
}()
}