mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2024-11-12 02:21:55 +08:00
102 lines
3.5 KiB
Go
102 lines
3.5 KiB
Go
|
package v1
|
||
|
|
||
|
import (
|
||
|
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
||
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
||
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
||
|
"github.com/1Panel-dev/1Panel/backend/global"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
)
|
||
|
|
||
|
// @Tags Firewall
|
||
|
// @Summary Page firewall rules
|
||
|
// @Description 获取防火墙规则列表分页
|
||
|
// @Accept json
|
||
|
// @Param request body dto.SearchWithPage true "request"
|
||
|
// @Success 200 {object} dto.PageResult
|
||
|
// @Security ApiKeyAuth
|
||
|
// @Router /hosts/firewall/search [post]
|
||
|
func (b *BaseApi) SearchFirewallRule(c *gin.Context) {
|
||
|
var req dto.RuleSearch
|
||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
total, list, err := firewallService.SearchWithPage(req)
|
||
|
if err != nil {
|
||
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
helper.SuccessWithData(c, dto.PageResult{
|
||
|
Items: list,
|
||
|
Total: total,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// @Tags Firewall
|
||
|
// @Summary Create group
|
||
|
// @Description 创建防火墙端口规则
|
||
|
// @Accept json
|
||
|
// @Param request body dto.PortRuleOperate true "request"
|
||
|
// @Success 200
|
||
|
// @Security ApiKeyAuth
|
||
|
// @Router /hosts/firewall/port [post]
|
||
|
// @x-panel-log {"bodyKeys":["port","strategy"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"添加端口规则 {[strategy] [port]}","formatEN":"create port rules {[strategy][port]}"}
|
||
|
func (b *BaseApi) OperatePortRule(c *gin.Context) {
|
||
|
var req dto.PortRuleOperate
|
||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||
|
return
|
||
|
}
|
||
|
if err := global.VALID.Struct(req); err != nil {
|
||
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||
|
return
|
||
|
}
|
||
|
if req.Protocol == "tcp/udp" {
|
||
|
req.Protocol = "tcp"
|
||
|
if err := firewallService.OperatePortRule(req); err != nil {
|
||
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||
|
return
|
||
|
}
|
||
|
req.Protocol = "udp"
|
||
|
if err := firewallService.OperatePortRule(req); err != nil {
|
||
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||
|
return
|
||
|
}
|
||
|
helper.SuccessWithData(c, nil)
|
||
|
}
|
||
|
if err := firewallService.OperatePortRule(req); err != nil {
|
||
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||
|
return
|
||
|
}
|
||
|
helper.SuccessWithData(c, nil)
|
||
|
}
|
||
|
|
||
|
// @Tags Firewall
|
||
|
// @Summary Create group
|
||
|
// @Description 创建防火墙 IP 规则
|
||
|
// @Accept json
|
||
|
// @Param request body dto.AddressCreate true "request"
|
||
|
// @Success 200
|
||
|
// @Security ApiKeyAuth
|
||
|
// @Router /hosts/firewall/ip [post]
|
||
|
// @x-panel-log {"bodyKeys":["strategy","address"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"添加 ip 规则 {[strategy] [address]}","formatEN":"create address rules {[strategy][address]}"}
|
||
|
func (b *BaseApi) OperateIPRule(c *gin.Context) {
|
||
|
var req dto.AddrRuleOperate
|
||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||
|
return
|
||
|
}
|
||
|
if err := global.VALID.Struct(req); err != nil {
|
||
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||
|
return
|
||
|
}
|
||
|
if err := firewallService.OperateAddressRule(req); err != nil {
|
||
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||
|
return
|
||
|
}
|
||
|
helper.SuccessWithData(c, nil)
|
||
|
}
|