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