diff --git a/args.go b/args.go index fed42d5..d4ccc9f 100644 --- a/args.go +++ b/args.go @@ -10,12 +10,8 @@ import ( "time" arg "github.com/alexflint/go-arg" "go.wit.com/gui" - // log "go.wit.com/gui/log" - "go.wit.com/control-panel-dns/cloudflare" ) -var newRR *cloudflare.RRT - var args struct { Verbose bool VerboseNet bool `arg:"--verbose-net" help:"debug your local OS network settings"` @@ -29,7 +25,6 @@ var args struct { User string `arg:"env:USER"` Demo bool `help:"run a demo"` gui.GuiArgs - // log.LogArgs } func init() { @@ -41,15 +36,13 @@ func init() { } log.Println(true, "INIT() args.GuiArg.Gui =", gui.GuiArg.Gui) - newRR = &cloudflare.CFdialog - - me.dnsTTL = 2 // how often to recheck DNS - me.dnsTTLsleep = 0.4 // sleep between loops +// me.dnsTTL = 2 // how often to recheck DNS +// me.dnsTTLsleep = 0.4 // sleep between loops me.dnsSleep = 500 * time.Millisecond me.localSleep = 100 * time.Millisecond - me.artificialSleep = me.dnsTTLsleep // seems to need to exist or GTK crashes + 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) diff --git a/cloudflare/curl.sh b/cloudflare/curl.sh new file mode 100755 index 0000000..13a477d --- /dev/null +++ b/cloudflare/curl.sh @@ -0,0 +1,23 @@ +# +# 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" diff --git a/cloudflare/durationSlider.go b/cloudflare/durationSlider.go new file mode 100644 index 0000000..0d9559b --- /dev/null +++ b/cloudflare/durationSlider.go @@ -0,0 +1,81 @@ +// This is a simple example +package cloudflare + +import ( + "log" + "fmt" + "time" + + "go.wit.com/gui" +) + +// ttl := cloudflare.DurationSlider(g2, "control panel TTL (in tenths of seconds)", 10 * time.Millisecond, 5 * time.Second) +// ttl.Set(200 * time.Millisecond) + +// The Node is a binary tree. This is how all GUI elements are stored +// simply the name and the size of whatever GUI element exists +type Duration struct { + p *gui.Node // parent widget + l *gui.Node // label widget + s *gui.Node // slider widget + + Label string + Low time.Duration + High time.Duration + Duration time.Duration + + Custom func() +} + +func (n *Duration) Set(d time.Duration) { + var timeRange, step, offset time.Duration + + if (d > n.High) { + d = n.High + } + if (d < n.Low) { + d = n.Low + } + + // set the duration + n.Duration = d + + // figure out the integer offset for the Slider GUI Widget + timeRange = n.High - n.Low + step = timeRange / 1000 + if (step == 0) { + log.Println("duration.Set() division by step == 0", n.Low, n.High, timeRange, step) + n.s.Set(0) + return + } + offset = d - n.Low + i := int(offset / step) + log.Println("duration.Set() =", n.Low, n.High, d, "i =", i) + n.s.I = i + n.s.Set(i) + n.s.Custom() +} + +func NewDurationSlider(n *gui.Node, label string, low time.Duration, high time.Duration) *Duration { + d := Duration { + p: n, + Label: label, + High: high, + Low: low, + } + + // various timeout settings + d.l = n.NewLabel(label) + d.s = n.NewSlider(label, 0, 1000) + d.s.Custom = func () { + d.Duration = low + (high - low) * time.Duration(d.s.I) / 1000 + log.Println("d.Duration =", d.Duration) + s := fmt.Sprintf("%s (%v)", d.Label, d.Duration) + d.l.SetText(s) + if (d.Custom != nil) { + d.Custom() + } + } + + return &d +} diff --git a/examples/cloudflare/curl.sh b/examples/cloudflare/curl.sh deleted file mode 100755 index 1edd53e..0000000 --- a/examples/cloudflare/curl.sh +++ /dev/null @@ -1,28 +0,0 @@ -#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" -}' diff --git a/gui.go b/gui.go index 0ba5d54..1688a9c 100644 --- a/gui.go +++ b/gui.go @@ -4,6 +4,7 @@ package main import ( "log" "fmt" + "time" "os" "os/user" "strconv" @@ -12,6 +13,7 @@ import ( "go.wit.com/gui" "go.wit.com/shell" + "go.wit.com/control-panel-dns/cloudflare" "github.com/davecgh/go-spew/spew" ) @@ -172,22 +174,13 @@ func debugTab(title string) { LogProc = me.dbProc.B } - // various timeout settings - g2.NewLabel("control panel TTL (in tenths of seconds)") - ttl := g2.NewSlider("dnsTTL", 1, 100) - ttl.Set(int(me.dnsTTL * 10)) - ttl.Custom = func () { - me.dnsTTL = ttl.I / 10 - log.Println("dnsTTL =", me.dnsTTL) - } + // makes a slider widget + me.ttl = cloudflare.NewDurationSlider(g2, "Loop Timeout", 10 * time.Millisecond, 5 * time.Second) + me.ttl.Set(300 * time.Millisecond) - g2.NewLabel("control panel loop delay (in tenths of seconds)") - ttl2 := g2.NewSlider("dnsTTL", 1, 100) - ttl2.Set(int(me.dnsTTLsleep * 10)) - ttl2.Custom = func () { - me.dnsTTLsleep = float64(ttl2.I) / 10 - log.Println("dnsTTLsleep =", me.dnsTTLsleep) - } + // makes a slider widget + me.dnsTtl = cloudflare.NewDurationSlider(g2, "DNS Timeout", 800 * time.Millisecond, 300 * time.Second) + me.dnsTtl.Set(60 * time.Second) g2.Margin() g2.Pad() diff --git a/main.go b/main.go index 13ab381..36be8cd 100644 --- a/main.go +++ b/main.go @@ -69,8 +69,14 @@ func timeFunction(f func()) time.Duration { return time.Since(startTime) // Calculate the elapsed time } */ + timer2 := time.NewTimer(time.Second) + go func() { + <-timer2.C + fmt.Println("Timer 2 fired") + }() + for { - sleep(me.dnsTTLsleep) + time.Sleep(me.ttl.Duration) if (time.Since(lastLocal) > me.localSleep) { if (runtime.GOOS == "linux") { duration := timeFunction(linuxLoop) @@ -82,10 +88,17 @@ func timeFunction(f func()) time.Duration { } lastLocal = time.Now() } - if (time.Since(lastDNS) > me.dnsSleep) { + if (time.Since(lastDNS) > me.dnsTtl.Duration) { DNSloop() lastDNS = time.Now() } + + /* + stop2 := timer2.Stop() + if stop2 { + fmt.Println("Timer 2 stopped") + } + */ } } diff --git a/structs.go b/structs.go index 7424e9c..3d1ba4a 100644 --- a/structs.go +++ b/structs.go @@ -5,6 +5,7 @@ import ( "net" "time" "go.wit.com/gui" + "go.wit.com/control-panel-dns/cloudflare" "github.com/miekg/dns" ) @@ -18,11 +19,13 @@ type Host struct { hostnameStatus *gui.Node // is the hostname configured correctly in the OS? // fqdn string // mirrors.kernel.org - dnsTTL int `default:"3"` // Recheck DNS is working every TTL (in seconds) - dnsTTLsleep float64 // sleep between loops +// dnsTTL int `default:"3"` // Recheck DNS is working every TTL (in seconds) +// dnsTTLsleep float64 // sleep between loops artificialSleep float64 `default:"0.7"` // artificial sleep on startup artificialS string `default:"abc"` // artificial sleep on startup + ttl *cloudflare.Duration + dnsTtl *cloudflare.Duration dnsSleep time.Duration localSleep time.Duration