add a DurationSlider()

widgets to adjust timeouts
    redo bash curl.sh example

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-12-21 17:56:56 -06:00
parent 7409b58ea3
commit 73b0cee933
7 changed files with 135 additions and 57 deletions

13
args.go
View File

@ -10,12 +10,8 @@ import (
"time" "time"
arg "github.com/alexflint/go-arg" arg "github.com/alexflint/go-arg"
"go.wit.com/gui" "go.wit.com/gui"
// log "go.wit.com/gui/log"
"go.wit.com/control-panel-dns/cloudflare"
) )
var newRR *cloudflare.RRT
var args struct { var args struct {
Verbose bool Verbose bool
VerboseNet bool `arg:"--verbose-net" help:"debug your local OS network settings"` VerboseNet bool `arg:"--verbose-net" help:"debug your local OS network settings"`
@ -29,7 +25,6 @@ var args struct {
User string `arg:"env:USER"` User string `arg:"env:USER"`
Demo bool `help:"run a demo"` Demo bool `help:"run a demo"`
gui.GuiArgs gui.GuiArgs
// log.LogArgs
} }
func init() { func init() {
@ -41,15 +36,13 @@ func init() {
} }
log.Println(true, "INIT() args.GuiArg.Gui =", gui.GuiArg.Gui) 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.dnsSleep = 500 * time.Millisecond
me.localSleep = 100 * 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" me.artificialS = "blah"
log.Println("init() me.artificialSleep =", me.artificialSleep) log.Println("init() me.artificialSleep =", me.artificialSleep)
log.Println("init() me.artificialS =", me.artificialS) log.Println("init() me.artificialS =", me.artificialS)

23
cloudflare/curl.sh Executable file
View File

@ -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"

View File

@ -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
}

View File

@ -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"
}'

23
gui.go
View File

@ -4,6 +4,7 @@ package main
import ( import (
"log" "log"
"fmt" "fmt"
"time"
"os" "os"
"os/user" "os/user"
"strconv" "strconv"
@ -12,6 +13,7 @@ import (
"go.wit.com/gui" "go.wit.com/gui"
"go.wit.com/shell" "go.wit.com/shell"
"go.wit.com/control-panel-dns/cloudflare"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
) )
@ -172,22 +174,13 @@ func debugTab(title string) {
LogProc = me.dbProc.B LogProc = me.dbProc.B
} }
// various timeout settings // makes a slider widget
g2.NewLabel("control panel TTL (in tenths of seconds)") me.ttl = cloudflare.NewDurationSlider(g2, "Loop Timeout", 10 * time.Millisecond, 5 * time.Second)
ttl := g2.NewSlider("dnsTTL", 1, 100) me.ttl.Set(300 * time.Millisecond)
ttl.Set(int(me.dnsTTL * 10))
ttl.Custom = func () {
me.dnsTTL = ttl.I / 10
log.Println("dnsTTL =", me.dnsTTL)
}
g2.NewLabel("control panel loop delay (in tenths of seconds)") // makes a slider widget
ttl2 := g2.NewSlider("dnsTTL", 1, 100) me.dnsTtl = cloudflare.NewDurationSlider(g2, "DNS Timeout", 800 * time.Millisecond, 300 * time.Second)
ttl2.Set(int(me.dnsTTLsleep * 10)) me.dnsTtl.Set(60 * time.Second)
ttl2.Custom = func () {
me.dnsTTLsleep = float64(ttl2.I) / 10
log.Println("dnsTTLsleep =", me.dnsTTLsleep)
}
g2.Margin() g2.Margin()
g2.Pad() g2.Pad()

17
main.go
View File

@ -69,8 +69,14 @@ func timeFunction(f func()) time.Duration {
return time.Since(startTime) // Calculate the elapsed time return time.Since(startTime) // Calculate the elapsed time
} }
*/ */
timer2 := time.NewTimer(time.Second)
go func() {
<-timer2.C
fmt.Println("Timer 2 fired")
}()
for { for {
sleep(me.dnsTTLsleep) time.Sleep(me.ttl.Duration)
if (time.Since(lastLocal) > me.localSleep) { if (time.Since(lastLocal) > me.localSleep) {
if (runtime.GOOS == "linux") { if (runtime.GOOS == "linux") {
duration := timeFunction(linuxLoop) duration := timeFunction(linuxLoop)
@ -82,10 +88,17 @@ func timeFunction(f func()) time.Duration {
} }
lastLocal = time.Now() lastLocal = time.Now()
} }
if (time.Since(lastDNS) > me.dnsSleep) { if (time.Since(lastDNS) > me.dnsTtl.Duration) {
DNSloop() DNSloop()
lastDNS = time.Now() lastDNS = time.Now()
} }
/*
stop2 := timer2.Stop()
if stop2 {
fmt.Println("Timer 2 stopped")
}
*/
} }
} }

View File

@ -5,6 +5,7 @@ import (
"net" "net"
"time" "time"
"go.wit.com/gui" "go.wit.com/gui"
"go.wit.com/control-panel-dns/cloudflare"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
@ -18,11 +19,13 @@ type Host struct {
hostnameStatus *gui.Node // is the hostname configured correctly in the OS? hostnameStatus *gui.Node // is the hostname configured correctly in the OS?
// fqdn string // mirrors.kernel.org // fqdn string // mirrors.kernel.org
dnsTTL int `default:"3"` // Recheck DNS is working every TTL (in seconds) // dnsTTL int `default:"3"` // Recheck DNS is working every TTL (in seconds)
dnsTTLsleep float64 // sleep between loops // dnsTTLsleep float64 // sleep between loops
artificialSleep float64 `default:"0.7"` // artificial sleep on startup artificialSleep float64 `default:"0.7"` // artificial sleep on startup
artificialS string `default:"abc"` // artificial sleep on startup artificialS string `default:"abc"` // artificial sleep on startup
ttl *cloudflare.Duration
dnsTtl *cloudflare.Duration
dnsSleep time.Duration dnsSleep time.Duration
localSleep time.Duration localSleep time.Duration