feat: 支持设置 cloudflare dns
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
"fmt"
|
||||
dnsUtil "github.com/Mmx233/BitSrunLoginGo/dns/util"
|
||||
"github.com/Mmx233/tool"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -29,8 +28,16 @@ func New(ttl uint, conf map[string]interface{}, Http *http.Client) (*DnsProvider
|
||||
TTL: ttl,
|
||||
Http: tool.NewHttpTool(Http),
|
||||
}
|
||||
defer log.Debugln("aliun dns provider:", &p)
|
||||
return &p, dnsUtil.DecodeConfig(conf, &p)
|
||||
e := dnsUtil.DecodeConfig(conf, &p)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
|
||||
if p.AccessKeyId == "" || p.AccessKeySecret == "" {
|
||||
return nil, errors.New("aliyun AccessKey 不能为空")
|
||||
}
|
||||
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
func (a DnsProvider) SendRequest(Type, Action string, data map[string]interface{}) (*http.Response, error) {
|
||||
|
||||
@@ -1,34 +1,63 @@
|
||||
package cloudflare
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
dnsUtil "github.com/Mmx233/BitSrunLoginGo/dns/util"
|
||||
"github.com/cloudflare/cloudflare-go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type DnsProvider struct {
|
||||
Api *cloudflare.API `mapstructure:"-"`
|
||||
TTL uint `mapstructure:"-"`
|
||||
TTL int `mapstructure:"-"`
|
||||
Zone string `mapstructure:"zone"`
|
||||
Email string `mapstructure:"email"`
|
||||
Token string `mapstructure:"token"`
|
||||
}
|
||||
|
||||
func New(ttl uint, conf map[string]interface{}, Http *http.Client) (*DnsProvider, error) {
|
||||
var p = DnsProvider{
|
||||
TTL: ttl,
|
||||
TTL: int(ttl),
|
||||
}
|
||||
e := dnsUtil.DecodeConfig(conf, &p)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
log.Debugln("cloudflare dns provider:", &p)
|
||||
|
||||
p.Api, e = cloudflare.New(p.Token, p.Email, cloudflare.HTTPClient(Http))
|
||||
if p.Zone == "" {
|
||||
return nil, errors.New("cloudflare zone 不能为空")
|
||||
} else if p.Token == "" {
|
||||
return nil, errors.New("cloudflare token 不能为空")
|
||||
}
|
||||
|
||||
p.Api, e = cloudflare.NewWithAPIToken(p.Token, cloudflare.HTTPClient(Http))
|
||||
return &p, e
|
||||
}
|
||||
|
||||
func (a DnsProvider) SetDomainRecord(domain, ip string) error {
|
||||
|
||||
records, e := a.Api.DNSRecords(context.Background(), a.Zone, cloudflare.DNSRecord{
|
||||
Type: "A",
|
||||
Name: domain,
|
||||
})
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
if len(records) == 0 {
|
||||
_, e = a.Api.CreateDNSRecord(context.Background(), a.Zone, cloudflare.DNSRecord{
|
||||
Type: "A",
|
||||
Name: domain,
|
||||
Content: ip,
|
||||
TTL: a.TTL,
|
||||
})
|
||||
return e
|
||||
} else {
|
||||
record := records[0]
|
||||
|
||||
if record.Content == ip {
|
||||
return nil
|
||||
}
|
||||
record.Content = ip
|
||||
return a.Api.UpdateDNSRecord(context.Background(), a.Zone, record.ID, record)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package dns
|
||||
|
||||
import (
|
||||
"github.com/Mmx233/BitSrunLoginGo/dns/aliyun"
|
||||
"github.com/Mmx233/BitSrunLoginGo/dns/cloudflare"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -24,6 +25,8 @@ func Run(c *Config) error {
|
||||
switch c.Provider {
|
||||
case "aliyun":
|
||||
dns, e = aliyun.New(meta.TTL, meta.Other, c.Http)
|
||||
case "cloudflare":
|
||||
dns, e = cloudflare.New(meta.TTL, meta.Other, c.Http)
|
||||
default:
|
||||
log.Warnf("DDNS 模块 dns 运营商 %s 不支持", c.Provider)
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user