summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/cloudflare/api.go219
-rwxr-xr-xexamples/cloudflare/cloudflarebin13104752 -> 0 bytes
-rw-r--r--examples/cloudflare/config.go71
-rw-r--r--examples/cloudflare/gui.go81
-rw-r--r--examples/cloudflare/main.go240
-rw-r--r--examples/cloudflare/structs.go80
6 files changed, 99 insertions, 592 deletions
diff --git a/examples/cloudflare/api.go b/examples/cloudflare/api.go
deleted file mode 100644
index c65fbde..0000000
--- a/examples/cloudflare/api.go
+++ /dev/null
@@ -1,219 +0,0 @@
-// 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.com.
- 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
-}
diff --git a/examples/cloudflare/cloudflare b/examples/cloudflare/cloudflare
deleted file mode 100755
index 8efdd89..0000000
--- a/examples/cloudflare/cloudflare
+++ /dev/null
Binary files differ
diff --git a/examples/cloudflare/config.go b/examples/cloudflare/config.go
new file mode 100644
index 0000000..a65ad25
--- /dev/null
+++ b/examples/cloudflare/config.go
@@ -0,0 +1,71 @@
+// This is a simple example
+package main
+
+import (
+ "os"
+ "log"
+ "bufio"
+ "strings"
+
+ "go.wit.com/control-panel-dns/cloudflare"
+)
+
+func saveConfig() {
+ log.Println("TODO")
+}
+
+func readConfig() {
+ homeDir, err := os.UserHomeDir()
+ if err != nil {
+ log.Println("searchPaths() error. exiting here?")
+ }
+ filename := homeDir + "/" + configfile
+ log.Println("filename =", filename)
+
+ readFileLineByLine(filename)
+ // os.Exit(0)
+}
+
+// readFileLineByLine opens a file and reads through each line.
+func readFileLineByLine(filename string) error {
+ // Open the file.
+ file, err := os.Open(filename)
+ if err != nil {
+ return err
+ }
+ defer file.Close()
+
+ log.Println("readFileLineByLine() =", filename)
+
+ // Create a new Scanner for the file.
+ scanner := bufio.NewScanner(file)
+
+ // Read through each line using scanner.
+ for scanner.Scan() {
+ var newc *cloudflare.ConfigT
+ newc = new(cloudflare.ConfigT)
+
+ line := scanner.Text()
+ parts := strings.Fields(line)
+
+ if (len(parts) < 4) {
+ log.Println("readFileLineByLine() SKIP =", parts)
+ continue
+ }
+
+ newc.Domain = parts[0]
+ newc.ZoneID = parts[1]
+ newc.Auth = parts[2]
+ newc.Email = parts[3]
+
+ cloudflare.Config[parts[0]] = newc
+ log.Println("readFileLineByLine() =", newc.Domain, newc.ZoneID, newc.Auth, newc.Email)
+ }
+
+ // Check for errors during Scan.
+ if err := scanner.Err(); err != nil {
+ return err
+ }
+
+ return nil
+}
diff --git a/examples/cloudflare/gui.go b/examples/cloudflare/gui.go
deleted file mode 100644
index 587a9bc..0000000
--- a/examples/cloudflare/gui.go
+++ /dev/null
@@ -1,81 +0,0 @@
-// This is a simple example
-package main
-
-import (
- "log"
- "strconv"
-
- "go.wit.com/control-panel-dns/cloudflare"
-)
-
-func loadDNS(c *configT) {
- hostname := c.domain
- log.Println("adding DNS record", hostname)
-
- newt := mainWindow.NewTab(hostname)
- vb := newt.NewBox("vBox", false)
- newg := vb.NewGroup("more zoneID = " + c.zoneID)
-
- // make a grid 6 things wide
- grid := newg.NewGrid("gridnuts", 6, gridH)
-
-// 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")
-
- masterSave = vb.NewButton("Master Save", func () {
- log.Println("save stuff to cloudflare")
- })
- masterSave.Disable()
-
- records := getZonefile(c)
- for _, record := range records.Result {
- var rr cloudflare.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.Ttl = record.TTL
-
- 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)
- // doChange(&rr)
- }
- }
-
- grid.Pad()
-}
diff --git a/examples/cloudflare/main.go b/examples/cloudflare/main.go
index 64dabc5..2308478 100644
--- a/examples/cloudflare/main.go
+++ b/examples/cloudflare/main.go
@@ -2,12 +2,6 @@
package main
import (
- "os"
- "fmt"
- "log"
- "bufio"
- "strings"
-
"go.wit.com/gui"
"go.wit.com/control-panel-dns/cloudflare"
)
@@ -15,222 +9,44 @@ import (
var title string = "Cloudflare DNS Control Panel"
var outfile string = "/tmp/guilogfile"
var configfile string = ".config/wit/cloudflare"
-var myGui *gui.Node
-
-var buttonCounter int = 5
-var gridW int = 5
-var gridH int = 3
-
-var mainWindow, more, more2 *gui.Node
-
-func main() {
- config = make(map[string]*configT)
- readConfig()
- myGui = gui.New().Default()
- makeCloudflareWindow()
-
- // This is just a optional goroutine to watch that things are alive
- gui.Watchdog()
- gui.StandardExit()
-}
-
-// This creates a window
-func makeCloudflareWindow() {
- var t *gui.Node
-
- log.Println("buttonWindow() START")
-
- mainWindow = myGui.NewWindow(title).SetText(title)
-
- // this tab has the master cloudflare API credentials
- makeConfigTab(mainWindow)
-
- t = mainWindow.NewTab("Zones")
- vb := t.NewBox("vBox", false)
- g1 := vb.NewGroup("zones")
-
- // make dropdown list of zones
- zonedrop = g1.NewDropdown("zone")
- zonedrop.AddText("example.org")
- for d, _ := range config {
- zonedrop.AddText(d)
- }
- zonedrop.AddText("stablesid.org")
-
- zonedrop.Custom = func () {
- domain := zonedrop.S
- log.Println("custom dropdown() zone (domain name) =", zonedrop.Name, domain)
- if (config[domain] == nil) {
- log.Println("custom dropdown() config[domain] = nil for domain =", domain)
- domainWidget.SetText(domain)
- zoneWidget.SetText("")
- authWidget.SetText("")
- emailWidget.SetText("")
- } else {
- log.Println("custom dropdown() a =", domain, config[domain].zoneID, config[domain].auth, config[domain].email)
- domainWidget.SetText(config[domain].domain)
- zoneWidget.SetText(config[domain].zoneID)
- authWidget.SetText(config[domain].auth)
- emailWidget.SetText(config[domain].email)
- }
- }
-
- more = g1.NewGroup("data")
- showCloudflareCredentials(more)
-
- makeDebugTab(mainWindow)
-}
-
-func makeConfigTab(window *gui.Node) {
- t := window.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)
- })
-
- vb.NewButton("cloudflare wit.com", func () {
- cloudflare.CreateRR(myGui, "wit.com", "3777302ac4a78cd7fa4f6d3f72086d06")
- })
-
- t.Pad()
- t.Margin()
- vb.Pad()
- vb.Margin()
- g1.Pad()
- g1.Margin()
-}
-
-func makeDebugTab(window *gui.Node) {
- t2 := window.NewTab("debug")
- g := t2.NewGroup("debug")
- g.NewButton("Load 'gocui'", func () {
- // this set the xterm and mate-terminal window title. maybe works generally?
- fmt.Println("\033]0;" + title + "blah \007")
- myGui.LoadToolkit("gocui")
- })
-
- g.NewButton("Load 'andlabs'", func () {
- myGui.LoadToolkit("andlabs")
- })
-
- g.NewButton("gui.DebugWindow()", func () {
- gui.DebugWindow()
- })
-
- g.NewButton("List all Widgets", func () {
- myGui.ListChildren(true)
- })
- g.NewButton("Dump all Widgets", func () {
- myGui.Dump()
- })
-}
-
-func showCloudflareCredentials(box *gui.Node) {
- // make grid to display credentials
- grid := box.NewGrid("credsGrid", 2, 4) // width = 2
-
- grid.NewLabel("Domain")
- domainWidget = grid.NewEntryLine("CF_API_DOMAIN")
-
- grid.NewLabel("Zone ID")
- zoneWidget = grid.NewEntryLine("CF_API_ZONEID")
-
- grid.NewLabel("Auth Key")
- authWidget = grid.NewEntryLine("CF_API_KEY")
-
- grid.NewLabel("Email")
- 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()
-
- loadButton = box.NewButton("Load Cloudflare DNS zonefile", func () {
- var domain configT
- domain.domain = domainWidget.S
- domain.zoneID = zoneWidget.S
- domain.auth = authWidget.S
- domain.email = emailWidget.S
- loadDNS(&domain)
- })
-}
-
-func readConfig() {
- homeDir, err := os.UserHomeDir()
- if err != nil {
- log.Println("searchPaths() error. exiting here?")
- }
- filename := homeDir + "/" + configfile
- log.Println("filename =", filename)
+var myGui *gui.Node
- readFileLineByLine(filename)
- // os.Exit(0)
-}
+// var buttonCounter int = 5
+// var gridW int = 5
+// var gridH int = 3
-// readFileLineByLine opens a file and reads through each line.
-func readFileLineByLine(filename string) error {
- // Open the file.
- file, err := os.Open(filename)
- if err != nil {
- return err
- }
- defer file.Close()
+// var mainWindow, more, more2 *gui.Node
- log.Println("readFileLineByLine() =", filename)
+// var cloudflareURL string = "https://api.cloudflare.com/client/v4/zones/"
- // Create a new Scanner for the file.
- scanner := bufio.NewScanner(file)
+/*
+var zonedrop *gui.Node
+var domainWidget *gui.Node
+var masterSave *gui.Node
- // Read through each line using scanner.
- for scanner.Scan() {
- var newc *configT
- newc = new(configT)
+var zoneWidget *gui.Node
+var authWidget *gui.Node
+var emailWidget *gui.Node
- line := scanner.Text()
- parts := strings.Fields(line)
+var loadButton *gui.Node
+var saveButton *gui.Node
+*/
- if (len(parts) < 4) {
- log.Println("readFileLineByLine() SKIP =", parts)
- continue
- }
+func main() {
+ // parse the config file
+ readConfig()
- newc.domain = parts[0]
- newc.zoneID = parts[1]
- newc.auth = parts[2]
- newc.email = parts[3]
+ // initialize a new GO GUI instance
+ myGui = gui.New().Default()
- config[parts[0]] = newc
- log.Println("readFileLineByLine() =", newc.domain, newc.zoneID, newc.auth, newc.email)
- }
+ // draw the cloudflare control panel window
+ cloudflare.MakeCloudflareWindow(myGui)
- // Check for errors during Scan.
- if err := scanner.Err(); err != nil {
- return err
- }
+ // This is just a optional goroutine to watch that things are alive
+ gui.Watchdog()
+ gui.StandardExit()
- return nil
+ // update the config file
+ saveConfig()
}
diff --git a/examples/cloudflare/structs.go b/examples/cloudflare/structs.go
deleted file mode 100644
index fa5516b..0000000
--- a/examples/cloudflare/structs.go
+++ /dev/null
@@ -1,80 +0,0 @@
-// This is a simple example
-package main
-
-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"`
-}
-
-var masterSave *gui.Node
-
-var domainWidget *gui.Node
-var zoneWidget *gui.Node
-var authWidget *gui.Node
-var emailWidget *gui.Node
-
-var loadButton *gui.Node
-var saveButton *gui.Node
-var zonedrop *gui.Node
-
-// Resource Record (used in a DNS zonefile)
-type RRT struct {
- typeNode *gui.Node // CNAME, A, AAAA, ...
- nameNode *gui.Node // www, mail, ...
- proxyNode *gui.Node // If cloudflare is a port 80 & 443 proxy
- ttlNode *gui.Node // just set to 1 which means automatic to cloudflare
- valueNode *gui.Node // 4.2.2.2, "dkim stuff", etc
- 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
-
- ID string
- Type string
- Name string
- Content string
- ProxyS string
- Proxied bool
- Proxiable bool
- Ttl 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