feat: 重构守护入口逻辑

This commit is contained in:
Mmx
2021-09-19 20:54:23 +08:00
parent b148eea874
commit 334f5bbcf0
4 changed files with 12 additions and 17 deletions

View File

@@ -21,7 +21,6 @@ func Guardian(output bool) {
os.Exit(1) os.Exit(1)
} }
global.Status.Output = output global.Status.Output = output
time.Sleep(time.Duration(global.Config.Settings.Guardian) * time.Second)
go func() { go func() {
defer func() { defer func() {
_ = recover() _ = recover()
@@ -37,6 +36,7 @@ func Guardian(output bool) {
c <- false c <- false
}() }()
<-c <-c
time.Sleep(time.Duration(global.Config.Settings.Guardian) * time.Second)
} }
} }

View File

@@ -15,8 +15,8 @@ func Login(output bool) error {
util.Log.Println("Step0: 检查状态…") util.Log.Println("Step0: 检查状态…")
G := global.Config.Generate() G := global.Config.Generate()
if !global.Status.Guardian && global.Config.Settings.QuitIfNetOk && util.Checker.NetOk() { if util.Checker.NetOk() {
util.Log.Println("网络正常,程序退出") util.Log.Println("网络 ok")
return nil return nil
} }

13
main.go
View File

@@ -4,23 +4,20 @@ import (
"autoLogin/controllers" "autoLogin/controllers"
"autoLogin/global" "autoLogin/global"
"autoLogin/util" "autoLogin/util"
"os"
) )
func main() { func main() {
defer util.Log.CatchRecover() defer util.Log.CatchRecover()
if err := controllers.Login(true); err != nil { if global.Config.Settings.Guardian != 0 {
controllers.EnterGuardian()
} else if err := controllers.Login(true); err != nil {
util.Log.Println("运行出错,状态异常") util.Log.Println("运行出错,状态异常")
if global.Config.Settings.ForceGuardianEvenFailed { if global.Config.Settings.DemoMode {
util.Log.Println(err)
} else if global.Config.Settings.DemoMode {
util.Log.Fatalln(err) util.Log.Fatalln(err)
} else { } else {
util.Log.Println(err) util.Log.Println(err)
os.Exit(3) return
} }
} }
controllers.EnterGuardian()
} }

View File

@@ -6,12 +6,10 @@ import (
) )
type Settings struct { type Settings struct {
QuitIfNetOk bool `json:"quit_if_net_ok"` DemoMode bool `json:"demo_mode"`
DemoMode bool `json:"demo_mode"` Dns string `json:"dns"`
Dns string `json:"dns"` Guardian uint `json:"guardian"`
Guardian uint `json:"guardian"` Daemon bool `json:"daemon"`
Daemon bool `json:"daemon"`
ForceGuardianEvenFailed bool `json:"force_guardian_even_failed"`
} }
type Config struct { type Config struct {