improve: 修改 ddns 包依赖结构

This commit is contained in:
Mmx233
2022-12-08 16:39:17 +08:00
parent 2a386ff049
commit a4e9f35303
6 changed files with 35 additions and 22 deletions

View File

@@ -13,13 +13,14 @@ import (
func Login(localAddr net.Addr, debugOutput bool) error {
// 登录状态检查
httpClient := util.HttpPackSelect(localAddr).Client
conf := &BitSrun.Conf{
Https: global.Config.Settings.Basic.Https,
LoginInfo: BitSrun.LoginInfo{
Form: &global.Config.Form,
Meta: &global.Config.Meta,
},
Client: util.HttpPackSelect(localAddr).Client,
Client: httpClient,
}
var output func(args ...interface{})
@@ -73,7 +74,12 @@ func Login(localAddr net.Addr, debugOutput bool) error {
delete(global.Config.Settings.DDNS, "provider")
_ = dns.Run(providerStr, ip, global.Config.Settings.DDNS)
_ = dns.Run(&dns.Config{
Provider: providerStr,
IP: ip,
Conf: global.Config.Settings.DDNS,
Http: httpClient,
})
}
return nil

View File

@@ -2,14 +2,13 @@ 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)
func Run(c *Config) error {
var meta BasicConfig
e := mapstructure.Decode(c.Conf, &meta)
if e != nil {
log.Warnf("解析 DDNS 配置失败:%v", e)
return e
@@ -17,12 +16,12 @@ func Run(provider, ip string, conf map[string]interface{}) error {
// 配置解析
var dns models.DnsProvider
switch provider {
var dns Provider
switch c.Provider {
case "aliyun":
dns, e = aliyun.New(meta.Other)
default:
log.Warnf("DDNS 模块 dns 运营商 %s 不支持", provider)
log.Warnf("DDNS 模块 dns 运营商 %s 不支持", c.Provider)
return nil
}
if e != nil {
@@ -32,7 +31,7 @@ func Run(provider, ip string, conf map[string]interface{}) error {
// 修改 dns 记录
if e = dns.SetDomainRecord(meta.Domain, ip); e != nil {
if e = dns.SetDomainRecord(meta.Domain, c.IP); e != nil {
log.Warnf("设置 dns 解析记录失败:%v", e)
return e
}

19
dns/models.go Normal file
View File

@@ -0,0 +1,19 @@
package dns
import "net/http"
type Provider interface {
SetDomainRecord(domain, ip string) error
}
type Config struct {
Provider string
IP string
Conf map[string]interface{}
Http *http.Client
}
type BasicConfig struct {
Domain string `mapstructure:"domain"`
Other map[string]interface{} `mapstructure:",remain"`
}

View File

@@ -1,6 +0,0 @@
package models
type BasicConfig struct {
Domain string `mapstructure:"domain"`
Other map[string]interface{} `mapstructure:",remain"`
}

View File

@@ -1,5 +0,0 @@
package models
type DnsProvider interface {
SetDomainRecord(domain, ip string) error
}

View File

@@ -1,4 +1,4 @@
package util
package dnsUtil
import (
"github.com/mitchellh/mapstructure"