2020-08-21 21:09:42 +08:00
|
|
|
package waf
|
|
|
|
|
|
|
|
|
|
import (
|
2023-11-29 16:57:58 +08:00
|
|
|
"encoding/json"
|
2020-08-21 21:09:42 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2023-06-30 18:08:30 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
2020-12-23 09:52:31 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
|
2020-09-20 20:12:43 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2021-10-06 09:08:09 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
2020-09-20 20:12:43 +08:00
|
|
|
"github.com/iwind/TeaGo/actions"
|
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
2021-10-07 16:47:14 +08:00
|
|
|
"github.com/iwind/TeaGo/types"
|
2020-08-21 21:09:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type IndexAction struct {
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) Init() {
|
|
|
|
|
this.Nav("", "setting", "index")
|
|
|
|
|
this.SecondMenu("waf")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) RunGet(params struct {
|
|
|
|
|
ServerId int64
|
|
|
|
|
}) {
|
2024-04-14 16:45:17 +08:00
|
|
|
// 只有HTTP服务才支持
|
|
|
|
|
if this.FilterHTTPFamily() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-07 16:47:14 +08:00
|
|
|
// 服务分组设置
|
|
|
|
|
groupResp, err := this.RPC().ServerGroupRPC().FindEnabledServerGroupConfigInfo(this.AdminContext(), &pb.FindEnabledServerGroupConfigInfoRequest{
|
|
|
|
|
ServerId: params.ServerId,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
this.Data["hasGroupConfig"] = groupResp.HasWAFConfig
|
|
|
|
|
this.Data["groupSettingURL"] = "/servers/groups/group/settings/waf?groupId=" + types.String(groupResp.ServerGroupId)
|
|
|
|
|
|
2021-01-03 20:25:15 +08:00
|
|
|
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
|
2020-09-20 20:12:43 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-09-21 19:51:50 +08:00
|
|
|
|
2020-09-20 20:12:43 +08:00
|
|
|
this.Data["webId"] = webConfig.Id
|
|
|
|
|
this.Data["firewallConfig"] = webConfig.FirewallRef
|
|
|
|
|
|
2024-05-05 19:51:17 +08:00
|
|
|
// 获取当前网站所在集群的WAF设置
|
2020-12-23 09:52:31 +08:00
|
|
|
firewallPolicy, err := dao.SharedHTTPFirewallPolicyDAO.FindEnabledHTTPFirewallPolicyWithServerId(this.AdminContext(), params.ServerId)
|
2020-09-20 20:12:43 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-12-17 15:50:44 +08:00
|
|
|
if firewallPolicy != nil {
|
2023-11-29 16:57:58 +08:00
|
|
|
// captcha action
|
2024-04-07 14:20:17 +08:00
|
|
|
var captchaOptions = firewallconfigs.NewHTTPFirewallCaptchaAction()
|
2023-11-29 16:57:58 +08:00
|
|
|
if len(firewallPolicy.CaptchaOptionsJSON) > 0 {
|
|
|
|
|
err = json.Unmarshal(firewallPolicy.CaptchaOptionsJSON, captchaOptions)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-17 15:50:44 +08:00
|
|
|
this.Data["firewallPolicy"] = maps.Map{
|
2023-11-29 16:57:58 +08:00
|
|
|
"id": firewallPolicy.Id,
|
|
|
|
|
"name": firewallPolicy.Name,
|
|
|
|
|
"isOn": firewallPolicy.IsOn,
|
|
|
|
|
"mode": firewallPolicy.Mode,
|
|
|
|
|
"modeInfo": firewallconfigs.FindFirewallMode(firewallPolicy.Mode),
|
|
|
|
|
"captchaAction": captchaOptions,
|
2020-12-17 15:50:44 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.Data["firewallPolicy"] = nil
|
2020-09-20 20:12:43 +08:00
|
|
|
}
|
2020-08-21 21:09:42 +08:00
|
|
|
|
2021-01-18 20:39:58 +08:00
|
|
|
// 当前的Server独立设置
|
|
|
|
|
if webConfig.FirewallRef == nil || webConfig.FirewallRef.FirewallPolicyId == 0 {
|
2021-10-07 16:47:14 +08:00
|
|
|
firewallPolicyId, err := dao.SharedHTTPWebDAO.InitEmptyHTTPFirewallPolicy(this.AdminContext(), 0, params.ServerId, webConfig.Id, webConfig.FirewallRef != nil && webConfig.FirewallRef.IsOn)
|
2021-01-18 20:39:58 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
this.Data["firewallPolicyId"] = firewallPolicyId
|
|
|
|
|
} else {
|
|
|
|
|
this.Data["firewallPolicyId"] = webConfig.FirewallRef.FirewallPolicyId
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 21:09:42 +08:00
|
|
|
this.Show()
|
|
|
|
|
}
|
2020-09-20 20:12:43 +08:00
|
|
|
|
|
|
|
|
func (this *IndexAction) RunPost(params struct {
|
|
|
|
|
WebId int64
|
|
|
|
|
FirewallJSON []byte
|
|
|
|
|
|
|
|
|
|
Must *actions.Must
|
|
|
|
|
}) {
|
2023-06-30 18:08:30 +08:00
|
|
|
defer this.CreateLogInfo(codes.ServerWAF_LogUpdateWAFSettings, params.WebId)
|
2020-11-20 15:32:42 +08:00
|
|
|
|
2020-09-20 20:12:43 +08:00
|
|
|
// TODO 检查配置
|
|
|
|
|
|
2020-09-21 19:51:50 +08:00
|
|
|
_, err := this.RPC().HTTPWebRPC().UpdateHTTPWebFirewall(this.AdminContext(), &pb.UpdateHTTPWebFirewallRequest{
|
2021-11-24 11:58:01 +08:00
|
|
|
HttpWebId: params.WebId,
|
2020-09-20 20:12:43 +08:00
|
|
|
FirewallJSON: params.FirewallJSON,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.Success()
|
|
|
|
|
}
|