Skip to content

Instantly share code, notes, and snippets.

@ahxxm
Last active November 21, 2022 15:07
Show Gist options
  • Save ahxxm/0b66927ef02672c159879168c139a33c to your computer and use it in GitHub Desktop.
Save ahxxm/0b66927ef02672c159879168c139a33c to your computer and use it in GitHub Desktop.
cloudflare ddns
package main
// env GOOS=linux GOARCH=arm go build -ldflags="-s -w" -v a.go
// upx --best --ultra-brute main
import (
"fmt"
"time"
"github.com/cloudflare/cloudflare-go"
externalip "github.com/glendc/go-external-ip"
)
const (
// cloudflare: create a token with read/edit DNS within your zone
apiToken = ""
zoneID = ""
host = ""
)
func CFupdate(ip string) error {
// Fetch record IDs for the records we need to update.
api, err := cloudflare.NewWithAPIToken(apiKey)
if err != nil {
return err
}
records, err := api.DNSRecords(zoneID, cloudflare.DNSRecord{Name: host, Type: "A"})
if err != nil {
return err
}
for _, r := range records {
err := api.UpdateDNSRecord(zoneID, r.ID, cloudflare.DNSRecord{Content: ip})
if err != nil {
return err
}
}
return nil
}
func main() {
for {
ip, err := consensus.ExternalIP()
if err == nil {
fmt.Printf("IP address from cip ... %v\n", ip)
if err := CFupdate(ip); err != nil {
fmt.Printf("failed update cf: %v", err)
}
}
time.Sleep(time.Minute * 30)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment