11package v1
22
33import (
4- "bufio"
54 "os"
65
76 "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
@@ -10,13 +9,13 @@ import (
109 "github.com/gin-gonic/gin"
1110)
1211
13- // @Tags Fail2ban
12+ // @Tags Fail2Ban
1413// @Summary Load fail2ban base info
15- // @Description 获取 Fail2ban 基础信息
16- // @Success 200 {object} dto.Fail2banBaseInfo
14+ // @Description 获取 Fail2Ban 基础信息
15+ // @Success 200 {object} dto.Fail2BanBaseInfo
1716// @Security ApiKeyAuth
1817// @Router /toolbox/fail2ban/base [get]
19- func (b *BaseApi) LoadFail2banBaseInfo (c *gin.Context) {
18+ func (b *BaseApi) LoadFail2BanBaseInfo (c *gin.Context) {
2019 data, err := fail2banService.LoadBaseInfo()
2120 if err != nil {
2221 helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
@@ -26,41 +25,38 @@ func (b *BaseApi) LoadFail2banBaseInfo(c *gin.Context) {
2625 helper.SuccessWithData(c, data)
2726}
2827
29- // @Tags Fail2ban
28+ // @Tags Fail2Ban
3029// @Summary Page fail2ban ip list
31- // @Description 获取 Fail2ban ip 列表
30+ // @Description 获取 Fail2Ban ip
3231// @Accept json
33- // @Param request body dto.Fail2banSearch true "request"
34- // @Success 200 {object} dto.PageResult
32+ // @Param request body dto.Fail2BanSearch true "request"
33+ // @Success 200 {Array} string
3534// @Security ApiKeyAuth
3635// @Router /toolbox/fail2ban/search [post]
37- func (b *BaseApi) SearchFail2ban (c *gin.Context) {
38- var req dto.Fail2banSearch
36+ func (b *BaseApi) SearchFail2Ban (c *gin.Context) {
37+ var req dto.Fail2BanSearch
3938 if err := helper.CheckBindAndValidate(&req, c); err != nil {
4039 return
4140 }
4241
43- total, list, err := fail2banService.SearchWithPage (req)
42+ list, err := fail2banService.Search (req)
4443 if err != nil {
4544 helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
4645 return
4746 }
4847
49- helper.SuccessWithData(c, dto.PageResult{
50- Items: list,
51- Total: total,
52- })
48+ helper.SuccessWithData(c, list)
5349}
5450
55- // @Tags Fail2ban
51+ // @Tags Fail2Ban
5652// @Summary Operate fail2ban
57- // @Description 修改 Fail2ban 状态
53+ // @Description 修改 Fail2Ban 状态
5854// @Accept json
5955// @Param request body dto.Operate true "request"
6056// @Security ApiKeyAuth
6157// @Router /toolbox/fail2ban/operate [post]
62- // @x-panel-log {"bodyKeys":["operation"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"[operation] Fail2ban ","formatEN":"[operation] Fail2ban "}
63- func (b *BaseApi) OperateFail2ban (c *gin.Context) {
58+ // @x-panel-log {"bodyKeys":["operation"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"[operation] Fail2Ban ","formatEN":"[operation] Fail2Ban "}
59+ func (b *BaseApi) OperateFail2Ban (c *gin.Context) {
6460 var req dto.Operate
6561 if err := helper.CheckBindAndValidate(&req, c); err != nil {
6662 return
@@ -74,16 +70,15 @@ func (b *BaseApi) OperateFail2ban(c *gin.Context) {
7470 helper.SuccessWithData(c, nil)
7571}
7672
77- // @Tags Fail2ban
73+ // @Tags Fail2Ban
7874// @Summary Operate sshd of fail2ban
7975// @Description 配置 sshd
8076// @Accept json
8177// @Param request body dto.Operate true "request"
8278// @Security ApiKeyAuth
8379// @Router /toolbox/fail2ban/operate/sshd [post]
84- // @x-panel-log {"bodyKeys":["operate", "ips"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"[operate] ips: [ips]","formatEN":"[operate] ips: [ips]"}
8580func (b *BaseApi) OperateSSHD(c *gin.Context) {
86- var req dto.Fail2banSet
81+ var req dto.Fail2BanSet
8782 if err := helper.CheckBindAndValidate(&req, c); err != nil {
8883 return
8984 }
@@ -96,17 +91,17 @@ func (b *BaseApi) OperateSSHD(c *gin.Context) {
9691 helper.SuccessWithData(c, nil)
9792}
9893
99- // @Tags Fail2ban
94+ // @Tags Fail2Ban
10095// @Summary Update fail2ban conf
101- // @Description 修改 Fail2ban 配置
96+ // @Description 修改 Fail2Ban 配置
10297// @Accept json
103- // @Param request body dto.Fail2banUpdate true "request"
98+ // @Param request body dto.Fail2BanUpdate true "request"
10499// @Success 200
105100// @Security ApiKeyAuth
106101// @Router /toolbox/fail2ban/update [post]
107- // @x-panel-log {"bodyKeys":["key","value"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改 Fail2ban 配置 [key] => [value]","formatEN":"update fail2ban conf [key] => [value]"}
108- func (b *BaseApi) UpdateFail2banConf (c *gin.Context) {
109- var req dto.Fail2banUpdate
102+ // @x-panel-log {"bodyKeys":["key","value"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改 Fail2Ban 配置 [key] => [value]","formatEN":"update fail2ban conf [key] => [value]"}
103+ func (b *BaseApi) UpdateFail2BanConf (c *gin.Context) {
104+ var req dto.Fail2BanUpdate
110105 if err := helper.CheckBindAndValidate(&req, c); err != nil {
111106 return
112107 }
@@ -118,14 +113,14 @@ func (b *BaseApi) UpdateFail2banConf(c *gin.Context) {
118113 helper.SuccessWithData(c, nil)
119114}
120115
121- // @Tags Fail2ban
116+ // @Tags Fail2Ban
122117// @Summary Load fail2ban conf
123118// @Description 获取 fail2ban 配置文件
124119// @Accept json
125- // @Success 200 string
120+ // @Success 200
126121// @Security ApiKeyAuth
127122// @Router /toolbox/fail2ban/load/conf [get]
128- func (b *BaseApi) LoadFail2banConf (c *gin.Context) {
123+ func (b *BaseApi) LoadFail2BanConf (c *gin.Context) {
129124 path := "/etc/fail2ban/jail.local"
130125 file, err := os.ReadFile(path)
131126 if err != nil {
@@ -136,29 +131,23 @@ func (b *BaseApi) LoadFail2banConf(c *gin.Context) {
136131 helper.SuccessWithData(c, string(file))
137132}
138133
139- // @Tags Fail2ban
134+ // @Tags Fail2Ban
140135// @Summary Update fail2ban conf by file
141136// @Description 通过文件修改 fail2ban 配置
142137// @Accept json
143138// @Param request body dto.UpdateByFile true "request"
144139// @Success 200
145140// @Security ApiKeyAuth
146141// @Router /toolbox/fail2ban/update/byconf [post]
147- func (b *BaseApi) UpdateFail2banConfByFile (c *gin.Context) {
142+ func (b *BaseApi) UpdateFail2BanConfByFile (c *gin.Context) {
148143 var req dto.UpdateByFile
149144 if err := helper.CheckBind(&req, c); err != nil {
150145 return
151146 }
152- path := "/etc/fail2ban/jail.local"
153- file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0640)
154- if err != nil {
147+ if err := fail2banService.UpdateConfByFile(req); err != nil {
155148 helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
156149 return
157150 }
158- defer file.Close()
159- write := bufio.NewWriter(file)
160- _, _ = write.WriteString(req.File)
161- write.Flush()
162151
163152 helper.SuccessWithData(c, nil)
164153}
0 commit comments