improve: 优化 ddns 触发逻辑

This commit is contained in:
Mmx233
2022-12-09 19:29:58 +08:00
parent 1cc47a39d0
commit b4f9b6d560
2 changed files with 33 additions and 18 deletions

View File

@@ -7,6 +7,7 @@ import (
BitSrun "github.com/Mmx233/BitSrunLoginGo/v1"
log "github.com/sirupsen/logrus"
"net"
"net/http"
)
// Login 登录逻辑
@@ -50,6 +51,12 @@ func Login(localAddr net.Addr, debugOutput bool) error {
if online {
output("已登录~")
if global.Config.Settings.DDNS.Enable && ipLast != ip {
if ddns(ip, httpClient) == nil {
ipLast = ip
}
}
return nil
} else {
log.Infoln("检测到用户未登录,开始尝试登录...")
@@ -60,25 +67,23 @@ func Login(localAddr net.Addr, debugOutput bool) error {
log.Infoln("登录成功~")
// DDNS
if global.Config.Settings.DDNS.Enable {
log.Debugln("开始 DDNS 设置流程")
if global.Config.Settings.DDNS.Provider == "" {
log.Warnln("DDNS 模块 dns 运营商不能为空")
return nil
}
_ = dns.Run(&dns.Config{
Provider: global.Config.Settings.DDNS.Provider,
IP: ip,
Domain: global.Config.Settings.DDNS.Domain,
TTL: global.Config.Settings.DDNS.TTL,
Conf: global.Config.Settings.DDNS.Config,
Http: httpClient,
})
_ = ddns(ip, httpClient)
}
}
return nil
}
var ipLast string
func ddns(ip string, httpClient *http.Client) error {
return dns.Run(&dns.Config{
Provider: global.Config.Settings.DDNS.Provider,
IP: ip,
Domain: global.Config.Settings.DDNS.Domain,
TTL: global.Config.Settings.DDNS.TTL,
Conf: global.Config.Settings.DDNS.Config,
Http: httpClient,
})
}

View File

@@ -1,12 +1,16 @@
package dns
import (
"errors"
"fmt"
"github.com/Mmx233/BitSrunLoginGo/dns/aliyun"
"github.com/Mmx233/BitSrunLoginGo/dns/cloudflare"
log "github.com/sirupsen/logrus"
)
func Run(c *Config) error {
log.Debugln("开始 DDNS 流程")
if c.TTL == 0 {
c.TTL = 600
}
@@ -21,8 +25,14 @@ func Run(c *Config) error {
case "cloudflare":
dns, e = cloudflare.New(c.TTL, c.Conf, c.Http)
default:
log.Warnf("DDNS 模块 dns 运营商 %s 不支持", c.Provider)
return nil
var msg string
if c.Provider == "" {
msg = "DDNS 模块 dns 运营商不能为空"
} else {
msg = fmt.Sprintf("DDNS 模块 dns 运营商 %s 不支持", c.Provider)
}
log.Warnln(msg)
return errors.New(msg)
}
if e != nil {
log.Warnf("解析 DDNS 配置失败:%v", e)