From fa36a36beb448df4676bc688a817718aba779d49 Mon Sep 17 00:00:00 2001 From: UUBulb <35923940+uubulb@users.noreply.github.com> Date: Wed, 19 Mar 2025 22:21:21 +0800 Subject: [PATCH] improve transfer record logic (#1033) * improve transfer record logic * refactor * modernize loops * remove unused type conversions * update dependencies * script: keep .gitkeep files * fix * remove clear --- cmd/dashboard/controller/jwt.go | 9 +-- cmd/dashboard/main.go | 12 ++-- go.mod | 35 ++++++------ go.sum | 88 ++++++++++++++++-------------- model/alertrule_test.go | 15 +++-- model/rule.go | 11 ++-- model/server.go | 4 +- pkg/geoip/geoip.go | 4 +- pkg/utils/utils.go | 37 +++++++++---- script/fetch-frontends.sh | 4 +- service/rpc/nezha.go | 17 +++--- service/singleton/alertsentinel.go | 12 ++-- service/singleton/crontask.go | 8 +-- service/singleton/singleton.go | 43 +++++++++------ 14 files changed, 168 insertions(+), 131 deletions(-) diff --git a/cmd/dashboard/controller/jwt.go b/cmd/dashboard/controller/jwt.go index 476b594..e48d4e6 100644 --- a/cmd/dashboard/controller/jwt.go +++ b/cmd/dashboard/controller/jwt.go @@ -7,6 +7,7 @@ import ( jwt "github.com/appleboy/gin-jwt/v2" "github.com/gin-gonic/gin" "github.com/goccy/go-json" + gjwt "github.com/golang-jwt/jwt/v4" "golang.org/x/crypto/bcrypt" "gorm.io/gorm" @@ -48,14 +49,14 @@ func initParams() *jwt.GinJWTMiddleware { } } -func payloadFunc() func(data any) jwt.MapClaims { - return func(data any) jwt.MapClaims { +func payloadFunc() func(data any) gjwt.MapClaims { + return func(data any) gjwt.MapClaims { if v, ok := data.(string); ok { - return jwt.MapClaims{ + return gjwt.MapClaims{ model.CtxKeyAuthorizedUser: v, } } - return jwt.MapClaims{} + return gjwt.MapClaims{} } } diff --git a/cmd/dashboard/main.go b/cmd/dashboard/main.go index 3130063..6db1540 100644 --- a/cmd/dashboard/main.go +++ b/cmd/dashboard/main.go @@ -69,7 +69,7 @@ func initSystem() error { } // 每小时对流量记录进行打点 - if _, err := singleton.CronShared.AddFunc("0 0 * * * *", singleton.RecordTransferHourlyUsage); err != nil { + if _, err := singleton.CronShared.AddFunc("0 0 * * * *", func() { singleton.RecordTransferHourlyUsage() }); err != nil { return err } return nil @@ -108,11 +108,11 @@ func main() { } // 初始化 dao 包 - singleton.InitFrontendTemplates() - singleton.InitConfigFromPath(dashboardCliParam.ConfigFile) - singleton.InitTimezoneAndCache() - singleton.InitDBFromPath(dashboardCliParam.DatabaseLocation) - if err := initSystem(); err != nil { + if err := utils.FirstError(singleton.InitFrontendTemplates, + func() error { return singleton.InitConfigFromPath(dashboardCliParam.ConfigFile) }, + singleton.InitTimezoneAndCache, + func() error { return singleton.InitDBFromPath(dashboardCliParam.DatabaseLocation) }, + initSystem); err != nil { log.Fatal(err) } diff --git a/go.mod b/go.mod index 5ba68e1..a9c1995 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,14 @@ module github.com/nezhahq/nezha go 1.24.0 require ( - github.com/appleboy/gin-jwt/v2 v2.10.1 + github.com/appleboy/gin-jwt/v2 v2.10.2 github.com/chai2010/gettext-go v1.0.3 github.com/dustinkirkland/golang-petname v0.0.0-20240428194347-eebcea082ee0 github.com/gin-contrib/pprof v1.5.2 github.com/gin-gonic/gin v1.10.0 github.com/go-viper/mapstructure/v2 v2.2.1 github.com/goccy/go-json v0.10.5 + github.com/golang-jwt/jwt/v4 v4.5.1 github.com/gorilla/websocket v1.5.3 github.com/hashicorp/go-uuid v1.0.3 github.com/jinzhu/copier v0.4.0 @@ -30,12 +31,12 @@ require ( github.com/swaggo/gin-swagger v1.6.0 github.com/swaggo/swag v1.16.4 github.com/tidwall/gjson v1.18.0 - golang.org/x/crypto v0.35.0 - golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa - golang.org/x/net v0.36.0 - golang.org/x/oauth2 v0.26.0 - golang.org/x/sync v0.11.0 - google.golang.org/grpc v1.70.0 + golang.org/x/crypto v0.36.0 + golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 + golang.org/x/net v0.37.0 + golang.org/x/oauth2 v0.28.0 + golang.org/x/sync v0.12.0 + google.golang.org/grpc v1.71.0 google.golang.org/protobuf v1.36.5 gorm.io/driver/sqlite v1.5.7 gorm.io/gorm v1.25.12 @@ -44,8 +45,8 @@ require ( require ( github.com/KyleBanks/depth v1.2.1 // indirect - github.com/bytedance/sonic v1.12.9 // indirect - github.com/bytedance/sonic/loader v0.2.3 // indirect + github.com/bytedance/sonic v1.13.1 // indirect + github.com/bytedance/sonic/loader v0.2.4 // indirect github.com/cloudwego/base64x v0.1.5 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/fsnotify/fsnotify v1.8.0 // indirect @@ -58,12 +59,11 @@ require ( github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.25.0 // indirect - github.com/golang-jwt/jwt/v4 v4.5.1 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/cpuid/v2 v2.2.9 // indirect + github.com/klauspost/cpuid/v2 v2.2.10 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/mailru/easyjson v0.9.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect @@ -80,12 +80,13 @@ require ( github.com/tidwall/sjson v1.2.5 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect - golang.org/x/arch v0.14.0 // indirect - golang.org/x/mod v0.23.0 // indirect - golang.org/x/sys v0.30.0 // indirect - golang.org/x/text v0.22.0 // indirect + github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect + golang.org/x/arch v0.15.0 // indirect + golang.org/x/mod v0.24.0 // indirect + golang.org/x/sys v0.31.0 // indirect + golang.org/x/text v0.23.0 // indirect golang.org/x/time v0.6.0 // indirect - golang.org/x/tools v0.30.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20250219182151-9fdb1cabc7b2 // indirect + golang.org/x/tools v0.31.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 6078996..13a4a39 100644 --- a/go.sum +++ b/go.sum @@ -1,14 +1,14 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= -github.com/appleboy/gin-jwt/v2 v2.10.1 h1:I68+9qGsgHDx8omd65MKhYXF7Qz5LtdFFTsB/kSU4z0= -github.com/appleboy/gin-jwt/v2 v2.10.1/go.mod h1:xuzn4aNUwqwR3+j+jbL6MhryiRKinUL1SJ7WUfB33vU= +github.com/appleboy/gin-jwt/v2 v2.10.2 h1:cnqgERDsLvyeTtc1gjVj7Fo9+V73nxVvNIrGOeTGR0Q= +github.com/appleboy/gin-jwt/v2 v2.10.2/go.mod h1:mGO+yS9+1sbFrMjN0RYhzs7r8dQtxblHkVEM/Nxsdxs= github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4= github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw= -github.com/bytedance/sonic v1.12.9 h1:Od1BvK55NnewtGaJsTDeAOSnLVO2BTSLOe0+ooKokmQ= -github.com/bytedance/sonic v1.12.9/go.mod h1:uVvFidNmlt9+wa31S1urfwwthTWteBgG0hWuoKAXTx8= +github.com/bytedance/sonic v1.13.1 h1:Jyd5CIvdFnkOWuKXr+wm4Nyk2h0yAFsr8ucJgEasO3g= +github.com/bytedance/sonic v1.13.1/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= -github.com/bytedance/sonic/loader v0.2.3 h1:yctD0Q3v2NOGfSWPLPvG2ggA2kV6TS6s4wioyEqssH0= -github.com/bytedance/sonic/loader v0.2.3/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= +github.com/bytedance/sonic/loader v0.2.4 h1:ZWCw4stuXUsn1/+zQDqeE7JKP+QO47tz7QCNan80NzY= +github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= github.com/chai2010/gettext-go v1.0.3 h1:9liNh8t+u26xl5ddmWLmsOsdNLwkdRTg5AG+JnTiM80= github.com/chai2010/gettext-go v1.0.3/go.mod h1:y+wnP2cHYaVj19NZhYKAwEMH2CI1gNHeQQ+5AjwawxA= github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4= @@ -81,8 +81,8 @@ github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFF github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY= -github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8= +github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE= +github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs= github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI= github.com/knadh/koanf/providers/env v1.0.0 h1:ufePaI9BnWH+ajuxGGiJ8pdTG0uLEUWC7/HDDPGLah0= @@ -173,40 +173,44 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM= +github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.opentelemetry.io/otel v1.32.0 h1:WnBN+Xjcteh0zdk01SVqV55d/m62NJLJdIyb4y/WO5U= -go.opentelemetry.io/otel v1.32.0/go.mod h1:00DCVSB0RQcnzlwyTfqtxSm+DRr9hpYrHjNGiBHVQIg= -go.opentelemetry.io/otel/metric v1.32.0 h1:xV2umtmNcThh2/a/aCP+h64Xx5wsj8qqnkYZktzNa0M= -go.opentelemetry.io/otel/metric v1.32.0/go.mod h1:jH7CIbbK6SH2V2wE16W05BHCtIDzauciCRLoc/SyMv8= -go.opentelemetry.io/otel/sdk v1.32.0 h1:RNxepc9vK59A8XsgZQouW8ue8Gkb4jpWtJm9ge5lEG4= -go.opentelemetry.io/otel/sdk v1.32.0/go.mod h1:LqgegDBjKMmb2GC6/PrTnteJG39I8/vJCAP9LlJXEjU= -go.opentelemetry.io/otel/sdk/metric v1.32.0 h1:rZvFnvmvawYb0alrYkjraqJq0Z4ZUJAiyYCU9snn1CU= -go.opentelemetry.io/otel/sdk/metric v1.32.0/go.mod h1:PWeZlq0zt9YkYAp3gjKZ0eicRYvOh1Gd+X99x6GHpCQ= -go.opentelemetry.io/otel/trace v1.32.0 h1:WIC9mYrXf8TmY/EXuULKc8hR17vE+Hjv2cssQDe03fM= -go.opentelemetry.io/otel/trace v1.32.0/go.mod h1:+i4rkvCraA+tG6AzwloGaCtkx53Fa+L+V8e9a7YvhT8= -golang.org/x/arch v0.14.0 h1:z9JUEZWr8x4rR0OU6c4/4t6E6jOZ8/QBS2bBYBm4tx4= -golang.org/x/arch v0.14.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA= +go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A= +go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY= +go.opentelemetry.io/otel v1.34.0/go.mod h1:OWFPOQ+h4G8xpyjgqo4SxJYdDQ/qmRH+wivy7zzx9oI= +go.opentelemetry.io/otel/metric v1.34.0 h1:+eTR3U0MyfWjRDhmFMxe2SsW64QrZ84AOhvqS7Y+PoQ= +go.opentelemetry.io/otel/metric v1.34.0/go.mod h1:CEDrp0fy2D0MvkXE+dPV7cMi8tWZwX3dmaIhwPOaqHE= +go.opentelemetry.io/otel/sdk v1.34.0 h1:95zS4k/2GOy069d321O8jWgYsW3MzVV+KuSPKp7Wr1A= +go.opentelemetry.io/otel/sdk v1.34.0/go.mod h1:0e/pNiaMAqaykJGKbi+tSjWfNNHMTxoC9qANsCzbyxU= +go.opentelemetry.io/otel/sdk/metric v1.34.0 h1:5CeK9ujjbFVL5c1PhLuStg1wxA7vQv7ce1EK0Gyvahk= +go.opentelemetry.io/otel/sdk/metric v1.34.0/go.mod h1:jQ/r8Ze28zRKoNRdkjCZxfs6YvBTG1+YIqyFVFYec5w= +go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC8mh/k= +go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE= +golang.org/x/arch v0.15.0 h1:QtOrQd0bTUnhNVNndMpLHNWrDmYzZ2KDqSrEymqInZw= +golang.org/x/arch v0.15.0/go.mod h1:JmwW7aLIoRUKgaTzhkiEFxvcEiQGyOg9BMonBJUS7EE= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs= -golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ= -golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa h1:t2QcU6V556bFjYgu4L6C+6VrCPyJZ+eyRsABUPs1mz4= -golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa/go.mod h1:BHOTPb3L19zxehTsLoJXVaTktb06DFgmdW6Wb9s8jqk= +golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34= +golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= +golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 h1:nDVHiLt8aIbd/VzvPWN6kSOPE7+F/fNFDSXLVYkE/Iw= +golang.org/x/exp v0.0.0-20250305212735-054e65f0b394/go.mod h1:sIifuuw/Yco/y6yb6+bDNfyeQ/MdPUy/hKEMYQV17cM= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM= -golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= +golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU= +golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.36.0 h1:vWF2fRbw4qslQsQzgFqZff+BItCvGFQqKzKIzx1rmoA= -golang.org/x/net v0.36.0/go.mod h1:bFmbeoIPfrw4sMHNhb4J9f6+tPziuGjq7Jk/38fxi1I= -golang.org/x/oauth2 v0.26.0 h1:afQXWNNaeC4nvZ0Ed9XvCCzXM6UHJG7iCg0W4fPqSBE= -golang.org/x/oauth2 v0.26.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= +golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c= +golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8= +golang.org/x/oauth2 v0.28.0 h1:CrgCKl8PPAVtLnU3c+EDw6x11699EWlsDeWNWKdIOkc= +golang.org/x/oauth2 v0.28.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w= -golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw= +golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -214,8 +218,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= -golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= +golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= @@ -223,20 +227,20 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= -golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= +golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= +golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U= golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.30.0 h1:BgcpHewrV5AUp2G9MebG4XPFI1E2W41zU1SaqVA9vJY= -golang.org/x/tools v0.30.0/go.mod h1:c347cR/OJfw5TI+GfX7RUPNMdDRRbjvYTS0jPyvsVtY= +golang.org/x/tools v0.31.0 h1:0EedkvKDbh+qistFTd0Bcwe/YLh4vHwWEkiI0toFIBU= +golang.org/x/tools v0.31.0/go.mod h1:naFTU+Cev749tSJRXJlna0T3WxKvb1kWEx15xA4SdmQ= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto/googleapis/rpc v0.0.0-20250219182151-9fdb1cabc7b2 h1:DMTIbak9GhdaSxEjvVzAeNZvyc03I61duqNbnm3SU0M= -google.golang.org/genproto/googleapis/rpc v0.0.0-20250219182151-9fdb1cabc7b2/go.mod h1:LuRYeWDFV6WOn90g357N17oMCaxpgCnbi/44qJvDn2I= -google.golang.org/grpc v1.70.0 h1:pWFv03aZoHzlRKHWicjsZytKAiYCtNS0dHbXnIdq7jQ= -google.golang.org/grpc v1.70.0/go.mod h1:ofIJqVKDXx/JiXrwr2IG4/zwdH9txy3IlF40RmcJSQw= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4 h1:iK2jbkWL86DXjEx0qiHcRE9dE4/Ahua5k6V8OWFb//c= +google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4/go.mod h1:LuRYeWDFV6WOn90g357N17oMCaxpgCnbi/44qJvDn2I= +google.golang.org/grpc v1.71.0 h1:kF77BGdPTQ4/JZWMlb9VpJ5pa25aqvVqogsxNHHdeBg= +google.golang.org/grpc v1.71.0/go.mod h1:H0GRtasmQOh9LkFoCPDu3ZrwUtD1YGE+b2vYBYd/8Ec= google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM= google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/model/alertrule_test.go b/model/alertrule_test.go index 6b327e6..8ec971b 100644 --- a/model/alertrule_test.go +++ b/model/alertrule_test.go @@ -13,7 +13,14 @@ type arSt struct { exp bool } -func TestCycleRules(t *testing.T) { +func TestAlertRules(t *testing.T) { + t.Run("CycleRules", testCycleRules) + t.Run("OfflineRules", testOfflineRules) + t.Run("GeneralRules", testGeneralRules) + t.Run("CombinedRules", testCombinedRules) +} + +func testCycleRules(t *testing.T) { cases := []arSt{ { rule: &AlertRule{ @@ -50,7 +57,7 @@ func TestCycleRules(t *testing.T) { } } -func TestOfflineRules(t *testing.T) { +func testOfflineRules(t *testing.T) { cases := []arSt{ { rule: &AlertRule{ @@ -117,7 +124,7 @@ func TestOfflineRules(t *testing.T) { } } -func TestGeneralRules(t *testing.T) { +func testGeneralRules(t *testing.T) { cases := []arSt{ { rule: &AlertRule{ @@ -193,7 +200,7 @@ func TestGeneralRules(t *testing.T) { } } -func TestCombinedRules(t *testing.T) { +func testCombinedRules(t *testing.T) { cases := []arSt{ { rule: &AlertRule{ diff --git a/model/rule.go b/model/rule.go index c822ddd..8159219 100644 --- a/model/rule.go +++ b/model/rule.go @@ -93,21 +93,21 @@ func (u *Rule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server, src = float64(server.LastActive.Unix()) } case "transfer_in_cycle": - src = float64(utils.Uint64SubInt64(server.State.NetInTransfer, server.PrevTransferInSnapshot)) + src = float64(utils.SubUintChecked(server.State.NetInTransfer, server.PrevTransferInSnapshot)) if u.CycleInterval != 0 { var res NResult db.Model(&Transfer{}).Select("SUM(`in`) AS n").Where("datetime(`created_at`) >= datetime(?) AND server_id = ?", u.GetTransferDurationStart().UTC(), server.ID).Scan(&res) src += float64(res.N) } case "transfer_out_cycle": - src = float64(utils.Uint64SubInt64(server.State.NetOutTransfer, server.PrevTransferOutSnapshot)) + src = float64(utils.SubUintChecked(server.State.NetOutTransfer, server.PrevTransferOutSnapshot)) if u.CycleInterval != 0 { var res NResult db.Model(&Transfer{}).Select("SUM(`out`) AS n").Where("datetime(`created_at`) >= datetime(?) AND server_id = ?", u.GetTransferDurationStart().UTC(), server.ID).Scan(&res) src += float64(res.N) } case "transfer_all_cycle": - src = float64(utils.Uint64SubInt64(server.State.NetOutTransfer, server.PrevTransferOutSnapshot) + utils.Uint64SubInt64(server.State.NetInTransfer, server.PrevTransferInSnapshot)) + src = float64(utils.SubUintChecked(server.State.NetOutTransfer, server.PrevTransferOutSnapshot) + utils.SubUintChecked(server.State.NetInTransfer, server.PrevTransferInSnapshot)) if u.CycleInterval != 0 { var res NResult db.Model(&Transfer{}).Select("SUM(`in`+`out`) AS n").Where("datetime(`created_at`) >= datetime(?) AND server_id = ?", u.GetTransferDurationStart().UTC(), server.ID).Scan(&res) @@ -139,10 +139,7 @@ func (u *Rule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server, // 循环区间流量检测 · 更新下次需要检测时间 if u.IsTransferDurationRule() { - seconds := 1800 * ((u.Max - src) / u.Max) - if seconds < 180 { - seconds = 180 - } + seconds := max(1800*((u.Max-src)/u.Max), 180) if u.NextTransferAt == nil { u.NextTransferAt = make(map[uint64]time.Time) } diff --git a/model/server.go b/model/server.go index e87276c..66b9b8a 100644 --- a/model/server.go +++ b/model/server.go @@ -35,8 +35,8 @@ type Server struct { TaskStream pb.NezhaService_RequestTaskServer `gorm:"-" json:"-"` ConfigCache chan any `gorm:"-" json:"-"` - PrevTransferInSnapshot int64 `gorm:"-" json:"-"` // 上次数据点时的入站使用量 - PrevTransferOutSnapshot int64 `gorm:"-" json:"-"` // 上次数据点时的出站使用量 + PrevTransferInSnapshot uint64 `gorm:"-" json:"-"` // 上次数据点时的入站使用量 + PrevTransferOutSnapshot uint64 `gorm:"-" json:"-"` // 上次数据点时的出站使用量 } func InitServer(s *Server) { diff --git a/pkg/geoip/geoip.go b/pkg/geoip/geoip.go index 5595219..6559db3 100644 --- a/pkg/geoip/geoip.go +++ b/pkg/geoip/geoip.go @@ -2,7 +2,7 @@ package geoip import ( _ "embed" - "fmt" + "errors" "net" "strings" "sync" @@ -48,5 +48,5 @@ func Lookup(ip net.IP) (string, error) { return strings.ToLower(record.Continent), nil } - return "", fmt.Errorf("IP not found") + return "", errors.New("IP not found") } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 53178ba..3a285a1 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -91,16 +91,6 @@ func MustGenerateRandomString(n int) string { return str } -func Uint64SubInt64(a uint64, b int64) uint64 { - if b < 0 { - return a + uint64(-b) - } - if a < uint64(b) { - return 0 - } - return a - uint64(b) -} - func IfOr[T any](a bool, x, y T) T { if a { return x @@ -159,6 +149,16 @@ func ConvertSeq2[KIn, VIn, KOut, VOut any](seq iter.Seq2[KIn, VIn], f func(KIn, } } +func Seq2To1[K, V any](seq iter.Seq2[K, V]) iter.Seq[V] { + return func(yield func(V) bool) { + for _, v := range seq { + if !yield(v) { + return + } + } + } +} + type WrapError struct { err, errIn error } @@ -174,3 +174,20 @@ func (e *WrapError) Error() string { func (e *WrapError) Unwrap() error { return e.errIn } + +func FirstError(errorer ...func() error) error { + for _, fn := range errorer { + if err := fn(); err != nil { + return err + } + } + return nil +} + +func SubUintChecked[T constraints.Unsigned](a, b T) T { + if a < b { + return 0 + } + + return a - b +} diff --git a/script/fetch-frontends.sh b/script/fetch-frontends.sh index 1dbb644..c613145 100755 --- a/script/fetch-frontends.sh +++ b/script/fetch-frontends.sh @@ -19,10 +19,10 @@ download_and_extract() { curl -L -o "dist.zip" "$repository/releases/download/$version/dist.zip" - [ -e "$targetDir" ] && rm -r "$targetDir" unzip -q dist.zip - mv dist "$targetDir" + cp -r dist "$targetDir" + rm -rf dist rm "dist.zip" popd || exit } diff --git a/service/rpc/nezha.go b/service/rpc/nezha.go index 7f29d4c..6a4a346 100644 --- a/service/rpc/nezha.go +++ b/service/rpc/nezha.go @@ -117,10 +117,10 @@ func (s *NezhaHandler) ReportSystemState(stream pb.NezhaService_ReportSystemStat server.LastActive = time.Now() server.State = &innerState - // 应对 dashboard 重启的情况,如果从未记录过,先打点,等到小时时间点时入库 + // 应对 dashboard / agent 重启的情况,如果从未记录过,先打点,等到小时时间点时入库 if server.PrevTransferInSnapshot == 0 || server.PrevTransferOutSnapshot == 0 { - server.PrevTransferInSnapshot = int64(state.NetInTransfer) - server.PrevTransferOutSnapshot = int64(state.NetOutTransfer) + server.PrevTransferInSnapshot = state.NetInTransfer + server.PrevTransferOutSnapshot = state.NetOutTransfer } if err = stream.Send(&pb.Receipt{Proced: true}); err != nil { @@ -139,17 +139,18 @@ func (s *NezhaHandler) onReportSystemInfo(c context.Context, r *pb.Host) error { server, ok := singleton.ServerShared.Get(clientID) if !ok || server == nil { - return fmt.Errorf("server not found") + return errors.New("server not found") } /** * 这里的 singleton 中的数据都是关机前的旧数据 * 当 agent 重启时,bootTime 变大,agent 端会先上报 host 信息,然后上报 state 信息 - * 这是可以借助上报顺序的空档,将停机前的流量统计数据标记下来,加到下一个小时的数据点上 + * 这时可以借助上报顺序的空档,立即记录停机前的数据并重置 Prev* 数据,并由接下来的 state 方法重新赋值 */ - if server.Host != nil && server.Host.BootTime < host.BootTime { - server.PrevTransferInSnapshot = server.PrevTransferInSnapshot - int64(server.State.NetInTransfer) - server.PrevTransferOutSnapshot = server.PrevTransferOutSnapshot - int64(server.State.NetOutTransfer) + if !server.LastActive.IsZero() && host.BootTime > server.Host.BootTime { + singleton.RecordTransferHourlyUsage(server) + server.PrevTransferInSnapshot = 0 + server.PrevTransferOutSnapshot = 0 } server.Host = &host diff --git a/service/singleton/alertsentinel.go b/service/singleton/alertsentinel.go index 0c26f10..6e923f0 100644 --- a/service/singleton/alertsentinel.go +++ b/service/singleton/alertsentinel.go @@ -36,19 +36,19 @@ func addCycleTransferStatsInfo(alert *model.AlertRule) { if !alert.Enabled() { return } - for j := 0; j < len(alert.Rules); j++ { - if !alert.Rules[j].IsTransferDurationRule() { + for _, rule := range alert.Rules { + if !rule.IsTransferDurationRule() { continue } if AlertsCycleTransferStatsStore[alert.ID] == nil { - from := alert.Rules[j].GetTransferDurationStart() - to := alert.Rules[j].GetTransferDurationEnd() + from := rule.GetTransferDurationStart() + to := rule.GetTransferDurationEnd() AlertsCycleTransferStatsStore[alert.ID] = &model.CycleTransferStats{ Name: alert.Name, From: from, To: to, - Max: uint64(alert.Rules[j].Max), - Min: uint64(alert.Rules[j].Min), + Max: uint64(rule.Max), + Min: uint64(rule.Min), ServerName: make(map[uint64]string), Transfer: make(map[uint64]uint64), NextUpdate: make(map[uint64]time.Time), diff --git a/service/singleton/crontask.go b/service/singleton/crontask.go index f851b65..7928c3d 100644 --- a/service/singleton/crontask.go +++ b/service/singleton/crontask.go @@ -132,8 +132,8 @@ func ManualTrigger(cr *model.Cron) { func CronTrigger(cr *model.Cron, triggerServer ...uint64) func() { crIgnoreMap := make(map[uint64]bool) - for j := 0; j < len(cr.Servers); j++ { - crIgnoreMap[cr.Servers[j]] = true + for _, server := range cr.Servers { + crIgnoreMap[server] = true } return func() { if cr.Cover == model.CronCoverAlertTrigger { @@ -151,7 +151,7 @@ func CronTrigger(cr *model.Cron, triggerServer ...uint64) func() { // 保存当前服务器状态信息 curServer := model.Server{} copier.Copy(&curServer, s) - NotificationShared.SendNotification(cr.NotificationGroupID, Localizer.Tf("[Task failed] %s: server %s is offline and cannot execute the task", cr.Name, s.Name), "", &curServer) + go NotificationShared.SendNotification(cr.NotificationGroupID, Localizer.Tf("[Task failed] %s: server %s is offline and cannot execute the task", cr.Name, s.Name), "", &curServer) } } return @@ -174,7 +174,7 @@ func CronTrigger(cr *model.Cron, triggerServer ...uint64) func() { // 保存当前服务器状态信息 curServer := model.Server{} copier.Copy(&curServer, s) - NotificationShared.SendNotification(cr.NotificationGroupID, Localizer.Tf("[Task failed] %s: server %s is offline and cannot execute the task", cr.Name, s.Name), "", &curServer) + go NotificationShared.SendNotification(cr.NotificationGroupID, Localizer.Tf("[Task failed] %s: server %s is offline and cannot execute the task", cr.Name, s.Name), "", &curServer) } } } diff --git a/service/singleton/singleton.go b/service/singleton/singleton.go index ba9788c..5f89ad1 100644 --- a/service/singleton/singleton.go +++ b/service/singleton/singleton.go @@ -39,14 +39,15 @@ var ( //go:embed frontend-templates.yaml var frontendTemplatesYAML []byte -func InitTimezoneAndCache() { +func InitTimezoneAndCache() error { var err error Loc, err = time.LoadLocation(Conf.Location) if err != nil { - panic(err) + return err } Cache = cache.New(5*time.Minute, 10*time.Minute) + return nil } // LoadSingleton 加载子服务并执行 @@ -61,21 +62,22 @@ func LoadSingleton() { } // InitFrontendTemplates 从内置文件中加载FrontendTemplates -func InitFrontendTemplates() { +func InitFrontendTemplates() error { err := yaml.Unmarshal(frontendTemplatesYAML, &FrontendTemplates) if err != nil { - panic(err) + return err } + return nil } // InitDBFromPath 从给出的文件路径中加载数据库 -func InitDBFromPath(path string) { +func InitDBFromPath(path string) error { var err error DB, err = gorm.Open(sqlite.Open(path), &gorm.Config{ CreateBatchSize: 200, }) if err != nil { - panic(err) + return err } if Conf.Debug { DB = DB.Debug() @@ -86,32 +88,39 @@ func InitDBFromPath(path string) { model.NAT{}, model.DDNSProfile{}, model.NotificationGroupNotification{}, model.WAF{}, model.Oauth2Bind{}) if err != nil { - panic(err) + return err } + return nil } // RecordTransferHourlyUsage 对流量记录进行打点 -func RecordTransferHourlyUsage() { - ServerShared.listMu.RLock() - defer ServerShared.listMu.RUnlock() - +func RecordTransferHourlyUsage(servers ...*model.Server) { now := time.Now() nowTrimSeconds := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location()) + var txs []model.Transfer - for id, server := range ServerShared.list { + var slist iter.Seq[*model.Server] + if len(servers) > 0 { + slist = slices.Values(servers) + } else { + slist = utils.Seq2To1(ServerShared.Range) + } + + for server := range slist { tx := model.Transfer{ - ServerID: id, - In: utils.Uint64SubInt64(server.State.NetInTransfer, server.PrevTransferInSnapshot), - Out: utils.Uint64SubInt64(server.State.NetOutTransfer, server.PrevTransferOutSnapshot), + ServerID: server.ID, + In: utils.SubUintChecked(server.State.NetInTransfer, server.PrevTransferInSnapshot), + Out: utils.SubUintChecked(server.State.NetOutTransfer, server.PrevTransferOutSnapshot), } if tx.In == 0 && tx.Out == 0 { continue } - server.PrevTransferInSnapshot = int64(server.State.NetInTransfer) - server.PrevTransferOutSnapshot = int64(server.State.NetOutTransfer) + server.PrevTransferInSnapshot = server.State.NetInTransfer + server.PrevTransferOutSnapshot = server.State.NetOutTransfer tx.CreatedAt = nowTrimSeconds txs = append(txs, tx) } + if len(txs) == 0 { return }