-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.go
138 lines (113 loc) · 2.91 KB
/
host.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
package main
import (
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"net"
"strings"
"github.com/cloudflare/cloudflare-go"
)
type Host struct {
Hostname string `json:"hostname"`
Zone string `json:"zone"`
APIToken string `json:"api_token"`
ZoneID string `json:"zone_id,omitempty"`
RecordID string `json:"record_id,omitempty"`
IP string `json:"-"`
WebToken string `json:"web_token,omitempty"`
}
func (h *Host) FQDN() string {
return h.Hostname + "." + h.Zone
}
func (h *Host) Populate() error {
err := h.populateWebToken()
if err != nil {
return err
}
err = h.populateAPIInfo()
if err != nil {
return err
}
return nil
}
func (h *Host) populateAPIInfo() error {
api, err := cloudflare.NewWithAPIToken(h.APIToken)
if err != nil {
return err
}
if strings.TrimSpace(h.ZoneID) == "" {
h.ZoneID, err = api.ZoneIDByName(h.Zone)
if err != nil {
return err
}
}
if strings.TrimSpace(h.RecordID) == "" {
records, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(h.ZoneID), cloudflare.ListDNSRecordsParams{Name: h.FQDN(), Type: "A"})
if err != nil {
return err
}
var record cloudflare.DNSRecord
if len(records) == 0 {
result, err := h.createRecord()
if err != nil {
return err
}
record = *result
} else {
record = records[0]
}
h.RecordID = record.ID
h.IP = record.Content
}
if strings.TrimSpace(h.IP) == "" {
record, err := api.GetDNSRecord(context.Background(), cloudflare.ZoneIdentifier(h.ZoneID), h.RecordID)
if err != nil {
return err
}
h.IP = record.Content
}
return nil
}
func (h *Host) populateWebToken() error {
if strings.TrimSpace(h.WebToken) != "" {
return nil
}
byteLength := 32
random := make([]byte, byteLength)
_, err := rand.Read(random)
if err != nil {
return err
}
h.WebToken = base64.RawURLEncoding.EncodeToString(random)
return nil
}
func (h *Host) updateIP(ip net.IP) error {
api, err := cloudflare.NewWithAPIToken(h.APIToken)
if err != nil {
return err
}
record, err := api.UpdateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(h.ZoneID), cloudflare.UpdateDNSRecordParams{ID: h.RecordID, Content: ip.String()})
if err != nil {
return err
}
if record.Content != ip.String() {
return fmt.Errorf("After UpdateDNSRecord, Cloudflare API did not reflect the IP changes on host " + h.Hostname)
}
h.IP = record.Content
return nil
}
func (h *Host) createRecord() (*cloudflare.DNSRecord, error) {
api, err := cloudflare.NewWithAPIToken(h.APIToken)
if err != nil {
return nil, err
}
// Context for why this pointer to bool is required
// https://github.com/cloudflare/cloudflare-go/issues/568
proxy := false
result, err := api.CreateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(h.ZoneID), cloudflare.CreateDNSRecordParams{Name: h.FQDN(), Type: "A", Content: "0.0.0.0", TTL: 300, Proxied: &proxy})
if err != nil {
return nil, err
}
return &result, nil
}