summaryrefslogtreecommitdiff
path: root/examples/cloudflare/api.go
blob: 9522b073f77f68871c2b71113abf0e688d8a1909 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// This is a simple example
package main

import 	(
	"os"
	"log"
	"fmt"
	"encoding/json"
	"io/ioutil"
	"net/http"
//	"strconv"
	"bytes"

	"github.com/davecgh/go-spew/spew"
)

func doChange(dnsRow *RRT) {
	log.Println("Look for changes in row", dnsRow.ID)
	log.Println("Proxy", dnsRow.Proxied, "vs", dnsRow.proxyNode.S)
	log.Println("Content", dnsRow.Content, "vs", dnsRow.valueNode.S)
	if (dnsRow.Content != dnsRow.valueNode.S) {
		log.Println("UPDATE VALUE", dnsRow.nameNode.Name, dnsRow.typeNode.Name, "to", dnsRow.valueNode.S)
		stuff, result := httpPut(dnsRow)
		if (dnsRow.curlNode != nil) {
			pretty, _ := formatJSON(stuff)
			log.Println("http PUT curl =", pretty)
			dnsRow.curlNode.SetText(pretty)
		}
		if (dnsRow.resultNode != nil) {
			pretty, _ := formatJSON(result)
			log.Println("http PUT result =", pretty)
			dnsRow.resultNode.SetText(pretty)
		}
	}
	dnsRow.saveNode.Disable()
}

func getZonefile(c *configT) *DNSRecords {
	var url = cloudflareURL + c.zoneID + "/dns_records/"
	log.Println("getZonefile()", c.domain, url)
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Println("http.NewRequest error:", err)
		return nil
	}

	// Set headers
	req.Header.Set("X-Auth-Key", c.auth)
	req.Header.Set("X-Auth-Email", c.email)

	log.Println("getZonefile() auth, email", c.auth, c.email)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Println("http.Client error:", err)
		return nil
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println("ioutil.ReadAll() error", err)
		return nil
	}

	var records DNSRecords
	if err := json.Unmarshal(body, &records); err != nil {
		log.Println("json.Unmarshal() error", err)
		return nil
	}

	log.Println("getZonefile() worked", records)
	return &records
}

/*
	pass in a DNS Resource Records (the stuff in a zonefile)

	This will talk to the cloudflare API and generate a resource record in the zonefile:

	For example:
	gitea.wit.com. 3600 IN CNAME git.wit.org.
	go.wit.com. 3600 IN A 1.1.1.9
	test.wit.com. 3600 IN NS ns1.wit.com.
*/
func httpPut(dnsRow *RRT) (string, string) {
	var url string = cloudflareURL + os.Getenv("CF_API_ZONEID") + "/dns_records/" + dnsRow.ID
	var authKey string = os.Getenv("CF_API_KEY")
	var email string = os.Getenv("CF_API_EMAIL")

	// make a json record to send on port 80 to cloudflare
	var tmp string
	tmp = `{"content": "` + dnsRow.valueNode.S + `", `
	tmp += `"name": "` + dnsRow.Name + `", `
	tmp += `"type": "` + dnsRow.Type + `", `
	tmp+= `"ttl": "` +  "1" + `", `
	tmp += `"comment": "WIT DNS Control Panel"`
	tmp +=  `}`
	data := []byte(tmp)

	log.Println("http PUT url =", url)
	// log.Println("http PUT data =", data)
	// spew.Dump(data)
	pretty, _ := formatJSON(string(data))
	log.Println("http PUT data =", pretty)

	req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(data))

	// Set headers
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("X-Auth-Key", authKey)
	req.Header.Set("X-Auth-Email", email)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Println(err)
		return tmp, fmt.Sprintf("blah err =", err)
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println(err)
		return tmp, fmt.Sprintf("blah err =", err)
	}
	// log.Println("http PUT body =", body)
	// spew.Dump(body)

	return tmp, string(body)
}

// https://api.cloudflare.com/client/v4/zones
func getZones(auth, email string) *DNSRecords {
	var url = "https://api.cloudflare.com/client/v4/zones"
	log.Println("getZones()", url)
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Println("http.NewRequest error:", err)
		return nil
	}

	// Set headers
	req.Header.Set("X-Auth-Key", auth)
	req.Header.Set("X-Auth-Email", email)

	log.Println("getZones() auth, email", auth, email)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Println("getZones() http.Client error:", err)
		return nil
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println("getZones() ioutil.ReadAll() error", err)
		return nil
	}

	var records DNSRecords
	if err := json.Unmarshal(body, &records); err != nil {
		log.Println("getZones() json.Unmarshal() error", err)
		return nil
	}

	/* Cloudflare API returns struct[] of:
	  struct { ID string "json:\"id\""; Type string "json:\"type\""; Name string "json:\"name\"";
		Content string "json:\"content\""; Proxied bool "json:\"proxied\"";
		Proxiable bool "json:\"proxiable\""; TTL int "json:\"ttl\"" }
	*/

	// log.Println("getZones() worked", records)
	// log.Println("spew dump:")
	spew.Dump(records)
	for _, record := range records.Result {
		log.Println("spew record:", record)
		log.Println("record:", record.Name, record.ID)

		var newc *configT
		newc = new(configT)

		newc.domain = record.Name
		newc.zoneID = record.ID
		newc.auth = auth
		newc.email = email

		config[record.Name] = newc
		zonedrop.AddText(record.Name)
		log.Println("zonedrop.AddText:", record.Name, record.ID)
	}
	for d, _ := range config {
		log.Println("config entry:", d)
	}

	return &records
}

// formatJSON takes an unformatted JSON string and returns a formatted version.
func formatJSON(unformattedJSON string) (string, error) {
	var jsonData interface{}

	// Decode the JSON string into an interface
	err := json.Unmarshal([]byte(unformattedJSON), &jsonData)
	if err != nil {
		return "", err
	}

	// Re-encode the JSON with indentation for formatting
	formattedJSON, err := json.MarshalIndent(jsonData, "", "    ")
	if err != nil {
		return "", err
	}

	return string(formattedJSON), nil
}