improve: 使用标准项目结构
This commit is contained in:
60
internal/controllers/daemon.go
Normal file
60
internal/controllers/daemon.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Mmx233/BitSrunLoginGo/internal/global"
|
||||
"github.com/Mmx233/tool"
|
||||
"github.com/howeyc/fsnotify"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type daemon struct {
|
||||
Mark string
|
||||
Path string
|
||||
}
|
||||
|
||||
// Daemon 后台模式控制包
|
||||
var Daemon = daemon{
|
||||
Mark: fmt.Sprint(time.Now().UnixNano()),
|
||||
Path: global.Config.Settings.Daemon.Path,
|
||||
}
|
||||
|
||||
// MarkDaemon 写入后台标记文件
|
||||
func (a *daemon) MarkDaemon() error {
|
||||
return tool.File.WriteAll(a.Path, []byte(a.Mark))
|
||||
}
|
||||
|
||||
// CheckDaemon 检查后台标记文件
|
||||
func (a *daemon) CheckDaemon() bool {
|
||||
if data, err := tool.File.ReadAll(a.Path); err != nil {
|
||||
return false
|
||||
} else {
|
||||
return string(data) == a.Mark
|
||||
}
|
||||
}
|
||||
|
||||
// DaemonChan 后台标记文件监听
|
||||
func (a *daemon) DaemonChan() bool {
|
||||
f, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = f.Watch(Daemon.Path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case event := <-f.Event:
|
||||
if event.IsModify() && a.CheckDaemon() {
|
||||
continue
|
||||
}
|
||||
os.Exit(0)
|
||||
case e := <-f.Error:
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
67
internal/controllers/guardian.go
Normal file
67
internal/controllers/guardian.go
Normal file
@@ -0,0 +1,67 @@
|
||||
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.Addr, 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()
|
||||
}
|
||||
89
internal/controllers/login.go
Normal file
89
internal/controllers/login.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
global2 "github.com/Mmx233/BitSrunLoginGo/internal/global"
|
||||
dns2 "github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns"
|
||||
"github.com/Mmx233/BitSrunLoginGo/pkg/srun"
|
||||
"github.com/Mmx233/BitSrunLoginGo/tools"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Login 登录逻辑
|
||||
func Login(localAddr net.Addr, debugOutput bool) error {
|
||||
// 登录状态检查
|
||||
|
||||
httpClient := tools.HttpPackSelect(localAddr).Client
|
||||
conf := &srun.Conf{
|
||||
Https: global2.Config.Settings.Basic.Https,
|
||||
LoginInfo: srun.LoginInfo{
|
||||
Form: &global2.Config.Form,
|
||||
Meta: &global2.Config.Meta,
|
||||
},
|
||||
Client: httpClient,
|
||||
}
|
||||
|
||||
var output func(args ...interface{})
|
||||
if debugOutput {
|
||||
output = log.Debugln
|
||||
} else {
|
||||
output = log.Infoln
|
||||
}
|
||||
|
||||
output("正在获取登录状态")
|
||||
|
||||
online, ip, e := srun.LoginStatus(conf)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
if localAddr != nil && global2.Config.Settings.Basic.UseDhcpIP {
|
||||
ip = localAddr.(*net.TCPAddr).IP.String()
|
||||
} else if global2.Flags.ClientIP != "" {
|
||||
ip = global2.Flags.ClientIP
|
||||
}
|
||||
|
||||
log.Debugln("认证客户端 ip: ", ip)
|
||||
|
||||
// 登录执行
|
||||
|
||||
if online {
|
||||
output("已登录~")
|
||||
|
||||
if global2.Config.Settings.DDNS.Enable && global2.Config.Settings.Guardian.Enable && ipLast != ip {
|
||||
if ddns(ip, httpClient) == nil {
|
||||
ipLast = ip
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
} else {
|
||||
log.Infoln("检测到用户未登录,开始尝试登录...")
|
||||
|
||||
if e = srun.DoLogin(ip, conf); e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
log.Infoln("登录成功~")
|
||||
|
||||
if global2.Config.Settings.DDNS.Enable {
|
||||
_ = ddns(ip, httpClient)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var ipLast string
|
||||
|
||||
func ddns(ip string, httpClient *http.Client) error {
|
||||
return dns2.Run(&dns2.Config{
|
||||
Provider: global2.Config.Settings.DDNS.Provider,
|
||||
IP: ip,
|
||||
Domain: global2.Config.Settings.DDNS.Domain,
|
||||
TTL: global2.Config.Settings.DDNS.TTL,
|
||||
Conf: global2.Config.Settings.DDNS.Config,
|
||||
Http: httpClient,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user