增加IP级别和WAF动作相关接口和配置

This commit is contained in:
刘祥超
2021-02-06 17:37:27 +08:00
parent 310872f1d6
commit 6b2cbc2939
12 changed files with 1529 additions and 32 deletions

View File

@@ -0,0 +1,16 @@
package firewallconfigs
import "github.com/iwind/TeaGo/maps"
// 防火墙动作配置
type FirewallActionConfig struct {
Id int64 `yaml:"id" json:"id"` // Id
Type string `yaml:"type" json:"type"` // 类型
Params maps.Map `yaml:"params" json:"params"` // 参数
EventLevel string `yaml:"eventLevel" json:"eventLevel"` // 事件级别
}
// 初始化
func (this *FirewallActionConfig) Init() error {
return nil
}

View File

@@ -0,0 +1,95 @@
package firewallconfigs
type FirewallActionType = string
const (
FirewallActionTypeIPSet FirewallActionType = "ipset"
FirewallActionTypeFirewalld FirewallActionType = "firewalld"
FirewallActionTypeIPTables FirewallActionType = "iptables"
FirewallActionTypeScript FirewallActionType = "script"
FirewallActionTypeHTTPAPI FirewallActionType = "httpAPI"
)
type FirewallActionTypeDefinition struct {
Name string `json:"name"`
Code FirewallActionType `json:"code"`
Description string `json:"description"`
}
func FindAllFirewallActionTypes() []*FirewallActionTypeDefinition {
return []*FirewallActionTypeDefinition{
{
Name: "ipset",
Code: FirewallActionTypeIPSet,
Description: "使用特定的ipset管理IP可以结合iptables和firewalld等工具一起工作。",
},
{
Name: "firewalld",
Code: FirewallActionTypeFirewalld,
Description: "使用Firewalld管理IP非持久保存reload之后重置规则。",
},
{
Name: "iptables",
Code: FirewallActionTypeIPTables,
Description: "使用IPTables管理IP不支持超时时间设定非持久保存reload之后重置规则。",
},
{
Name: "自定义脚本",
Code: FirewallActionTypeScript,
Description: "使用自定义的脚本执行IP操作。",
},
{
Name: "自定义HTTP API",
Code: FirewallActionTypeHTTPAPI,
Description: "使用自定义的HTTP API执行IP操作。",
},
}
}
func FindFirewallActionTypeName(actionType FirewallActionType) string {
for _, a := range FindAllFirewallActionTypes() {
if a.Code == actionType {
return a.Name
}
}
return ""
}
type FirewallActionIPSetConfig struct {
Path string `json:"path"` // 命令路径 TODO 暂时不实现
WhiteName string `json:"whiteName"` // IPSet白名单名称
BlackName string `json:"blackName"` // IPSet黑名单名称
MaxElements int `json:"maxElements"` // 最多IP数量 TODO 暂时不实现
AutoAddToIPTables bool `json:"autoAddToIPTables"` // 是否自动创建IPTables规则
AutoAddToFirewalld bool `json:"autoAddToFirewalld"` // 是否自动加入到Firewalld
// TODO 添加需要阻止的端口列表
}
type FirewallActionFirewalldConfig struct {
Path string `json:"path"` // 命令路径 TODO 暂时不实现
// TODO 添加需要阻止的端口列表
}
type FirewallActionIPTablesConfig struct {
Path string `json:"path"` // 命令路径 TODO 暂时不实现
// TODO 添加需要阻止的端口列表
}
type FirewallActionScriptConfig struct {
Path string `json:"path"` // 脚本路径
Cwd string `json:"cwd"` // 工作目录 TODO 暂时不实现
Args []string `json:"args"` // 附加参数 TODO 暂时不实现
// TODO 添加需要阻止的端口列表
}
type FirewallActionHTTPAPIConfig struct {
URL string `json:"url"` // URL路径
TimeoutSeconds int `json:"timeoutSeconds"` // 超时时间 TODO 暂时不实现
Secret string `json:"secret"` // 认证密钥 TODO 暂时不实现
// TODO 添加需要阻止的端口列表
}

View File

@@ -0,0 +1,51 @@
package firewallconfigs
type FirewallEventLevelDefinition struct {
Name string `json:"name"`
Code string `json:"code"`
Description string `json:"description"`
}
func FindAllFirewallEventLevels() []*FirewallEventLevelDefinition {
return []*FirewallEventLevelDefinition{
{
Name: "调试",
Code: "debug",
Description: "仅作为调试用途",
},
{
Name: "通知",
Code: "notice",
Description: "需要通知的事件",
},
{
Name: "警告",
Code: "warning",
Description: "需要警告的事件",
},
{
Name: "错误",
Code: "error",
Description: "发生系统错误的事件",
},
{
Name: "严重",
Code: "critical",
Description: "性质较为严重的事件",
},
{
Name: "致命",
Code: "fatal",
Description: "对系统有重大影响的事件",
},
}
}
func FindFirewallEventLevelName(code string) string {
for _, level := range FindAllFirewallEventLevels() {
if level.Code == code {
return level.Name
}
}
return ""
}