Compare commits

...

8 Commits

Author SHA1 Message Date
刘祥超
0e912b79cd TOA通讯失败时,关闭连接 2021-06-27 17:31:10 +08:00
刘祥超
12f3916e45 ip2region增加IP格式检查 2021-06-27 17:30:45 +08:00
刘祥超
7813e2c3d2 更新TOA 2021-06-24 17:38:29 +08:00
刘祥超
54eff9bfae 删除TOA 2021-06-24 17:29:40 +08:00
刘祥超
635cdd4338 上传TOA编译文件 2021-06-24 16:59:52 +08:00
刘祥超
4c64d3ab0f 实现公用的IP名单 2021-06-23 13:14:37 +08:00
刘祥超
93a5c90fcb 应用网站自定义的WAF出站规则 2021-06-21 15:29:07 +08:00
刘祥超
eb5e863146 变更版本 2021-06-21 14:43:29 +08:00
7 changed files with 74 additions and 44 deletions

View File

@@ -1 +0,0 @@
edge-toa

BIN
build/edge-toa/edge-toa Executable file

Binary file not shown.

View File

@@ -1,7 +1,7 @@
package teaconst package teaconst
const ( const (
Version = "0.2.3" Version = "0.2.4"
ProductName = "Edge Node" ProductName = "Edge Node"
ProcessName = "edge-node" ProcessName = "edge-node"

View File

@@ -5,6 +5,7 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/errors" "github.com/TeaOSLab/EdgeNode/internal/errors"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/lionsoul2014/ip2region/binding/golang/ip2region" "github.com/lionsoul2014/ip2region/binding/golang/ip2region"
"net"
"strings" "strings"
) )
@@ -27,6 +28,9 @@ func (this *IP2RegionLibrary) Lookup(ip string) (*Result, error) {
if strings.Contains(ip, ":") { if strings.Contains(ip, ":") {
return nil, nil return nil, nil
} }
if net.ParseIP(ip) == nil {
return nil, nil
}
if this.db == nil { if this.db == nil {
return nil, errors.New("library has not been loaded") return nil, errors.New("library has not been loaded")

View File

@@ -47,48 +47,55 @@ func (this *HTTPRequest) checkWAFRequest(firewallPolicy *firewallconfigs.HTTPFir
// 检查IP白名单 // 检查IP白名单
remoteAddrs := this.requestRemoteAddrs() remoteAddrs := this.requestRemoteAddrs()
inbound := firewallPolicy.Inbound inbound := firewallPolicy.Inbound
if inbound.AllowListRef != nil && inbound.AllowListRef.IsOn && inbound.AllowListRef.ListId > 0 { if inbound == nil {
list := iplibrary.SharedIPListManager.FindList(inbound.AllowListRef.ListId) return
if list != nil { }
found, _ := list.ContainsIPStrings(remoteAddrs) for _, ref := range inbound.AllAllowListRefs() {
if found { if ref.IsOn && ref.ListId > 0 {
breakChecking = true list := iplibrary.SharedIPListManager.FindList(ref.ListId)
return if list != nil {
found, _ := list.ContainsIPStrings(remoteAddrs)
if found {
breakChecking = true
return
}
} }
} }
} }
// 检查IP黑名单 // 检查IP黑名单
if inbound.DenyListRef != nil && inbound.DenyListRef.IsOn && inbound.DenyListRef.ListId > 0 { for _, ref := range inbound.AllDenyListRefs() {
list := iplibrary.SharedIPListManager.FindList(inbound.DenyListRef.ListId) if ref.IsOn && ref.ListId > 0 {
if list != nil { list := iplibrary.SharedIPListManager.FindList(ref.ListId)
found, item := list.ContainsIPStrings(remoteAddrs) if list != nil {
if found { found, item := list.ContainsIPStrings(remoteAddrs)
// 触发事件 if found {
if item != nil && len(item.EventLevel) > 0 { // 触发事件
actions := iplibrary.SharedActionManager.FindEventActions(item.EventLevel) if item != nil && len(item.EventLevel) > 0 {
for _, action := range actions { actions := iplibrary.SharedActionManager.FindEventActions(item.EventLevel)
goNext, err := action.DoHTTP(this.RawReq, this.RawWriter) for _, action := range actions {
if err != nil { goNext, err := action.DoHTTP(this.RawReq, this.RawWriter)
remotelogs.Error("HTTP_REQUEST_WAF", "do action '"+err.Error()+"' failed: "+err.Error()) if err != nil {
return true, false remotelogs.Error("HTTP_REQUEST_WAF", "do action '"+err.Error()+"' failed: "+err.Error())
} return true, false
if !goNext { }
this.disableLog = true if !goNext {
return true, false this.disableLog = true
return true, false
}
} }
} }
// TODO 需要记录日志信息
this.writer.WriteHeader(http.StatusForbidden)
this.writer.Close()
// 停止日志
this.disableLog = true
return true, false
} }
// TODO 需要记录日志信息
this.writer.WriteHeader(http.StatusForbidden)
this.writer.Close()
// 停止日志
this.disableLog = true
return true, false
} }
} }
} }
@@ -169,7 +176,25 @@ func (this *HTTPRequest) checkWAFRequest(firewallPolicy *firewallconfigs.HTTPFir
// call response waf // call response waf
func (this *HTTPRequest) doWAFResponse(resp *http.Response) (blocked bool) { func (this *HTTPRequest) doWAFResponse(resp *http.Response) (blocked bool) {
firewallPolicy := sharedNodeConfig.HTTPFirewallPolicy // 当前服务的独立设置
if this.web.FirewallPolicy != nil && this.web.FirewallPolicy.IsOn {
blocked := this.checkWAFResponse(this.web.FirewallPolicy, resp)
if blocked {
return true
}
}
// 公用的防火墙设置
if sharedNodeConfig.HTTPFirewallPolicy != nil {
blocked := this.checkWAFResponse(sharedNodeConfig.HTTPFirewallPolicy, resp)
if blocked {
return true
}
}
return
}
func (this *HTTPRequest) checkWAFResponse(firewallPolicy *firewallconfigs.HTTPFirewallPolicy, resp *http.Response) (blocked bool) {
if firewallPolicy == nil || !firewallPolicy.IsOn || !firewallPolicy.Outbound.IsOn { if firewallPolicy == nil || !firewallPolicy.IsOn || !firewallPolicy.Outbound.IsOn {
return return
} }

View File

@@ -86,6 +86,7 @@ func (this *TOAManager) SendMsg(msg string) error {
if this.conn != nil { if this.conn != nil {
_, err := this.conn.Write([]byte(msg + "\n")) _, err := this.conn.Write([]byte(msg + "\n"))
if err != nil { if err != nil {
_ = this.conn.Close()
this.conn = nil this.conn = nil
} }
return err return err

View File

@@ -17,7 +17,7 @@ import (
var SharedHTTPRequestStatManager = NewHTTPRequestStatManager() var SharedHTTPRequestStatManager = NewHTTPRequestStatManager()
// HTTP请求相关的统计 // HTTPRequestStatManager HTTP请求相关的统计
// 这里的统计是一个辅助统计,注意不要因为统计而影响服务工作性能 // 这里的统计是一个辅助统计,注意不要因为统计而影响服务工作性能
type HTTPRequestStatManager struct { type HTTPRequestStatManager struct {
ipChan chan string ipChan chan string
@@ -32,7 +32,7 @@ type HTTPRequestStatManager struct {
dailyFirewallRuleGroupMap map[string]int64 // serverId@firewallRuleGroupId@action => count dailyFirewallRuleGroupMap map[string]int64 // serverId@firewallRuleGroupId@action => count
} }
// 获取新对象 // NewHTTPRequestStatManager 获取新对象
func NewHTTPRequestStatManager() *HTTPRequestStatManager { func NewHTTPRequestStatManager() *HTTPRequestStatManager {
return &HTTPRequestStatManager{ return &HTTPRequestStatManager{
ipChan: make(chan string, 10_000), // TODO 将来可以配置容量 ipChan: make(chan string, 10_000), // TODO 将来可以配置容量
@@ -46,7 +46,7 @@ func NewHTTPRequestStatManager() *HTTPRequestStatManager {
} }
} }
// 启动 // Start 启动
func (this *HTTPRequestStatManager) Start() { func (this *HTTPRequestStatManager) Start() {
loopTicker := time.NewTicker(1 * time.Second) loopTicker := time.NewTicker(1 * time.Second)
uploadTicker := time.NewTicker(30 * time.Minute) uploadTicker := time.NewTicker(30 * time.Minute)
@@ -76,7 +76,7 @@ func (this *HTTPRequestStatManager) Start() {
} }
} }
// 添加客户端地址 // AddRemoteAddr 添加客户端地址
func (this *HTTPRequestStatManager) AddRemoteAddr(serverId int64, remoteAddr string) { func (this *HTTPRequestStatManager) AddRemoteAddr(serverId int64, remoteAddr string) {
if len(remoteAddr) == 0 { if len(remoteAddr) == 0 {
return return
@@ -100,7 +100,7 @@ func (this *HTTPRequestStatManager) AddRemoteAddr(serverId int64, remoteAddr str
} }
} }
// 添加UserAgent // AddUserAgent 添加UserAgent
func (this *HTTPRequestStatManager) AddUserAgent(serverId int64, userAgent string) { func (this *HTTPRequestStatManager) AddUserAgent(serverId int64, userAgent string) {
if len(userAgent) == 0 { if len(userAgent) == 0 {
return return
@@ -113,7 +113,7 @@ func (this *HTTPRequestStatManager) AddUserAgent(serverId int64, userAgent strin
} }
} }
// 添加防火墙拦截动作 // AddFirewallRuleGroupId 添加防火墙拦截动作
func (this *HTTPRequestStatManager) AddFirewallRuleGroupId(serverId int64, firewallRuleGroupId int64, action string) { func (this *HTTPRequestStatManager) AddFirewallRuleGroupId(serverId int64, firewallRuleGroupId int64, action string) {
if firewallRuleGroupId <= 0 { if firewallRuleGroupId <= 0 {
return return
@@ -125,7 +125,7 @@ func (this *HTTPRequestStatManager) AddFirewallRuleGroupId(serverId int64, firew
} }
} }
// 单个循环 // Loop 单个循环
func (this *HTTPRequestStatManager) Loop() error { func (this *HTTPRequestStatManager) Loop() error {
timeout := time.NewTimer(10 * time.Minute) // 执行的最大时间 timeout := time.NewTimer(10 * time.Minute) // 执行的最大时间
userAgentParser := &user_agent.UserAgent{} userAgentParser := &user_agent.UserAgent{}
@@ -189,6 +189,7 @@ Loop:
return nil return nil
} }
// Upload 上传数据
func (this *HTTPRequestStatManager) Upload() error { func (this *HTTPRequestStatManager) Upload() error {
// 上传统计数据 // 上传统计数据
rpcClient, err := rpc.SharedRPC() rpcClient, err := rpc.SharedRPC()