Skip to content

Commit

Permalink
build: remove dependency of mapstructre
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmx233 committed Mar 23, 2024
1 parent dbf46c7 commit cac851d
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 71 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 0 additions & 4 deletions internal/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 17 additions & 5 deletions internal/config/models.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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"`
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 DdnsProviderConfigSum `json:"config" yaml:"config"`
}

RealityConf struct {
Expand Down
32 changes: 15 additions & 17 deletions internal/pkg/dns/aliyun/aliyun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 14 additions & 16 deletions internal/pkg/dns/cloudflare/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 Cloudflare struct {
Zone string `json:"zone" yaml:"zone"`
Token string `json:"token" yaml:"token"`
}

type DnsProvider struct {
Api *cloudflare.API `mapstructure:"-"`
TTL int `mapstructure:"-"`
Zone string `mapstructure:"zone"`
ZoneResource *cloudflare.ResourceContainer `mapstructure:"-"`
Token string `mapstructure:"token"`
Api *cloudflare.API
TTL int
ZoneResource *cloudflare.ResourceContainer
Cloudflare
}

func New(ttl int, conf map[string]interface{}, Http *http.Client) (*DnsProvider, error) {
func New(ttl int, conf Cloudflare, Http *http.Client) (*DnsProvider, error) {
var p = DnsProvider{
TTL: ttl,
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
}
Expand Down
8 changes: 3 additions & 5 deletions internal/pkg/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
21 changes: 11 additions & 10 deletions internal/pkg/dns/dnspod/dnspod.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ import (
"strings"
)

type DnsPod struct {
SecretId string `json:"secret_id,omitempty" yaml:"secret_id,omitempty"`
SecretKey string `json:"secret_key,omitempty" yaml:"secret_key,omitempty"`
}

type DnsProvider struct {
Client *dnspod.Client `mapstructure:"-"`
TTL uint64 `mapstructure:"-"`
SecretId string `mapstructure:"secret_id"`
SecretKey string `mapstructure:"secret_key"`
Client *dnspod.Client
TTL uint64
DnsPod
}

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
}
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
Expand Down
7 changes: 5 additions & 2 deletions internal/pkg/dns/models.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,6 +14,6 @@ type Config struct {
IP string
Domain string
TTL uint
Conf map[string]interface{}
Conf config.DdnsProviderConfigSum
Http *http.Client
}
9 changes: 0 additions & 9 deletions internal/pkg/dns/util/config.go

This file was deleted.

0 comments on commit cac851d

Please sign in to comment.