fixed stuff

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-01-03 12:40:31 -06:00
parent 1324717de0
commit e14770e94c
39 changed files with 99 additions and 2468 deletions

3
.gitignore vendored
View File

@ -4,5 +4,4 @@ control-panel-dns
*.swp
/plugins/*
examples/control-panel-digitalocean/control-panel-digitalocean
examples/control-panel-cloudflare/control-panel-cloudflare
control-panel-dns

View File

@ -8,24 +8,14 @@ install:
go install -v go.wit.com/control-panel-dns@latest
# go install -v go.wit.com/control-panel-dns@latest
gocui: build
./control-panel-dns -gui gocui >/tmp/witgui.log.stderr 2>&1
debug: build
./control-panel-dns --verbose --verbose-net --gui-debug
dns: build
./control-panel-dns --verbose-dns
build-release:
reset
go get -v -u -x .
go build
build:
reset
-mkdir -p plugins/
-cp ~/go/src/go.wit.com/gui/toolkit/*.so plugins/
-cp ~/go/src/go.wit.com/gui/toolkits/*.so plugins/
# GO111MODULE="off" go get -v -x .
GO111MODULE="off" go build -v -o control-panel-dns

27
args.go
View File

@ -7,10 +7,10 @@ package main
*/
import (
"log"
"time"
arg "github.com/alexflint/go-arg"
"go.wit.com/gui"
"go.wit.com/log"
"go.wit.com/gui/gui"
)
var args struct {
@ -18,14 +18,29 @@ var args struct {
VerboseDNS bool `arg:"--verbose-dns" help:"debug your dns settings"`
}
var NET log.LogFlag
var SPEW log.LogFlag
func init() {
arg.MustParse(&args)
// fmt.Println(args.Foo, args.Bar, args.User)
NET.B = false
NET.Name = "NET"
NET.Subsystem = "cpdns"
NET.Desc = "Network logging"
NET.Register()
SPEW.B = false
SPEW.Name = "SPEW"
SPEW.Subsystem = "cpdns"
SPEW.Desc = "spew logging"
SPEW.Register()
if gui.ArgDebug() {
log.Println(true, "INIT() gui debug == true")
log.Log(true, "INIT() gui debug == true")
} else {
log.Println(true, "INIT() gui debug == false")
log.Log(true, "INIT() gui debug == false")
}
me.dnsSleep = 500 * time.Millisecond
@ -33,7 +48,7 @@ func init() {
me.artificialSleep = 0.4 // seems to need to exist or GTK crashes. TODO: fix andlabs plugin
me.artificialS = "blah"
log.Println("init() me.artificialSleep =", me.artificialSleep)
log.Println("init() me.artificialS =", me.artificialS)
log.Log(true, "init() me.artificialSleep =", me.artificialSleep)
log.Log(true, "init() me.artificialS =", me.artificialS)
sleep(me.artificialSleep)
}

View File

@ -1,228 +0,0 @@
// This is a simple example
package cloudflare
import (
"encoding/json"
"io/ioutil"
"net/http"
"go.wit.com/log"
)
/*
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
*/
func DoChange() *RRT {
var dnsRow *RRT
dnsRow = new(RRT)
log.Println("DoChange() START")
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)
}
}
func GetZonefile(c *ConfigT) *DNSRecords {
var url = cloudflareURL + c.ZoneID + "/dns_records/?per_page=100"
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?per_page=100"
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
}

View File

@ -1,57 +0,0 @@
/*
This will attempt to create a RR in a DNS zone file.
Create("wit.com", "test.wit.com", "1.1.1.1"
*/
package cloudflare
import (
"os"
"go.wit.com/log"
)
func Create(zone string, hostname string, value string) bool {
log.Info("cloudflare.Create() START", zone, hostname, value)
key := os.Getenv("CF_API_KEY")
email := os.Getenv("CF_API_EMAIL")
if (key == "") {
log.Warn("cloudflare.Create() MISSING environment variable CF_API_KEY")
return false
}
if (email == "") {
log.Warn("cloudflare.Create() MISSING environment variable CF_API_EMAIL")
return false
}
GetZones(key, email)
var z *ConfigT
for d, v := range Config {
log.Info("cloudflare.Create() zone =", d, "value =", v)
if (zone == d) {
z = Config[zone]
log.Info("cloudflare.Create() FOUND ZONE", zone, "ID =", z.ZoneID)
}
}
if (z == nil) {
log.Warn("cloudflare.Create() COULD NOT FIND ZONE", zone)
return false
}
log.Info("cloudflare.Create() FOUND ZONE", z)
// make a json record to send on port 80 to cloudflare
var data string
data = `{"content": "` + value + `", `
data += `"name": "` + hostname + `", `
data += `"type": "AAAA", `
data += `"ttl": "1", `
data += `"comment": "WIT DNS Control Panel"`
data += `}`
result := doCurlCreate(key, email, z.ZoneID, data)
pretty, _ := FormatJSON(result)
log.Info("cloudflare.Create() result =", pretty)
return true
}

View File

@ -1,23 +0,0 @@
#
# this curl POST will create a new DNS resource record (RR) in zone the wit.com
# In this case it will map www3.wit.com to a IPv6 address
# replace the auth key (e088...) and zone ID (27b9...) with the ones from your cloudflare account
#
curl --request POST \
--url https://api.cloudflare.com/client/v4/zones/27llxxPutYourZoneIDherexxx497f90/dns_records \
--header 'Content-Type: application/json' \
--header 'X-Auth-Key: e08806adxxxPutYourAPIKeyHerexxxxa7d417a7x' \
--header 'X-Auth-Email: test@wit.com' \
--data '{
"name": "www3",
"type": "AAAA"
"content": "2001:4860:4860::5555",
"ttl": 3600,
"proxied": false,
"comment": "WIT DNS Control Panel",
}'
# This will verify an API token
curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \
-H "Authorization: Bearer AAAPutYourTokenInHereSoYouCanTestItL5Cl3" \
-H "Content-Type:application/json"

View File

@ -1,64 +0,0 @@
/*
This will attempt to delete a RR in a DNS zone file.
Delete("wit.com", "test.wit.com", "1.1.1.1"
*/
package cloudflare
import (
"os"
"go.wit.com/log"
)
func Delete(zone string, hostname string, value string) bool {
// CFdialog.emailNode.SetText(os.Getenv("CF_API_EMAIL"))
// CFdialog.apiNode.SetText(os.Getenv("CF_API_KEY"))
log.Info("cloudflare.Delete() START", zone, hostname, value)
key := os.Getenv("CF_API_KEY")
email := os.Getenv("CF_API_EMAIL")
if (key == "") {
log.Warn("cloudflare.Delete() MISSING environment variable CF_API_KEY")
return false
}
if (email == "") {
log.Warn("cloudflare.Delete() MISSING environment variable CF_API_EMAIL")
return false
}
GetZones(key, email)
var z *ConfigT
for d, v := range Config {
log.Info("cloudflare.Delete() zone =", d, "value =", v)
if (zone == d) {
z = Config[zone]
log.Info("cloudflare.Delete() FOUND ZONE", zone, "ID =", z.ZoneID)
}
}
if (z == nil) {
log.Warn("cloudflare.Delete() COULD NOT FIND ZONE", zone)
return false
}
log.Info("cloudflare.Delete() FOUND ZONE", z)
records := GetZonefile(z)
for i, record := range records.Result {
if (record.Name == hostname) {
log.Info("cloudflare.Delete() FOUND hostname:", i, record.ID, record.Type, record.Name, record.Content)
}
if (record.Content == value) {
log.Info("cloudflare.Delete() FOUND CONTENT:", i, record.ID, record.Type, record.Name, record.Content)
log.Info("cloudflare.Delete() DO THE ACTUAL cloudflare DELETE here")
result := doCurlDelete(key, email, z.ZoneID, record.ID)
pretty, _ := FormatJSON(result)
log.Info("cloudflare.Delete() result =", pretty)
return true
}
}
log.Info("cloudflare.Delete() NEVER FOUND cloudflare value:", value)
return false
}

