Files
EdgeAdmin/internal/web/actions/default/servers/iplists/createIPPopup.go

161 lines
3.6 KiB
Go
Raw Normal View History

2021-06-23 13:12:33 +08:00
package iplists
2024-04-06 10:07:53 +08:00
import (
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
2021-06-23 13:12:33 +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"
2021-06-23 13:12:33 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
2021-06-23 13:12:33 +08:00
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"strings"
2021-06-23 13:12:33 +08:00
)
type CreateIPPopupAction struct {
actionutils.ParentAction
}
func (this *CreateIPPopupAction) Init() {
this.Nav("", "", "")
}
func (this *CreateIPPopupAction) RunGet(params struct {
ListId int64
}) {
this.Data["listId"] = params.ListId
listResp, err := this.RPC().IPListRPC().FindEnabledIPList(this.AdminContext(), &pb.FindEnabledIPListRequest{
IpListId: params.ListId,
})
if err != nil {
this.ErrorPage(err)
return
}
var ipList = listResp.IpList
if ipList == nil {
this.NotFound("ipList", params.ListId)
return
}
this.Data["list"] = maps.Map{
"type": ipList.Type,
}
2021-06-23 13:12:33 +08:00
this.Show()
}
func (this *CreateIPPopupAction) RunPost(params struct {
ListId int64
Method string
Value string
IpData string
2021-06-23 13:12:33 +08:00
ExpiredAt int64
Reason string
Type string
EventLevel string
Must *actions.Must
CSRF *actionutils.CSRF
}) {
// 校验IPList
if params.ListId != firewallconfigs.GlobalListId {
existsResp, err := this.RPC().IPListRPC().ExistsEnabledIPList(this.AdminContext(), &pb.ExistsEnabledIPListRequest{IpListId: params.ListId})
if err != nil {
this.ErrorPage(err)
return
}
if !existsResp.Exists {
this.Fail("IP名单不存在")
}
2021-06-23 13:12:33 +08:00
}
type ipData struct {
value string
ipFrom string
ipTo string
}
var batchIPs = []*ipData{}
2021-06-23 13:12:33 +08:00
switch params.Type {
case "ip":
if params.Method == "single" {
// 校验IP格式
params.Must.
Field("value", params.Value).
Require("请输入IP或IP段")
_, _, _, ok := utils.ParseIPValue(params.Value)
if !ok {
this.FailField("value", "请输入正确的IP格式")
return
}
} else if params.Method == "batch" {
if len(params.IpData) == 0 {
this.FailField("ipData", "请输入IP")
}
var lines = strings.Split(params.IpData, "\n")
for index, line := range lines {
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
_, ipFrom, ipTo, ok := utils.ParseIPValue(line)
if !ok {
this.FailField("ipData", "第"+types.String(index+1)+"行IP格式错误"+line)
return
}
batchIPs = append(batchIPs, &ipData{
value: line,
ipFrom: ipFrom,
ipTo: ipTo,
})
}
2021-06-23 13:12:33 +08:00
}
case "all":
params.Value = "0.0.0.0"
2021-06-23 13:12:33 +08:00
}
if len(batchIPs) > 0 {
for _, ip := range batchIPs {
_, err := this.RPC().IPItemRPC().CreateIPItem(this.AdminContext(), &pb.CreateIPItemRequest{
IpListId: params.ListId,
Value: ip.value,
IpFrom: ip.ipFrom,
IpTo: ip.ipTo,
ExpiredAt: params.ExpiredAt,
Reason: params.Reason,
Type: params.Type,
EventLevel: params.EventLevel,
})
if err != nil {
this.ErrorPage(err)
return
}
}
2021-06-23 13:12:33 +08:00
// 日志
2023-06-30 18:08:30 +08:00
defer this.CreateLogInfo(codes.IPList_LogCreateIPItemsBatch, params.ListId)
} else {
createResp, err := this.RPC().IPItemRPC().CreateIPItem(this.AdminContext(), &pb.CreateIPItemRequest{
IpListId: params.ListId,
Value: params.Value,
ExpiredAt: params.ExpiredAt,
Reason: params.Reason,
Type: params.Type,
EventLevel: params.EventLevel,
})
if err != nil {
this.ErrorPage(err)
return
}
itemId := createResp.IpItemId
// 日志
2023-06-30 18:08:30 +08:00
defer this.CreateLogInfo(codes.IPItem_LogCreateIPItem, params.ListId, itemId)
}
2021-06-23 13:12:33 +08:00
this.Success()
}