From 2a386ff0499bcc1b8a7d6a9f5d5ad5727377f67c Mon Sep 17 00:00:00 2001 From: Mmx233 Date: Thu, 8 Dec 2022 16:30:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ddns=20=E5=8A=9F=E8=83=BD=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/login.go | 21 +++++++++++++++++++++ dns/aliyun/aliyun.go | 14 ++++++++++++++ dns/initer.go | 41 +++++++++++++++++++++++++++++++++++++++++ dns/models/config.go | 6 ++++++ dns/models/interface.go | 5 +++++ dns/util/config.go | 9 +++++++++ global/config.go | 4 ++++ 7 files changed, 100 insertions(+) create mode 100644 dns/aliyun/aliyun.go create mode 100644 dns/initer.go create mode 100644 dns/models/config.go create mode 100644 dns/models/interface.go create mode 100644 dns/util/config.go diff --git a/controllers/login.go b/controllers/login.go index e3e3006..22c28c0 100644 --- a/controllers/login.go +++ b/controllers/login.go @@ -1,6 +1,7 @@ package controllers import ( + "github.com/Mmx233/BitSrunLoginGo/dns" "github.com/Mmx233/BitSrunLoginGo/global" "github.com/Mmx233/BitSrunLoginGo/util" BitSrun "github.com/Mmx233/BitSrunLoginGo/v1" @@ -10,6 +11,8 @@ import ( // Login 登录逻辑 func Login(localAddr net.Addr, debugOutput bool) error { + // 登录状态检查 + conf := &BitSrun.Conf{ Https: global.Config.Settings.Basic.Https, LoginInfo: BitSrun.LoginInfo{ @@ -41,6 +44,8 @@ func Login(localAddr net.Addr, debugOutput bool) error { log.Debugln("认证客户端 ip: ", ip) + // 登录执行 + if online { output("已登录~") @@ -55,5 +60,21 @@ func Login(localAddr net.Addr, debugOutput bool) error { log.Infoln("登录成功~") } + // DDNS + + enable, _ := global.Config.Settings.DDNS["enable"] + if open, _ := enable.(bool); open { + provider, _ := global.Config.Settings.DDNS["provider"] + providerStr, _ := provider.(string) + if providerStr == "" { + log.Warnln("DDNS 模块 dns 运营商不能为空") + return nil + } + + delete(global.Config.Settings.DDNS, "provider") + + _ = dns.Run(providerStr, ip, global.Config.Settings.DDNS) + } + return nil } diff --git a/dns/aliyun/aliyun.go b/dns/aliyun/aliyun.go new file mode 100644 index 0000000..8ba2ddc --- /dev/null +++ b/dns/aliyun/aliyun.go @@ -0,0 +1,14 @@ +package aliyun + +import ( + "github.com/Mmx233/BitSrunLoginGo/dns/models" + "github.com/Mmx233/BitSrunLoginGo/dns/util" +) + +type DnsProvider struct { +} + +func New(conf map[string]interface{}) (models.DnsProvider, error) { + var p DnsProvider + return &p, util.DecodeConfig(conf, &p) +} diff --git a/dns/initer.go b/dns/initer.go new file mode 100644 index 0000000..f438415 --- /dev/null +++ b/dns/initer.go @@ -0,0 +1,41 @@ +package dns + +import ( + "github.com/Mmx233/BitSrunLoginGo/dns/aliyun" + "github.com/Mmx233/BitSrunLoginGo/dns/models" + "github.com/mitchellh/mapstructure" + log "github.com/sirupsen/logrus" +) + +func Run(provider, ip string, conf map[string]interface{}) error { + var meta models.BasicConfig + e := mapstructure.Decode(conf, &meta) + if e != nil { + log.Warnf("解析 DDNS 配置失败:%v", e) + return e + } + + // 配置解析 + + var dns models.DnsProvider + switch provider { + case "aliyun": + dns, e = aliyun.New(meta.Other) + default: + log.Warnf("DDNS 模块 dns 运营商 %s 不支持", provider) + return nil + } + if e != nil { + log.Warnf("解析 DDNS 配置失败:%v", e) + return e + } + + // 修改 dns 记录 + + if e = dns.SetDomainRecord(meta.Domain, ip); e != nil { + log.Warnf("设置 dns 解析记录失败:%v", e) + return e + } + + return nil +} diff --git a/dns/models/config.go b/dns/models/config.go new file mode 100644 index 0000000..cf42ea5 --- /dev/null +++ b/dns/models/config.go @@ -0,0 +1,6 @@ +package models + +type BasicConfig struct { + Domain string `mapstructure:"domain"` + Other map[string]interface{} `mapstructure:",remain"` +} diff --git a/dns/models/interface.go b/dns/models/interface.go new file mode 100644 index 0000000..29a233f --- /dev/null +++ b/dns/models/interface.go @@ -0,0 +1,5 @@ +package models + +type DnsProvider interface { + SetDomainRecord(domain, ip string) error +} diff --git a/dns/util/config.go b/dns/util/config.go new file mode 100644 index 0000000..4bf5465 --- /dev/null +++ b/dns/util/config.go @@ -0,0 +1,9 @@ +package util + +import ( + "github.com/mitchellh/mapstructure" +) + +func DecodeConfig(conf map[string]interface{}, output interface{}) error { + return mapstructure.Decode(conf, output) +} diff --git a/global/config.go b/global/config.go index 09aaf82..cb121b6 100644 --- a/global/config.go +++ b/global/config.go @@ -39,6 +39,10 @@ func readConfig() { Log: srunModels.Log{ FilePath: "./", }, + DDNS: map[string]interface{}{ + "enable": false, + "provider": "", + }, }) //生成配置文件