-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwhitelist.go
130 lines (103 loc) · 2.86 KB
/
whitelist.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/gin-gonic/gin"
)
type Whitelist struct {
PathList []PathItem `json:"pathlist"`
ReferList []ReferItem `json:"referlist"`
AllowEmptyReferer bool `json:"allowEmptyReferer,omitempty"`
}
func isPathWhitelisted(path string) bool {
for _, item := range whitelist.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 isRefererWhitelisted(referer string) bool {
fmt.Printf("Checking referer: %s\n", referer) // 打印传入的 referer
if whitelist.AllowEmptyReferer && referer == "" {
return true
}
for _, item := range whitelist.ReferList {
if strings.Contains(referer, item.Refer) {
return true
}
}
return false
}
func getWhitelist(c *gin.Context) {
c.JSON(http.StatusOK, whitelist)
}
func updatePathWhitelist(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
}
// 更新白名单数据
whitelist.PathList = append(whitelist.PathList, pathItem)
syncWhitelistToDB()
whitelistData, _ := json.Marshal(whitelist)
os.WriteFile("whitelist.json", whitelistData, 0644)
c.JSON(http.StatusOK, gin.H{"message": "路径白名单已更新"})
}
func updateReferWhitelist(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
}
whitelist.ReferList = append(whitelist.ReferList, referItem)
syncWhitelistToDB()
whitelistData, _ := json.Marshal(whitelist)
os.WriteFile("whitelist.json", whitelistData, 0644)
c.JSON(http.StatusOK, gin.H{"message": "Referer白名单已更新"})
}
func syncWhitelistToDB() {
whitelistData, _ := json.Marshal(whitelist)
redisClient.Set("whitelist", string(whitelistData), 0)
}
func loadWhitelist() {
// 从JSON文件加载白名单数据
data, err := os.ReadFile("whitelist.json")
if err != nil {
fmt.Println("无法加载白名单数据:", err)
return
}
if err := json.Unmarshal(data, &whitelist); err != nil {
fmt.Println("无法解析白名单数据:", err)
return
}
syncWhitelistToDB()
ticker := time.NewTicker(5 * time.Minute)
go func() {
for range ticker.C {
syncWhitelistToDB()
}
}()
}