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() +}