Files
BitSrunLoginGo/internal/controllers/guardian.go

68 lines
1.5 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/internal/global"
"os"
"os/exec"
"time"
"github.com/Mmx233/BitSrunLoginGo/tools"
log "github.com/sirupsen/logrus"
)
// Guardian 守护模式逻辑
func Guardian() {
GuardianDuration := time.Duration(global.Config.Settings.Guardian.Duration) * time.Second
if global.Config.Settings.Daemon.Enable {
go Daemon.DaemonChan()
if e := Daemon.MarkDaemon(); e != nil {
log.Warnln("写入daemon标记文件失败: ", e)
}
}
var c = make(chan bool)
for {
go func() {
defer func() {
_ = recover()
}()
if global.Config.Settings.Basic.Interfaces == "" { //单网卡
e := Login(nil, true)
if e != nil {
log.Errorln("登录出错: ", e)
}
} else { //多网卡
interfaces, e := tools.GetInterfaceAddr()
if e == nil {
for _, eth := range interfaces {
log.Debugf("使用 %s 网口登录 ", eth.Name)
e = Login(&eth, true)
if e != nil {
log.Errorln("网口 ", eth.Name+" 登录出错: ", e)
}
}
}
}
c <- false
}()
<-c
time.Sleep(GuardianDuration)
}
}
// EnterGuardian 守护模式入口控制是否进入daemon
func EnterGuardian() {
log.Infoln("[以守护模式启动]")
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 {
log.Fatalln("启动守护失败: ", err)
}
log.Infoln("[进入后台进程模式]")
return
}
Guardian()
}