diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/cloudflare/api.go | 62 | ||||
| -rwxr-xr-x[-rw-r--r--] | examples/cloudflare/curl.sh | 31 | ||||
| -rw-r--r-- | examples/cloudflare/gui.go | 91 | ||||
| -rw-r--r-- | examples/cloudflare/main.go | 32 | ||||
| -rw-r--r-- | examples/cloudflare/structs.go | 17 |
5 files changed, 128 insertions, 105 deletions
diff --git a/examples/cloudflare/api.go b/examples/cloudflare/api.go index 8cf550e..9522b07 100644 --- a/examples/cloudflare/api.go +++ b/examples/cloudflare/api.go @@ -4,10 +4,11 @@ package main import ( "os" "log" + "fmt" "encoding/json" "io/ioutil" "net/http" - "strconv" +// "strconv" "bytes" "github.com/davecgh/go-spew/spew" @@ -19,7 +20,17 @@ func doChange(dnsRow *RRT) { 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) - httpPut(dnsRow) + 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() } @@ -73,24 +84,26 @@ func getZonefile(c *configT) *DNSRecords { go.wit.com. 3600 IN A 1.1.1.9 test.wit.com. 3600 IN NS ns1.wit.com. */ -func httpPut(dnsRow *RRT) { - var url string = cloudflareURL + os.Getenv("CLOUDFLARE_ZONEID") + "/dns_records/" + dnsRow.ID - var authKey string = os.Getenv("CLOUDFLARE_AUTHKEY") - var email string = os.Getenv("CLOUDFLARE_EMAIL") +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 90 to cloudflare + // 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": "` + strconv.Itoa(dnsRow.TTL) + `", ` + 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) + // 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)) @@ -103,19 +116,19 @@ func httpPut(dnsRow *RRT) { resp, err := client.Do(req) if err != nil { log.Println(err) - return + return tmp, fmt.Sprintf("blah err =", err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Println(err) - return + return tmp, fmt.Sprintf("blah err =", err) } - log.Println("http PUT body =", body) - spew.Dump(body) + // log.Println("http PUT body =", body) + // spew.Dump(body) - return + return tmp, string(body) } // https://api.cloudflare.com/client/v4/zones @@ -185,3 +198,22 @@ func getZones(auth, email string) *DNSRecords { 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/curl.sh b/examples/cloudflare/curl.sh index ec8c014..1edd53e 100644..100755 --- a/examples/cloudflare/curl.sh +++ b/examples/cloudflare/curl.sh @@ -1,3 +1,28 @@ -curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \ - -H "Authorization: Bearer AAAPutYourTokenInHereSoYouCanTestItL5Cl3" \ - -H "Content-Type:application/json" +#curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \ +# -H "Authorization: Bearer AAAPutYourTokenInHereSoYouCanTestItL5Cl3" \ +# -H "Content-Type:application/json" + +# https://api.cloudflare.com/client/v4/zones/27b900d9e05cfb9f3a64fecff2497f90/dns_records +# +# { +# "comment": "WIT DNS Control Panel", +# "content": "2001:4860:4860::8888", +# "name": "www", +# "proxied": false, +# "ttl": 3600, +# "type": "AAAA" +#} + +curl --request POST \ + --url https://api.cloudflare.com/client/v4/zones/27b900d9e05cfb9f3a64fecff2497f90/dns_records \ + --header 'Content-Type: application/json' \ + --header 'X-Auth-Key: e08806ad85ef97aebaacd2d7fa462a7d417a7x' \ + --header 'X-Auth-Email: [email protected]' \ + --data '{ + "comment": "WIT DNS Control Panel", + "content": "2001:4860:4860::5555", + "name": "www5", + "proxied": false, + "ttl": 3600, + "type": "AAAA" +}' diff --git a/examples/cloudflare/gui.go b/examples/cloudflare/gui.go index 60fd5fe..ed08482 100644 --- a/examples/cloudflare/gui.go +++ b/examples/cloudflare/gui.go @@ -4,6 +4,8 @@ package main import ( "log" "strconv" + + "git.wit.org/jcarr/control-panel-dns/cloudflare" ) func loadDNS(c *configT) { @@ -11,7 +13,8 @@ func loadDNS(c *configT) { log.Println("adding DNS record", hostname) newt := mainWindow.NewTab(hostname) - newg := newt.NewGroup("more") + vb := newt.NewBox("vBox", false) + newg := vb.NewGroup("more zoneID = " + c.zoneID) // make a grid 6 things wide grid := newg.NewGrid("gridnuts", 6, gridH) @@ -19,41 +22,22 @@ func loadDNS(c *configT) { // grid.NewButton("Type", func () { // log.Println("sort by Type") // }) - typedrop := grid.NewDropdown("type") - typedrop.AddText("A") - typedrop.AddText("AAAA") - typedrop.AddText("CNAME") - typedrop.Custom = func () { - log.Println("custom dropdown() a =", typedrop.Name, typedrop.S) - } - nb := grid.NewButton("Name", func () { - log.Println("sort by Name") - }) - nb.Disable() + grid.NewLabel("RR type") + grid.NewLabel("hostname") - grid.NewButton("Protection", func () { - log.Println("sort proxied") - }) - grid.NewButton("TTL", func () { - log.Println("sort by TTL") - }) - nb = grid.NewButton("Value", func () { - log.Println("sort by Value") - }) - nb.Disable() - nb = grid.NewButton("Save", func () { - log.Println("click below to save") - }) - nb.Disable() + grid.NewLabel("Proxy") + grid.NewLabel("TTL") + grid.NewLabel("Value") + grid.NewLabel("Save") - masterSave = newt.NewButton("Master Save", func () { + masterSave = vb.NewButton("Master Save", func () { log.Println("save stuff to cloudflare") }) masterSave.Disable() records := getZonefile(c) for _, record := range records.Result { - var rr RRT // dns zonefile resource record + var rr cloudflare.RRT // dns zonefile resource record // copy all the JSON values into the row record. rr.ID = record.ID @@ -62,59 +46,34 @@ func loadDNS(c *configT) { rr.Content = record.Content rr.Proxied = record.Proxied rr.Proxiable = record.Proxiable - rr.TTL = record.TTL + // rr.Ttl = record.TTL - rr.typeNode = grid.NewLabel(record.Type) - rr.nameNode = grid.NewEntryLine(record.Name) - rr.nameNode.SetText(record.Name) - rr.nameNode.Disable() + grid.NewLabel(record.Type) + grid.NewLabel(record.Name) - // set proxy or unproxied - rr.proxyNode = grid.NewDropdown("proxy") + proxy := grid.NewLabel("proxy") if (record.Proxied) { - rr.proxyNode.AddText("Proxied") - rr.proxyNode.AddText("DNS") + proxy.SetText("On") } else { - rr.proxyNode.AddText("DNS") - rr.proxyNode.AddText("Proxied") - } - rr.proxyNode.Custom = func () { - log.Println("proxy dropdown() a =", rr.proxyNode.Name, rr.proxyNode.S, rr.ID) - rr.saveNode.Enable() - masterSave.Enable() + proxy.SetText("Off") } - var ttl, short string + var ttl string if (record.TTL == 1) { ttl = "Auto" } else { ttl = strconv.Itoa(record.TTL) } - rr.ttlNode = grid.NewLabel(ttl) - // short = fmt.Sprintf("%80s", record.Content) - short = record.Content - if len(short) > 40 { - short = short[:40] // Slice the first 20 characters - } - - rr.valueNode = grid.NewEntryLine(short) - rr.valueNode.SetText(record.Content) - - rr.valueNode.Custom = func () { - log.Println("value changed =", rr.valueNode.Name, rr.proxyNode.S, rr.ID) - rr.saveNode.Enable() - masterSave.Enable() - } + grid.NewLabel(ttl) - // fmt.Printf("ID: %s, Type: %s, Name: %s, short Content: %s\n", record.ID, record.Type, record.Name, short) - // fmt.Printf("\tproxied: %b, %b, string TTL: %i\n", record.Proxied, record.Proxiable, ttl) + val := grid.NewLabel("Value") + val.SetText(record.Content) - rr.saveNode = grid.NewButton("Save", nil) - rr.saveNode.Disable() - rr.saveNode.Custom = func () { + load := grid.NewButton("Load", nil) + load.Custom = func () { name := "save stuff to cloudflare for " + rr.ID log.Println(name) - doChange(&rr) + // doChange(&rr) } } diff --git a/examples/cloudflare/main.go b/examples/cloudflare/main.go index 4e72668..aeb8570 100644 --- a/examples/cloudflare/main.go +++ b/examples/cloudflare/main.go @@ -7,7 +7,9 @@ import ( "log" "bufio" "strings" + "git.wit.org/wit/gui" + "git.wit.org/jcarr/control-panel-dns/cloudflare" ) var title string = "Cloudflare DNS Control Panel" @@ -44,7 +46,8 @@ func makeCloudflareWindow() { makeConfigTab(mainWindow) t = mainWindow.NewTab("Zones") - g1 := t.NewGroup("zones") + vb := t.NewBox("vBox", false) + g1 := vb.NewGroup("zones") // make dropdown list of zones zonedrop = g1.NewDropdown("zone") @@ -52,6 +55,7 @@ func makeCloudflareWindow() { for d, _ := range config { zonedrop.AddText(d) } + zonedrop.AddText("stablesid.org") zonedrop.Custom = func () { domain := zonedrop.S @@ -82,18 +86,18 @@ func makeConfigTab(window *gui.Node) { 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 $CLOUDFLARE_AUTHKEY \n env $CLOUDFLARE_EMAIL \n env $CLOUDFLARE_URL \n") + 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("CLOUDFLARE_AUTHKEY") - aw.SetText(os.Getenv("CLOUDFLARE_AUTHKEY")) + aw := grid.NewEntryLine("CF_API_KEY") + aw.SetText(os.Getenv("CF_API_KEY")) grid.NewLabel("Email") - ew := grid.NewEntryLine("CLOUDFLARE_EMAIL") - ew.SetText(os.Getenv("CLOUDFLARE_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") @@ -106,6 +110,10 @@ func makeConfigTab(window *gui.Node) { getZones(aw.S, ew.S) }) + vb.NewButton("cloudflare wit.com", func () { + cloudflare.CreateRR(myGui, "wit.com", "3777302ac4a78cd7fa4f6d3f72086d06") + }) + t.Pad() t.Margin() vb.Pad() @@ -144,16 +152,16 @@ func showCloudflareCredentials(box *gui.Node) { grid := box.NewGrid("credsGrid", 2, 4) // width = 2 grid.NewLabel("Domain") - domainWidget = grid.NewEntryLine("CLOUDFLARE_DOMAIN") + domainWidget = grid.NewEntryLine("CF_API_DOMAIN") grid.NewLabel("Zone ID") - zoneWidget = grid.NewEntryLine("CLOUDFLARE_ZONEID") + zoneWidget = grid.NewEntryLine("CF_API_ZONEID") grid.NewLabel("Auth Key") - authWidget = grid.NewEntryLine("CLOUDFLARE_AUTHKEY") + authWidget = grid.NewEntryLine("CF_API_KEY") grid.NewLabel("Email") - emailWidget = grid.NewEntryLine("CLOUDFLARE_EMAIL") + emailWidget = grid.NewEntryLine("CF_API_EMAIL") var url string = "https://api.cloudflare.com/client/v4/zones/" grid.NewLabel("Cloudflare API") @@ -161,10 +169,6 @@ func showCloudflareCredentials(box *gui.Node) { grid.Pad() - saveButton = box.NewButton("Save to config", func () { - }) - saveButton.Disable() - loadButton = box.NewButton("Load Cloudflare DNS zonefile", func () { var domain configT domain.domain = domainWidget.S diff --git a/examples/cloudflare/structs.go b/examples/cloudflare/structs.go index af4d7f3..50647d7 100644 --- a/examples/cloudflare/structs.go +++ b/examples/cloudflare/structs.go @@ -34,20 +34,23 @@ var zonedrop *gui.Node // Resource Record (used in a DNS zonefile) type RRT struct { - typeNode *gui.Node - nameNode *gui.Node - proxyNode *gui.Node - ttlNode *gui.Node - valueNode *gui.Node - saveNode *gui.Node + 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 int + Ttl string } /* |
