-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule_static.go
233 lines (213 loc) · 8.27 KB
/
rule_static.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package model
import (
"regexp"
"strconv"
"strings"
"github.com/admpub/nging/v5/application/library/common"
"github.com/webx-top/com"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
"github.com/webx-top/echo/param"
"github.com/nging-plugins/firewallmanager/application/dbschema"
"github.com/nging-plugins/firewallmanager/application/library/driver"
"github.com/nging-plugins/firewallmanager/application/library/enums"
"github.com/nging-plugins/firewallmanager/application/library/netutils"
)
func NewRuleStatic(ctx echo.Context) *RuleStatic {
return &RuleStatic{
NgingFirewallRuleStatic: dbschema.NewNgingFirewallRuleStatic(ctx),
}
}
type RuleStatic struct {
*dbschema.NgingFirewallRuleStatic
}
var rateLimitRegex = regexp.MustCompile(`^[\d]+[+]?/[pb]/[smhd]$`)
var connLimitRegex = regexp.MustCompile(`^[\d]+[+]?$`)
func MatchRateLimit(rateLimit string) bool {
return rateLimitRegex.MatchString(rateLimit)
}
func MatchConnLimit(connLimit string) bool {
return connLimitRegex.MatchString(connLimit)
}
func RemovePrefixNeq(s string) string {
return strings.TrimPrefix(s, `!`)
}
func (r *RuleStatic) check() error {
ctx := r.Context()
if !enums.Types.Has(r.Type) {
return ctx.NewError(code.InvalidParameter, `类型无效`).SetZone(`type`)
}
if !com.InSlice(r.Direction, enums.TablesChains[r.Type]) {
return ctx.NewError(code.InvalidParameter, `类型“%v”不支持设置“%s”规则`, r.Type, ctx.T(enums.Directions.Get(r.Direction))).SetZone(`direction`)
}
if !enums.IPProtocols.Has(r.IpVersion) {
return ctx.NewError(code.InvalidParameter, `IP版本值“%s”无效`, r.IpVersion).SetZone(`ipVersion`)
}
if len(r.State) > 0 {
states := strings.Split(r.State, `,`)
for _, state := range states {
if !com.InSlice(state, enums.StateList) {
return ctx.NewError(code.InvalidParameter, `网络连接状态值“%s”无效`, r.State).SetZone(`state`)
}
}
}
if len(r.ConnLimit) > 0 && !MatchConnLimit(r.ConnLimit) {
return ctx.NewError(code.InvalidParameter, `连接限制“%s”格式无效`, r.ConnLimit).SetZone(`connLimit`)
}
if len(r.RateLimit) > 0 {
if !MatchRateLimit(r.RateLimit) {
return ctx.NewError(code.InvalidParameter, `频率限制规则“%s”格式无效`, r.RateLimit).SetZone(`rateLimit`)
}
if r.RateBurst == 0 {
return ctx.NewError(code.InvalidParameter, `您设置了“频率限制”规则,必须同时设置“频率峰值”`).SetZone(`rateBurst`)
}
rateLimit := strings.SplitN(r.RateLimit, `/`, 2)[0]
rateLimit = strings.TrimSuffix(rateLimit, `+`)
limit, err := strconv.ParseUint(rateLimit, 10, 64)
if err != nil {
return ctx.NewError(code.InvalidParameter, `“频率限制”规则中的“限制数量”(%v)不是有效的数字`, r.RateLimit).SetZone(`rateLimit`)
}
if uint64(r.RateBurst) < limit {
return ctx.NewError(code.InvalidParameter, `“频率峰值”(%v)不可小于“频率限制”规则中的“限制数量”(%v)`, r.RateBurst, r.RateLimit).SetZone(`rateBurst`)
}
} else if r.RateBurst > 0 {
return ctx.NewError(code.InvalidParameter, `您设置了“频率峰值”,必须同时设置“频率限制”规则`).SetZone(`rateLimit`)
}
if r.Type != enums.TableNAT {
if !enums.Actions.Has(r.Action) {
return ctx.NewError(code.InvalidParameter, `操作值“%s”无效`, r.Action).SetZone(`action`)
}
if len(r.NatIp) > 0 {
r.NatIp = ``
}
if len(r.NatPort) > 0 {
r.NatPort = ``
}
} else {
if len(r.Action) > 0 && !enums.Actions.Has(r.Action) {
return ctx.NewError(code.InvalidParameter, `操作值“%s”无效`, r.Action).SetZone(`action`)
}
switch r.Direction {
case enums.ChainPreRouting:
if len(r.NatIp) == 0 && len(r.NatPort) == 0 {
return ctx.NewError(code.InvalidParameter, `NAT IP 和 NAT 端口 不能同时为空`).SetZone(`natPort`)
}
case enums.ChainPostRouting:
}
}
if len(r.Protocol) > 0 && !com.InSlice(r.Protocol, enums.ProtocolList) {
return ctx.NewError(code.InvalidParameter, `网络协议值“%s”无效`, r.Protocol).SetZone(`protocol`)
}
if (len(r.Protocol) == 0 || r.Protocol == enums.ProtocolAll) && (len(r.LocalPort) > 0 || len(r.RemotePort) > 0 || len(r.NatPort) > 0) {
return ctx.NewError(code.InvalidParameter, `当指定了端口时,必须明确的指定网络协议`).SetZone(`protocol`)
}
if len(r.LocalPort) > 0 {
localPort := RemovePrefixNeq(r.LocalPort)
ports := param.Split(localPort, `,`).Unique().Filter().String()
for _, port := range ports {
if err := netutils.ValidatePort(ctx, port); err != nil {
return ctx.NewError(code.InvalidParameter, `本机%v`, err.Error()).SetZone(`localPort`)
}
}
}
if len(r.RemotePort) > 0 {
remotePort := RemovePrefixNeq(r.RemotePort)
ports := param.Split(remotePort, `,`).Unique().Filter().String()
for _, port := range ports {
if err := netutils.ValidatePort(ctx, port); err != nil {
return ctx.NewError(code.InvalidParameter, `远程%v`, err.Error()).SetZone(`remotePort`)
}
}
}
if len(r.NatPort) > 0 {
if err := netutils.ValidatePort(ctx, r.NatPort); err != nil {
return ctx.NewError(code.InvalidParameter, `NAT %v`, err.Error()).SetZone(`natPort`)
}
}
if len(r.LocalIp) > 0 {
localIP := RemovePrefixNeq(r.LocalIp)
ips := param.Split(localIP, `,`).Unique().Filter().String()
for _, ip := range ips {
if _, err := netutils.ValidateIP(ctx, ip); err != nil {
return ctx.NewError(code.InvalidParameter, `本机%v`, err.Error()).SetZone(`localIp`)
}
}
}
if len(r.RemoteIp) > 0 {
remoteIP := RemovePrefixNeq(r.RemoteIp)
ips := param.Split(remoteIP, `,`).Unique().Filter().String()
for _, ip := range ips {
if _, err := netutils.ValidateIP(ctx, ip); err != nil {
return ctx.NewError(code.InvalidParameter, `远程%v`, err.Error()).SetZone(`remoteIp`)
}
}
}
if len(r.NatIp) > 0 {
if _, err := netutils.ValidateIP(ctx, r.NatIp); err != nil {
return ctx.NewError(code.InvalidParameter, `NAT %v`, err.Error()).SetZone(`natIp`)
}
}
r.Disabled = common.GetBoolFlag(r.Disabled)
return nil
}
func (r *RuleStatic) Add() (interface{}, error) {
if err := r.check(); err != nil {
return nil, err
}
return r.NgingFirewallRuleStatic.Insert()
}
func (r *RuleStatic) Edit(mw func(db.Result) db.Result, args ...interface{}) error {
if err := r.check(); err != nil {
return err
}
return r.NgingFirewallRuleStatic.Update(mw, args...)
}
func (r *RuleStatic) ListPage(cond *db.Compounds, sorts ...interface{}) ([]*dbschema.NgingFirewallRuleStatic, error) {
err := r.NgingFirewallRuleStatic.ListPage(cond, sorts...)
if err != nil {
return nil, err
}
return r.Objects(), nil
}
func (r *RuleStatic) AsRule(row ...*dbschema.NgingFirewallRuleStatic) driver.Rule {
m := r.NgingFirewallRuleStatic
if len(row) > 0 && row[0] != nil {
m = row[0]
}
return AsRule(m)
}
func (r *RuleStatic) NextRow(table string, chain string, ipVer string, position int, id uint, excludeOther ...uint) (*dbschema.NgingFirewallRuleStatic, error) {
row := dbschema.NewNgingFirewallRuleStatic(r.Context())
cond := db.NewCompounds()
cond.Add(db.Cond{`type`: table})
cond.Add(db.Cond{`direction`: chain})
cond.Add(db.Cond{`disabled`: `N`})
cond.Add(db.Cond{`ip_version`: ipVer})
exclude := make([]uint, 0, len(excludeOther)+1)
exclude = append(exclude, id)
exclude = append(exclude, excludeOther...)
cond.Add(db.Cond{`id`: db.NotIn(exclude)})
cond.Add(db.Or(
db.And(db.Cond{`position`: position}, db.Cond{`id`: db.Gt(id)}),
db.And(db.Cond{`position`: db.Gt(position)}),
))
err := row.Get(func(r db.Result) db.Result {
return r.OrderBy(`position`, `id`)
}, cond.And())
return row, err
}