From 1fff989ad3082400636cfe078900aac602137947 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sun, 18 Sep 2022 16:18:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/list_file_db.go | 3 ++ internal/caches/storage_file.go | 2 +- internal/caches/storage_memory.go | 2 - internal/configs/locker.go | 5 -- internal/firewalls/ddos_protection_others.go | 1 - internal/firewalls/nftables/conn.go | 1 - internal/firewalls/nftables/set_batch.go | 1 + internal/firewalls/nftables/set_test.go | 1 + internal/iplibrary/action_manager.go | 2 +- internal/nodes/client_conn.go | 3 -- internal/nodes/http_request.go | 2 - internal/nodes/listener.go | 5 +- internal/nodes/listener_base.go | 20 -------- internal/nodes/server_cname_manager.go | 48 -------------------- internal/nodes/server_cname_manager_test.go | 19 -------- internal/re/regexp.go | 1 - internal/utils/expires/id_key_map.go | 4 -- internal/utils/ip.go | 3 -- internal/waf/rule.go | 2 +- 19 files changed, 10 insertions(+), 115 deletions(-) delete mode 100644 internal/configs/locker.go delete mode 100644 internal/nodes/server_cname_manager.go delete mode 100644 internal/nodes/server_cname_manager_test.go diff --git a/internal/caches/list_file_db.go b/internal/caches/list_file_db.go index 86038ab..a371849 100644 --- a/internal/caches/list_file_db.go +++ b/internal/caches/list_file_db.go @@ -200,6 +200,9 @@ func (this *FileListDB) Init() error { } this.listOlderItemsStmt, err = this.readDB.Prepare(`SELECT "hash" FROM "` + this.itemsTableName + `" ORDER BY "accessWeek" ASC, "id" ASC LIMIT ?`) + if err != nil { + return err + } this.updateAccessWeekSQL = `UPDATE "` + this.itemsTableName + `" SET "accessWeek"=? WHERE "hash"=?` diff --git a/internal/caches/storage_file.go b/internal/caches/storage_file.go index e66e6e9..4c29b27 100644 --- a/internal/caches/storage_file.go +++ b/internal/caches/storage_file.go @@ -179,7 +179,7 @@ func (this *FileStorage) UpdatePolicy(newPolicy *serverconfigs.HTTPCachePolicy) // open cache oldOpenFileCacheJSON, _ := json.Marshal(oldOpenFileCache) newOpenFileCacheJSON, _ := json.Marshal(this.options.OpenFileCache) - if bytes.Compare(oldOpenFileCacheJSON, newOpenFileCacheJSON) != 0 { + if !bytes.Equal(oldOpenFileCacheJSON, newOpenFileCacheJSON) { this.initOpenFileCache() } diff --git a/internal/caches/storage_memory.go b/internal/caches/storage_memory.go index 226867d..f0acc6c 100644 --- a/internal/caches/storage_memory.go +++ b/internal/caches/storage_memory.go @@ -520,8 +520,6 @@ func (this *MemoryStorage) flushItem(key string) { // 从内存中移除 _ = this.Delete(key) - - return } func (this *MemoryStorage) memoryCapacityBytes() int64 { diff --git a/internal/configs/locker.go b/internal/configs/locker.go deleted file mode 100644 index 5768a10..0000000 --- a/internal/configs/locker.go +++ /dev/null @@ -1,5 +0,0 @@ -package configs - -import "sync" - -var sharedLocker = &sync.RWMutex{} diff --git a/internal/firewalls/ddos_protection_others.go b/internal/firewalls/ddos_protection_others.go index 8f3afb5..ce53b8a 100644 --- a/internal/firewalls/ddos_protection_others.go +++ b/internal/firewalls/ddos_protection_others.go @@ -11,7 +11,6 @@ import ( var SharedDDoSProtectionManager = NewDDoSProtectionManager() type DDoSProtectionManager struct { - nftPath string } func NewDDoSProtectionManager() *DDoSProtectionManager { diff --git a/internal/firewalls/nftables/conn.go b/internal/firewalls/nftables/conn.go index d859ee4..d2e63e7 100644 --- a/internal/firewalls/nftables/conn.go +++ b/internal/firewalls/nftables/conn.go @@ -1,6 +1,5 @@ // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. //go:build linux -// +build linux package nftables diff --git a/internal/firewalls/nftables/set_batch.go b/internal/firewalls/nftables/set_batch.go index c296561..499322a 100644 --- a/internal/firewalls/nftables/set_batch.go +++ b/internal/firewalls/nftables/set_batch.go @@ -1,4 +1,5 @@ // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. +//go:build linux package nftables diff --git a/internal/firewalls/nftables/set_test.go b/internal/firewalls/nftables/set_test.go index baa7c59..47b969d 100644 --- a/internal/firewalls/nftables/set_test.go +++ b/internal/firewalls/nftables/set_test.go @@ -1,4 +1,5 @@ // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. +//go:build linux package nftables_test diff --git a/internal/iplibrary/action_manager.go b/internal/iplibrary/action_manager.go index 6c6b20d..86e6a51 100644 --- a/internal/iplibrary/action_manager.go +++ b/internal/iplibrary/action_manager.go @@ -68,7 +68,7 @@ func (this *ActionManager) UpdateActions(actions []*firewallconfigs.FirewallActi remotelogs.Error("IPLIBRARY/ACTION_MANAGER", "action "+strconv.FormatInt(newAction.Id, 10)+", type:"+newAction.Type+": "+err.Error()) continue } - if bytes.Compare(newConfigJSON, oldConfigJSON) != 0 { + if !bytes.Equal(newConfigJSON, oldConfigJSON) { _ = oldInstance.Close() // 重新创建 diff --git a/internal/nodes/client_conn.go b/internal/nodes/client_conn.go index fe0b83a..e4e6fae 100644 --- a/internal/nodes/client_conn.go +++ b/internal/nodes/client_conn.go @@ -17,7 +17,6 @@ import ( "net" "os" "strings" - "sync" "sync/atomic" "time" ) @@ -26,8 +25,6 @@ import ( type ClientConn struct { BaseClientConn - once sync.Once - isTLS bool hasDeadline bool hasRead bool diff --git a/internal/nodes/http_request.go b/internal/nodes/http_request.go index d24ad93..bea8cbb 100644 --- a/internal/nodes/http_request.go +++ b/internal/nodes/http_request.go @@ -1444,8 +1444,6 @@ func (this *HTTPRequest) Close() { _ = conn.Close() return } - - return } // Allow 放行 diff --git a/internal/nodes/listener.go b/internal/nodes/listener.go index de1021d..e0115b4 100644 --- a/internal/nodes/listener.go +++ b/internal/nodes/listener.go @@ -15,9 +15,8 @@ import ( ) type Listener struct { - group *serverconfigs.ServerAddressGroup - isListening bool - listener ListenerInterface // 监听器 + group *serverconfigs.ServerAddressGroup + listener ListenerInterface // 监听器 locker sync.RWMutex } diff --git a/internal/nodes/listener_base.go b/internal/nodes/listener_base.go index 8563d36..a48c75c 100644 --- a/internal/nodes/listener_base.go +++ b/internal/nodes/listener_base.go @@ -182,23 +182,3 @@ func (this *BaseListener) findNamedServerMatched(name string) (serverConfig *ser return nil, name } - -// 使用CNAME来查找服务 -// TODO 防止单IP随机生成域名攻击 -func (this *BaseListener) findServerWithCNAME(domain string) *serverconfigs.ServerConfig { - if !sharedNodeConfig.SupportCNAME { - return nil - } - - var realName = sharedCNAMEManager.Lookup(domain) - if len(realName) == 0 { - return nil - } - - group := this.Group - if group == nil { - return nil - } - - return group.MatchServerCNAME(realName) -} diff --git a/internal/nodes/server_cname_manager.go b/internal/nodes/server_cname_manager.go deleted file mode 100644 index 1e5790c..0000000 --- a/internal/nodes/server_cname_manager.go +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. - -package nodes - -import ( - "github.com/TeaOSLab/EdgeNode/internal/ttlcache" - "github.com/TeaOSLab/EdgeNode/internal/utils" - "github.com/iwind/TeaGo/types" - "strings" - "sync" - "time" -) - -var sharedCNAMEManager = NewServerCNAMEManager() - -// ServerCNAMEManager 服务CNAME管理 -// TODO 需要自动更新缓存里的记录 -type ServerCNAMEManager struct { - ttlCache *ttlcache.Cache - - locker sync.Mutex -} - -func NewServerCNAMEManager() *ServerCNAMEManager { - return &ServerCNAMEManager{ - ttlCache: ttlcache.NewCache(), - } -} - -func (this *ServerCNAMEManager) Lookup(domain string) string { - if len(domain) == 0 { - return "" - } - - var item = this.ttlCache.Read(domain) - if item != nil { - return types.String(item.Value) - } - - cname, _ := utils.LookupCNAME(domain) - if len(cname) > 0 { - cname = strings.TrimSuffix(cname, ".") - } - - this.ttlCache.Write(domain, cname, time.Now().Unix()+600) - - return cname -} diff --git a/internal/nodes/server_cname_manager_test.go b/internal/nodes/server_cname_manager_test.go deleted file mode 100644 index 8af805c..0000000 --- a/internal/nodes/server_cname_manager_test.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. - -package nodes - -import ( - "testing" - "time" -) - -func TestServerCNameManager_Lookup(t *testing.T) { - var cnameManager = NewServerCNAMEManager() - t.Log(cnameManager.Lookup("www.yun4s.cn")) - - var before = time.Now() - defer func() { - t.Log(time.Since(before).Seconds()*1000, "ms") - }() - t.Log(cnameManager.Lookup("www.yun4s.cn")) -} diff --git a/internal/re/regexp.go b/internal/re/regexp.go index 6ce892e..a42e9f0 100644 --- a/internal/re/regexp.go +++ b/internal/re/regexp.go @@ -11,7 +11,6 @@ import ( ) var prefixReg = regexp.MustCompile(`^\(\?([\w\s]+)\)`) // (?x) -var prefixReg2 = regexp.MustCompile(`^\(\?([\w\s]*:)`) // (?x: ... var braceZeroReg = regexp.MustCompile(`^{\s*0*\s*}`) // {0} var braceZeroReg2 = regexp.MustCompile(`^{\s*0*\s*,`) // {0, x} diff --git a/internal/utils/expires/id_key_map.go b/internal/utils/expires/id_key_map.go index 6448acd..2001bba 100644 --- a/internal/utils/expires/id_key_map.go +++ b/internal/utils/expires/id_key_map.go @@ -2,13 +2,9 @@ package expires -import "sync" - type IdKeyMap struct { idKeys map[int64]string // id => key keyIds map[string]int64 // key => id - - locker sync.Mutex } func NewIdKeyMap() *IdKeyMap { diff --git a/internal/utils/ip.go b/internal/utils/ip.go index 35aa56d..2f9c300 100644 --- a/internal/utils/ip.go +++ b/internal/utils/ip.go @@ -5,12 +5,9 @@ import ( "github.com/cespare/xxhash" "math" "net" - "regexp" "strings" ) -var ipv4Reg = regexp.MustCompile(`\d+\.`) - // IP2Long 将IP转换为整型 // 注意IPv6没有顺序 func IP2Long(ip string) uint64 { diff --git a/internal/waf/rule.go b/internal/waf/rule.go index 8ece2d5..28c3531 100644 --- a/internal/waf/rule.go +++ b/internal/waf/rule.go @@ -528,7 +528,7 @@ func (this *Rule) Test(value interface{}) bool { if ip == nil { return false } - return this.isIP && bytes.Compare(this.ipValue, ip) == 0 + return this.isIP && bytes.Equal(this.ipValue, ip) case RuleOperatorGtIP: ip := net.ParseIP(types.String(value)) if ip == nil {