Skip to content

Commit 552eaa3

Browse files
committed
增加通用的API
1 parent 5fbbf51 commit 552eaa3

File tree

4 files changed

+125
-4
lines changed

4 files changed

+125
-4
lines changed

api.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# coding: utf-8
2+
import time
3+
import hashlib
4+
import json
5+
import requests
6+
7+
8+
class WafApi:
9+
__WAF_KEY = 'op51JCeKAdqa7AUtKae7ONrMeW8FdAWA'
10+
__WAF_PANEL = 'https://192.168.10.10:8379'
11+
12+
def __get_md5(self, s: str) -> str:
13+
"""计算字符串的 MD5 值"""
14+
return hashlib.md5(s.encode('utf-8')).hexdigest()
15+
16+
def __get_headers(self) -> dict:
17+
"""生成请求头中的 waf_request_time 和 waf_request_token"""
18+
time_now = str(int(time.time()))
19+
token = self.__get_md5(time_now + self.__get_md5(self.__WAF_KEY))
20+
return {
21+
'waf_request_time': time_now,
22+
'waf_request_token': token
23+
}
24+
25+
def get_logs(self):
26+
"""获取站点日志列表"""
27+
url = f"{self.__WAF_PANEL}/api/wafmastersite/get_site_list"
28+
headers = self.__get_headers()
29+
payload = {'p': 1, 'p_size': 20, 'site_name': ""}
30+
31+
try:
32+
response = requests.post(
33+
url,
34+
headers=headers,
35+
data=json.dumps(payload),
36+
timeout=10,
37+
verify=False
38+
)
39+
40+
if response.status_code == 200:
41+
json_data = response.json()
42+
if json_data.get('code') == 0:
43+
return json_data
44+
else:
45+
print("接口返回失败:", json_data.get('msg', '未知错误'))
46+
else:
47+
print(f"请求失败,状态码:{response.status_code}")
48+
49+
except requests.RequestException as e:
50+
print(f"网络请求异常: {e}")
51+
52+
return None
53+
54+
55+
if __name__ == '__main__':
56+
waf_api = WafApi()
57+
logs = waf_api.get_logs()
58+
print(logs)

main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ var staticFs embed.FS
3939
func registerMiddlewares(srv *core.Server) {
4040
srv.Before(func(w http.ResponseWriter, r *http.Request) bool {
4141
// API暂时不开放,等前端对接后开放
42-
if true {
43-
return true
44-
}
4542

4643
if len(r.URL.Path) < 5 || !strings.EqualFold(r.URL.Path[:5], "/api/") {
4744
return true

modules/config.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ import (
88
"CloudWaf/public"
99
"CloudWaf/public/db"
1010
"CloudWaf/public/validate"
11+
"encoding/base64"
12+
"encoding/json"
1113
"fmt"
1214
"html"
15+
"io"
16+
"io/fs"
1317
"net/http"
18+
"net/url"
1419
"os"
1520
"os/exec"
1621
"regexp"
1722
"strconv"
1823
"strings"
1924
"time"
20-
"CloudWaf/providers"
2125

2226
"github.com/pquerna/otp"
2327
"github.com/pquerna/otp/totp"
@@ -100,15 +104,71 @@ func (config *Config) GetConfig(request *http.Request) core.Response {
100104
}
101105
s := time.Now().String()
102106
systemTime := s[:19] + " " + s[30:39]
107+
apiinfo, err := public.GetWAFApi()
103108
return core.Success(map[string]interface{}{
104109
"config": data,
105110
"port": core.GetServerPort(),
106111
"two_step_auth": status,
107112
"basic_auth": basicAuth,
108113
"systemdate": systemTime,
114+
"apiinfo": apiinfo,
109115
})
110116
}
111117

118+
// 开启API
119+
func (config *Config) SetOpenApi(request *http.Request) core.Response {
120+
params := struct {
121+
Type int `json:"type"`
122+
LimitAddr string `json:"limit_addr"`
123+
}{}
124+
if err := core.GetParamsFromRequestToStruct(request, &params); err != nil {
125+
return core.Fail(err)
126+
}
127+
// t_type 类型 1 重置Token
128+
// t_type 类型 2 开启或者关闭API
129+
// t_type 类型 3 设置地址
130+
//limit_addr 限制地址
131+
t_type := params.Type
132+
limit_addr := params.LimitAddr
133+
if params.Type == 3 {
134+
// 判断
135+
apiinfo, _ := public.GetWAFApi()
136+
limit_addr_arr := strings.Split(limit_addr, "\n")
137+
apiinfo.LimitAddr = limit_addr_arr
138+
public.SaveWAFApi(apiinfo)
139+
return core.Success("设置成功")
140+
}
141+
if t_type == 2 {
142+
apiinfo, _ := public.GetWAFApi()
143+
if apiinfo.Open == false && apiinfo.Token == "" {
144+
apiinfo.Token = public.RandomStr(32)
145+
apiinfo.Open = true
146+
public.SaveWAFApi(apiinfo)
147+
return core.Success("设置成功")
148+
}
149+
if apiinfo.Open == true {
150+
apiinfo.Open = false
151+
apiinfo.Token = ""
152+
public.SaveWAFApi(apiinfo)
153+
return core.Success("设置成功")
154+
} else {
155+
apiinfo.Open = true
156+
apiinfo.Token = public.RandomStr(32)
157+
public.SaveWAFApi(apiinfo)
158+
return core.Success("设置成功")
159+
}
160+
}
161+
if t_type == 1 {
162+
apiinfo, _ := public.GetWAFApi()
163+
apiinfo.Token = public.RandomStr(32)
164+
public.SaveWAFApi(apiinfo)
165+
return core.Success("设置成功")
166+
}
167+
168+
return core.Success("设置成功")
169+
170+
}
171+
112172
func (config *Config) SetCert(request *http.Request) core.Response {
113173
params, err := core.GetParamsFromRequest(request)
114174
if err != nil {

public/public.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,3 +2758,9 @@ func (api *ApiInfo) CheckWafApiAccess(clientIP string) bool {
27582758
}
27592759
return false
27602760
}
2761+
2762+
func SaveWAFApi(ApiInfo ApiInfo) {
2763+
apiinfoJson, _ := json.Marshal(ApiInfo)
2764+
WriteFile("/www/cloud_waf/console/data/btwaf_api.json", string(apiinfoJson))
2765+
2766+
}

0 commit comments

Comments
 (0)