add the cloudflare example app

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-12-20 18:36:58 -06:00
parent 379fee6d96
commit 1b32ded4dc
9 changed files with 709 additions and 0 deletions

View File

@ -103,3 +103,14 @@ go-get:
log:
reset
tail -f /tmp/witgui.* /tmp/guilogfile
# sync repo to the github backup
github:
-git remote add github git@github.com:wit-go/control-panel-dns.git
-git branch -M main
git push origin main
git push origin devel
git push origin --tags
git push github main
git push github devel
git push github --tags

View File

@ -0,0 +1,24 @@
run: build
./cloudflare
build-release:
go get -v -u -x .
go build
./cloudflare
build:
GO111MODULE="off" go get -v -x .
GO111MODULE="off" go build
update:
GO111MODULE="off" go get -v -u -x .
log:
reset
tail -f /tmp/witgui.* /tmp/guilogfile
gocui: build
./cloudflare -gui gocui >/tmp/witgui.log.stderr 2>&1
quiet:
./cloudflare >/tmp/witgui.log.stderr 2>&1

219
examples/cloudflare/api.go Normal file
View File

@ -0,0 +1,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.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
}

View File

@ -0,0 +1,30 @@
// This creates a simple hello world window
package main
import (
"fmt"
arg "github.com/alexflint/go-arg"
"go.wit.com/gui"
log "go.wit.com/gui/log"
)
var args struct {
Foo string
Bar bool
User string `arg:"env:USER"`
Demo bool `help:"run a demo"`
gui.GuiArgs
log.LogArgs
}
func init() {
arg.MustParse(&args)
fmt.Println(args.Foo, args.Bar, args.User)
if (args.Gui != "") {
gui.GuiArg.Gui = args.Gui
}
log.Log(true, "INIT() args.GuiArg.Gui =", gui.GuiArg.Gui)
}

BIN
examples/cloudflare/cloudflare Executable file

Binary file not shown.

28
examples/cloudflare/curl.sh Executable file
View File

@ -0,0 +1,28 @@
#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: basilarchia@gmail.com' \
--data '{
"comment": "WIT DNS Control Panel",
"content": "2001:4860:4860::5555",
"name": "www5",
"proxied": false,
"ttl": 3600,
"type": "AAAA"
}'

View File

@ -0,0 +1,81 @@
// 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()
}

236
examples/cloudflare/main.go Normal file
View File

@ -0,0 +1,236 @@
// This is a simple example
package main
import (
"os"
"fmt"
"log"
"bufio"
"strings"
"go.wit.com/gui"
"go.wit.com/control-panel-dns/cloudflare"
)
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)
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 *configT
newc = new(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]
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
}

View File

@ -0,0 +1,80 @@
// 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 test@wit.com
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