Files
BitSrunLoginGo/controllers/guardian.go
2021-12-02 13:47:46 +08:00

61 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controllers
import (
"github.com/Mmx233/BitSrunLoginGo/global"
"github.com/Mmx233/BitSrunLoginGo/util"
"os"
"os/exec"
"time"
)
// Guardian 守护模式逻辑
func Guardian(output bool) {
util.Log.OutPut = output
if global.Config.Settings.Daemon.Enable {
go Daemon.DaemonChan()
if e := Daemon.MarkDaemon(); e != nil {
util.Log.Fatalln(e)
}
}
var c = make(chan bool)
for {
util.Log.OutPut = output
go func() {
defer func() {
_ = recover()
}()
if !util.Checker.NetOk(global.Config.Settings.Timeout) {
util.Log.Println("Network down, trying to login")
e := Login(output, true)
if e != nil {
util.Log.Println("Error: ", e)
}
} else {
if global.Config.Settings.DemoMode {
util.Log.Println("Network ok")
}
}
c <- false
}()
<-c
time.Sleep(time.Duration(global.Config.Settings.Guardian.Duration) * time.Second)
}
}
// EnterGuardian 守护模式入口控制是否进入daemon
func EnterGuardian() {
util.Log.OutPut = true
util.Log.Println("[Guardian mode]")
if global.Config.Settings.Daemon.Enable || global.Flags.Daemon {
if err := exec.Command(os.Args[0], append(os.Args[1:], "--running-daemon")...).Start(); err != nil {
util.Log.Fatalln(err)
}
util.Log.Println("[Daemon mode entered]")
return
}
Guardian(true)
}