-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (47 loc) · 1.37 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/cloudflare/cloudflare-go"
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func getIP() string {
res, _ := http.Get("https://api.ipify.org")
ip1, _ := ioutil.ReadAll(res.Body)
res, _ = http.Get("https://ident.me")
ip2, _ := ioutil.ReadAll(res.Body)
if string(ip1) != string(ip2) {
log.Fatalf("Got different IPs: ipify.org: %s, ident.me: %s", ip1, ip2)
}
return string(ip1)
}
func main() {
var apiKey = flag.String("apikey", "", "Cloudflare API key, find it in your profile")
var apiEmail = flag.String("email", "", "Cloudflare account email")
var zone = flag.String("zone", "", "Name of the cloudflare zone, e.g. example.com")
var record = flag.String("record", "", "Name of the record to update, e.g. www.example.com")
flag.Parse()
if *apiKey == "" || *apiEmail == "" || *zone == "" || *record == "" {
flag.PrintDefaults()
os.Exit(1)
}
api, err := cloudflare.New(*apiKey, *apiEmail)
check(err)
zoneID, err := api.ZoneIDByName(*zone)
check(err)
records, err := api.DNSRecords(zoneID, cloudflare.DNSRecord{Name: *record, Type: "A"})
check(err)
if len(records) != 1 {
log.Fatalf("Error, %d records found for \"%s\", expected 1!", len(records), *record)
}
newRecord := records[0]
newRecord.Content = getIP()
api.UpdateDNSRecord(zoneID, records[0].ID, newRecord)
}