-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
64 lines (51 loc) · 1.45 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
61
62
63
64
package main
import (
"net"
"os"
"github.com/Sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
"net/http"
"crypto/tls"
)
var log = logrus.New()
var Version string
func main() {
var (
app = kingpin.New("cf-ddns", "Cloudflare DynDNS Updater").Version(Version)
ipAddress = app.Flag("ip-address", "Skip resolving external IP and use provided IP").String()
noVerify = app.Flag("no-verify", "Don't verify ssl certificates").Bool()
cfEmail = app.Flag("cf-email", "Cloudflare Email").Required().String()
cfApiKey = app.Flag("cf-api-key", "Cloudflare API key").Required().String()
cfZoneId = app.Flag("cf-zone-id", "Cloudflare Zone ID").Required().String()
hostnames = app.Arg("hostnames", "Hostnames to update").Required().Strings()
)
kingpin.MustParse(app.Parse(os.Args[1:]))
var ip IPService
var dns *CFDNSUpdater
var err error
if *ipAddress != "" {
ip = &FakeIPService{
fakeIp: net.ParseIP(*ipAddress),
}
} else {
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: *noVerify},
},
}
ip = &IpifyIPService{HttpClient: httpClient}
}
if dns, err = NewCFDNSUpdater(*cfZoneId, *cfApiKey, *cfEmail, log.WithField("component", "cf-dns-updater")); err != nil {
log.Panic(err)
}
res, err := ip.GetExternalIP()
if err != nil {
log.Panic(err)
}
for _, hostname := range *hostnames {
err := dns.UpdateRecordA(hostname, res)
if err != nil {
log.Panic(err)
}
}
}