fix: cloudflare update performance

This commit is contained in:
chinhwajie
2024-10-28 11:59:54 +08:00
parent 7490b4566c
commit 43932e4b98
7 changed files with 156 additions and 134 deletions

View File

@@ -1,15 +1,22 @@
package cloudflare
import (
"context"
"bytes"
"encoding/json"
"errors"
"github.com/cloudflare/cloudflare-go"
"fmt"
"io"
"net/http"
"time"
"github.com/cloudflare/cloudflare-go"
)
type Cloudflare struct {
Zone string `json:"zone" yaml:"zone"`
Token string `json:"token" yaml:"token"`
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"`
}
type DnsProvider struct {
@@ -37,31 +44,43 @@ func New(ttl int, conf Cloudflare, Http *http.Client) (*DnsProvider, error) {
}
func (a DnsProvider) SetDomainRecord(domain, ip string) error {
records, _, err := a.Api.ListDNSRecords(context.Background(), a.ZoneResource, cloudflare.ListDNSRecordsParams{
Type: "A",
Name: domain,
})
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,
Content: ip,
TTL: a.TTL,
Proxied: &proxied,
Comment: "DDNS for " + domain,
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
return err
}
if len(records) == 0 {
_, err = a.Api.CreateDNSRecord(context.Background(), a.ZoneResource, cloudflare.CreateDNSRecordParams{
Type: "A",
Name: domain,
Content: ip,
TTL: a.TTL,
})
return err
} else {
record := records[0]
if record.Content == ip {
return nil
}
_, err = a.Api.UpdateDNSRecord(context.Background(), a.ZoneResource, cloudflare.UpdateDNSRecordParams{
ID: record.ID,
Content: ip,
})
// 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)
return nil
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/Mmx233/BitSrunLoginGo/internal/dns/cloudflare"
"github.com/Mmx233/BitSrunLoginGo/internal/dns/dnspod"
"github.com/Mmx233/BitSrunLoginGo/internal/dns/dynv6"
"github.com/Mmx233/BitSrunLoginGo/internal/dns/pubyun"
log "github.com/sirupsen/logrus"
)
@@ -32,6 +33,8 @@ func Run(c *Config) error {
dns, err = dnspod.New(uint64(c.TTL), c.Conf.DnsPod, c.Http.Transport)
case "dynv6":
dns, err = dynv6.New(c.Domain, c.Conf.Token, c.IP)
case "pubyun":
dns, err = pubyun.New(c.Conf.Zone, c.Conf.Token)
default:
var msg string
if c.Provider == "" {

View File

@@ -0,0 +1,51 @@
package pubyun
import (
"encoding/base64"
"fmt"
"io"
"net/http"
"time"
)
type PubYun struct {
UserName string
Password string
}
func New(UserName string, Password string) (*PubYun, error) {
p := PubYun{
Password: Password,
UserName: UserName}
return &p, nil
}
func (p PubYun) SetDomainRecord(domain, ip string) error {
url := fmt.Sprintf(
"http://members.3322.net/dyndns/update?hostname=%s&myip=%s",
domain,
ip)
// Make the HTTP GET request
req, err := http.NewRequest("GET", url, nil)
auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", p.UserName, p.Password)))
req.Header.Set("Authorization", "Basic "+auth)
if err != nil {
return err
}
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)
return nil
}