-
Notifications
You must be signed in to change notification settings - Fork 4
/
createIPPopup.go
160 lines (143 loc) · 3.6 KB
/
createIPPopup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package iplists
import (
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"strings"
)
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,
}
this.Show()
}
func (this *CreateIPPopupAction) RunPost(params struct {
ListId int64
Method string
Value string
IpData string
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名单不存在")
}
}
type ipData struct {
value string
ipFrom string
ipTo string
}
var batchIPs = []*ipData{}
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,
})
}
}
case "all":
params.Value = "0.0.0.0"
}
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
}
}
// 日志
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
// 日志
defer this.CreateLogInfo(codes.IPItem_LogCreateIPItem, params.ListId, itemId)
}
this.Success()
}