forked from siennathesane/cloudflare-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
178 lines (133 loc) · 3.79 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"strings"
"time"
"github.com/cloudflare/cloudflare-go"
"go.uber.org/ratelimit"
)
var (
filePath string
internalRecords []cloudflare.DNSRecord
upstreamRecords []cloudflare.DNSRecord
zoneId string
apiToken string
quit = make(chan struct{})
frequency int
limiter ratelimit.Limiter
)
type IP struct {
IP string
}
type IPify struct {
client *cloudflare.API
c chan IP
log *log.Logger
}
var Usage = func() {
var s []string
switch runtime.GOOS {
case "windows":
s = strings.Split(os.Args[0], `\`)
default:
s = strings.Split(os.Args[0], "/")
}
fmt.Fprintf(os.Stderr, "\nUse Cloudflare as a dynamic DNS provider.\n\n"+
"Arguments of %s:\n", s[len(s)-1])
flag.PrintDefaults()
}
func init() {
flag.StringVar(&filePath, "records-file-name", "production.json", "Path to the "+
"production.json file.")
flag.StringVar(&zoneId, "zone-id", "", "ID of the zone in Cloudflare.")
flag.StringVar(&apiToken, "api-token", "", "Cloudflare API token.")
flag.IntVar(&frequency, "frequency", 30, "Frequency in seconds to update the records. Will "+
"respect Cloudflare's rate limit, regardless of how many records are configured.")
flag.Usage = Usage
}
func main() {
flag.Parse()
logger := log.New(os.Stdout, "", log.LstdFlags)
logger.Println("hello from boulder.")
limiter = ratelimit.New(4, ratelimit.WithoutSlack) // cloudflare's rate limit.
ipNotifier := make(chan IP, 10)
client, err := cloudflare.NewWithAPIToken(apiToken)
if err != nil {
logger.Fatalf("cannot instantiate cloudflare client: %s", err)
}
fh, err := ioutil.ReadFile(filePath)
if err != nil {
logger.Fatalf("error reading reference file: %s", err)
}
if err := json.Unmarshal(fh, &internalRecords); err != nil {
logger.Fatalf("cannot marshal json from reference file: %s", err)
}
ipy := &IPify{
client: client,
c: ipNotifier,
log: logger,
}
logger.Println("starting up workers.")
go ipy.findIPAddress()
go ipy.updateCloudflare()
logger.Println("workers booted.")
<-quit
}
func (ipy *IPify) findIPAddress() {
ticker := time.NewTicker(time.Second * time.Duration(frequency))
for _ = range ticker.C {
ipy.log.Println("refreshing public ip.")
resp, err := http.Get("https://api.ipify.org?format=json")
if err != nil {
ipy.log.Fatalf("cannot get ip: %s", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
ipy.log.Fatalf("cannot read ipify response: %s", err)
}
var ip IP
if err := json.Unmarshal(body, &ip); err != nil {
ipy.log.Fatal(err)
}
ipy.log.Printf("current public ip is %s.", ip.IP)
if err := resp.Body.Close(); err != nil {
ipy.log.Fatal(err)
}
limiter.Take()
upstreamRecords, err = ipy.client.DNSRecords(zoneId, cloudflare.DNSRecord{})
if err != nil {
ipy.log.Fatalf("cannot get upstream records: %s", err)
}
ipy.c <- ip
ipy.log.Println("sent record update.")
}
}
func (ipy *IPify) updateCloudflare() {
for update := range ipy.c {
go func() {
ipy.log.Println("pushing cloudflare update.")
for idx, _ := range internalRecords {
internalRecords[idx].Content = update.IP
for nidx := range upstreamRecords {
if internalRecords[idx].Name == upstreamRecords[nidx].Name {
internalRecords[idx].ID = upstreamRecords[nidx].ID
}
}
ipy.log.Printf("updating %s.", internalRecords[idx].Name)
limiter.Take()
if err := ipy.client.UpdateDNSRecord(zoneId, internalRecords[idx].ID, internalRecords[idx]); err != nil {
ipy.log.Fatalf("cannot update cloudflare dns record %s: %s", internalRecords[idx].Name, err)
}
ipy.log.Printf("updated %s.", internalRecords[idx].Name)
}
}()
ipy.log.Println("cloudflare update pushed.")
}
}