diff --git a/main.go b/main.go index 6e90c09..966b407 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,7 @@ package main import ( "bytes" - "context" - "crypto/md5" //#nosec + "context" //#nosec "embed" "encoding/json" "fmt" @@ -24,7 +23,9 @@ import ( "github.com/gin-contrib/gzip" "github.com/gin-gonic/gin" "github.com/soulteary/hosts-blackhole/internal/logger" - provider "github.com/soulteary/hosts-blackhole/pkg/provider" + "github.com/soulteary/hosts-blackhole/pkg/crypto" + "github.com/soulteary/hosts-blackhole/pkg/provider" + "github.com/soulteary/hosts-blackhole/pkg/system" flag "github.com/spf13/pflag" ) @@ -176,7 +177,7 @@ func main() { if err != nil { log.Fatal("程序无法创建缓存数据: ", err) } - provider.ManualGC() + system.ManualGC() } c.Redirect(302, ROUTE_LIST) @@ -248,8 +249,7 @@ func main() { // https://github.com/gin-gonic/gin/issues/1222 func optimizeResourceCacheTime() gin.HandlerFunc { data := []byte(time.Now().String()) - /* #nosec */ - etag := fmt.Sprintf("W/%x", md5.Sum(data)) + etag := crypto.ETag(data) return func(c *gin.Context) { if strings.HasPrefix(c.Request.RequestURI, ROUTE_HOMEPAGE) || strings.HasPrefix(c.Request.RequestURI, ROUTE_FAVICON) { diff --git a/pkg/crypto/md5.go b/pkg/crypto/md5.go new file mode 100644 index 0000000..e7067c7 --- /dev/null +++ b/pkg/crypto/md5.go @@ -0,0 +1,18 @@ +package crypto + +import ( + "crypto/md5" //#nosec + "fmt" +) + +func Md5(str string) string { + data := []byte(str) + /* #nosec */ + has := md5.Sum(data) + return fmt.Sprintf("%x", has) +} + +func ETag(data []byte) string { + /* #nosec */ + return fmt.Sprintf("W/%x", md5.Sum(data)) +} diff --git a/pkg/provider/common.go b/pkg/provider/common.go index 4d105a7..ea41005 100644 --- a/pkg/provider/common.go +++ b/pkg/provider/common.go @@ -2,19 +2,16 @@ package provider import ( "bufio" - "crypto/md5" //#nosec - "fmt" "log" "os" "path/filepath" - "runtime" - "runtime/debug" "sort" "strconv" "strings" "time" "github.com/soulteary/hosts-blackhole/internal/logger" + "github.com/soulteary/hosts-blackhole/pkg/crypto" ) type Lines struct { @@ -38,13 +35,6 @@ const baseDir = "./" var cacheKey = "" var cacheHash = "" -func calcMd5(str string) string { - data := []byte(str) - /* #nosec */ - has := md5.Sum(data) - return fmt.Sprintf("%x", has) -} - func CacheHash(files []string, update bool) (string, string) { sort.Strings(files) key := "" @@ -57,8 +47,8 @@ func CacheHash(files []string, update bool) (string, string) { } } - key = calcMd5(key) - val = calcMd5(val) + key = crypto.Md5(key) + val = crypto.Md5(val) if update { cacheKey = key @@ -193,27 +183,3 @@ func Purge(files []string) (mixed []string, success bool) { return mixed, true } - -func ManualGC() { - log := logger.GetLogger() - - log.Info("Runtime Information:") - - runtime.GC() - debug.FreeOSMemory() - - var m runtime.MemStats - runtime.ReadMemStats(&m) - - log.Infof(" MEM Alloc = %10v MB", toMB(m.Alloc)) - log.Infof(" MEM HeapAlloc = %10v MB", toMB(m.HeapAlloc)) - log.Infof(" MEM Sys = %10v MB", toMB(m.Sys)) - log.Infof(" MEM NumGC = %10v", m.NumGC) - log.Infof(" RUN NumCPU = %10d", runtime.NumCPU()) - log.Infof(" RUN NumGoroutine = %10d", runtime.NumGoroutine()) -} - -func toMB(b uint64) uint64 { - const bytesInKB = 1024 - return b / bytesInKB / bytesInKB -} diff --git a/pkg/system/gc.go b/pkg/system/gc.go new file mode 100644 index 0000000..cf8bc95 --- /dev/null +++ b/pkg/system/gc.go @@ -0,0 +1,32 @@ +package system + +import ( + "runtime" + "runtime/debug" + + "github.com/soulteary/hosts-blackhole/internal/logger" +) + +func ManualGC() { + log := logger.GetLogger() + + log.Info("Runtime Information:") + + runtime.GC() + debug.FreeOSMemory() + + var m runtime.MemStats + runtime.ReadMemStats(&m) + + log.Infof(" MEM Alloc = %10v MB", toMB(m.Alloc)) + log.Infof(" MEM HeapAlloc = %10v MB", toMB(m.HeapAlloc)) + log.Infof(" MEM Sys = %10v MB", toMB(m.Sys)) + log.Infof(" MEM NumGC = %10v", m.NumGC) + log.Infof(" RUN NumCPU = %10d", runtime.NumCPU()) + log.Infof(" RUN NumGoroutine = %10d", runtime.NumGoroutine()) +} + +func toMB(b uint64) uint64 { + const bytesInKB = 1024 + return b / bytesInKB / bytesInKB +}