fix: cloudflare dns update bug
This commit is contained in:
@@ -1,22 +1,19 @@
|
||||
package cloudflare
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/cloudflare/cloudflare-go"
|
||||
)
|
||||
|
||||
type Cloudflare struct {
|
||||
Zone string `json:"zone" yaml:"zone"`
|
||||
RecordId string `json:"record_id" yaml:"record_id"`
|
||||
Token string `json:"token" yaml:"token"`
|
||||
Email string `json:"email" yaml:"email"`
|
||||
Zone string `json:"zone" yaml:"zone"`
|
||||
Token string `json:"token" yaml:"token"`
|
||||
Email string `json:"email" yaml:"email"`
|
||||
}
|
||||
|
||||
type DnsProvider struct {
|
||||
@@ -31,56 +28,55 @@ func New(ttl int, conf Cloudflare, Http *http.Client) (*DnsProvider, error) {
|
||||
TTL: ttl,
|
||||
Cloudflare: conf,
|
||||
}
|
||||
if p.Zone == "" {
|
||||
return nil, errors.New("cloudflare zone 不能为空")
|
||||
}
|
||||
if p.Token == "" {
|
||||
return nil, errors.New("cloudflare token 不能为空")
|
||||
|
||||
if p.Zone == "" || p.Email == "" || p.Token == "" {
|
||||
return nil, errors.New("cloudflare config error")
|
||||
}
|
||||
|
||||
p.ZoneResource = cloudflare.ZoneIdentifier(p.Zone)
|
||||
var err error
|
||||
p.Api, err = cloudflare.NewWithAPIToken(p.Token, cloudflare.HTTPClient(Http))
|
||||
p.Api, err = cloudflare.New(p.Cloudflare.Token, p.Cloudflare.Email)
|
||||
return &p, err
|
||||
}
|
||||
|
||||
func (a DnsProvider) SetDomainRecord(domain, ip string) error {
|
||||
url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records/%s", a.Zone, a.RecordId)
|
||||
proxied := false
|
||||
payload := cloudflare.DNSRecord{
|
||||
Type: "A",
|
||||
Name: domain,
|
||||
rc := cloudflare.ResourceContainer{Identifier: a.Cloudflare.Zone}
|
||||
dnsRecords, resultInfo, err := a.Api.ListDNSRecords(
|
||||
context.TODO(),
|
||||
&rc,
|
||||
cloudflare.ListDNSRecordsParams{
|
||||
Name: domain,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if resultInfo.Count == 0 {
|
||||
log.Println("not found record for domain:", domain)
|
||||
return errors.New("not found record for domain:" + domain)
|
||||
}
|
||||
|
||||
log.Println("found record(s) for domain:", domain)
|
||||
for _, record := range dnsRecords {
|
||||
fmt.Println(record.ID, record.Name, record.Content)
|
||||
}
|
||||
|
||||
// update record
|
||||
record := dnsRecords[0]
|
||||
record, err = a.Api.UpdateDNSRecord(context.TODO(), &rc, cloudflare.UpdateDNSRecordParams{
|
||||
ID: record.ID,
|
||||
Name: record.Name,
|
||||
Type: record.Type,
|
||||
Content: ip,
|
||||
TTL: a.TTL,
|
||||
Proxied: &proxied,
|
||||
Comment: "DDNS for " + domain,
|
||||
}
|
||||
TTL: record.TTL,
|
||||
Proxied: record.Proxied,
|
||||
Comment: &record.Comment,
|
||||
})
|
||||
|
||||
jsonPayload, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Make the HTTP GET request
|
||||
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonPayload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("X-Auth-Email", a.Email)
|
||||
req.Header.Set("X-Auth-Key", a.Token)
|
||||
time.Sleep(time.Millisecond * 200) // avoid request too fast after login
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
}
|
||||
fmt.Println(string(body), resp.Status)
|
||||
fmt.Println("update record success:", record.ID, record.Name, record.Content)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user