View File

@ -1,190 +0,0 @@
// This is a simple example
package cloudflare
import (
"io/ioutil"
"net/http"
"bytes"
"go.wit.com/log"
)
/*
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 doCurlDelete(auth string, email string, zoneId string, rrId string) string {
var err error
var req *http.Request
if zoneId == "" {
log.Warn("doCurlDelete() zoneId == nil")
return ""
}
if rrId == "" {
log.Warn("doCurlDelete() rrId == nil")
return ""
}
data := []byte("")
url := "https://api.cloudflare.com/client/v4/zones/" + zoneId + "/dns_records/" + rrId
req, err = http.NewRequest(http.MethodDelete, url, bytes.NewBuffer(data))
// Set headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Auth-Key", auth)
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 ""
}
return string(body)
}
func doCurlCreate(auth string, email string, zoneId string, data string) string {
var err error
var req *http.Request
if zoneId == "" {
log.Warn("doCurlDelete() zoneId == nil")
return ""
}
url := "https://api.cloudflare.com/client/v4/zones/" + zoneId + "/dns_records/"
log.Info("doCurlCreate() POST url =", url)
log.Info("doCurlCreate() POST Auth =", auth)
log.Info("doCurlCreate() POST Email =", email)
log.Info("doCurlCreate() POST data =", data)
req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer( []byte(data) ))
// Set headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Auth-Key", auth)
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 ""
}
return string(body)
}
func doCurl(method string, rr *RRT) string {
var err error
var req *http.Request
data := []byte(rr.data)
if (method == "PUT") {
req, err = http.NewRequest(http.MethodPut, rr.url, bytes.NewBuffer(data))
} else {
req, err = http.NewRequest(http.MethodPost, rr.url, bytes.NewBuffer(data))
}
// Set headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Auth-Key", rr.Auth)
req.Header.Set("X-Auth-Email", rr.Email)
log.Println("http PUT url =", rr.url)
log.Println("http PUT Auth =", rr.Auth)
log.Println("http PUT Email =", rr.Email)
log.Println("http PUT data =", rr.data)
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 ""
}
return 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("curlPost() START")
log.Println("curlPost() authkey = ", authKey)
log.Println("curlPost() email = ", email)
log.Println("curlPost() 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
}

View File

@ -1,25 +0,0 @@
// This is a simple example
package cloudflare
import (
"encoding/json"
)
// 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

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

View File

@ -1,170 +0,0 @@
// This is a simple example
package cloudflare
import (
"os"
"log"
"go.wit.com/gui"
"go.wit.com/gui/gadgets"
)
// This creates a window
func MakeCloudflareWindow(n *gui.Node) *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)
return 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)
hostname := gadgets.NewBasicEntry(grid, "hostname")
zone := gadgets.NewBasicEntry(grid, "domain name")
grid.Pad()
vb.NewButton("Lookup Hostname", func () {
log.Println("Find all the Resource Records for hostname:", hostname.Get())
log.Println("Find all the Resource Records for zone:", zone.Get())
GetZones(aw.S, ew.S)
for d, v := range Config {
log.Println("Zone =", d, "v =", v)
}
})
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)
})
}

View File

@ -1,157 +0,0 @@
/*
This will let you edit a single Resource Record within
a DNS zone file. For example:
google-dns.wit.com. 1 IN A 8.8.8.8
*/
package cloudflare
import (
"log"
"os"
"go.wit.com/gui"
)
func init() {
Config = make(map[string]*ConfigT)
}
func CreateRR(myGui *gui.Node, zone string, zoneID string) {
if (CFdialog.cloudflareW != nil) {
// skip this if the window has already been created
log.Println("createRR() the cloudflare window already exists")
CFdialog.cloudflareB.Disable()
return
}
CFdialog.cloudflareW = myGui.NewWindow("cloudflare " + zone + " API")
CFdialog.cloudflareW.Custom = func () {
log.Println("createRR() don't really exit here")
CFdialog.cloudflareW = nil
CFdialog.cloudflareB.Enable()
}
group := CFdialog.cloudflareW.NewGroup("Create a new DNS Resource Record (rr)")
// make a grid 2 things wide
grid := group.NewGrid("gridnuts", 2, 3)
grid.NewLabel("zone")
CFdialog.zoneNode = grid.NewLabel("zone")
CFdialog.zoneNode.SetText(zone)
grid.NewLabel("zone ID")
CFdialog.zoneIdNode = grid.NewLabel("zoneID")
CFdialog.zoneIdNode.SetText(zoneID)
grid.NewLabel("shell env $CF_API_EMAIL")
CFdialog.emailNode = grid.NewLabel("type")
CFdialog.emailNode.SetText(os.Getenv("CF_API_EMAIL"))
grid.NewLabel("shell env $CF_API_KEY")
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")
CFdialog.TypeNode.AddText("AAAA")
CFdialog.TypeNode.AddText("CNAME")
CFdialog.TypeNode.AddText("TXT")
CFdialog.TypeNode.AddText("MX")
CFdialog.TypeNode.AddText("NS")
CFdialog.TypeNode.Custom = func () {
DoChange()
}
CFdialog.TypeNode.SetText("AAAA")
grid.NewLabel("Name (usually the hostname)")
CFdialog.NameNode = grid.NewCombobox("name")
CFdialog.NameNode.AddText("www")
CFdialog.NameNode.AddText("mail")
CFdialog.NameNode.AddText("git")
CFdialog.NameNode.AddText("go")
CFdialog.NameNode.AddText("blog")
CFdialog.NameNode.AddText("ns1")
CFdialog.NameNode.Custom = func () {
DoChange()
}
CFdialog.NameNode.SetText("www")
grid.NewLabel("Cloudflare Proxy")
CFdialog.proxyNode = grid.NewDropdown("proxy")
CFdialog.proxyNode.AddText("On")
CFdialog.proxyNode.AddText("Off")
CFdialog.proxyNode.Custom = func () {
DoChange()
}
CFdialog.proxyNode.SetText("Off")
grid.NewLabel("Value")
CFdialog.ValueNode = grid.NewCombobox("value")
CFdialog.ValueNode.AddText("127.0.0.1")
CFdialog.ValueNode.AddText("2001:4860:4860::8888")
CFdialog.ValueNode.AddText("ipv6.wit.com")
CFdialog.ValueNode.Custom = func () {
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 () {
DoChange()
}
CFdialog.curlNode.SetText("put the curl text here")
CFdialog.resultNode = group.NewTextbox("result")
CFdialog.resultNode.SetText("API response will show here")
CFdialog.SaveNode = group.NewButton("Save curlPost()", func () {
dnsRow := DoChange()
result := curlPost(dnsRow)
CFdialog.resultNode.SetText(result)
// CreateCurlRR()
// url, data := CreateCurlRR()
// result := curl(url, data)
// CFdialog.resultNode.SetText(result)
})
// CFdialog.saveNode.Disable()
group.NewButton("New RR doCurl(PUT)", func () {
rr := DoChange()
rr.url = "https://api.cloudflare.com/client/v4/zones/" + rr.ZoneID + "/dns_records"
result := doCurl("POST", rr)
CFdialog.resultNode.SetText(result)
pretty, _ := FormatJSON(result)
log.Println(pretty)
})
group.NewButton("Update RR doCurl(PUT)", func () {
rr := DoChange()
rr.url = "https://api.cloudflare.com/client/v4/zones/" + rr.ZoneID + "/dns_records/" + rr.ID
result := doCurl("PUT", rr)
CFdialog.resultNode.SetText(result)
pretty, _ := FormatJSON(result)
log.Println(pretty)
})
// CFdialog.saveNode.Disable()
group.Pad()
grid.Pad()
grid.Expand()
}

View File

@ -1,105 +0,0 @@
// 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 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

View File

@ -1,36 +0,0 @@
package digitalocean
import (
"context"
"golang.org/x/oauth2"
"github.com/digitalocean/godo"
"go.wit.com/log"
)
func (d *DigitalOcean) listRegions() []godo.Region {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
client := godo.NewClient(oauthClient)
ctx := context.TODO()
// Retrieve all regions.
regions, _, err := client.Regions.List(ctx, &godo.ListOptions{})
if err != nil {
d.err = err
log.Warn(err, "digitalocean.listRegions() failed")
return nil
}
/*
// Print details of each region.
fmt.Println("Available Regions:")
for _, region := range regions {
fmt.Printf("Slug: %s, Name: %s, Available: %v\n", region.Slug, region.Name, region.Available)
}
*/
return regions
}

View File

@ -1,27 +0,0 @@
package digitalocean
// initializes logging and command line options
import (
arg "github.com/alexflint/go-arg"
log "go.wit.com/log"
)
var INFO log.LogFlag
var argDo ArgsDo
// This struct can be used with the go-arg package
type ArgsDo struct {
DigitalOceanTimer bool `arg:"--digitalocean-poll-interval" help:"how often to poll droplet status (default 60 seconds)"`
}
func init() {
arg.Register(&argDo)
INFO.B = false
INFO.Name = "INFO"
INFO.Subsystem = "digitalocean"
INFO.Desc = "Enable log.Info()"
INFO.Register()
}

View File

@ -1,293 +0,0 @@
package digitalocean
import (
"context"
"strings"
"golang.org/x/oauth2"
"github.com/digitalocean/godo"
"go.wit.com/log"
"go.wit.com/gui/gadgets"
// "go.wit.com/gui"
)
/*
// createDroplet creates a new droplet in the specified region with the given name.
func createDroplet(token, name, region, size, image string) (*godo.Droplet, error) {
// Create an OAuth2 token.
tokenSource := &oauth2.Token{
AccessToken: token,
}
// Create an OAuth2 client.
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
// Create a DigitalOcean client with the OAuth2 client.
client := godo.NewClient(oauthClient)
// Define the create request.
createRequest := &godo.DropletCreateRequest{
Name: name,
Region: region,
Size: size,
Image: godo.DropletCreateImage{
Slug: image,
},
}
// Create the droplet.
ctx := context.TODO()
newDroplet, _, err := client.Droplets.Create(ctx, createRequest)
if err != nil {
return nil, err
}
return newDroplet, nil
}
*/
func (d *DigitalOcean) Create(name string, region string, size string, image string) {
// Create a new droplet.
droplet, err := d.createDropletNew(name, region, size, image)
if err != nil {
log.Fatalf("digitalocean.Create() Something went wrong: %s\n", err)
}
log.Infof("digitalocean.Create() droplet ID %d with name %s\n", droplet.ID, droplet.Name)
}
// createDroplet creates a new droplet in the specified region with the given name.
func (d *DigitalOcean) createDropletNew(name, region, size, image string) (*godo.Droplet, error) {
log.Infof("digitalocean.createDropletNew() START name =", name)
// Create an OAuth2 token.
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
// Create an OAuth2 client.
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
// Create a DigitalOcean client with the OAuth2 client.
client := godo.NewClient(oauthClient)
var sshKeys []godo.DropletCreateSSHKey
log.Info("digitalocean.createDropletNew() about to get keys. client =", client)
// Find the key by name.
for i, key := range d.sshKeys {
log.Info("found ssh i =", i, key.Name)
log.Verbose("found ssh key.Name =", key.Name)
log.Verbose("found ssh key.Fingerprint =", key.Fingerprint)
log.Verbose("found ssh key:", key)
/*
sshKeys = []godo.DropletCreateSSHKey{
{ID: key.ID},
}
*/
sshKeys = append(sshKeys, godo.DropletCreateSSHKey{ID: key.ID})
}
// Define the create request.
createRequest := &godo.DropletCreateRequest{
Name: name,
Region: region,
Size: size,
Image: godo.DropletCreateImage{
Slug: image,
},
IPv6: true, // Enable IPv6
SSHKeys: sshKeys, // Add SSH key IDs here
}
// Create the droplet.
ctx := context.TODO()
log.Info("digitalocean.createDropletNew() about to do client.Create(). ctx =", ctx)
newDroplet, _, err := client.Droplets.Create(ctx, createRequest)
log.Infof("digitalocean.createDropletNew() END newDroplet =", newDroplet)
if err != nil {
return nil, err
}
return newDroplet, nil
}
var myCreate *windowCreate
// This is initializes the main DO object
// You can only have one of these
func InitCreateWindow() *windowCreate {
if ! myDo.Ready() {return nil}
if myCreate != nil {
myCreate.Show()
return myCreate
}
myCreate = new(windowCreate)
myCreate.ready = false
myCreate.window = myDo.parent.NewWindow("Create Droplet")
// make a group label and a grid
myCreate.group = myCreate.window.NewGroup("droplets:").Pad()
myCreate.grid = myCreate.group.NewGrid("grid", 2, 1).Pad()
myCreate.name = gadgets.NewBasicEntry(myCreate.grid, "Name").Set("test.wit.com")
myCreate.region = gadgets.NewBasicDropdown(myCreate.grid, "Region")
regions := myDo.listRegions()
// Print details of each region.
log.Info("Available Regions:")
for i, region := range regions {
log.Infof("i: %d, Slug: %s, Name: %s, Available: %v\n", i, region.Slug, region.Name, region.Available)
log.Spew(i, region)
if len(region.Sizes) == 0 {
log.Info("Skipping region. No available sizes region =", region.Name)
} else {
s := region.Name + " (" + region.Slug + ")"
if (myCreate.regionSlug == "") {
myCreate.regionSlug = region.Slug
}
myCreate.region.Add(s)
}
}
myCreate.region.Custom = func() {
s := myCreate.region.Get()
log.Info("create droplet region changed to:", s)
for _, region := range regions {
if s == region.Name {
log.Info("Found region! slug =", myCreate.regionSlug, region)
myCreate.regionSelected = region
log.Info("Found region! Now update all the sizes count =", len(region.Sizes))
for _, size := range region.Sizes {
log.Info("Size: ", size)
}
}
}
}
myCreate.size = gadgets.NewBasicCombobox(myCreate.grid, "Size")
myCreate.size.Add("s-1vcpu-1gb")
myCreate.size.Add("s-1vcpu-1gb-amd")
myCreate.size.Add("s-1vcpu-1gb-intel")
myCreate.size.Add("s-2vcpu-4gb-120gb-intel")
myCreate.size.Set("s-2vcpu-4gb-120gb-intel")
myCreate.size.Custom = func() {
size := myCreate.size.Get()
log.Info("Create() need to verify size exists in region. Digital Ocean size.Slug =", size)
}
myCreate.memory = gadgets.NewBasicDropdown(myCreate.grid, "Memory")
myCreate.memory.Add("1 GB")
myCreate.memory.Add("2 GB")
myCreate.memory.Add("4 GB")
myCreate.memory.Add("8 GB")
myCreate.memory.Add("16 GB")
myCreate.memory.Add("32 GB")
myCreate.memory.Add("64 GB")
myCreate.memory.Add("96 GB")
myCreate.memory.Add("128 GB")
myCreate.memory.Add("256 GB")
myCreate.memory.Custom = func() {
for _, size := range myCreate.regionSelected.Sizes {
log.Info("Size: ", size)
}
myCreate.UpdateSize()
}
myCreate.image = gadgets.NewBasicCombobox(myCreate.grid, "Image")
myCreate.image.Add("debian-12-x64")
myCreate.image.Add("ubuntu-20-04-x64")
myCreate.image.Set("debian-12-x64")
// myCreate.nvme = gadgets.NewBasicCheckbox(myCreate.grid, "NVMe")
myCreate.group.NewLabel("Create Droplet")
// box := myCreate.group.NewBox("vBox", false).Pad()
box := myCreate.group.NewBox("hBox", true).Pad()
box.NewButton("Cancel", func () {
myCreate.Hide()
})
box.NewButton("Create", func () {
name := myCreate.name.Get()
size := myCreate.size.Get()
region := myCreate.regionSlug
image := myCreate.image.Get()
if (region == "") {
log.Info("Create() droplet name =", name, "region =", region, "size =", size, "image", image)
log.Info("Create() region lookup failed")
return
}
log.Info("Create() droplet name =", name, "region =", region, "size =", size, "image", image)
myDo.Create(name, region, size, image)
myCreate.Hide()
})
myCreate.ready = true
myDo.create = myCreate
return myCreate
}
// Find the size
func (d *windowCreate) UpdateSize() {
if ! d.Ready() {return}
log.Info("Now find the size. sizes count =", len(myCreate.regionSelected.Sizes))
var s string
m := myCreate.memory.Get()
switch m {
case "1 GB":
s = "cpu-1gb-"
case "2 GB":
s = "cpu-2gb-"
case "4 GB":
s = "cpu-4gb-"
case "8 GB":
s = "cpu-8gb-"
case "16 GB":
s = "cpu-16gb-"
case "32 GB":
s = "cpu-32gb-"
case "64 GB":
s = "cpu-64gb-"
case "96 GB":
s = "cpu-96gb-"
case "128 GB":
s = "cpu-128gb-"
case "256 GB":
s = "cpu-256gb-"
default:
s = "cpu-4gb-"
}
for _, size := range myCreate.regionSelected.Sizes {
if strings.Contains(size, s) {
log.Info("Found Size! size.Slug =", size, "contains", s)
myCreate.size.Set(size)
return
}
}
log.Info("memory =", myCreate.memory.Get())
}
// Returns true if the status is valid
func (d *windowCreate) Ready() bool {
if d == nil {return false}
return d.ready
}
func (d *windowCreate) Show() {
if ! d.Ready() {return}
log.Info("digitalocean.Show() window")
if d.hidden {
d.window.Show()
}
d.hidden = false
}
func (d *windowCreate) Hide() {
if ! d.Ready() {return}
log.Info("digitalocean.Hide() window")
if ! d.hidden {
d.window.Hide()
}
d.hidden = true
}

View File

@ -1,266 +0,0 @@
package digitalocean
import (
"errors"
"sort"
"strings"
"strconv"
"github.com/digitalocean/godo"
"go.wit.com/log"
// "go.wit.com/gui"
)
func (d *DigitalOcean) NewDroplet(dd *godo.Droplet) *Droplet {
if ! myDo.Ready() {return nil}
// check if the droplet ID already exists
if (d.dropMap[dd.ID] != nil) {
log.Error(errors.New("droplet.NewDroplet() already exists"))
return d.dropMap[dd.ID]
}
droplet := new(Droplet)
droplet.ready = false
droplet.poll = dd // the information polled from the digital ocean API
droplet.ID = dd.ID
droplet.image = dd.Image.Name + " (" + dd.Image.Slug + ")"
if (d.dGrid == nil) {
d.dGrid = d.group.NewGrid("grid", 12, 1).Pad()
}
droplet.nameN = d.dGrid.NewLabel(dd.Name)
d.dGrid.NewLabel(dd.Region.Slug)
var ipv4 []string
var ipv6 []string
for _, network := range dd.Networks.V4 {
if network.Type == "public" {
ipv4 = append(ipv4, network.IPAddress)
}
}
for _, network := range dd.Networks.V6 {
if network.Type == "public" {
ipv6 = append(ipv6, network.IPAddress)
}
}
sort.Strings(ipv4)
sort.Strings(ipv6)
droplet.ipv4 = d.dGrid.NewLabel(strings.Join(ipv4, "\n"))
droplet.ipv6 = d.dGrid.NewLabel(strings.Join(ipv6, "\n"))
droplet.sizeSlugN = d.dGrid.NewLabel(dd.SizeSlug)
droplet.imageN = d.dGrid.NewLabel(dd.Image.Slug)
droplet.statusN = d.dGrid.NewLabel(dd.Status)
droplet.connect = d.dGrid.NewButton("Connect", func () {
droplet.Connect()
})
droplet.edit = d.dGrid.NewButton("Edit", func () {
droplet.Show()
})
droplet.poweroff = d.dGrid.NewButton("Power Off", func () {
droplet.PowerOff()
})
droplet.poweron = d.dGrid.NewButton("Power On", func () {
droplet.PowerOn()
})
droplet.destroy = d.dGrid.NewButton("Destroy", func () {
droplet.Destroy()
})
droplet.ready = true
return droplet
}
func (d *Droplet) Active() bool {
if ! d.Ready() {return false}
log.Info("droplet.Active() status: ", d.poll.Status, "d.statusN.GetText() =", d.statusN.GetText())
if (d.statusN.GetText() == "active") {
return true
}
return false
}
// Returns true if the droplet is finished installing
func (d *Droplet) Ready() bool {
if d == nil {return false}
return d.ready
}
// Returns true if the droplet is running
func (d *Droplet) On() bool {
if ! d.Ready() {return false}
return true
}
func (d *Droplet) HasIPv4() bool {
if ! d.Ready() {return false}
if d.ipv4.GetText() == "" {
return false
}
return true
}
func (d *Droplet) HasIPv6() bool {
if ! d.Ready() {return false}
if d.ipv6.GetText() == "" {
return false
}
return true
}
func (d *Droplet) GetIPv4() string {
if ! d.Ready() {return ""}
return d.ipv4.GetText()
}
func (d *Droplet) GetIPv6() string {
if ! d.Ready() {return ""}
log.Info("droplet GetIPv6 has: n.GetText()", d.ipv6.GetText())
return d.ipv6.GetText()
}
func (d *Droplet) Connect() {
if ! d.Ready() {return}
if d.HasIPv4() {
ipv4 := d.GetIPv4()
log.Info("droplet has IPv4 =", ipv4)
xterm("ssh root@" + ipv4)
return
}
if d.HasIPv6() {
ipv6 := d.GetIPv6()
log.Info("droplet has IPv6 =", ipv6)
xterm("ssh root@[" + ipv6 + "]")
return
}
log.Info("droplet.Connect() here", d.GetIPv4(), d.GetIPv6())
}
func (d *Droplet) Update(dpoll *godo.Droplet) {
if ! d.Exists() {return}
d.poll = dpoll
log.Info("droplet.Update()", dpoll.Name, "dpoll.Status =", dpoll.Status)
log.Spew(dpoll)
d.statusN.SetText(dpoll.Status)
if d.Active() {
d.poweron.Disable()
d.destroy.Disable()
d.connect.Enable()
d.poweroff.Enable()
} else {
d.poweron.Enable()
d.destroy.Enable()
d.poweroff.Disable()
d.connect.Disable()
}
}
func (d *Droplet) PowerOn() {
if ! d.Exists() {return}
log.Info("droplet.PowerOn() should do it here")
myDo.PowerOn(d.ID)
}
func (d *Droplet) PowerOff() {
if ! d.Exists() {return}
log.Info("droplet.PowerOff() here")
myDo.PowerOff(d.ID)
}
func (d *Droplet) Destroy() {
if ! d.Exists() {return}
log.Info("droplet.Destroy() ID =", d.ID, "Name =", d.nameN.GetText())
myDo.deleteDroplet(d)
}
/*
type Droplet struct {
ID int `json:"id,float64,omitempty"`
Name string `json:"name,omitempty"`
Memory int `json:"memory,omitempty"`
Vcpus int `json:"vcpus,omitempty"`
Disk int `json:"disk,omitempty"`
Region *Region `json:"region,omitempty"`
Image *Image `json:"image,omitempty"`
Size *Size `json:"size,omitempty"`
SizeSlug string `json:"size_slug,omitempty"`
BackupIDs []int `json:"backup_ids,omitempty"`
NextBackupWindow *BackupWindow `json:"next_backup_window,omitempty"`
SnapshotIDs []int `json:"snapshot_ids,omitempty"`
Features []string `json:"features,omitempty"`
Locked bool `json:"locked,bool,omitempty"`
Status string `json:"status,omitempty"`
Networks *Networks `json:"networks,omitempty"`
Created string `json:"created_at,omitempty"`
Kernel *Kernel `json:"kernel,omitempty"`
Tags []string `json:"tags,omitempty"`
VolumeIDs []string `json:"volume_ids"`
VPCUUID string `json:"vpc_uuid,omitempty"`
}
*/
func (d *Droplet) Show() {
if ! d.Exists() {return}
log.Info("droplet: ID =", d.ID)
log.Info("droplet: Name =", d.GetName())
log.Info("droplet: Size =", d.GetSize())
log.Info("droplet: Memory =", d.GetMemory())
log.Info("droplet: Disk =", d.GetDisk())
log.Info("droplet: Image =", d.GetImage())
log.Info("droplet: Status =", d.GetStatus())
log.Info("droplet: ", d.poll.Name, d.poll.Image.Slug, d.poll.Region.Slug)
log.Spew(d.poll)
}
func (d *Droplet) Hide() {
if ! d.Exists() {return}
log.Info("droplet.Hide() window")
if ! d.hidden {
// d.window.Hide()
}
d.hidden = true
}
func (d *Droplet) Exists() bool {
if ! myDo.Ready() {return false}
if d == nil {return false}
if d.poll == nil {return false}
return d.ready
}
func (d *Droplet) GetName() string {
if ! d.Ready() {return ""}
return d.nameN.GetText()
}
func (d *Droplet) GetSize() string {
if ! d.Ready() {return ""}
return d.sizeSlugN.GetText()
}
func (d *Droplet) GetMemory() string {
if ! d.Ready() {return ""}
return strconv.Itoa(d.memory)
}
func (d *Droplet) GetDisk() string {
if ! d.Ready() {return ""}
return strconv.Itoa(d.disk)
}
func (d *Droplet) GetImage() string {
if ! d.Ready() {return ""}
return d.imageN.GetText()
}
func (d *Droplet) GetStatus() string {
if ! d.Ready() {return ""}
return d.statusN.GetText()
}

View File

@ -1,24 +0,0 @@
package digitalocean
import (
"encoding/json"
)
// 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

@ -1,39 +0,0 @@
package digitalocean
import (
"context"
"golang.org/x/oauth2"
"github.com/digitalocean/godo"
"go.wit.com/log"
)
// func (d *DigitalOcean) ListDroplets() bool {
func (d *DigitalOcean) ListSSHKeyID() error {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
client := godo.NewClient(oauthClient)
// List all keys.
keys, _, err := client.Keys.List(context.Background(), &godo.ListOptions{})
if err != nil {
return err
}
d.sshKeys = keys
// Find the key by name.
for _, key := range keys {
log.Info("found ssh wierd", key.Name)
log.Verbose("found ssh key:", key)
}
/*
sshKeys := []godo.DropletCreateSSHKey{
{ID: 22994569},
{ID: 333},
}
*/
// return fmt.Errorf("SSH Key not found")
return nil
}

View File

@ -1,78 +0,0 @@
package digitalocean
import (
"os"
"go.wit.com/log"
"go.wit.com/gui"
)
var myDo *DigitalOcean
// This is initializes the main DO object
// You can only have one of these
func New(p *gui.Node) *DigitalOcean {
if myDo != nil {return myDo}
myDo = new(DigitalOcean)
myDo.ready = false
myDo.parent = p
myDo.dropMap = make(map[int]*Droplet)
// Your personal API token from DigitalOcean.
myDo.token = os.Getenv("DIGITALOCEAN_TOKEN")
myDo.window = p.NewWindow("DigitalOcean Control Panel")
// make a group label and a grid
myDo.group = myDo.window.NewGroup("droplets:").Pad()
myDo.grid = myDo.group.NewGrid("grid", 2, 1).Pad()
myDo.ready = true
myDo.Hide()
return myDo
}
// Returns true if the status is valid
func (d *DigitalOcean) Ready() bool {
if d == nil {return false}
return d.ready
}
func (d *DigitalOcean) Show() {
if ! d.Ready() {return}
log.Info("digitalocean.Show() window")
if d.hidden {
d.window.Show()
}
d.hidden = false
}
func (d *DigitalOcean) Hide() {
if ! d.Ready() {return}
log.Info("digitalocean.Hide() window")
if ! d.hidden {
d.window.Hide()
}
d.hidden = true
}
func (d *DigitalOcean) Update() bool {
if ! d.Ready() {return false}
d.ListSSHKeyID()
if d.ListDroplets() {
for _, droplet := range d.dpolled {
// check if the droplet ID already exists
if (d.dropMap[droplet.ID] == nil) {
d.dropMap[droplet.ID] = d.NewDroplet(&droplet)
} else {
log.Info("droplet.Update()", droplet.ID, droplet.Name, "already exists")
d.dropMap[droplet.ID].Update(&droplet)
continue
}
}
} else {
log.Error(d.err, "Error listing droplets")
return false
}
return true
}

View File

@ -1,50 +0,0 @@
package digitalocean
import (
"context"
"golang.org/x/oauth2"
"github.com/digitalocean/godo"
)
// ListDroplets fetches and prints out the droplets along with their IPv4 and IPv6 addresses.
func (d *DigitalOcean) ListDroplets() bool {
// OAuth token for authentication.
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
// OAuth2 client.
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
// DigitalOcean client.
client := godo.NewClient(oauthClient)
// Context.
ctx := context.TODO()
// List all droplets.
d.dpolled, _, d.err = client.Droplets.List(ctx, &godo.ListOptions{})
if d.err != nil {
return false
}
// Iterate over droplets and print their details.
/*
for _, droplet := range d.polled {
fmt.Printf("Droplet: %s\n", droplet.Name)
for _, network := range droplet.Networks.V4 {
if network.Type == "public" {
fmt.Printf("IPv4: %s\n", network.IPAddress)
}
}
for _, network := range droplet.Networks.V6 {
if network.Type == "public" {
fmt.Printf("IPv6: %s\n", network.IPAddress)
}
}
fmt.Println("-------------------------")
}
*/
return true
}

View File

@ -1,82 +0,0 @@
package digitalocean
import (
"context"
"golang.org/x/oauth2"
"github.com/digitalocean/godo"
"go.wit.com/log"
)
func (d *DigitalOcean) PowerOn(dropletID int) error {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
client := godo.NewClient(oauthClient)
ctx := context.TODO()
// Create a request to power on the droplet.
_, _, err := client.DropletActions.PowerOn(ctx, dropletID)
if err != nil {
return err
}
log.Printf("Power-on signal sent to droplet with ID: %d\n", dropletID)
return nil
}
func (d *DigitalOcean) PowerOff(dropletID int) error {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
client := godo.NewClient(oauthClient)
ctx := context.TODO()
// Create a request to power on the droplet.
_, _, err := client.DropletActions.PowerOff(ctx, dropletID)
if err != nil {
return err
}
log.Printf("Power-off signal sent to droplet with ID: %d\n", dropletID)
return nil
}
/*
func (d *DigitalOcean) Destroy(dropletID int) error {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
client := godo.NewClient(oauthClient)
ctx := context.TODO()
// Create a request to power on the droplet.
_, _, err := client.DropletActions.Delete(ctx, dropletID)
if err != nil {
return err
}
log.Printf("Destroy sent to droplet with ID: %d\n", dropletID)
return nil
}
*/
// createDroplet creates a new droplet in the specified region with the given name.
func (d *DigitalOcean) deleteDroplet(drop *Droplet) error {
// Create an OAuth2 token.
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
// Create an OAuth2 client.
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
// Create a DigitalOcean client with the OAuth2 client.
client := godo.NewClient(oauthClient)
ctx := context.TODO()
log.Warn("deleteDroplet() going to delete ID =", drop.ID, "Name =", drop.GetName())
response, err := client.Droplets.Delete(ctx, drop.ID)
log.Warn(response)
return err
}

View File

@ -1,91 +0,0 @@
/*
The Digital Ocean Struct
*/
package digitalocean
import (
"github.com/digitalocean/godo"
"go.wit.com/gui"
"go.wit.com/gui/gadgets"
)
type DigitalOcean struct {
ready bool
hidden bool
err error
token string // You're Digital Ocean API key
dpolled []godo.Droplet
sshKeys []godo.Key
dropMap map[int]*Droplet
create *windowCreate
parent *gui.Node // should be the root of the 'gui' package binary tree
window *gui.Node // our window for displaying digital ocean droplets
group *gui.Node
grid *gui.Node
dGrid *gui.Node // the grid for the droplets
// Primary Directives
status *gadgets.OneLiner
summary *gadgets.OneLiner
statusIPv4 *gadgets.OneLiner
statusIPv6 *gadgets.OneLiner
}
type windowCreate struct {
ready bool
hidden bool
err error
parent *gui.Node // should be the root of the 'gui' package binary tree
window *gui.Node // our window for displaying digital ocean droplets
group *gui.Node
grid *gui.Node
regionSelected godo.Region
regionSlug string
tag *gadgets.OneLiner
name *gadgets.BasicEntry
region *gadgets.BasicDropdown
size *gadgets.BasicCombobox
memory *gadgets.BasicDropdown
image *gadgets.BasicCombobox
// nvme *gadgets.BasicCheckbox
}
type ipButton struct {
ip *gui.Node
c *gui.Node
}
type Droplet struct {
ID int
image string
memory int
disk int
ready bool
hidden bool
err error
poll *godo.Droplet // store what the digital ocean API returned
nameN *gui.Node
sizeSlugN *gui.Node
statusN *gui.Node
imageN *gui.Node
destroy *gui.Node
connect *gui.Node
poweron *gui.Node
poweroff *gui.Node
edit *gui.Node
ipv4 *gui.Node
ipv6 *gui.Node
}

View File

@ -1,31 +0,0 @@
package digitalocean
import (
"os/exec"
"go.wit.com/log"
)
var geom string = "120x30+500+500"
func xterm(cmd string) {
var tmp []string
var argsXterm = []string{"nohup", "xterm", "-geometry", geom}
// tmp = append(argsXterm, "-hold", "-e", cmd)
tmp = append(argsXterm, "-e", cmd)
log.Println("xterm cmd=", cmd)
go runCommand(tmp)
}
func runCommand(cmdArgs []string) {
log.Println("runCommand() START", cmdArgs)
process := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
// process := exec.Command("xterm", "-e", "ping localhost")
log.Println("runCommand() process.Start()")
process.Start()
log.Println("runCommand() process.Wait()")
err := process.Wait()
log.Error(err, "on process.Wait")
log.Println("runCommand() NEED TO CHECK THE TIME HERE TO SEE IF THIS WORKED")
log.Println("runCommand() OTHERWISE INFORM THE USER")
log.Println("runCommand() END", cmdArgs)
}

View File

@ -18,9 +18,10 @@ import (
"time"
"strconv"
"reflect"
"errors"
"go.wit.com/log"
"go.wit.com/gui"
"go.wit.com/gui/gui"
"go.wit.com/gui/gadgets"
"go.wit.com/shell"
@ -127,7 +128,7 @@ func NewDigStatusWindow(p *gui.Node) *digStatus {
func (ds *digStatus) Update() {
log.Info("digStatus() Update() START")
if ds == nil {
log.Error("digStatus() Update() ds == nil")
log.Error(errors.New("digStatus() Update() ds == nil"))
return
}
duration := timeFunction(func () {
@ -206,7 +207,7 @@ func (ds *digStatus) set(a any, s string) {
ol.Set(s)
return
}
log.Error("unknown type TypeOf(a) =", reflect.TypeOf(a), "a =", a)
log.Warn("unknown type TypeOf(a) =", reflect.TypeOf(a), "a =", a)
os.Exit(0)
}
@ -216,12 +217,12 @@ func (ds *digStatus) updateDnsStatus() {
log.Info("updateDnsStatus() START")
if (ds == nil) {
log.Error("updateDnsStatus() not initialized yet. ds == nil")
log.Error(errors.New("updateDnsStatus() not initialized yet. ds == nil"))
return
}
if (! ds.ready) {
log.Error("updateDnsStatus() not ready yet")
log.Error(errors.New("updateDnsStatus() not ready yet"))
return
}

View File

@ -1,27 +0,0 @@
run: build
./control-panel-cloudflare
build-release:
go get -v -u -x .
go build
./control-panel-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
debug: build
./control-panel-cloudflare --gui-debug
gocui: build
./control-panel-cloudflare --gui gocui >/tmp/witgui.log.stderr 2>&1
quiet:
./control-panel-cloudflare >/tmp/witgui.log.stderr 2>&1

View File

@ -1,17 +0,0 @@
package main
/*
this enables command line options from other packages like 'gui' and 'log'
*/
import (
arg "github.com/alexflint/go-arg"
"go.wit.com/gui"
"go.wit.com/log"
)
func init() {
arg.MustParse()
log.Bool(true, "INIT() args.ArgDebug =", gui.ArgDebug())
}

View File

@ -1,72 +0,0 @@
package main
import (
"os"
"log"
"bufio"
"strings"
"go.wit.com/control-panel-dns/cloudflare"
)
var configfile string = ".config/wit/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
}

View File

@ -1,35 +0,0 @@
package main
import (
"go.wit.com/log"
"go.wit.com/gui"
"go.wit.com/control-panel-dns/cloudflare"
)
var title string = "Cloudflare DNS Control Panel"
var myGui *gui.Node
// var cloudflareURL string = "https://api.cloudflare.com/client/v4/zones/"
func main() {
// send all log() output to a file in /tmp
log.SetTmp()
// parse the config file
readConfig()
// initialize a new GO GUI instance
myGui = gui.New().Default()
// draw the cloudflare control panel window
win := cloudflare.MakeCloudflareWindow(myGui)
win.SetText(title)
// This is just a optional goroutine to watch that things are alive
gui.Watchdog()
gui.StandardExit()
// update the config file
saveConfig()
}

View File

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

View File

@ -1,17 +0,0 @@
package main
/*
this enables command line options from other packages like 'gui' and 'log'
*/
import (
arg "github.com/alexflint/go-arg"
"go.wit.com/gui"
"go.wit.com/log"
)
func init() {
arg.MustParse()
log.Bool(true, "INIT() args.ArgDebug =", gui.ArgDebug())
}

View File

@ -1,64 +0,0 @@
package main
import (
"go.wit.com/log"
"go.wit.com/gui"
"go.wit.com/control-panel-dns/digitalocean"
)
var title string = "Cloud App"
var myGui *gui.Node
var myDo *digitalocean.DigitalOcean
func main() {
// initialize a new GO GUI instance
myGui = gui.New().Default()
// draw the main window
cloudApp(myGui)
log.Sleep(1)
myDo = digitalocean.New(myGui)
myDo.Update()
myDo.Show()
// This is just a optional goroutine to watch that things are alive
gui.Watchdog()
gui.StandardExit()
}
func cloudApp(n *gui.Node) *gui.Node {
win := n.NewWindow(title)
// make a group label and a grid
group := win.NewGroup("data").Pad()
grid := group.NewGrid("grid", 2, 1).Pad()
grid.NewButton("New()", func () {
myDo = digitalocean.New(myGui)
})
grid.NewLabel("initializes the DO golang gui package")
grid.NewButton("Show", func () {
myDo.Show()
})
grid.NewLabel("will show the DO window")
grid.NewButton("Hide", func () {
myDo.Hide()
})
grid.NewLabel("will hide the DO window")
grid.NewButton("Update", func () {
myDo.Update()
})
grid.NewLabel("polls DO via the API to find the state of all your droplets")
grid.NewButton("Create", func () {
// myDo.Create("jcarr.wit.com")
digitalocean.InitCreateWindow()
})
grid.NewLabel("makes a new droplet")
return win
}

7
gui.go
View File

@ -9,11 +9,12 @@ import (
"net"
"strings"
"go.wit.com/gui"
"go.wit.com/gui/gadgets"
"go.wit.com/log"
"go.wit.com/shell"
"go.wit.com/control-panel-dns/cloudflare"
"go.wit.com/gui/gui"
"go.wit.com/gui/gadgets"
"go.wit.com/gui/cloudflare"
)
// This setups up the dns control panel window

View File

@ -7,7 +7,7 @@ import (
"go.wit.com/log"
"go.wit.com/shell"
"go.wit.com/control-panel-dns/cloudflare"
"go.wit.com/gui/cloudflare"
"github.com/miekg/dns"
// will try to get this hosts FQDN

View File

@ -11,11 +11,12 @@ import (
"time"
"reflect"
"strings"
"errors"
"go.wit.com/log"
"go.wit.com/gui"
"go.wit.com/gui/gui"
"go.wit.com/gui/gadgets"
"go.wit.com/control-panel-dns/cloudflare"
"go.wit.com/gui/cloudflare"
)
type hostnameStatus struct {
@ -156,7 +157,7 @@ func (hs *hostnameStatus) createDNSrecord(value string) bool {
func (hs *hostnameStatus) Update() {
log.Info("hostnameStatus() Update() START")
if hs == nil {
log.Error("hostnameStatus() Update() hs == nil")
log.Error(errors.New("hostnameStatus() Update() hs == nil"))
return
}
duration := timeFunction(func () {
@ -237,7 +238,7 @@ func (hs *hostnameStatus) set(a any, s string) {
ol.Set(s)
return
}
log.Error("unknown type TypeOf(a) =", reflect.TypeOf(a), "a =", a)
log.Warn("unknown type TypeOf(a) =", reflect.TypeOf(a), "a =", a)
os.Exit(0)
}

4
log.go
View File

@ -3,7 +3,7 @@ package main
import (
"log"
"reflect"
witlog "go.wit.com/gui/log"
witlog "go.wit.com/log"
)
var LogPrefix = "ipv6cp" // ipv6 control panel debugging line
@ -19,7 +19,7 @@ var LogNet bool = false // general network debugging
var LogProc bool = false // turn on /proc debugging output
var LogExec bool = false // turn on os.Exec() debugging
var SPEW witlog.Spewt
// var SPEW witlog.Spewt
// var log interface{}

View File

@ -14,7 +14,9 @@ import (
"embed"
"go.wit.com/log"
"go.wit.com/gui"
"go.wit.com/gui/gui"
"go.wit.com/gui/debugger"
"github.com/miekg/dns"
)
@ -44,6 +46,11 @@ func main() {
sleep(me.artificialSleep)
setupControlPanelWindow()
sleep(2)
if gui.ArgDebug() {
debugger.DebugWindow(myGui)
}
// forever monitor for network and dns changes
sleep(me.artificialSleep)
checkNetworkChanges()

96
net.go
View File

@ -5,6 +5,8 @@ import (
// "log"
"net"
"strings"
"go.wit.com/log"
)
// this doesn't work
@ -16,21 +18,21 @@ func watchNetworkInterfaces() {
// Set up a notification channel
notification := make(chan net.Interface)
debug(LogNet, "watchNet()")
log.Log(NET, "watchNet()")
// Start goroutine to watch for changes
go func() {
debug(LogNet, "watchNet() func")
log.Log(NET, "watchNet() func")
for {
debug(LogNet, "forever loop start")
log.Log(NET, "forever loop start")
// Check for changes in each interface
for _, i := range interfaces {
debug(LogNet, "something on i =", i)
log.Log(NET, "something on i =", i)
if status := i.Flags & net.FlagUp; status != 0 {
notification <- i
debug(LogNet, "something on i =", i)
log.Log(NET, "something on i =", i)
}
}
debug(LogNet, "forever loop end")
log.Log(NET, "forever loop end")
}
}()
}
@ -42,20 +44,20 @@ func IsIPv6(address string) bool {
func (t *IPtype) IsReal() bool {
if (t.ip.IsPrivate() || t.ip.IsLoopback() || t.ip.IsLinkLocalUnicast()) {
debug(LogNet, "\t\tIP is Real = false")
log.Log(NET, "\t\tIP is Real = false")
return false
} else {
debug(LogNet, "\t\tIP is Real = true")
log.Log(NET, "\t\tIP is Real = true")
return true
}
}
func IsReal(ip *net.IP) bool {
if (ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast()) {
debug(LogNet, "\t\tIP is Real = false")
log.Log(NET, "\t\tIP is Real = false")
return false
} else {
debug(LogNet, "\t\tIP is Real = true")
log.Log(NET, "\t\tIP is Real = true")
return true
}
}
@ -72,7 +74,7 @@ func renameInterface(i *net.Interface) {
func checkInterface(i net.Interface) {
val, ok := me.ifmap[i.Index]
if ! ok {
debug(i.Name, "is a new network interface. The linux kernel index =", i.Index)
log.Info(i.Name, "is a new network interface. The linux kernel index =", i.Index)
me.ifmap[i.Index] = new(IFtype)
me.ifmap[i.Index].gone = false
me.ifmap[i.Index].iface = &i
@ -84,9 +86,9 @@ func checkInterface(i net.Interface) {
return
}
me.ifmap[i.Index].gone = false
debug(LogNet, "me.ifmap[i] does exist. Need to compare everything.", i.Index, i.Name, val.iface.Index, val.iface.Name)
log.Log(NET, "me.ifmap[i] does exist. Need to compare everything.", i.Index, i.Name, val.iface.Index, val.iface.Name)
if (val.iface.Name != i.Name) {
debug(val.iface.Name, "has changed to it's name to", i.Name)
log.Info(val.iface.Name, "has changed to it's name to", i.Name)
me.ifmap[i.Index].iface = &i
me.changed = true
if (me.Interfaces != nil) {
@ -141,14 +143,14 @@ func checkDNS() (map[string]*IPtype, map[string]*IPtype) {
ipt = "IPv6"
}
if (t.IsReal()) {
debug("\tIP is Real ", ipt, i.Index, i.Name, s)
log.Info("\tIP is Real ", ipt, i.Index, i.Name, s)
if (t.ipv6) {
ipv6s[s] = t
} else {
ipv4s[s] = t
}
} else {
debug("\tIP is not Real", ipt, i.Index, i.Name, s)
log.Info("\tIP is not Real", ipt, i.Index, i.Name, s)
}
}
return ipv6s, ipv4s
@ -156,14 +158,14 @@ func checkDNS() (map[string]*IPtype, map[string]*IPtype) {
// Will figure out if an IP address is new
func checkIP(ip *net.IPNet, i net.Interface) bool {
debug(LogNet, "\t\taddr.(type) = *net.IPNet")
debug(LogNet, "\t\taddr.(type) =", ip)
log.Log(NET, "\t\taddr.(type) = *net.IPNet")
log.Log(NET, "\t\taddr.(type) =", ip)
var realip string
realip = ip.IP.String()
val, ok := me.ipmap[realip]
if ok {
debug(LogNet, val.ipnet.IP.String(), "is already a defined IP address")
log.Log(NET, val.ipnet.IP.String(), "is already a defined IP address")
me.ipmap[realip].gone = false
return false
}
@ -190,50 +192,50 @@ func checkIP(ip *net.IPNet, i net.Interface) bool {
}
}
if (IsReal(&ip.IP)) {
debug("\tIP is Real ", t, i.Index, i.Name, realip)
log.Info("\tIP is Real ", t, i.Index, i.Name, realip)
} else {
debug("\tIP is not Real", t, i.Index, i.Name, realip)
log.Info("\tIP is not Real", t, i.Index, i.Name, realip)
}
debug(LogNet, "\t\tIP is IsPrivate() =", ip.IP.IsPrivate())
debug(LogNet, "\t\tIP is IsLoopback() =", ip.IP.IsLoopback())
debug(LogNet, "\t\tIP is IsLinkLocalUnicast() =", ip.IP.IsLinkLocalUnicast())
// debug("HERE HERE", "realip =", realip, "me.ip[realip]=", me.ipmap[realip])
log.Log(NET, "\t\tIP is IsPrivate() =", ip.IP.IsPrivate())
log.Log(NET, "\t\tIP is IsLoopback() =", ip.IP.IsLoopback())
log.Log(NET, "\t\tIP is IsLinkLocalUnicast() =", ip.IP.IsLinkLocalUnicast())
// log.Info("HERE HERE", "realip =", realip, "me.ip[realip]=", me.ipmap[realip])
return true
}
func scanInterfaces() {
debug(LogNet, "scanInterfaces() START")
log.Log(NET, "scanInterfaces() START")
ifaces, _ := net.Interfaces()
// me.ifnew = ifaces
debug(LogNet, SPEW, ifaces)
log.Log(NET, SPEW, ifaces)
for _, i := range ifaces {
addrs, _ := i.Addrs()
// debug("range ifaces = ", i)
// log.Info("range ifaces = ", i)
checkInterface(i)
debug(LogNet, "*net.Interface.Name = ", i.Name, i.Index)
debug(LogNet, SPEW, i)
debug(LogNet, SPEW, addrs)
log.Log(NET, "*net.Interface.Name = ", i.Name, i.Index)
log.Log(NET, SPEW, i)
log.Log(NET, SPEW, addrs)
for _, addr := range addrs {
debug(LogNet, "\taddr =", addr)
debug(LogNet, SPEW, addrs)
log.Log(NET, "\taddr =", addr)
log.Log(NET, SPEW, addrs)
ips, _ := net.LookupIP(addr.String())
debug(LogNet, "\tLookupIP(addr) =", ips)
log.Log(NET, "\tLookupIP(addr) =", ips)
switch v := addr.(type) {
case *net.IPNet:
if checkIP(v, i) {
debug(true, "scanInterfaces() IP is new () i =", v.IP.String())
log.Log(true, "scanInterfaces() IP is new () i =", v.IP.String())
}
default:
debug(LogNet, "\t\taddr.(type) = NO IDEA WHAT TO DO HERE v =", v)
log.Log(NET, "\t\taddr.(type) = NO IDEA WHAT TO DO HERE v =", v)
}
}
}
if deleteChanges() {
me.changed = true
debug(LogNow, "deleteChanges() detected network changes")
log.Log(NET, "deleteChanges() detected network changes")
}
updateRealAAAA()
debug(LogNet, "scanInterfaces() END")
log.Log(NET, "scanInterfaces() END")
}
// displays the IP address found on your network interfaces
@ -243,22 +245,22 @@ func updateRealAAAA() {
for s, t := range me.ipmap {
if (t.ipv4) {
all4 += s + "\n"
debug(LogNet, "IPv4 =", s)
log.Log(NET, "IPv4 =", s)
} else if (t.ipv6) {
all6 += s + "\n"
debug(LogNet, "IPv6 =", s)
log.Log(NET, "IPv6 =", s)
} else {
debug(LogNet, "???? =", s)
log.Log(NET, "???? =", s)
}
}
all4 = sortLines(all4)
all6 = sortLines(all6)
if (me.IPv4.S != all4) {
debug(LogNow, "IPv4 addresses have changed", all4)
log.Log(NET, "IPv4 addresses have changed", all4)
me.IPv4.SetText(all4)
}
if (me.IPv6.S != all6) {
debug(LogNow, "IPv6 addresses have changed", all6)
log.Log(NET, "IPv6 addresses have changed", all6)
me.IPv6.SetText(all6)
}
}
@ -268,7 +270,7 @@ func deleteChanges() bool {
var changed bool = false
for i, t := range me.ifmap {
if (t.gone) {
debug(LogChange, "DELETE int =", i, "name =", t.name, t.iface)
log.Log(LogChange, "DELETE int =", i, "name =", t.name, t.iface)
delete(me.ifmap, i)
changed = true
}
@ -276,10 +278,10 @@ func deleteChanges() bool {
}
for s, t := range me.ipmap {
if (t.gone) {
debug(LogChange, "DELETE name =", s, "IPv4 =", t.ipv4)
debug(LogChange, "DELETE name =", s, "IPv6 =", t.ipv6)
debug(LogChange, "DELETE name =", s, "iface =", t.iface)
debug(LogChange, "DELETE name =", s, "ip =", t.ip)
log.Log(LogChange, "DELETE name =", s, "IPv4 =", t.ipv4)
log.Log(LogChange, "DELETE name =", s, "IPv6 =", t.ipv6)
log.Log(LogChange, "DELETE name =", s, "iface =", t.iface)
log.Log(LogChange, "DELETE name =", s, "ip =", t.ip)
delete(me.ipmap, s)
changed = true
}

View File

@ -4,7 +4,7 @@ package main
import (
"net"
"time"
"go.wit.com/gui"
"go.wit.com/gui/gui"
"go.wit.com/gui/gadgets"
"github.com/miekg/dns"
)