build: remove dependency of mapstructre

This commit is contained in:
Mmx233
2024-03-23 19:49:11 +08:00
parent dbf46c715b
commit cac851d751
10 changed files with 68 additions and 74 deletions

1
go.mod
View File

@@ -6,7 +6,6 @@ require (
github.com/Mmx233/tool v0.7.4 github.com/Mmx233/tool v0.7.4
github.com/antonfisher/nested-logrus-formatter v1.3.1 github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/cloudflare/cloudflare-go v0.91.0 github.com/cloudflare/cloudflare-go v0.91.0
github.com/mitchellh/mapstructure v1.5.0
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.883 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.883
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.883 github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.883

2
go.sum
View File

@@ -32,8 +32,6 @@ github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZb
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

View File

@@ -34,10 +34,6 @@ var defaultConfig = ConfFromFile{
TTL: 600, TTL: 600,
Domain: "www.example.com", Domain: "www.example.com",
Provider: "cloudflare", Provider: "cloudflare",
Config: map[string]interface{}{
"zone": "",
"token": "",
},
}, },
Reality: RealityConf{ Reality: RealityConf{
Addr: "http://www.baidu.com", Addr: "http://www.baidu.com",

View File

@@ -1,5 +1,11 @@
package config package config
import (
"github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns/aliyun"
"github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns/cloudflare"
"github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns/dnspod"
)
type ( type (
GuardianConf struct { GuardianConf struct {
Enable bool `json:"enable" yaml:"enable"` Enable bool `json:"enable" yaml:"enable"`
@@ -20,12 +26,18 @@ type (
FileName string `json:"log_name" yaml:"log_name"` FileName string `json:"log_name" yaml:"log_name"`
} }
DdnsProviderConfigSum struct {
dnspod.DnsPod `yaml:",inline"`
cloudflare.Cloudflare `yaml:",inline"`
aliyun.Aliyun `yaml:",inline"`
}
DdnsConf struct { DdnsConf struct {
Enable bool `json:"enable" yaml:"enable"` Enable bool `json:"enable" yaml:"enable"`
TTL uint `json:"ttl" yaml:"ttl"` TTL uint `json:"ttl" yaml:"ttl"`
Domain string `json:"domain" yaml:"domain"` Domain string `json:"domain" yaml:"domain"`
Provider string `json:"provider" yaml:"provider"` Provider string `json:"provider" yaml:"provider"`
Config map[string]interface{} `json:"config" yaml:"config"` Config DdnsProviderConfigSum `json:"config" yaml:"config"`
} }
RealityConf struct { RealityConf struct {

View File

@@ -16,28 +16,26 @@ import (
"time" "time"
) )
type DnsProvider struct { type Aliyun struct {
TTL uint `mapstructure:"-"` AccessKeyId string `json:"access_key_id,omitempty" yaml:"access_key_id,omitempty"`
Http *tool.Http `mapstructure:"-"` AccessKeySecret string `json:"access_key_secret,omitempty" yaml:"access_key_secret,omitempty"`
AccessKeyId string `mapstructure:"access_key_id"`
AccessKeySecret string `mapstructure:"access_key_secret"`
} }
func New(ttl uint, conf map[string]interface{}, Http *http.Client) (*DnsProvider, error) { type DnsProvider struct {
var p = DnsProvider{ TTL uint
TTL: ttl, Http *tool.Http
Http: tool.NewHttpTool(Http), Aliyun
} }
err := dnsUtil.DecodeConfig(conf, &p)
if err != nil {
return nil, err
}
if p.AccessKeyId == "" || p.AccessKeySecret == "" { func New(ttl uint, conf Aliyun, Http *http.Client) (*DnsProvider, error) {
if conf.AccessKeyId == "" || conf.AccessKeySecret == "" {
return nil, errors.New("aliyun AccessKey 不能为空") return nil, errors.New("aliyun AccessKey 不能为空")
} }
return &DnsProvider{
return &p, nil TTL: ttl,
Http: tool.NewHttpTool(Http),
Aliyun: conf,
}, nil
} }
func (a DnsProvider) SendRequest(Type, Action string, data map[string]interface{}) (*http.Response, error) { func (a DnsProvider) SendRequest(Type, Action string, data map[string]interface{}) (*http.Response, error) {

View File

@@ -3,37 +3,35 @@ package cloudflare
import ( import (
"context" "context"
"errors" "errors"
"github.com/Mmx233/BitSrunLoginGo/internal/pkg/dns/util"
"github.com/cloudflare/cloudflare-go" "github.com/cloudflare/cloudflare-go"
"net/http" "net/http"
) )
type DnsProvider struct { type Cloudflare struct {
Api *cloudflare.API `mapstructure:"-"` Zone string `json:"zone" yaml:"zone"`
TTL int `mapstructure:"-"` Token string `json:"token" yaml:"token"`
Zone string `mapstructure:"zone"`
ZoneResource *cloudflare.ResourceContainer `mapstructure:"-"`
Token string `mapstructure:"token"`
} }
func New(ttl int, conf map[string]interface{}, Http *http.Client) (*DnsProvider, error) { type DnsProvider struct {
var p = DnsProvider{ Api *cloudflare.API
TTL: ttl, TTL int
} ZoneResource *cloudflare.ResourceContainer
err := dnsUtil.DecodeConfig(conf, &p) Cloudflare
if err != nil { }
return nil, err
}
func New(ttl int, conf Cloudflare, Http *http.Client) (*DnsProvider, error) {
var p = DnsProvider{
TTL: ttl,
Cloudflare: conf,
}
if p.Zone == "" { if p.Zone == "" {
return nil, errors.New("cloudflare zone 不能为空") return nil, errors.New("cloudflare zone 不能为空")
} }
p.ZoneResource = cloudflare.ZoneIdentifier(p.Zone)
if p.Token == "" { if p.Token == "" {
return nil, errors.New("cloudflare token 不能为空") return nil, errors.New("cloudflare token 不能为空")
} }
p.ZoneResource = cloudflare.ZoneIdentifier(p.Zone)
var err error
p.Api, err = cloudflare.NewWithAPIToken(p.Token, cloudflare.HTTPClient(Http)) p.Api, err = cloudflare.NewWithAPIToken(p.Token, cloudflare.HTTPClient(Http))
return &p, err return &p, err
} }

View File

@@ -16,17 +16,15 @@ func Run(c *Config) error {
c.TTL = 600 c.TTL = 600
} }
// 配置解析
var dns Provider var dns Provider
var err error var err error
switch c.Provider { switch c.Provider {
case "aliyun": case "aliyun":
dns, err = aliyun.New(c.TTL, c.Conf, c.Http) dns, err = aliyun.New(c.TTL, c.Conf.Aliyun, c.Http)
case "cloudflare": case "cloudflare":
dns, err = cloudflare.New(int(c.TTL), c.Conf, c.Http) dns, err = cloudflare.New(int(c.TTL), c.Conf.Cloudflare, c.Http)
case "dnspod": case "dnspod":
dns, err = dnspod.New(uint64(c.TTL), c.Conf, c.Http.Transport) dns, err = dnspod.New(uint64(c.TTL), c.Conf.DnsPod, c.Http.Transport)
default: default:
var msg string var msg string
if c.Provider == "" { if c.Provider == "" {

View File

@@ -10,19 +10,20 @@ import (
"strings" "strings"
) )
type DnsProvider struct { type DnsPod struct {
Client *dnspod.Client `mapstructure:"-"` SecretId string `json:"secret_id,omitempty" yaml:"secret_id,omitempty"`
TTL uint64 `mapstructure:"-"` SecretKey string `json:"secret_key,omitempty" yaml:"secret_key,omitempty"`
SecretId string `mapstructure:"secret_id"`
SecretKey string `mapstructure:"secret_key"`
} }
func New(ttl uint64, conf map[string]interface{}, Http http.RoundTripper) (*DnsProvider, error) { type DnsProvider struct {
var p = DnsProvider{TTL: ttl} Client *dnspod.Client
err := dnsUtil.DecodeConfig(conf, &p) TTL uint64
if err != nil { DnsPod
return nil, err }
}
func New(ttl uint64, conf DnsPod, Http http.RoundTripper) (*DnsProvider, error) {
var p = DnsProvider{TTL: ttl, DnsPod: conf}
var err error
p.Client, err = dnspod.NewClient(common.NewCredential(p.SecretId, p.SecretKey), regions.Guangzhou, profile.NewClientProfile()) p.Client, err = dnspod.NewClient(common.NewCredential(p.SecretId, p.SecretKey), regions.Guangzhou, profile.NewClientProfile())
p.Client.WithHttpTransport(Http) p.Client.WithHttpTransport(Http)
return &p, err return &p, err

View File

@@ -1,6 +1,9 @@
package dns package dns
import "net/http" import (
"github.com/Mmx233/BitSrunLoginGo/internal/config"
"net/http"
)
type Provider interface { type Provider interface {
SetDomainRecord(domain, ip string) error SetDomainRecord(domain, ip string) error
@@ -11,6 +14,6 @@ type Config struct {
IP string IP string
Domain string Domain string
TTL uint TTL uint
Conf map[string]interface{} Conf config.DdnsProviderConfigSum
Http *http.Client Http *http.Client
} }

View File

@@ -1,9 +0,0 @@
package dnsUtil
import (
"github.com/mitchellh/mapstructure"
)
func DecodeConfig(conf map[string]interface{}, output interface{}) error {
return mapstructure.Decode(conf, output)
}