2020-10-06 21:02:21 +08:00
|
|
|
package firewallconfigs
|
|
|
|
|
|
2020-10-07 11:18:24 +08:00
|
|
|
import "encoding/json"
|
|
|
|
|
|
2023-12-05 16:53:43 +08:00
|
|
|
const DefaultMaxRequestBodySize int64 = 256 << 10
|
2023-08-02 17:00:40 +08:00
|
|
|
|
2021-07-14 22:46:31 +08:00
|
|
|
// HTTPFirewallPolicy 防火墙策略
|
2020-10-06 21:02:21 +08:00
|
|
|
type HTTPFirewallPolicy struct {
|
2023-08-02 17:00:40 +08:00
|
|
|
Id int64 `yaml:"id" json:"id"`
|
|
|
|
|
IsOn bool `yaml:"isOn" json:"isOn"`
|
|
|
|
|
Name string `yaml:"name" json:"name"`
|
|
|
|
|
Description string `yaml:"description" json:"description"`
|
|
|
|
|
Inbound *HTTPFirewallInboundConfig `yaml:"inbound" json:"inbound"`
|
|
|
|
|
Outbound *HTTPFirewallOutboundConfig `yaml:"outbound" json:"outbound"`
|
|
|
|
|
BlockOptions *HTTPFirewallBlockAction `yaml:"blockOptions" json:"blockOptions"`
|
|
|
|
|
CaptchaOptions *HTTPFirewallCaptchaAction `yaml:"captchaOptions" json:"captchaOptions"`
|
|
|
|
|
Mode FirewallMode `yaml:"mode" json:"mode"`
|
|
|
|
|
UseLocalFirewall bool `yaml:"useLocalFirewall" json:"useLocalFirewall"`
|
|
|
|
|
SYNFlood *SYNFloodConfig `yaml:"synFlood" json:"synFlood"`
|
|
|
|
|
Log *HTTPFirewallPolicyLogConfig `yaml:"log" json:"log"` // 强制记录日志
|
|
|
|
|
MaxRequestBodySize int64 `yaml:"maxRequestBodySize" json:"maxRequestBodySize"` // 读取的请求最大尺寸
|
2023-08-10 10:30:05 +08:00
|
|
|
DenyCountryHTML string `yaml:"denyCountryHTML" json:"denyCountryHTML"` // 默认地区禁用提示
|
|
|
|
|
DenyProvinceHTML string `yaml:"denyProvinceHTML" json:"denyProvinceHTML"` // 默认省份禁用提示
|
2020-10-06 21:02:21 +08:00
|
|
|
}
|
|
|
|
|
|
2021-07-14 22:46:31 +08:00
|
|
|
// Init 初始化
|
2020-10-06 21:02:21 +08:00
|
|
|
func (this *HTTPFirewallPolicy) Init() error {
|
|
|
|
|
if this.Inbound != nil {
|
|
|
|
|
err := this.Inbound.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-10 19:54:18 +08:00
|
|
|
|
2020-10-06 21:02:21 +08:00
|
|
|
if this.Outbound != nil {
|
|
|
|
|
err := this.Outbound.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 19:54:18 +08:00
|
|
|
if this.SYNFlood != nil {
|
|
|
|
|
err := this.SYNFlood.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 09:38:56 +08:00
|
|
|
if this.Log != nil {
|
|
|
|
|
err := this.Log.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-06 21:02:21 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-14 22:46:31 +08:00
|
|
|
// AllRuleGroups 获取所有分组
|
2020-10-06 21:02:21 +08:00
|
|
|
func (this *HTTPFirewallPolicy) AllRuleGroups() []*HTTPFirewallRuleGroup {
|
|
|
|
|
result := []*HTTPFirewallRuleGroup{}
|
|
|
|
|
if this.Inbound != nil {
|
|
|
|
|
result = append(result, this.Inbound.Groups...)
|
|
|
|
|
}
|
|
|
|
|
if this.Outbound != nil {
|
|
|
|
|
result = append(result, this.Outbound.Groups...)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-14 22:46:31 +08:00
|
|
|
// FindRuleGroupWithCode 根据代号查找分组
|
2020-10-06 21:02:21 +08:00
|
|
|
func (this *HTTPFirewallPolicy) FindRuleGroupWithCode(code string) *HTTPFirewallRuleGroup {
|
|
|
|
|
for _, g := range this.AllRuleGroups() {
|
|
|
|
|
if g.Code == code {
|
|
|
|
|
return g
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-10-07 11:18:24 +08:00
|
|
|
|
2021-12-12 20:24:41 +08:00
|
|
|
// FindRuleGroupWithName 根据名称查找分组
|
|
|
|
|
func (this *HTTPFirewallPolicy) FindRuleGroupWithName(name string) *HTTPFirewallRuleGroup {
|
|
|
|
|
for _, g := range this.AllRuleGroups() {
|
|
|
|
|
if g.Name == name {
|
|
|
|
|
return g
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-14 22:46:31 +08:00
|
|
|
// FindRuleGroup 根据ID查找分组
|
2020-10-08 11:11:29 +08:00
|
|
|
func (this *HTTPFirewallPolicy) FindRuleGroup(groupId int64) *HTTPFirewallRuleGroup {
|
|
|
|
|
for _, g := range this.AllRuleGroups() {
|
|
|
|
|
if g.Id == groupId {
|
|
|
|
|
return g
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-14 22:46:31 +08:00
|
|
|
// RemoveRuleGroup 删除某个分组
|
2020-10-07 11:18:24 +08:00
|
|
|
func (this *HTTPFirewallPolicy) RemoveRuleGroup(groupId int64) {
|
|
|
|
|
if this.Inbound != nil {
|
|
|
|
|
this.Inbound.RemoveRuleGroup(groupId)
|
|
|
|
|
}
|
|
|
|
|
if this.Outbound != nil {
|
|
|
|
|
this.Outbound.RemoveRuleGroup(groupId)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-14 22:46:31 +08:00
|
|
|
// InboundJSON Inbound JSON
|
2020-10-07 11:18:24 +08:00
|
|
|
func (this *HTTPFirewallPolicy) InboundJSON() ([]byte, error) {
|
|
|
|
|
if this.Inbound == nil {
|
|
|
|
|
return []byte("null"), nil
|
|
|
|
|
}
|
|
|
|
|
groups := this.Inbound.Groups
|
|
|
|
|
this.Inbound.Groups = nil
|
|
|
|
|
defer func() {
|
|
|
|
|
this.Inbound.Groups = groups
|
|
|
|
|
}()
|
|
|
|
|
return json.Marshal(this.Inbound)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-14 22:46:31 +08:00
|
|
|
// OutboundJSON Outbound JSON
|
2020-10-07 11:18:24 +08:00
|
|
|
func (this *HTTPFirewallPolicy) OutboundJSON() ([]byte, error) {
|
|
|
|
|
if this.Inbound == nil {
|
|
|
|
|
return []byte("null"), nil
|
|
|
|
|
}
|
|
|
|
|
groups := this.Outbound.Groups
|
|
|
|
|
this.Outbound.Groups = nil
|
|
|
|
|
defer func() {
|
|
|
|
|
this.Outbound.Groups = groups
|
|
|
|
|
}()
|
|
|
|
|
return json.Marshal(this.Outbound)
|
|
|
|
|
}
|