improve: 优化 ddns 触发逻辑
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
BitSrun "github.com/Mmx233/BitSrunLoginGo/v1"
|
BitSrun "github.com/Mmx233/BitSrunLoginGo/v1"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Login 登录逻辑
|
// Login 登录逻辑
|
||||||
@@ -50,6 +51,12 @@ func Login(localAddr net.Addr, debugOutput bool) error {
|
|||||||
if online {
|
if online {
|
||||||
output("已登录~")
|
output("已登录~")
|
||||||
|
|
||||||
|
if global.Config.Settings.DDNS.Enable && ipLast != ip {
|
||||||
|
if ddns(ip, httpClient) == nil {
|
||||||
|
ipLast = ip
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
log.Infoln("检测到用户未登录,开始尝试登录...")
|
log.Infoln("检测到用户未登录,开始尝试登录...")
|
||||||
@@ -60,25 +67,23 @@ func Login(localAddr net.Addr, debugOutput bool) error {
|
|||||||
|
|
||||||
log.Infoln("登录成功~")
|
log.Infoln("登录成功~")
|
||||||
|
|
||||||
// DDNS
|
|
||||||
if global.Config.Settings.DDNS.Enable {
|
if global.Config.Settings.DDNS.Enable {
|
||||||
log.Debugln("开始 DDNS 设置流程")
|
_ = ddns(ip, httpClient)
|
||||||
|
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
package dns
|
package dns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"github.com/Mmx233/BitSrunLoginGo/dns/aliyun"
|
"github.com/Mmx233/BitSrunLoginGo/dns/aliyun"
|
||||||
"github.com/Mmx233/BitSrunLoginGo/dns/cloudflare"
|
"github.com/Mmx233/BitSrunLoginGo/dns/cloudflare"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Run(c *Config) error {
|
func Run(c *Config) error {
|
||||||
|
log.Debugln("开始 DDNS 流程")
|
||||||
|
|
||||||
if c.TTL == 0 {
|
if c.TTL == 0 {
|
||||||
c.TTL = 600
|
c.TTL = 600
|
||||||
}
|
}
|
||||||
@@ -21,8 +25,14 @@ func Run(c *Config) error {
|
|||||||
case "cloudflare":
|
case "cloudflare":
|
||||||
dns, e = cloudflare.New(c.TTL, c.Conf, c.Http)
|
dns, e = cloudflare.New(c.TTL, c.Conf, c.Http)
|
||||||
default:
|
default:
|
||||||
log.Warnf("DDNS 模块 dns 运营商 %s 不支持", c.Provider)
|
var msg string
|
||||||
return nil
|
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 {
|
if e != nil {
|
||||||
log.Warnf("解析 DDNS 配置失败:%v", e)
|
log.Warnf("解析 DDNS 配置失败:%v", e)
|
||||||
|
|||||||
Reference in New Issue
Block a user