diff --git a/.github/workflows/GoCompileTest.yml b/.github/workflows/GoCompileTest.yml index d304d83..cbe31eb 100644 --- a/.github/workflows/GoCompileTest.yml +++ b/.github/workflows/GoCompileTest.yml @@ -41,12 +41,8 @@ jobs: echo "${{env.GIT_COMMIT_LOG}}" echo "${{env.BUILD_TIME}}" - - name: Install GF CLI dependencies + - name: Go Build Binary - Windows amd64 run: | - wget -O gf "https://github.com/gogf/gf/releases/latest/download/gf_$(go env GOOS)_$(go env GOARCH)" && chmod +x gf && ./gf install -y && rm ./gf - - - name: GF CLI Build Binary - run: | - gf build + GOOS=windows GOARCH=amd64 go build pwd diff --git a/internal/boot/boot.go b/internal/boot/boot.go index 794ec91..184084c 100644 --- a/internal/boot/boot.go +++ b/internal/boot/boot.go @@ -5,14 +5,15 @@ import ( "github.com/gogf/gf/v2/os/gcache" "github.com/gogf/gf/v2/os/gcron" "github.com/gogf/gf/v2/os/glog" - "kes-cron/internal/global/g_consts" + "kes-cron/internal/global/g_cache" + "kes-cron/internal/global/g_structs" "kes-cron/utility/cli_utils" "kes-cron/utility/cron_utils" "kes-cron/utility/net_utils" "kes-cron/utility/update_utils" ) -func Boot(initData *g_consts.InitData) (err error) { +func Boot(initData *g_structs.InitData) (err error) { _, err = gcron.AddOnce(context.TODO(), "@every 1s", func(ctx context.Context) { glog.Debug(context.Background(), "定时任务启动中...") if err := bootMethod(initData); err != nil { @@ -39,7 +40,7 @@ func Boot(initData *g_consts.InitData) (err error) { } // bootCheck 测试初次启动任务 -func bootCheck(initData *g_consts.InitData) (err error) { +func bootCheck(initData *g_structs.InitData) (err error) { err = cli_utils.CmdCore.StartSpeedCmd(context.Background(), initData) if err != nil { glog.Error(context.Background(), "测试测速服务", err) @@ -55,7 +56,7 @@ func bootCheck(initData *g_consts.InitData) (err error) { } // bootMethod 初始化定时任务 -func bootMethod(initData *g_consts.InitData) (err error) { +func bootMethod(initData *g_structs.InitData) (err error) { var ctx = context.TODO() glog.Debug(ctx, "开始初始化定时任务管理器") @@ -79,8 +80,8 @@ func bootMethod(initData *g_consts.InitData) (err error) { glog.Error(ctx, "自动更新服务失败: ", err) return } - if !gcache.MustGet(ctx, "updateStatus").IsNil() { - _, _ = gcache.Remove(ctx, "updateStatus") + if !gcache.MustGet(ctx, g_cache.UpdateCacheKey).IsNil() { + _, _ = gcache.Remove(ctx, g_cache.UpdateCacheKey) } }, "Cron-Update") if err != nil { diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 1bd3c05..7c10c59 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -8,7 +8,7 @@ import ( "github.com/gogf/gf/v2/os/glog" "github.com/gogf/gf/v2/os/gres" "kes-cron/internal/boot" - "kes-cron/internal/global/g_consts" + "kes-cron/internal/global/g_structs" "kes-cron/utility/cron_utils" ) @@ -39,7 +39,7 @@ var ( } // 初始化数据 - initData := &g_consts.InitData{ + initData := &g_structs.InitData{ Department: parser.GetOpt("department").String(), Name: parser.GetOpt("name").String(), } diff --git a/internal/global/g_cache/cache_keys.go b/internal/global/g_cache/cache_keys.go new file mode 100644 index 0000000..d3d47e4 --- /dev/null +++ b/internal/global/g_cache/cache_keys.go @@ -0,0 +1,6 @@ +package g_cache + +const ( + UpdateCacheKey = "updateStatus" + SpeedCacheKey = "speedStatus" +) diff --git a/internal/global/g_consts/consts.go b/internal/global/g_consts/consts.go index f4825c8..f6ec7f6 100644 --- a/internal/global/g_consts/consts.go +++ b/internal/global/g_consts/consts.go @@ -2,10 +2,11 @@ package g_consts const ( PingUrl = "https://www.ithome.com/" - SpeedBackendUrl = "http://120.24.211.49:10441/UploadSpeedData" - PingBackendUrl = "http://120.24.211.49:10441/UploadPingData" - AuthBackendUrl = "http://120.24.211.49:10441/DeviceAuth" - ConfigBackendUrl = "http://120.24.211.49:10441/GetCronConfig" - UpdateBackendUrl = "http://120.24.211.49:10441/GetLatestVersion" + BackendBaseUrl = "http://120.24.211.49:10441" + SpeedBackendUrl = BackendBaseUrl + "/UploadSpeedData" + PingBackendUrl = BackendBaseUrl + "/UploadPingData" + AuthBackendUrl = BackendBaseUrl + "/DeviceAuth" + ConfigBackendUrl = BackendBaseUrl + "/GetCronConfig" + UpdateBackendUrl = BackendBaseUrl + "/GetLatestVersion" DownloadExeUrl = "https://gh.xinyu.today/https://github.com/hamster1963/Speed-Cron/releases/latest/download/speed_cron_windows_amd64.exe" ) diff --git a/internal/global/g_consts/structs.go b/internal/global/g_structs/structs.go similarity index 92% rename from internal/global/g_consts/structs.go rename to internal/global/g_structs/structs.go index 182206b..b331501 100644 --- a/internal/global/g_consts/structs.go +++ b/internal/global/g_structs/structs.go @@ -1,4 +1,4 @@ -package g_consts +package g_structs type InitData struct { Department string `json:"department" description:"部门"` diff --git a/utility/cli_utils/cmd_core.go b/utility/cli_utils/cmd_core.go index 8225536..32b5469 100644 --- a/utility/cli_utils/cmd_core.go +++ b/utility/cli_utils/cmd_core.go @@ -6,7 +6,8 @@ import ( "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/os/gcache" "github.com/gogf/gf/v2/os/glog" - "kes-cron/internal/global/g_consts" + "kes-cron/internal/global/g_cache" + "kes-cron/internal/global/g_structs" ) type uCmdCore struct{} @@ -17,7 +18,7 @@ var CmdCore = &uCmdCore{} // // @dc: 启动speedtest命令 // @author: hamster @date:2023/6/20 10:06:06 -func (u *uCmdCore) StartSpeedCmd(ctx context.Context, initData *g_consts.InitData) (err error) { +func (u *uCmdCore) StartSpeedCmd(ctx context.Context, initData *g_structs.InitData) (err error) { cmd := CliUtils.CreateSpeedCmd() if cmd == nil { glog.Warning(ctx, "创建命令失败,获取测速节点失败") @@ -48,7 +49,7 @@ func (u *uCmdCore) StartSpeedCmd(ctx context.Context, initData *g_consts.InitDat // 持续获取输出 for scanner.Scan() { // 获取更新核心状态,判断是否即将更新 - if gcache.MustGet(ctx, "speedtestStatus").Bool() { + if gcache.MustGet(ctx, g_cache.UpdateCacheKey).Bool() { glog.Warning(ctx, "正在更新客户端程序,中止测速服务") break } diff --git a/utility/cron_utils/auth.go b/utility/cron_utils/auth.go index ac9fc8a..61338b9 100644 --- a/utility/cron_utils/auth.go +++ b/utility/cron_utils/auth.go @@ -8,6 +8,7 @@ import ( "github.com/gogf/gf/v2/net/gclient" "github.com/gogf/gf/v2/os/glog" "kes-cron/internal/global/g_consts" + "kes-cron/internal/global/g_structs" "kes-cron/utility/net_utils" "time" ) @@ -20,7 +21,7 @@ var Auth = &uAuth{} // // @dc: 设备认证 // @author: hamster @date:2023/6/20 11:42:38 -func (u *uAuth) DeviceAuth(initData *g_consts.InitData) (err error) { +func (u *uAuth) DeviceAuth(initData *g_structs.InitData) (err error) { // 获取内网与mac地址 internalIp, macAddress := net_utils.NetworkInfo.GetMacAddress() // 进行设备认证 @@ -51,7 +52,7 @@ func (u *uAuth) DeviceAuth(initData *g_consts.InitData) (err error) { // // @dc: 获取设备信息 // @author: hamster @date:2023/6/20 17:34:24 -func (u *uAuth) GetDeviceInfo() (getInitData *g_consts.InitData, err error) { +func (u *uAuth) GetDeviceInfo() (getInitData *g_structs.InitData, err error) { // 获取Mac地址 _, macAddress := net_utils.NetworkInfo.GetMacAddress() // 获取配置 @@ -74,7 +75,7 @@ func (u *uAuth) GetDeviceInfo() (getInitData *g_consts.InitData, err error) { } // 解析配置 configMap := gjson.New(response.ReadAllString()) - getInitData = &g_consts.InitData{ + getInitData = &g_structs.InitData{ Department: configMap.Get("data.department").String(), Name: configMap.Get("data.staff_name").String(), } diff --git a/utility/cron_utils/cron_manage.go b/utility/cron_utils/cron_manage.go index 4d9fbe5..0d06d64 100644 --- a/utility/cron_utils/cron_manage.go +++ b/utility/cron_utils/cron_manage.go @@ -9,7 +9,9 @@ import ( "github.com/gogf/gf/v2/os/gcache" "github.com/gogf/gf/v2/os/gcron" "github.com/gogf/gf/v2/os/glog" + "kes-cron/internal/global/g_cache" "kes-cron/internal/global/g_consts" + "kes-cron/internal/global/g_structs" "kes-cron/utility/cli_utils" "kes-cron/utility/net_utils" "reflect" @@ -24,7 +26,7 @@ var CronManage = &uCronManage{} // // @dc: 获取定时任务管理器配置并启动 // @author: hamster @date:2023/6/20 13:41:26 -func (u *uCronManage) GetConfigAndStart(ctx context.Context, initData *g_consts.InitData) (err error) { +func (u *uCronManage) GetConfigAndStart(ctx context.Context, initData *g_structs.InitData) (err error) { glog.Debug(ctx, "开始获取定时任务管理器配置") // 设备认证 err = Auth.DeviceAuth(initData) @@ -157,15 +159,15 @@ func getConfig() (speedInterval string, pingInterval string, cronStatus int, err } // addSpeedCron 添加测速定时任务 -func addSpeedCron(ctx context.Context, initData *g_consts.InitData, timePattern string) (err error) { +func addSpeedCron(ctx context.Context, initData *g_structs.InitData, timePattern string) (err error) { glog.Notice(ctx, "开始定时测速服务", timePattern) _, err = gcron.AddSingleton(ctx, timePattern, func(ctx context.Context) { // 判断是否在更新中 - if gcache.MustGet(ctx, "speedtestStatus").Bool() { + if gcache.MustGet(ctx, g_cache.UpdateCacheKey).Bool() { glog.Warning(ctx, "正在更新客户端程序,跳过本次测速") return } - _ = gcache.Set(ctx, "speedtestStatus", true, 1*time.Minute) + _ = gcache.Set(ctx, g_cache.SpeedCacheKey, true, 1*time.Minute) err := cli_utils.CmdCore.StartSpeedCmd(ctx, initData) if err != nil { @@ -173,7 +175,7 @@ func addSpeedCron(ctx context.Context, initData *g_consts.InitData, timePattern return } - _, _ = gcache.Remove(ctx, "speedtestStatus") + _, _ = gcache.Remove(ctx, g_cache.SpeedCacheKey) }, "Speed-Cron") if err != nil { glog.Warning(ctx, "添加定时测速服务失败: ", err) @@ -183,7 +185,7 @@ func addSpeedCron(ctx context.Context, initData *g_consts.InitData, timePattern } // addPingCron 添加延迟检测定时任务 -func addPingCron(ctx context.Context, initData *g_consts.InitData, timePattern string) (err error) { +func addPingCron(ctx context.Context, initData *g_structs.InitData, timePattern string) (err error) { glog.Notice(ctx, "开始HTTPS延迟定时检测服务", timePattern) _, err = gcron.AddSingleton(ctx, timePattern, func(ctx context.Context) { err := net_utils.NetUtils.CoreLatency(initData) diff --git a/utility/net_utils/https_latency.go b/utility/net_utils/https_latency.go index 536b92b..59b65b2 100644 --- a/utility/net_utils/https_latency.go +++ b/utility/net_utils/https_latency.go @@ -7,6 +7,7 @@ import ( "github.com/gogf/gf/v2/util/gconv" "io" "kes-cron/internal/global/g_consts" + "kes-cron/internal/global/g_structs" "net/http" "time" ) @@ -40,7 +41,7 @@ func (u *uNetUtils) HttpsLatency() (latency int, err error) { // // @dc: 测试延迟核心服务 // @author: Hamster @date:2023-06-17 14:03:41 -func (u *uNetUtils) CoreLatency(initData *g_consts.InitData) (err error) { +func (u *uNetUtils) CoreLatency(initData *g_structs.InitData) (err error) { latency, err := u.HttpsLatency() if err != nil { glog.Warning(context.Background(), "请求出错:", err) @@ -59,7 +60,7 @@ func (u *uNetUtils) CoreLatency(initData *g_consts.InitData) (err error) { // // @dc: 推送延迟到服务器 // @author: Hamster @date:2023-06-17 14:02:59 -func (u *uNetUtils) PushLatencyToServer(initData *g_consts.InitData, latency int) (err error) { +func (u *uNetUtils) PushLatencyToServer(initData *g_structs.InitData, latency int) (err error) { _, macAddress := NetworkInfo.GetMacAddress() params := g.Map{ "department": initData.Department, diff --git a/utility/update_utils/auto_update.go b/utility/update_utils/auto_update.go index e1d49bb..fb3c6ea 100644 --- a/utility/update_utils/auto_update.go +++ b/utility/update_utils/auto_update.go @@ -11,7 +11,9 @@ import ( "github.com/gogf/gf/v2/os/glog" "github.com/kardianos/osext" "gopkg.in/inconshreveable/go-update.v0" + "kes-cron/internal/global/g_cache" "kes-cron/internal/global/g_consts" + "kes-cron/internal/global/g_structs" "os" "path/filepath" "time" @@ -25,9 +27,9 @@ var AutoUpdate = &uAutoUpdate{} // // @dc: 更新任务核心程序 // @author: laixin @date:2023/7/14 09:37:24 -func (u *uAutoUpdate) UpdateCore(ctx context.Context, initData *g_consts.InitData) (err error) { +func (u *uAutoUpdate) UpdateCore(ctx context.Context, initData *g_structs.InitData) (err error) { // 检测是否在进行测速服务 - if gcache.MustGet(ctx, "speedtestStatus").Bool() { + if gcache.MustGet(ctx, g_cache.SpeedCacheKey).Bool() { glog.Warning(ctx, "正在进行测速服务,无法更新") return nil } @@ -46,7 +48,7 @@ func (u *uAutoUpdate) UpdateCore(ctx context.Context, initData *g_consts.InitDat } // 设置更新状态缓存 - _ = gcache.Set(ctx, "updateStatus", true, 0) + _ = gcache.Set(ctx, g_cache.UpdateCacheKey, true, 0) glog.Debug(ctx, "开始更新speed_cron...") err = updateFunc()