diff options
Diffstat (limited to 'cloudflare')
| -rw-r--r-- | cloudflare/api.go | 235 | ||||
| -rw-r--r-- | cloudflare/cloudflare.go | 123 | ||||
| -rw-r--r-- | cloudflare/http.go | 110 | ||||
| -rw-r--r-- | cloudflare/json.go | 2 | ||||
| -rw-r--r-- | cloudflare/loadZoneWindow.go | 91 | ||||
| -rw-r--r-- | cloudflare/mainWindow.go | 156 | ||||
| -rw-r--r-- | cloudflare/structs.go | 105 |
7 files changed, 722 insertions, 100 deletions
diff --git a/cloudflare/api.go b/cloudflare/api.go new file mode 100644 index 0000000..1769f6b --- /dev/null +++ b/cloudflare/api.go @@ -0,0 +1,235 @@ +// This is a simple example +package cloudflare + +import ( + "log" + "encoding/json" + "io/ioutil" + "net/http" + + "github.com/davecgh/go-spew/spew" +) + +func DoChange() *RRT { + var dnsRow *RRT + dnsRow = new(RRT) + + log.Println("Look for changes in row", dnsRow.ID) + if (CFdialog.proxyNode.S == "On") { + dnsRow.Proxied = true + } else { + dnsRow.Proxied = false + } + dnsRow.Auth = CFdialog.apiNode.S + dnsRow.Email = CFdialog.emailNode.S + + dnsRow.Domain = CFdialog.zoneNode.S + dnsRow.ZoneID = CFdialog.zoneIdNode.S + dnsRow.ID = CFdialog.rrNode.S + + dnsRow.Content = CFdialog.ValueNode.S + dnsRow.Name = CFdialog.NameNode.S + dnsRow.Type = CFdialog.TypeNode.S + dnsRow.url = CFdialog.urlNode.S + + dnsRow.data = makeJSON(dnsRow) + // show the JSON + log.Println(dnsRow) + + if (CFdialog.curlNode != nil) { + pretty, _ := FormatJSON(dnsRow.data) + log.Println("http PUT curl =", pretty) + CFdialog.curlNode.SetText(pretty) + } + return dnsRow +} + +func SetRow(dnsRow *RRT) { + log.Println("Look for changes in row", dnsRow.ID) + if (CFdialog.proxyNode != nil) { + log.Println("Proxy", dnsRow.Proxied, "vs", CFdialog.proxyNode.S) + if (dnsRow.Proxied == true) { + CFdialog.proxyNode.SetText("On") + } else { + CFdialog.proxyNode.SetText("Off") + } + } + if (CFdialog.zoneNode != nil) { + CFdialog.zoneNode.SetText(dnsRow.Domain) + } + if (CFdialog.zoneIdNode != nil) { + CFdialog.zoneIdNode.SetText(dnsRow.ZoneID) + } + log.Println("zoneIdNode =", dnsRow.ZoneID) + if (CFdialog.rrNode != nil) { + CFdialog.rrNode.SetText(dnsRow.ID) + } + if (CFdialog.ValueNode != nil) { + log.Println("Content", dnsRow.Content, "vs", CFdialog.ValueNode.S) + CFdialog.ValueNode.SetText(dnsRow.Content) + } + if (CFdialog.NameNode != nil) { + CFdialog.NameNode.SetText(dnsRow.Name) + } + if (CFdialog.TypeNode != nil) { + CFdialog.TypeNode.SetText(dnsRow.Type) + } + + if (CFdialog.urlNode != nil) { + url := cloudflareURL + dnsRow.ZoneID + "/dns_records/" + dnsRow.ID + CFdialog.urlNode.SetText(url) + } + + // show the JSON + tmp := makeJSON(dnsRow) + log.Println(tmp) + if (CFdialog.curlNode != nil) { + pretty, _ := FormatJSON(tmp) + log.Println("http PUT curl =", pretty) + CFdialog.curlNode.SetText(pretty) + } + + return + log.Println("UPDATE VALUE", CFdialog.NameNode.Name, CFdialog.TypeNode.Name, "to", CFdialog.ValueNode.S) + stuff, result := httpPut(dnsRow) + if (CFdialog.curlNode != nil) { + pretty, _ := FormatJSON(stuff) + log.Println("http PUT curl =", pretty) + CFdialog.curlNode.SetText(pretty) + } + if (CFdialog.resultNode != nil) { + pretty, _ := FormatJSON(result) + log.Println("http PUT result =", pretty) + CFdialog.resultNode.SetText(pretty) + } + // CFdialog.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.com. + go.wit.com. 3600 IN A 1.1.1.9 + test.wit.com. 3600 IN NS ns1.wit.com. +*/ +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 + `", ` + tmp+= `"ttl": "` + "1" + `", ` + tmp += `"comment": "WIT DNS Control Panel"` + tmp += `}` + + return tmp +} + +// 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 + log.Println("zonedrop.AddText:", record.Name, record.ID) + } + for d, _ := range Config { + log.Println("Config entry:", d) + } + + return &records +} diff --git a/cloudflare/cloudflare.go b/cloudflare/cloudflare.go index f9452ce..db98c92 100644 --- a/cloudflare/cloudflare.go +++ b/cloudflare/cloudflare.go @@ -4,63 +4,12 @@ package cloudflare import ( "log" "os" - "bytes" - "io/ioutil" - "net/http" "go.wit.com/gui" ) -/* -curl --request POST \ - --url https://api.cloudflare.com/client/v4/zones/zone_identifier/dns_records \ - --header 'Content-Type: application/json' \ - --header 'X-Auth-Email: ' \ - --data '{ - "content": "198.51.100.4", - "name": "example.com", - "proxied": false, - "type": "A", - "comment": "Domain verification record", - "tags": [ - "owner:dns-team" - ], - "ttl": 3600 -}' -*/ - -// CFdialog is everything you need forcreating -// a new record: name, TTL, type (CNAME, A, etc) -var CFdialog RRT - -// Resource Record (used in a DNS zonefile) -type RRT struct { - cloudflareW *gui.Node // the window node - cloudflareB *gui.Node // the cloudflare button - - TypeNode *gui.Node // CNAME, A, AAAA, ... - NameNode *gui.Node // www, mail, ... - ValueNode *gui.Node // 4.2.2.2, "dkim stuff", etc - - proxyNode *gui.Node // If cloudflare is a port 80 & 443 proxy - ttlNode *gui.Node // just set to 1 which means automatic to cloudflare - curlNode *gui.Node // shows you what you could run via curl - resultNode *gui.Node // what the cloudflare API returned - saveNode *gui.Node // button to send it to cloudflare - - zoneNode *gui.Node // "wit.com" - zoneIdNode *gui.Node // cloudflare zone ID - apiNode *gui.Node // cloudflare API key (from environment var CF_API_KEY) - emailNode *gui.Node // cloudflare email (from environment var CF_API_EMAIL) - - ID string - Type string - Name string - Content string - ProxyS string - Proxied bool - Proxiable bool - Ttl string +func init() { + Config = make(map[string]*ConfigT) } func CreateRR(myGui *gui.Node, zone string, zoneID string) { @@ -77,8 +26,6 @@ func CreateRR(myGui *gui.Node, zone string, zoneID string) { CFdialog.cloudflareB.Enable() } - CFdialog.ID = zoneID - group := CFdialog.cloudflareW.NewGroup("Create a new DNS Resource Record (rr)") // make a grid 2 things wide @@ -100,6 +47,10 @@ func CreateRR(myGui *gui.Node, zone string, zoneID string) { CFdialog.apiNode = grid.NewLabel("type") CFdialog.apiNode.SetText(os.Getenv("CF_API_KEY")) + grid.NewLabel("Resource Record ID") + CFdialog.rrNode = grid.NewLabel("type") + CFdialog.rrNode.SetText(os.Getenv("cloudflare RR id")) + grid.NewLabel("Record Type") CFdialog.TypeNode = grid.NewCombobox("type") CFdialog.TypeNode.AddText("A") @@ -109,7 +60,7 @@ func CreateRR(myGui *gui.Node, zone string, zoneID string) { CFdialog.TypeNode.AddText("MX") CFdialog.TypeNode.AddText("NS") CFdialog.TypeNode.Custom = func () { - CreateCurlRR() + DoChange() } CFdialog.TypeNode.SetText("AAAA") @@ -122,7 +73,7 @@ func CreateRR(myGui *gui.Node, zone string, zoneID string) { CFdialog.NameNode.AddText("blog") CFdialog.NameNode.AddText("ns1") CFdialog.NameNode.Custom = func () { - CreateCurlRR() + DoChange() } CFdialog.NameNode.SetText("www") @@ -131,7 +82,7 @@ func CreateRR(myGui *gui.Node, zone string, zoneID string) { CFdialog.proxyNode.AddText("On") CFdialog.proxyNode.AddText("Off") CFdialog.proxyNode.Custom = func () { - CreateCurlRR() + DoChange() } CFdialog.proxyNode.SetText("Off") @@ -141,15 +92,18 @@ func CreateRR(myGui *gui.Node, zone string, zoneID string) { CFdialog.ValueNode.AddText("2001:4860:4860::8888") CFdialog.ValueNode.AddText("ipv6.wit.com") CFdialog.ValueNode.Custom = func () { - CreateCurlRR() + DoChange() } CFdialog.ValueNode.SetText("127.0.0.1") CFdialog.ValueNode.Expand() + grid.NewLabel("URL") + CFdialog.urlNode = grid.NewLabel("URL") + group.NewLabel("curl") CFdialog.curlNode = group.NewTextbox("curl") CFdialog.curlNode.Custom = func () { - CreateCurlRR() + DoChange() } CFdialog.curlNode.SetText("put the curl text here") @@ -157,17 +111,22 @@ func CreateRR(myGui *gui.Node, zone string, zoneID string) { CFdialog.resultNode.SetText("API response will show here") CFdialog.saveNode = group.NewButton("Save", func () { - url, data := CreateCurlRR() - result := curl(url, data) + dnsRow := DoChange() + result := curlPost(dnsRow) CFdialog.resultNode.SetText(result) + // CreateCurlRR() + // url, data := CreateCurlRR() + // result := curl(url, data) + // CFdialog.resultNode.SetText(result) }) - CFdialog.saveNode.Disable() + // CFdialog.saveNode.Disable() group.Pad() grid.Pad() grid.Expand() } +/* func CreateCurlRR() (string, string) { // enable the Save/Create Button if (CFdialog.saveNode != nil) { @@ -211,7 +170,7 @@ func CreateCurlRR() (string, string) { log.Println("http PUT url =", url) // log.Println("http PUT data =", data) // spew.Dump(data) - pretty, _ := formatJSON(string(data)) + pretty, _ := FormatJSON(string(data)) log.Println("http URL =", url) log.Println("http PUT data =", pretty) if (CFdialog.curlNode != nil) { @@ -220,38 +179,4 @@ func CreateCurlRR() (string, string) { return url, tmp } - -func curl(url string, tmp string) string { - var authKey string = CFdialog.apiNode.S - var email string = CFdialog.emailNode.S - - log.Println("curl() START") - data := []byte(tmp) - req, err := http.NewRequest(http.MethodPost, 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 "" - } - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Println(err) - return "" - } - // log.Println("http PUT body =", body) - // spew.Dump(body) - - log.Println("result =", string(body)) - log.Println("curl() END") - pretty, _ := formatJSON(string(body)) - return pretty -} +*/ diff --git a/cloudflare/http.go b/cloudflare/http.go new file mode 100644 index 0000000..f258f61 --- /dev/null +++ b/cloudflare/http.go @@ -0,0 +1,110 @@ +// This is a simple example +package cloudflare + +import ( + "log" + "fmt" + "io/ioutil" + "net/http" + "bytes" +) + +/* +curl --request POST \ + --url https://api.cloudflare.com/client/v4/zones/zone_identifier/dns_records \ + --header 'Content-Type: application/json' \ + --header 'X-Auth-Email: ' \ + --data '{ + "content": "198.51.100.4", + "name": "example.com", + "proxied": false, + "type": "A", + "comment": "Domain verification record", + "tags": [ + "owner:dns-team" + ], + "ttl": 3600 +}' +*/ + +func httpPut(dnsRow *RRT) (string, string) { + var url string = cloudflareURL + dnsRow.ZoneID + "/dns_records/" + dnsRow.ID + var authKey string = dnsRow.Auth + var email string = dnsRow.Email + + var tmp string + tmp = makeJSON(dnsRow) + 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) +} + +func curlPost(dnsRow *RRT) string { + var authKey string = dnsRow.Auth + var email string = dnsRow.Email + + url := dnsRow.url + tmp := dnsRow.data + + log.Println("curl() START") + log.Println("curl() authkey = ", authKey) + log.Println("curl() email = ", email) + log.Println("curl() url = ", url) + data := []byte(tmp) + req, err := http.NewRequest(http.MethodPost, 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 "" + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Println(err) + return "" + } + // log.Println("http PUT body =", body) + // spew.Dump(body) + + log.Println("result =", string(body)) + log.Println("curl() END") + pretty, _ := FormatJSON(string(body)) + return pretty +} diff --git a/cloudflare/json.go b/cloudflare/json.go index f91b724..b7c71a8 100644 --- a/cloudflare/json.go +++ b/cloudflare/json.go @@ -6,7 +6,7 @@ import ( ) // formatJSON takes an unformatted JSON string and returns a formatted version. -func formatJSON(unformattedJSON string) (string, error) { +func FormatJSON(unformattedJSON string) (string, error) { var jsonData interface{} // Decode the JSON string into an interface diff --git a/cloudflare/loadZoneWindow.go b/cloudflare/loadZoneWindow.go new file mode 100644 index 0000000..71fc2d3 --- /dev/null +++ b/cloudflare/loadZoneWindow.go @@ -0,0 +1,91 @@ +// This is a simple example +package cloudflare + +import ( + "log" + "strconv" + + "go.wit.com/gui" +) + +func LoadZoneWindow(n *gui.Node, c *ConfigT) { + hostname := c.Domain + zoneID := c.ZoneID + log.Println("adding DNS record", hostname) + + newt := n.NewTab(hostname) + vb := newt.NewBox("vBox", false) + newg := vb.NewGroup("more zoneID = " + zoneID) + + // make a grid 6 things wide + grid := newg.NewGrid("gridnuts", 6, 1) + +// grid.NewButton("Type", func () { +// log.Println("sort by Type") +// }) + grid.NewLabel("RR type") + grid.NewLabel("hostname") + + grid.NewLabel("Proxy") + grid.NewLabel("TTL") + grid.NewLabel("Value") + grid.NewLabel("Save") + + records := GetZonefile(c) + for _, record := range records.Result { + var rr RRT // dns zonefile resource record + + // copy all the JSON values into the row record. + rr.ID = record.ID + rr.Type = record.Type + rr.Name = record.Name + rr.Content = record.Content + rr.Proxied = record.Proxied + rr.Proxiable = record.Proxiable + rr.ZoneID = zoneID + // rr.Ttl = record.TTL + + rr.Domain = hostname + rr.ZoneID = zoneID + rr.Auth = c.Auth + rr.Email = c.Email + + grid.NewLabel(record.Type) + grid.NewLabel(record.Name) + + proxy := grid.NewLabel("proxy") + if (record.Proxied) { + proxy.SetText("On") + } else { + proxy.SetText("Off") + } + + var ttl string + if (record.TTL == 1) { + ttl = "Auto" + } else { + ttl = strconv.Itoa(record.TTL) + } + grid.NewLabel(ttl) + + val := grid.NewLabel("Value") + val.SetText(record.Content) + + load := grid.NewButton("Load", nil) + load.Custom = func () { + name := "save stuff to cloudflare for " + rr.ID + log.Println(name) + + /* + rr.Domain = domainWidget.S + rr.ZoneID = zoneWidget.S + rr.Auth = authWidget.S + rr.Email = emailWidget.S + */ + + SetRow(&rr) + } + } + + grid.Pad() +} diff --git a/cloudflare/mainWindow.go b/cloudflare/mainWindow.go new file mode 100644 index 0000000..dddb0b7 --- /dev/null +++ b/cloudflare/mainWindow.go @@ -0,0 +1,156 @@ +// This is a simple example +package cloudflare + +import ( + "os" + "log" + + "go.wit.com/gui" +) + +// This creates a window +func MakeCloudflareWindow(n *gui.Node) { + CFdialog.rootGui = n + var t *gui.Node + + log.Println("buttonWindow() START") + + CFdialog.mainWindow = n.NewWindow("Cloudflare Config") + + // this tab has the master cloudflare API credentials + makeConfigWindow(CFdialog.mainWindow) + + t = CFdialog.mainWindow.NewTab("Zones") + vb := t.NewBox("vBox", false) + g1 := vb.NewGroup("zones") + + // make dropdown list of zones + CFdialog.zonedrop = g1.NewDropdown("zone") + CFdialog.zonedrop.AddText("example.org") + for d, _ := range Config { + CFdialog.zonedrop.AddText(d) + } + CFdialog.zonedrop.AddText("stablesid.org") + + CFdialog.zonedrop.Custom = func () { + domain := CFdialog.zonedrop.S + log.Println("custom dropdown() zone (domain name) =", CFdialog.zonedrop.Name, domain) + if (Config[domain] == nil) { + log.Println("custom dropdown() Config[domain] = nil for domain =", domain) + CFdialog.domainWidget.SetText(domain) + CFdialog.zoneWidget.SetText("") + CFdialog.authWidget.SetText("") + CFdialog.emailWidget.SetText("") + } else { + log.Println("custom dropdown() a =", domain, Config[domain].ZoneID, Config[domain].Auth, Config[domain].Email) + CFdialog.domainWidget.SetText(Config[domain].Domain) + CFdialog.zoneWidget.SetText(Config[domain].ZoneID) + CFdialog.authWidget.SetText(Config[domain].Auth) + CFdialog.emailWidget.SetText(Config[domain].Email) + } + } + + more := g1.NewGroup("data") + showCloudflareCredentials(more) + + makeDebugWindow(CFdialog.mainWindow) +} + +func makeConfigWindow(n *gui.Node) { + t := n.NewTab("Get Zones") + vb := t.NewBox("vBox", false) + g1 := vb.NewGroup("Cloudflare API Config") + + g1.NewLabel("If you have an API key with access to list all of /n your zone files, enter it here. \n \n Alternatively, you can set the enviroment variables: \n env $CF_API_KEY \n env $CF_API_EMAIL\n") + + // make grid to display credentials + grid := g1.NewGrid("credsGrid", 2, 4) // width = 2 + + grid.NewLabel("Auth Key") + aw := grid.NewEntryLine("CF_API_KEY") + aw.SetText(os.Getenv("CF_API_KEY")) + + grid.NewLabel("Email") + ew := grid.NewEntryLine("CF_API_EMAIL") + ew.SetText(os.Getenv("CF_API_EMAIL")) + + var url string = "https://api.cloudflare.com/client/v4/zones/" + grid.NewLabel("Cloudflare API") + grid.NewLabel(url) + + grid.Pad() + + vb.NewButton("getZones()", func () { + log.Println("getZones()") + GetZones(aw.S, ew.S) + for d, _ := range Config { + CFdialog.zonedrop.AddText(d) + } + }) + + vb.NewButton("cloudflare wit.com", func () { + CreateRR(CFdialog.rootGui, "wit.com", "3777302ac4a78cd7fa4f6d3f72086d06") + }) + + t.Pad() + t.Margin() + vb.Pad() + vb.Margin() + g1.Pad() + g1.Margin() +} + +func makeDebugWindow(window *gui.Node) { + t2 := window.NewTab("debug") + g := t2.NewGroup("debug") + g.NewButton("Load 'gocui'", func () { + CFdialog.rootGui.LoadToolkit("gocui") + }) + + g.NewButton("Load 'andlabs'", func () { + CFdialog.rootGui.LoadToolkit("andlabs") + }) + + g.NewButton("gui.DebugWindow()", func () { + gui.DebugWindow() + }) + + g.NewButton("List all Widgets", func () { + CFdialog.rootGui.ListChildren(true) + }) + g.NewButton("Dump all Widgets", func () { + CFdialog.rootGui.Dump() + }) +} + +func showCloudflareCredentials(box *gui.Node) { + // make grid to display credentials + grid := box.NewGrid("credsGrid", 2, 4) // width = 2 + + grid.NewLabel("Domain") + CFdialog.domainWidget = grid.NewEntryLine("CF_API_DOMAIN") + + grid.NewLabel("Zone ID") + CFdialog.zoneWidget = grid.NewEntryLine("CF_API_ZONEID") + + grid.NewLabel("Auth Key") + CFdialog.authWidget = grid.NewEntryLine("CF_API_KEY") + + grid.NewLabel("Email") + CFdialog.emailWidget = grid.NewEntryLine("CF_API_EMAIL") + + var url string = "https://api.cloudflare.com/client/v4/zones/" + grid.NewLabel("Cloudflare API") + grid.NewLabel(url) + + grid.Pad() + + CFdialog.loadButton = box.NewButton("Load Cloudflare DNS zonefile", func () { + var domain ConfigT + domain.Domain = CFdialog.domainWidget.S + domain.ZoneID = CFdialog.zoneWidget.S + domain.Auth = CFdialog.authWidget.S + domain.Email = CFdialog.emailWidget.S + LoadZoneWindow(CFdialog.mainWindow, &domain) + }) +} diff --git a/cloudflare/structs.go b/cloudflare/structs.go new file mode 100644 index 0000000..f0a23d8 --- /dev/null +++ b/cloudflare/structs.go @@ -0,0 +1,105 @@ +// This is a simple example +package cloudflare + +import ( + "go.wit.com/gui" +) + +var cloudflareURL string = "https://api.cloudflare.com/client/v4/zones/" + +// Define a struct to match the JSON structure of the response. +// This structure should be adjusted based on the actual format of the response. +type DNSRecords struct { + Result []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"` + } `json:"result"` +} + +// CFdialog is everything you need forcreating +// a new record: name, TTL, type (CNAME, A, etc) +var CFdialog dialogT + +type dialogT struct { + rootGui *gui.Node // the root node + mainWindow *gui.Node // the window node + zonedrop *gui.Node // the drop down menu of zones + + domainWidget *gui.Node + zoneWidget *gui.Node + authWidget *gui.Node + emailWidget *gui.Node + + loadButton *gui.Node + saveButton *gui.Node + + cloudflareW *gui.Node // the window node + cloudflareB *gui.Node // the cloudflare button + + TypeNode *gui.Node // CNAME, A, AAAA, ... + NameNode *gui.Node // www, mail, ... + ValueNode *gui.Node // 4.2.2.2, "dkim stuff", etc + + rrNode *gui.Node // cloudflare Resource Record ID + proxyNode *gui.Node // If cloudflare is a port 80 & 443 proxy + ttlNode *gui.Node // just set to 1 which means automatic to cloudflare + curlNode *gui.Node // shows you what you could run via curl + resultNode *gui.Node // what the cloudflare API returned + saveNode *gui.Node // button to send it to cloudflare + + zoneNode *gui.Node // "wit.com" + zoneIdNode *gui.Node // cloudflare zone ID + apiNode *gui.Node // cloudflare API key (from environment var CF_API_KEY) + emailNode *gui.Node // cloudflare email (from environment var CF_API_EMAIL) + urlNode *gui.Node // the URL to POST, PUT, DELETE, etc +} + +// Resource Record (used in a DNS zonefile) +type RRT struct { + ID string + Type string + Name string + Content string + ProxyS string + Proxied bool + Proxiable bool + Ttl string + + Domain string + ZoneID string + Auth string + Email string + url string + data string +} + +/* + This is a structure of all the RR's (Resource Records) + in the DNS zonefiile for a hostname. For example: + + For the host test.wit.com: + + test.wit.com A 127.0.0.1 + test.wit.com AAAA + test.wit.com TXT email [email protected] + test.wit.com TXT phone 212-555-1212 + test.wit.com CNAME real.wit.com +*/ +type hostT struct { + hostname string + RRs []ConfigT +} + +type ConfigT struct { + Domain string + ZoneID string + Auth string + Email string +} + +var Config map[string]*ConfigT |
