mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-03 23:20:25 +08:00
优化代码
This commit is contained in:
@@ -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 ?`)
|
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"=?`
|
this.updateAccessWeekSQL = `UPDATE "` + this.itemsTableName + `" SET "accessWeek"=? WHERE "hash"=?`
|
||||||
|
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ func (this *FileStorage) UpdatePolicy(newPolicy *serverconfigs.HTTPCachePolicy)
|
|||||||
// open cache
|
// open cache
|
||||||
oldOpenFileCacheJSON, _ := json.Marshal(oldOpenFileCache)
|
oldOpenFileCacheJSON, _ := json.Marshal(oldOpenFileCache)
|
||||||
newOpenFileCacheJSON, _ := json.Marshal(this.options.OpenFileCache)
|
newOpenFileCacheJSON, _ := json.Marshal(this.options.OpenFileCache)
|
||||||
if bytes.Compare(oldOpenFileCacheJSON, newOpenFileCacheJSON) != 0 {
|
if !bytes.Equal(oldOpenFileCacheJSON, newOpenFileCacheJSON) {
|
||||||
this.initOpenFileCache()
|
this.initOpenFileCache()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -520,8 +520,6 @@ func (this *MemoryStorage) flushItem(key string) {
|
|||||||
|
|
||||||
// 从内存中移除
|
// 从内存中移除
|
||||||
_ = this.Delete(key)
|
_ = this.Delete(key)
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *MemoryStorage) memoryCapacityBytes() int64 {
|
func (this *MemoryStorage) memoryCapacityBytes() int64 {
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
package configs
|
|
||||||
|
|
||||||
import "sync"
|
|
||||||
|
|
||||||
var sharedLocker = &sync.RWMutex{}
|
|
||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
var SharedDDoSProtectionManager = NewDDoSProtectionManager()
|
var SharedDDoSProtectionManager = NewDDoSProtectionManager()
|
||||||
|
|
||||||
type DDoSProtectionManager struct {
|
type DDoSProtectionManager struct {
|
||||||
nftPath string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDDoSProtectionManager() *DDoSProtectionManager {
|
func NewDDoSProtectionManager() *DDoSProtectionManager {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
//go:build linux
|
//go:build linux
|
||||||
// +build linux
|
|
||||||
|
|
||||||
package nftables
|
package nftables
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
//go:build linux
|
||||||
|
|
||||||
package nftables
|
package nftables
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
//go:build linux
|
||||||
|
|
||||||
package nftables_test
|
package nftables_test
|
||||||
|
|
||||||
|
|||||||
@@ -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())
|
remotelogs.Error("IPLIBRARY/ACTION_MANAGER", "action "+strconv.FormatInt(newAction.Id, 10)+", type:"+newAction.Type+": "+err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if bytes.Compare(newConfigJSON, oldConfigJSON) != 0 {
|
if !bytes.Equal(newConfigJSON, oldConfigJSON) {
|
||||||
_ = oldInstance.Close()
|
_ = oldInstance.Close()
|
||||||
|
|
||||||
// 重新创建
|
// 重新创建
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -26,8 +25,6 @@ import (
|
|||||||
type ClientConn struct {
|
type ClientConn struct {
|
||||||
BaseClientConn
|
BaseClientConn
|
||||||
|
|
||||||
once sync.Once
|
|
||||||
|
|
||||||
isTLS bool
|
isTLS bool
|
||||||
hasDeadline bool
|
hasDeadline bool
|
||||||
hasRead bool
|
hasRead bool
|
||||||
|
|||||||
@@ -1444,8 +1444,6 @@ func (this *HTTPRequest) Close() {
|
|||||||
_ = conn.Close()
|
_ = conn.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allow 放行
|
// Allow 放行
|
||||||
|
|||||||
@@ -15,9 +15,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Listener struct {
|
type Listener struct {
|
||||||
group *serverconfigs.ServerAddressGroup
|
group *serverconfigs.ServerAddressGroup
|
||||||
isListening bool
|
listener ListenerInterface // 监听器
|
||||||
listener ListenerInterface // 监听器
|
|
||||||
|
|
||||||
locker sync.RWMutex
|
locker sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -182,23 +182,3 @@ func (this *BaseListener) findNamedServerMatched(name string) (serverConfig *ser
|
|||||||
|
|
||||||
return nil, name
|
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)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -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"))
|
|
||||||
}
|
|
||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var prefixReg = regexp.MustCompile(`^\(\?([\w\s]+)\)`) // (?x)
|
var prefixReg = regexp.MustCompile(`^\(\?([\w\s]+)\)`) // (?x)
|
||||||
var prefixReg2 = regexp.MustCompile(`^\(\?([\w\s]*:)`) // (?x: ...
|
|
||||||
var braceZeroReg = regexp.MustCompile(`^{\s*0*\s*}`) // {0}
|
var braceZeroReg = regexp.MustCompile(`^{\s*0*\s*}`) // {0}
|
||||||
var braceZeroReg2 = regexp.MustCompile(`^{\s*0*\s*,`) // {0, x}
|
var braceZeroReg2 = regexp.MustCompile(`^{\s*0*\s*,`) // {0, x}
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,9 @@
|
|||||||
|
|
||||||
package expires
|
package expires
|
||||||
|
|
||||||
import "sync"
|
|
||||||
|
|
||||||
type IdKeyMap struct {
|
type IdKeyMap struct {
|
||||||
idKeys map[int64]string // id => key
|
idKeys map[int64]string // id => key
|
||||||
keyIds map[string]int64 // key => id
|
keyIds map[string]int64 // key => id
|
||||||
|
|
||||||
locker sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIdKeyMap() *IdKeyMap {
|
func NewIdKeyMap() *IdKeyMap {
|
||||||
|
|||||||
@@ -5,12 +5,9 @@ import (
|
|||||||
"github.com/cespare/xxhash"
|
"github.com/cespare/xxhash"
|
||||||
"math"
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ipv4Reg = regexp.MustCompile(`\d+\.`)
|
|
||||||
|
|
||||||
// IP2Long 将IP转换为整型
|
// IP2Long 将IP转换为整型
|
||||||
// 注意IPv6没有顺序
|
// 注意IPv6没有顺序
|
||||||
func IP2Long(ip string) uint64 {
|
func IP2Long(ip string) uint64 {
|
||||||
|
|||||||
@@ -528,7 +528,7 @@ func (this *Rule) Test(value interface{}) bool {
|
|||||||
if ip == nil {
|
if ip == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return this.isIP && bytes.Compare(this.ipValue, ip) == 0
|
return this.isIP && bytes.Equal(this.ipValue, ip)
|
||||||
case RuleOperatorGtIP:
|
case RuleOperatorGtIP:
|
||||||
ip := net.ParseIP(types.String(value))
|
ip := net.ParseIP(types.String(value))
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user