Merge duplicate phishCache/phishTankCache into shared feedCache

This commit is contained in:
nemunaire 2026-05-15 18:38:25 +08:00
commit 061b5361ca
3 changed files with 127 additions and 172 deletions

View file

@ -10,7 +10,6 @@ import (
"net/http"
"strconv"
"strings"
"sync"
"time"
sdk "git.happydns.org/checker-sdk-go/checker"
@ -21,7 +20,7 @@ const (
phishTankDefaultTTL = 12 * time.Hour
)
var phishTankGlobalCache = newPhishTankCache()
var phishTankGlobalCache = newFeedCache(phishTankDefaultTTL, phishTankFetch)
func init() { Register(&phishTankSource{}) }
@ -107,71 +106,8 @@ func (*phishTankSource) Diagnose(res SourceResult) Diagnosis {
}
}
// ---------- feed cache ----------
type phishTankCache struct {
mu sync.Mutex
urls []string
byHost map[string][]string
fetchedAt time.Time
lastAttemptAt time.Time
refreshing bool
ttl time.Duration
failBackoff time.Duration
}
func newPhishTankCache() *phishTankCache {
return &phishTankCache{
ttl: phishTankDefaultTTL,
failBackoff: 1 * time.Minute,
}
}
func (c *phishTankCache) setTTL(d time.Duration) {
c.mu.Lock()
c.ttl = d
c.mu.Unlock()
}
func (c *phishTankCache) lookup(ctx context.Context, domain string) (urls []string, size int, fetchedAt time.Time, err error) {
domain = strings.ToLower(strings.TrimSuffix(domain, "."))
c.mu.Lock()
stale := c.byHost == nil || time.Since(c.fetchedAt) > c.ttl
doRefresh := stale && !c.refreshing && time.Since(c.lastAttemptAt) > c.failBackoff
if doRefresh {
c.refreshing = true
}
c.mu.Unlock()
if doRefresh {
newURLs, newByHost, ferr := c.fetch(ctx)
c.mu.Lock()
c.refreshing = false
c.lastAttemptAt = time.Now()
if ferr == nil {
c.urls = newURLs
c.byHost = newByHost
c.fetchedAt = c.lastAttemptAt
} else {
err = ferr
}
c.mu.Unlock()
}
c.mu.Lock()
for host, hostURLs := range c.byHost {
if host == domain || strings.HasSuffix(host, "."+domain) {
urls = append(urls, hostURLs...)
}
}
size = len(c.urls)
fetchedAt = c.fetchedAt
c.mu.Unlock()
return urls, size, fetchedAt, err
}
func (c *phishTankCache) fetch(ctx context.Context) ([]string, map[string][]string, error) {
// phishTankFetch downloads and parses the PhishTank gzip-compressed CSV feed.
func phishTankFetch(ctx context.Context) ([]string, map[string][]string, error) {
reqCtx, cancel := context.WithTimeout(ctx, 120*time.Second)
defer cancel()