Files
EdgeNode/internal/nodes/http_request_waf.go

172 lines
4.8 KiB
Go
Raw Normal View History

2020-10-08 15:06:42 +08:00
package nodes
import (
2021-01-03 20:18:47 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
2020-10-08 15:06:42 +08:00
"github.com/TeaOSLab/EdgeNode/internal/waf"
"github.com/iwind/TeaGo/lists"
2020-11-02 15:49:30 +08:00
"github.com/iwind/TeaGo/types"
2020-10-08 15:06:42 +08:00
"net/http"
)
// 调用WAF
func (this *HTTPRequest) doWAFRequest() (blocked bool) {
2021-01-03 20:18:47 +08:00
// 当前服务的独立设置
if this.web.FirewallPolicy != nil && this.web.FirewallPolicy.IsOn {
blocked, breakChecking := this.checkWAFRequest(this.web.FirewallPolicy)
2021-01-03 20:18:47 +08:00
if blocked {
return true
}
if breakChecking {
return false
2021-01-03 20:18:47 +08:00
}
}
// 公用的防火墙设置
if sharedNodeConfig.HTTPFirewallPolicy != nil {
blocked, breakChecking := this.checkWAFRequest(sharedNodeConfig.HTTPFirewallPolicy)
2021-01-03 20:18:47 +08:00
if blocked {
return true
}
if breakChecking {
return false
2021-01-03 20:18:47 +08:00
}
}
return
}
func (this *HTTPRequest) checkWAFRequest(firewallPolicy *firewallconfigs.HTTPFirewallPolicy) (blocked bool, breakChecking bool) {
// 检查配置是否为空
if firewallPolicy == nil || !firewallPolicy.IsOn || firewallPolicy.Inbound == nil || !firewallPolicy.Inbound.IsOn {
return
}
// 检查IP白名单
remoteAddr := this.requestRemoteAddr()
inbound := firewallPolicy.Inbound
2021-01-03 20:18:47 +08:00
if inbound.AllowListRef != nil && inbound.AllowListRef.IsOn && inbound.AllowListRef.ListId > 0 {
list := iplibrary.SharedIPListManager.FindList(inbound.AllowListRef.ListId)
if list != nil && list.Contains(iplibrary.IP2Long(remoteAddr)) {
breakChecking = true
return
}
}
// 检查IP黑名单
2021-01-03 20:18:47 +08:00
if inbound.DenyListRef != nil && inbound.DenyListRef.IsOn && inbound.DenyListRef.ListId > 0 {
list := iplibrary.SharedIPListManager.FindList(inbound.DenyListRef.ListId)
if list != nil && list.Contains(iplibrary.IP2Long(remoteAddr)) {
// TODO 可以配置对封禁的处理方式等
// TODO 需要记录日志信息
this.writer.WriteHeader(http.StatusForbidden)
this.writer.Close()
// 停止日志
this.disableLog = true
return true, false
}
}
// 检查地区封禁
if iplibrary.SharedLibrary != nil {
if firewallPolicy.Inbound.Region != nil && firewallPolicy.Inbound.Region.IsOn {
regionConfig := firewallPolicy.Inbound.Region
if regionConfig.IsNotEmpty() {
result, err := iplibrary.SharedLibrary.Lookup(remoteAddr)
if err != nil {
remotelogs.Error("REQUEST", "iplibrary lookup failed: "+err.Error())
} else if result != nil {
// 检查国家级别封禁
if len(regionConfig.DenyCountryIds) > 0 && len(result.Country) > 0 {
countryId := iplibrary.SharedCountryManager.Lookup(result.Country)
if countryId > 0 && lists.ContainsInt64(regionConfig.DenyCountryIds, countryId) {
// TODO 可以配置对封禁的处理方式等
2021-01-21 11:00:46 +08:00
// TODO 需要记录日志信息
this.writer.WriteHeader(http.StatusForbidden)
this.writer.Close()
// 停止日志
this.disableLog = true
return true, false
}
}
// 检查省份封禁
if len(regionConfig.DenyProvinceIds) > 0 && len(result.Province) > 0 {
provinceId := iplibrary.SharedProvinceManager.Lookup(result.Province)
if provinceId > 0 && lists.ContainsInt64(regionConfig.DenyProvinceIds, provinceId) {
// TODO 可以配置对封禁的处理方式等
2021-01-21 11:00:46 +08:00
// TODO 需要记录日志信息
this.writer.WriteHeader(http.StatusForbidden)
this.writer.Close()
// 停止日志
this.disableLog = true
return true, false
}
}
}
}
}
}
// 规则测试
w := sharedWAFManager.FindWAF(firewallPolicy.Id)
2020-10-08 15:06:42 +08:00
if w == nil {
return
}
2020-11-02 15:49:30 +08:00
goNext, ruleGroup, ruleSet, err := w.MatchRequest(this.RawReq, this.writer)
2020-10-08 15:06:42 +08:00
if err != nil {
remotelogs.Error("REQUEST", this.rawURI+": "+err.Error())
2020-10-08 15:06:42 +08:00
return
}
if ruleSet != nil {
if ruleSet.Action != waf.ActionAllow {
this.firewallPolicyId = firewallPolicy.Id
2020-11-02 15:49:30 +08:00
this.firewallRuleGroupId = types.Int64(ruleGroup.Id)
this.firewallRuleSetId = types.Int64(ruleSet.Id)
2020-10-08 15:06:42 +08:00
}
2020-11-02 15:49:30 +08:00
this.logAttrs["waf.action"] = ruleSet.Action
2020-10-08 15:06:42 +08:00
}
return !goNext, false
2020-10-08 15:06:42 +08:00
}
// call response waf
func (this *HTTPRequest) doWAFResponse(resp *http.Response) (blocked bool) {
firewallPolicy := sharedNodeConfig.HTTPFirewallPolicy
if firewallPolicy == nil || !firewallPolicy.IsOn || !firewallPolicy.Outbound.IsOn {
return
}
w := sharedWAFManager.FindWAF(firewallPolicy.Id)
2020-10-08 15:06:42 +08:00
if w == nil {
return
}
2020-11-02 15:49:30 +08:00
goNext, ruleGroup, ruleSet, err := w.MatchResponse(this.RawReq, resp, this.writer)
2020-10-08 15:06:42 +08:00
if err != nil {
remotelogs.Error("REQUEST", this.rawURI+": "+err.Error())
2020-10-08 15:06:42 +08:00
return
}
if ruleSet != nil {
if ruleSet.Action != waf.ActionAllow {
this.firewallPolicyId = firewallPolicy.Id
2020-11-02 15:49:30 +08:00
this.firewallRuleGroupId = types.Int64(ruleGroup.Id)
this.firewallRuleSetId = types.Int64(ruleSet.Id)
2020-10-08 15:06:42 +08:00
}
2020-11-02 15:49:30 +08:00
this.logAttrs["waf.action"] = ruleSet.Action
2020-10-08 15:06:42 +08:00
}
return !goNext
}