2024-01-01 15:31:33 -06:00
|
|
|
// This is a simple example
|
|
|
|
package cloudflare
|
|
|
|
|
2024-01-18 01:33:28 -06:00
|
|
|
import (
|
2024-01-01 15:31:33 -06:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
2024-01-18 01:33:28 -06:00
|
|
|
This function should run each time
|
|
|
|
the user chanegs anything in the GUi
|
|
|
|
or each time something in general changes
|
|
|
|
|
|
|
|
It returns a RR record which then can be
|
|
|
|
turned into JSON and sent via http
|
|
|
|
to cloudflare's API
|
2024-01-01 15:31:33 -06:00
|
|
|
*/
|
|
|
|
func DoChange() *RRT {
|
|
|
|
var dnsRow *RRT
|
|
|
|
dnsRow = new(RRT)
|
|
|
|
|
2024-01-05 19:33:41 -06:00
|
|
|
log.Log(INFO, "DoChange() START")
|
2024-01-18 01:33:28 -06:00
|
|
|
if CFdialog.proxyNode.String() == "On" {
|
2024-01-01 15:31:33 -06:00
|
|
|
dnsRow.Proxied = true
|
|
|
|
} else {
|
|
|
|
dnsRow.Proxied = false
|
|
|
|
}
|
2024-01-18 01:23:49 -06:00
|
|
|
dnsRow.Auth = CFdialog.apiNode.String()
|
|
|
|
dnsRow.Email = CFdialog.emailNode.String()
|
2024-01-01 15:31:33 -06:00
|
|
|
|
2024-01-18 01:23:49 -06:00
|
|
|
dnsRow.Domain = CFdialog.zoneNode.String()
|
|
|
|
dnsRow.ZoneID = CFdialog.zoneIdNode.String()
|
|
|
|
dnsRow.ID = CFdialog.rrNode.String()
|
2024-01-01 15:31:33 -06:00
|
|
|
|
2024-01-18 01:23:49 -06:00
|
|
|
dnsRow.Content = CFdialog.ValueNode.String()
|
|
|
|
dnsRow.Name = CFdialog.NameNode.String()
|
|
|
|
dnsRow.Type = CFdialog.TypeNode.String()
|
|
|
|
dnsRow.url = CFdialog.urlNode.String()
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
dnsRow.data = makeJSON(dnsRow)
|
|
|
|
// show the JSON
|
2024-01-05 19:33:41 -06:00
|
|
|
log.Log(INFO, dnsRow)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
2024-01-18 01:33:28 -06:00
|
|
|
if CFdialog.curlNode != nil {
|
2024-01-01 15:31:33 -06:00
|
|
|
pretty, _ := FormatJSON(dnsRow.data)
|
2024-01-05 19:33:41 -06:00
|
|
|
log.Log(INFO, "http PUT curl =", pretty)
|
2024-01-01 15:31:33 -06:00
|
|
|
CFdialog.curlNode.SetText(pretty)
|
|
|
|
}
|
|
|
|
return dnsRow
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetRow(dnsRow *RRT) {
|
2024-01-05 19:33:41 -06:00
|
|
|
log.Log(INFO, "Look for changes in row", dnsRow.ID)
|
2024-01-18 01:33:28 -06:00
|
|
|
if CFdialog.proxyNode != nil {
|
2024-01-18 01:23:49 -06:00
|
|
|
log.Log(INFO, "Proxy", dnsRow.Proxied, "vs", CFdialog.proxyNode.String())
|
2024-01-18 01:33:28 -06:00
|
|
|
if dnsRow.Proxied == true {
|
2024-01-01 15:31:33 -06:00
|
|
|
CFdialog.proxyNode.SetText("On")
|
|
|
|
} else {
|
|
|
|
CFdialog.proxyNode.SetText("Off")
|
|
|
|
}
|
|
|
|
}
|
2024-01-18 01:33:28 -06:00
|
|
|
if CFdialog.zoneNode != nil {
|
2024-01-01 15:31:33 -06:00
|
|
|
CFdialog.zoneNode.SetText(dnsRow.Domain)
|
|
|
|
}
|
2024-01-18 01:33:28 -06:00
|
|
|
if CFdialog.zoneIdNode != nil {
|
2024-01-01 15:31:33 -06:00
|
|
|
CFdialog.zoneIdNode.SetText(dnsRow.ZoneID)
|
|
|
|
}
|
2024-01-05 19:33:41 -06:00
|
|
|
log.Log(INFO, "zoneIdNode =", dnsRow.ZoneID)
|
2024-01-18 01:33:28 -06:00
|
|
|
if CFdialog.rrNode != nil {
|
2024-01-01 15:31:33 -06:00
|
|
|
CFdialog.rrNode.SetText(dnsRow.ID)
|
|
|
|
}
|
2024-01-18 01:33:28 -06:00
|
|
|
if CFdialog.ValueNode != nil {
|
2024-01-18 01:23:49 -06:00
|
|
|
log.Log(INFO, "Content", dnsRow.Content, "vs", CFdialog.ValueNode.String())
|
2024-01-01 15:31:33 -06:00
|
|
|
CFdialog.ValueNode.SetText(dnsRow.Content)
|
|
|
|
}
|
2024-01-18 01:33:28 -06:00
|
|
|
if CFdialog.NameNode != nil {
|
2024-01-01 15:31:33 -06:00
|
|
|
CFdialog.NameNode.SetText(dnsRow.Name)
|
|
|
|
}
|
2024-01-18 01:33:28 -06:00
|
|
|
if CFdialog.TypeNode != nil {
|
2024-01-01 15:31:33 -06:00
|
|
|
CFdialog.TypeNode.SetText(dnsRow.Type)
|
|
|
|
}
|
|
|
|
|
2024-01-18 01:33:28 -06:00
|
|
|
if CFdialog.urlNode != nil {
|
2024-01-01 15:31:33 -06:00
|
|
|
url := cloudflareURL + dnsRow.ZoneID + "/dns_records/" + dnsRow.ID
|
|
|
|
CFdialog.urlNode.SetText(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
// show the JSON
|
|
|
|
tmp := makeJSON(dnsRow)
|
2024-01-05 19:33:41 -06:00
|
|
|
log.Spew(tmp)
|
2024-01-18 01:33:28 -06:00
|
|
|
if CFdialog.curlNode != nil {
|
2024-01-01 15:31:33 -06:00
|
|
|
pretty, _ := FormatJSON(tmp)
|
2024-01-05 19:33:41 -06:00
|
|
|
log.Spew("http PUT curl =", pretty)
|
2024-01-01 15:31:33 -06:00
|
|
|
CFdialog.curlNode.SetText(pretty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetZonefile(c *ConfigT) *DNSRecords {
|
2024-01-05 19:18:36 -06:00
|
|
|
var url = cloudflareURL + c.ZoneID + "/dns_records/?per_page=100"
|
2024-01-05 19:33:41 -06:00
|
|
|
log.Log(ZONE, "getZonefile()", c.Domain, url)
|
2024-01-01 15:31:33 -06:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
2024-01-05 18:52:47 -06:00
|
|
|
log.Error(err, "http.NewRequest error")
|
2024-01-01 15:31:33 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set headers
|
2024-01-18 01:33:28 -06:00
|
|
|
req.Header.Set("Authorization", "Bearer "+c.Auth)
|
2024-01-05 18:52:47 -06:00
|
|
|
// req.Header.Set("X-Auth-Key", c.Auth)
|
|
|
|
// req.Header.Set("X-Auth-Email", c.Email)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
2024-01-05 19:33:41 -06:00
|
|
|
log.Log(ZONE, "getZonefile() auth, email", c.Auth, c.Email)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
2024-01-05 18:52:47 -06:00
|
|
|
// log.Spew("SPEWING", resp)
|
2024-01-01 15:31:33 -06:00
|
|
|
if err != nil {
|
2024-01-05 18:52:47 -06:00
|
|
|
log.Error(err, "http.Client error")
|
2024-01-01 15:31:33 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2024-01-05 18:52:47 -06:00
|
|
|
log.Error(err, "ioutil.ReadAll() error")
|
2024-01-01 15:31:33 -06:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-05 18:52:47 -06:00
|
|
|
log.Spew("SPEWING", body)
|
|
|
|
pretty, err := FormatJSON(string(body))
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "FormatJSON error")
|
|
|
|
} else {
|
|
|
|
log.Warn(pretty)
|
|
|
|
}
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
var records DNSRecords
|
|
|
|
if err := json.Unmarshal(body, &records); err != nil {
|
2024-01-05 18:52:47 -06:00
|
|
|
log.Error(err, "json.Unmarshal() error")
|
2024-01-01 15:31:33 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-05 19:33:41 -06:00
|
|
|
log.Log(ZONE, "getZonefile() worked", records)
|
2024-01-01 15:31:33 -06:00
|
|
|
return &records
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2024-01-18 01:33:28 -06:00
|
|
|
pass in a DNS Resource Records (the stuff in a zonefile)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
2024-01-18 01:33:28 -06:00
|
|
|
This will talk to the cloudflare API and generate a resource record in the zonefile:
|
2024-01-01 15:31:33 -06:00
|
|
|
|
2024-01-18 01:33:28 -06:00
|
|
|
For example:
|
|
|
|
gitea.wit.com. 3600 IN CNAME git.wit.com.
|
|
|
|
go.wit.com. 3600 IN A 1.1.1.9
|
|
|
|
test.wit.com. 3600 IN NS ns1.wit.com.
|
2024-01-01 15:31:33 -06:00
|
|
|
*/
|
|
|
|
func makeJSON(dnsRow *RRT) string {
|
|
|
|
// make a json record to send on port 80 to cloudflare
|
|
|
|
var tmp string
|
|
|
|
tmp = `{"content": "` + dnsRow.Content + `", `
|
|
|
|
tmp += `"name": "` + dnsRow.Name + `", `
|
|
|
|
tmp += `"type": "` + dnsRow.Type + `", `
|
2024-01-18 01:33:28 -06:00
|
|
|
tmp += `"ttl": "` + "1" + `", `
|
2024-01-01 15:31:33 -06:00
|
|
|
tmp += `"comment": "WIT DNS Control Panel"`
|
2024-01-18 01:33:28 -06:00
|
|
|
tmp += `}`
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
return tmp
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://api.cloudflare.com/client/v4/zones
|
|
|
|
func GetZones(auth, email string) *DNSRecords {
|
|
|
|
var url = "https://api.cloudflare.com/client/v4/zones?per_page=100"
|
2024-01-05 19:33:41 -06:00
|
|
|
log.Log(ZONE, "getZones()", url)
|
2024-01-01 15:31:33 -06:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
2024-01-05 18:52:47 -06:00
|
|
|
log.Error(err, "http.NewRequest error")
|
2024-01-01 15:31:33 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set headers
|
2024-01-18 01:33:28 -06:00
|
|
|
req.Header.Set("Authorization", "Bearer "+auth)
|
2024-01-05 18:52:47 -06:00
|
|
|
// req.Header.Set("X-Auth-Key", auth)
|
|
|
|
// req.Header.Set("X-Auth-Email", email)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
2024-01-05 19:33:41 -06:00
|
|
|
log.Log(ZONE, "getZones() auth, email", auth, email)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2024-01-05 18:52:47 -06:00
|
|
|
log.Error(err, "getZones() http.Client error")
|
2024-01-01 15:31:33 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2024-01-05 18:52:47 -06:00
|
|
|
log.Error(err, "getZones() ioutil.ReadAll() error")
|
2024-01-01 15:31:33 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var records DNSRecords
|
|
|
|
if err := json.Unmarshal(body, &records); err != nil {
|
2024-01-05 18:52:47 -06:00
|
|
|
log.Error(err, "getZones() json.Unmarshal() error")
|
2024-01-01 15:31:33 -06:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-05 18:52:47 -06:00
|
|
|
pretty, err := FormatJSON(string(body))
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "FormatJSON error")
|
|
|
|
} else {
|
|
|
|
log.Warn(pretty)
|
|
|
|
}
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
/* 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\"" }
|
|
|
|
*/
|
|
|
|
|
|
|
|
for _, record := range records.Result {
|
2024-01-05 18:52:47 -06:00
|
|
|
log.Spew("spew record:", record)
|
|
|
|
log.Info("record:", record.Name, record.ID)
|
2024-01-01 15:31:33 -06:00
|
|
|
|
|
|
|
var newc *ConfigT
|
|
|
|
newc = new(ConfigT)
|
|
|
|
|
|
|
|
newc.Domain = record.Name
|
|
|
|
newc.ZoneID = record.ID
|
|
|
|
newc.Auth = auth
|
|
|
|
newc.Email = email
|
|
|
|
|
|
|
|
Config[record.Name] = newc
|
2024-01-05 19:33:41 -06:00
|
|
|
log.Log(ZONE, "zonedrop.AddText:", record.Name, record.ID)
|
2024-01-01 15:31:33 -06:00
|
|
|
}
|
|
|
|
for d, _ := range Config {
|
2024-01-05 19:33:41 -06:00
|
|
|
log.Log(ZONE, "Config entry:", d)
|
2024-01-01 15:31:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return &records
|
|
|
|
}
|