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/antonfisher/nested-logrus-formatter v1.3.1
github.com/cloudflare/cloudflare-go v0.91.0
github.com/mitchellh/mapstructure v1.5.0
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/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-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

View File

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

View File

@@ -1,5 +1,11 @@
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 (
GuardianConf struct {
Enable bool `json:"enable" yaml:"enable"`
@@ -20,12 +26,18 @@ type (
FileName string `json:"log_name" yaml:"log_name"`
}
DdnsProviderConfigSum struct {
dnspod.DnsPod `yaml:",inline"`
cloudflare.Cloudflare `yaml:",inline"`
aliyun.Aliyun `yaml:",inline"`
}
DdnsConf struct {
Enable bool `json:"enable" yaml:"enable"`
TTL uint `json:"ttl" yaml:"ttl"`
Domain string `json:"domain" yaml:"domain"`
Provider string `json:"provider" yaml:"provider"`
Config map[string]interface{} `json:"config" yaml:"config"`
Config DdnsProviderConfigSum `json:"config" yaml:"config"`
}
RealityConf struct {

View File

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

View File

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

View File

@@ -16,17 +16,15 @@ func Run(c *Config) error {
c.TTL = 600
}
// 配置解析
var dns Provider
var err error
switch c.Provider {
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":
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":
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:
var msg string
if c.Provider == "" {

View File

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

View File

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