From fb9e9fb94b0bf600a48e6eb6a1ea55a4347e8e85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sun, 14 Nov 2021 20:34:04 +0800 Subject: [PATCH] =?UTF-8?q?IP=E5=90=8D=E5=8D=95=E5=90=8C=E6=AD=A5=E6=97=B6?= =?UTF-8?q?=E5=9C=A8=E6=9C=AC=E5=9C=B0=E8=AE=B0=E5=BD=95=E4=B8=8A=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E5=90=8C=E6=AD=A5=E7=9A=84=E4=BD=8D=E7=BD=AE=EF=BC=8C?= =?UTF-8?q?=E4=BB=A5=E4=BE=BF=E4=BA=8E=E4=B8=8B=E6=AC=A1=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E4=B8=8D=E5=86=8D=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/iplibrary/manager_ip_list.go | 38 ++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/internal/iplibrary/manager_ip_list.go b/internal/iplibrary/manager_ip_list.go index beaab20..62d6d62 100644 --- a/internal/iplibrary/manager_ip_list.go +++ b/internal/iplibrary/manager_ip_list.go @@ -8,6 +8,9 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/waf" "github.com/iwind/TeaGo/Tea" + "github.com/iwind/TeaGo/types" + "io/ioutil" + "os" "sync" "time" ) @@ -21,6 +24,8 @@ func init() { }) } +var versionCacheFile = "ip_list_version.cache" + // IPListManager IP名单管理 type IPListManager struct { // 缓存文件 @@ -37,7 +42,7 @@ type IPListManager struct { func NewIPListManager() *IPListManager { return &IPListManager{ cacheFile: Tea.Root + "/configs/ip_list.cache", - pageSize: 1000, + pageSize: 500, listMap: map[int64]*IPList{}, } } @@ -45,6 +50,9 @@ func NewIPListManager() *IPListManager { func (this *IPListManager) Start() { // TODO 从缓存当中读取数据 + // 从缓存中读取位置 + this.version = this.readLocalVersion() + // 第一次读取 err := this.loop() if err != nil { @@ -89,10 +97,9 @@ func (this *IPListManager) loop() error { if !hasNext { break } + time.Sleep(1 * time.Second) } - // TODO 写入到缓存当中 - return nil } @@ -163,6 +170,9 @@ func (this *IPListManager) fetch() (hasNext bool, err error) { this.locker.Unlock() this.version = items[len(items)-1].Version + // 写入版本号到缓存当中 + this.updateLocalVersion(this.version) + return true, nil } @@ -172,3 +182,25 @@ func (this *IPListManager) FindList(listId int64) *IPList { this.locker.Unlock() return list } + +func (this *IPListManager) readLocalVersion() int64 { + data, err := ioutil.ReadFile(Tea.ConfigFile(versionCacheFile)) + if err != nil || len(data) == 0 { + return 0 + } + return types.Int64(string(data)) +} + +func (this *IPListManager) updateLocalVersion(version int64) { + fp, err := os.OpenFile(Tea.ConfigFile(versionCacheFile), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) + if err != nil { + remotelogs.Warn("IP_LIST", "write local version cache failed: "+err.Error()) + return + } + _, err = fp.WriteString(types.String(version)) + if err != nil { + remotelogs.Warn("IP_LIST", "write local version cache failed: "+err.Error()) + return + } + _ = fp.Close() +}