Compare commits

..

No commits in common. "guimaster" and "v0.5.5" have entirely different histories.

21 changed files with 583 additions and 303 deletions

5
.gitignore vendored
View File

@ -1,10 +1,7 @@
*.swp
go.sum
go.mod
control-panel-dns control-panel-dns
/files/* /files/*
/*.deb /*.deb
*.swp
/plugins/* /plugins/*
control-panel-dns control-panel-dns

View File

@ -1,17 +0,0 @@
all: goimports vet
vet:
GO111MODULE=off go vet
goimports:
goimports -w *.go
redomod:
rm -f go.*
goimports -w *.go
GO111MODULE= go mod init
GO111MODULE= go mod tidy
clean:
-rm -f go.*
-go-mod-clean --purge

View File

@ -1,3 +0,0 @@
# linuxstatus
this is being moved to go.wit.com/lib/hostname

View File

@ -3,14 +3,13 @@ package linuxstatus
import ( import (
"go.wit.com/log" "go.wit.com/log"
"go.wit.com/gui/gui"
) )
// reports externally if something has changed // reports externally if something has changed
// since the last time it was asked about it // since the last time it was asked about it
func (ls *LinuxStatus) Changed() bool { func (ls *LinuxStatus) Changed() bool {
if !ls.Ready() { if ! ls.Ready() {return false}
return false
}
if ls.changed { if ls.changed {
ls.changed = false ls.changed = false
return true return true
@ -18,28 +17,40 @@ func (ls *LinuxStatus) Changed() bool {
return false return false
} }
func (ls *LinuxStatus) Show() { func (ls *LinuxStatus) Make() {
if !ls.Ready() { if ! ls.Ready() {return}
return log.Log(CHANGE, "Make() window ready =", ls.ready)
ls.window.Make()
ls.ready = true
} }
func (ls *LinuxStatus) Draw() {
if ! ls.Ready() {return}
log.Log(CHANGE, "Draw() window ready =", ls.ready)
ls.window.Draw()
ls.ready = true
}
func (ls *LinuxStatus) Draw2() {
if ! ls.Ready() {return}
log.Log(CHANGE, "draw(ls) ready =", ls.ready)
draw(ls)
}
func (ls *LinuxStatus) Show() {
if ! ls.Ready() {return}
log.Log(CHANGE, "Show() window ready =", ls.ready) log.Log(CHANGE, "Show() window ready =", ls.ready)
ls.window.Show() ls.window.Show()
ls.hidden = false ls.hidden = false
} }
func (ls *LinuxStatus) Hide() { func (ls *LinuxStatus) Hide() {
if !ls.Ready() { if ! ls.Ready() {return}
return
}
log.Log(CHANGE, "Hide() window ready =", ls.ready) log.Log(CHANGE, "Hide() window ready =", ls.ready)
ls.window.Hide() ls.window.Hide()
ls.hidden = true ls.hidden = true
} }
func (ls *LinuxStatus) Toggle() { func (ls *LinuxStatus) Toggle() {
if !ls.Ready() { if ! ls.Ready() {return}
return
}
log.Log(CHANGE, "Toggle() window ready =", ls.ready) log.Log(CHANGE, "Toggle() window ready =", ls.ready)
if ls.hidden { if ls.hidden {
ls.Show() ls.Show()
@ -50,14 +61,29 @@ func (ls *LinuxStatus) Toggle() {
func (ls *LinuxStatus) Ready() bool { func (ls *LinuxStatus) Ready() bool {
log.Log(SPEW, "Ready() maybe not ready? ls =", ls) log.Log(SPEW, "Ready() maybe not ready? ls =", ls)
if me == nil { if me == nil {return false}
return false if ls == nil {return false}
} if ls.window == nil {return false}
if ls == nil {
return false
}
if ls.window == nil {
return false
}
return me.ready return me.ready
} }
func (ls *LinuxStatus) Initialized() bool {
log.Log(CHANGE, "checking Initialized()")
if me == nil {return false}
if ls == nil {return false}
if ls.parent == nil {return false}
return true
}
func (ls *LinuxStatus) SetParent(p *gui.Node) {
log.Log(CHANGE, "Attempting SetParent")
if me == nil {return}
if ls == nil {return}
if ls.parent == nil {
log.Log(CHANGE, "SetParent =", p)
ls.parent = p
return
} else {
log.Log(CHANGE, "SetParent was already set to =", ls.parent)
}
}

View File

@ -2,12 +2,13 @@
package linuxstatus package linuxstatus
import ( import (
"go.wit.com/lib/gadgets" "go.wit.com/gui/gadgets"
) )
// creates the actual widgets. // creates the actual widgets.
// it's assumed you are always passing in a box // it's assumed you are always passing in a box
func draw(ls *LinuxStatus) { func draw(ls *LinuxStatus) {
if ! ls.Ready() {return}
ls.group = ls.window.Box().NewGroup("What Linux Knows It Is") ls.group = ls.window.Box().NewGroup("What Linux Knows It Is")
ls.grid = ls.group.NewGrid("gridnuts", 2, 2) ls.grid = ls.group.NewGrid("gridnuts", 2, 2)
@ -17,7 +18,7 @@ func draw(ls *LinuxStatus) {
ls.hostshort = gadgets.NewOneLiner(ls.grid, "hostname -s") ls.hostshort = gadgets.NewOneLiner(ls.grid, "hostname -s")
ls.domainname = gadgets.NewOneLiner(ls.grid, "domain name") ls.domainname = gadgets.NewOneLiner(ls.grid, "domain name")
ls.resolver = gadgets.NewOneLiner(ls.grid, "nameservers =") ls.resolver = gadgets.NewOneLiner(ls.grid, "nameservers =")
ls.resolver.SetText("TODO") ls.resolver.Set("TODO")
ls.uid = gadgets.NewOneLiner(ls.grid, "UID =") ls.uid = gadgets.NewOneLiner(ls.grid, "UID =")
ls.IPv4 = gadgets.NewOneLiner(ls.grid, "Current IPv4 =") ls.IPv4 = gadgets.NewOneLiner(ls.grid, "Current IPv4 =")
ls.IPv6 = gadgets.NewOneLiner(ls.grid, "Current IPv6 =") ls.IPv6 = gadgets.NewOneLiner(ls.grid, "Current IPv6 =")
@ -26,7 +27,7 @@ func draw(ls *LinuxStatus) {
// ls.nics = gadgets.NewOneLiner(ls.grid, "network intefaces =") // ls.nics = gadgets.NewOneLiner(ls.grid, "network intefaces =")
ls.grid.NewLabel("interfaces =") ls.grid.NewLabel("interfaces =")
ls.Interfaces = ls.grid.NewCombobox() ls.Interfaces = ls.grid.NewCombobox("Interfaces")
ls.speed = gadgets.NewOneLiner(ls.grid, "refresh speed =") ls.speed = gadgets.NewOneLiner(ls.grid, "refresh speed =")
ls.speedActual = gadgets.NewOneLiner(ls.grid, "refresh speed =") ls.speedActual = gadgets.NewOneLiner(ls.grid, "refresh speed =")

View File

@ -3,11 +3,7 @@
package linuxstatus package linuxstatus
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"strings"
"go.wit.com/lib/gui/shell"
"go.wit.com/log" "go.wit.com/log"
// will try to get this hosts FQDN // will try to get this hosts FQDN
@ -15,93 +11,65 @@ import (
) )
func (ls *LinuxStatus) GetDomainName() string { func (ls *LinuxStatus) GetDomainName() string {
if !me.Ready() { if ! me.Ready() {return ""}
return "" return me.domainname.Get()
}
return me.domainname.String()
} }
func (ls *LinuxStatus) setDomainName() { func (ls *LinuxStatus) setDomainName() {
if !me.Ready() { if ! me.Ready() {return}
return
}
dn := run("domainname") dn := run("domainname")
if me.domainname.String() != dn { if (me.domainname.Get() != dn) {
log.Log(CHANGE, "domainname has changed from", me.GetDomainName(), "to", dn) log.Log(CHANGE, "domainname has changed from", me.GetDomainName(), "to", dn)
me.domainname.SetText(dn) me.domainname.Set(dn)
me.changed = true me.changed = true
} }
} }
func (ls *LinuxStatus) GetHostname() string { func (ls *LinuxStatus) GetHostname() string {
if !me.Ready() { if ! me.Ready() {return ""}
return "" return me.hostname.Get()
}
return me.hostname.String()
} }
func (ls *LinuxStatus) ValidHostname() bool { func (ls *LinuxStatus) ValidHostname() bool {
if !me.Ready() { if ! me.Ready() {return false}
log.Info("ValidHostname() not ready") if me.hostnameStatus.Get() == "WORKING" {
return false
}
if me.hostnameStatus.String() == "WORKING" {
return true return true
} }
return false return false
} }
func (ls *LinuxStatus) setHostname(newname string) { func (ls *LinuxStatus) setHostname(newname string) {
if !me.Ready() { if ! me.Ready() {return}
return if newname == me.hostname.Get() {
}
if newname == me.hostname.String() {
return return
} }
log.Log(CHANGE, "hostname has changed from", me.GetHostname(), "to", newname) log.Log(CHANGE, "hostname has changed from", me.GetHostname(), "to", newname)
me.hostname.SetText(newname) me.hostname.Set(newname)
me.changed = true me.changed = true
} }
func (ls *LinuxStatus) GetHostShort() string { func (ls *LinuxStatus) GetHostShort() string {
if !me.Ready() { if ! me.Ready() {return ""}
return "" return me.hostshort.Get()
}
return me.hostshort.String()
} }
func (ls *LinuxStatus) setHostShort() { func (ls *LinuxStatus) setHostShort() {
if !me.Ready() { if ! me.Ready() {return}
return
}
hshort := run("hostname -s") hshort := run("hostname -s")
if me.hostshort.String() != hshort { if (me.hostshort.Get() != hshort) {
log.Log(CHANGE, "hostname -s has changed from", me.hostshort.String(), "to", hshort) log.Log(CHANGE, "hostname -s has changed from", me.hostshort.Get(), "to", hshort)
me.hostshort.SetText(hshort) me.hostshort.Set(hshort)
me.changed = true me.changed = true
} }
} }
// getDomainName extracts the domain name from a given hostname
func getDomainName(hostname string) (string, error) {
parts := strings.Split(hostname, ".")
if len(parts) < 3 {
return "", fmt.Errorf("hostname '%s' is too short to extract a domain name", hostname)
}
// Join all parts except the first one, which is assumed to be a subdomain
domain := strings.Join(parts[1:], ".")
return domain, nil
}
func lookupHostname() { func lookupHostname() {
if !me.Ready() { if ! me.Ready() {return}
return
}
var err error var err error
var hostfqdn string = "broken" var hostfqdn string = "broken"
hostfqdn, err = fqdn.FqdnHostname() hostfqdn, err = fqdn.FqdnHostname()
if err != nil { if (err != nil) {
log.Error(err, "FQDN hostname error") log.Error(err, "FQDN hostname error")
return return
} }
@ -121,27 +89,6 @@ func lookupHostname() {
if hostfqdn != hostname { if hostfqdn != hostname {
log.Log(WARN, "hostname", hostname, "does not equal fqdn.FqdnHostname()", hostfqdn) log.Log(WARN, "hostname", hostname, "does not equal fqdn.FqdnHostname()", hostfqdn)
// TODO: figure out what is wrong // TODO: figure out what is wrong
if dn == "(none)" {
realdn, err := getDomainName(hostfqdn)
if err == nil {
log.Log(WARN, "need to run: 'domainname", realdn, "' here")
me.changed = true
me.hostnameStatus.SetText("FIXING")
shell.Run([]string{"domainname", realdn})
return
} else {
log.Log(WARN, "getDomainName() err =", err)
log.Log(WARN, "/etc/hostname is too short. let the user set the name here.")
// me.changed = true
me.hostnameStatus.SetText("INVALID DOMAIN NAME")
// return
}
}
log.Log(WARN, "don't know what to do here with domainname")
log.Log(WARN, "check that /etc/hostname is valid?")
// me.changed = true
me.hostnameStatus.SetText("UNKNOWN")
// return
} }
var test string var test string
@ -149,17 +96,17 @@ func lookupHostname() {
me.setHostname(test) me.setHostname(test)
if hostname != test { if (hostname != test) {
log.Log(CHANGE, "hostname", hostname, "does not equal", test) log.Log(CHANGE, "hostname", hostname, "does not equal", test)
if me.hostnameStatus.String() != "BROKEN" { if (me.hostnameStatus.Get() != "BROKEN") {
log.Log(CHANGE, "hostname", hostname, "does not equal", test) log.Log(CHANGE, "hostname", hostname, "does not equal", test)
me.changed = true me.changed = true
me.hostnameStatus.SetText("BROKEN") me.hostnameStatus.Set("BROKEN")
} }
} else { } else {
if me.hostnameStatus.String() != "WORKING" { if (me.hostnameStatus.Get() != "WORKING") {
log.Log(CHANGE, "hostname", hostname, "is valid") log.Log(CHANGE, "hostname", hostname, "is valid")
me.hostnameStatus.SetText("WORKING") me.hostnameStatus.Set("WORKING")
me.changed = true me.changed = true
} }
} }
@ -168,7 +115,6 @@ func lookupHostname() {
// returns true if the hostname is good // returns true if the hostname is good
// check that all the OS settings are correct here // check that all the OS settings are correct here
// On Linux, /etc/hosts, /etc/hostname // On Linux, /etc/hosts, /etc/hostname
//
// and domainname and hostname // and domainname and hostname
func goodHostname() bool { func goodHostname() bool {
content, err := ioutil.ReadFile("/etc/hostname") content, err := ioutil.ReadFile("/etc/hostname")
@ -186,7 +132,7 @@ func goodHostname() bool {
log.Log(NOW, "hostname short =", hs, "domainname =", dn) log.Log(NOW, "hostname short =", hs, "domainname =", dn)
tmp := hs + "." + dn tmp := hs + "." + dn
if hostname == tmp { if (hostname == tmp) {
log.Log(NOW, "hostname seems to be good", hostname) log.Log(NOW, "hostname seems to be good", hostname)
return true return true
} }

View File

@ -11,12 +11,12 @@
package linuxstatus package linuxstatus
import ( import (
"io/ioutil"
"os" "os"
"os/user" "os/user"
"sort" "io/ioutil"
"strconv" "strconv"
"strings" "strings"
"sort"
"go.wit.com/log" "go.wit.com/log"
) )
@ -43,28 +43,28 @@ func linuxLoop() {
a := realA() a := realA()
sort.Strings(a) sort.Strings(a)
tmp := strings.Join(a, "\n") tmp := strings.Join(a, "\n")
if tmp != me.workingIPv4.String() { if tmp != me.workingIPv4.Get() {
log.Log(CHANGE, "realA() your real IPv6 addresses changed") log.Log(CHANGE, "realAAAA() your real IPv6 addresses changed")
me.changed = true me.changed = true
me.workingIPv4.SetText(tmp) me.workingIPv4.Set(tmp)
} }
// get all the real AAAA records from all the network interfaces linux can see // get all the real AAAA records from all the network interfaces linux can see
aaaa := realAAAA() aaaa := realAAAA()
sort.Strings(aaaa) sort.Strings(aaaa)
tmp = strings.Join(aaaa, "\n") tmp = strings.Join(aaaa, "\n")
if tmp != me.workingIPv6.String() { if tmp != me.workingIPv6.Get() {
log.Log(CHANGE, "realAAAA() your real IPv6 addresses changed") log.Log(CHANGE, "realAAAA() your real IPv6 addresses changed")
me.changed = true me.changed = true
me.workingIPv6.SetText(tmp) me.workingIPv6.Set(tmp)
} }
user, _ := user.Current() user, _ := user.Current()
tmp = user.Username + " (" + strconv.Itoa(os.Getuid()) + ")" tmp = user.Username + " (" + strconv.Itoa(os.Getuid()) + ")"
if tmp != me.uid.String() { if tmp != me.uid.Get() {
log.Log(CHANGE, "os.Getuid =", user.Username, os.Getuid()) log.Log(CHANGE, "os.Getuid =", user.Username, os.Getuid())
me.changed = true me.changed = true
me.uid.SetText(tmp) me.uid.Set(tmp)
} }
content, _ := ioutil.ReadFile("/etc/resolv.conf") content, _ := ioutil.ReadFile("/etc/resolv.conf")
@ -79,10 +79,10 @@ func linuxLoop() {
} }
sort.Strings(ns) sort.Strings(ns)
newNS := strings.Join(ns, "\n") newNS := strings.Join(ns, "\n")
if newNS != me.resolver.String() { if newNS != me.resolver.Get() {
log.Log(CHANGE, "resolver changed in /etc/resolv.conf to", ns) log.Log(CHANGE, "resolver changed in /etc/resolv.conf to", ns)
me.changed = true me.changed = true
me.resolver.SetText(newNS) me.resolver.Set(newNS)
} }
/* /*

72
net.go
View File

@ -15,7 +15,7 @@ func IsIPv6(address string) bool {
} }
func (t *IPtype) IsReal() bool { func (t *IPtype) IsReal() bool {
if t.ip.IsPrivate() || t.ip.IsLoopback() || t.ip.IsLinkLocalUnicast() { if (t.ip.IsPrivate() || t.ip.IsLoopback() || t.ip.IsLinkLocalUnicast()) {
log.Log(NET, "\t\tIP is Real = false") log.Log(NET, "\t\tIP is Real = false")
return false return false
} else { } else {
@ -25,7 +25,7 @@ func (t *IPtype) IsReal() bool {
} }
func IsReal(ip *net.IP) bool { func IsReal(ip *net.IP) bool {
if ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() { if (ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast()) {
log.Log(NET, "\t\tIP is Real = false") log.Log(NET, "\t\tIP is Real = false")
return false return false
} else { } else {
@ -51,7 +51,7 @@ func checkInterface(i net.Interface) {
me.ifmap[i.Index].gone = false me.ifmap[i.Index].gone = false
me.ifmap[i.Index].iface = &i me.ifmap[i.Index].iface = &i
me.changed = true me.changed = true
if me.Interfaces != nil { if (me.Interfaces != nil) {
me.Interfaces.AddText(i.Name) me.Interfaces.AddText(i.Name)
me.Interfaces.SetText(i.Name) me.Interfaces.SetText(i.Name)
} }
@ -59,11 +59,11 @@ func checkInterface(i net.Interface) {
} }
me.ifmap[i.Index].gone = false me.ifmap[i.Index].gone = false
log.Log(NET, "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 { if (val.iface.Name != i.Name) {
log.Log(INFO, val.iface.Name, "has changed to it's name to", i.Name) log.Log(INFO, val.iface.Name, "has changed to it's name to", i.Name)
me.ifmap[i.Index].iface = &i me.ifmap[i.Index].iface = &i
me.changed = true me.changed = true
if me.Interfaces != nil { if (me.Interfaces != nil) {
me.Interfaces.AddText(i.Name) me.Interfaces.AddText(i.Name)
me.Interfaces.SetText(i.Name) me.Interfaces.SetText(i.Name)
} }
@ -79,8 +79,8 @@ func realAAAA() []string {
var aaaa []string var aaaa []string
for s, t := range me.ipmap { for s, t := range me.ipmap {
if t.IsReal() { if (t.IsReal()) {
if t.ipv6 { if (t.ipv6) {
aaaa = append(aaaa, s) aaaa = append(aaaa, s)
} }
} }
@ -92,8 +92,8 @@ func realA() []string {
var a []string var a []string
for s, t := range me.ipmap { for s, t := range me.ipmap {
if t.IsReal() { if (t.IsReal()) {
if t.ipv4 { if (t.ipv4) {
a = append(a, s) a = append(a, s)
} }
} }
@ -101,7 +101,7 @@ func realA() []string {
return a return a
} }
func checkDNSOld() (map[string]*IPtype, map[string]*IPtype) { func checkDNS() (map[string]*IPtype, map[string]*IPtype) {
var ipv4s map[string]*IPtype var ipv4s map[string]*IPtype
var ipv6s map[string]*IPtype var ipv6s map[string]*IPtype
@ -111,12 +111,12 @@ func checkDNSOld() (map[string]*IPtype, map[string]*IPtype) {
for s, t := range me.ipmap { for s, t := range me.ipmap {
i := t.iface i := t.iface
ipt := "IPv4" ipt := "IPv4"
if t.ipv6 { if (t.ipv6) {
ipt = "IPv6" ipt = "IPv6"
} }
if t.IsReal() { if (t.IsReal()) {
log.Log(INFO, "\tIP is Real ", ipt, i.Index, i.Name, s) log.Log(INFO, "\tIP is Real ", ipt, i.Index, i.Name, s)
if t.ipv6 { if (t.ipv6) {
ipv6s[s] = t ipv6s[s] = t
} else { } else {
ipv4s[s] = t ipv4s[s] = t
@ -149,21 +149,21 @@ func checkIP(ip *net.IPNet, i net.Interface) bool {
me.ipmap[realip].ip = ip.IP me.ipmap[realip].ip = ip.IP
me.ipmap[realip].iface = &i me.ipmap[realip].iface = &i
t := "IPv4" t := "IPv4"
if IsIPv6(ip.String()) { if (IsIPv6(ip.String())) {
me.ipmap[realip].ipv6 = true me.ipmap[realip].ipv6 = true
me.ipmap[realip].ipv4 = false me.ipmap[realip].ipv4 = false
t = "IPv6" t = "IPv6"
if me.IPv6 != nil { if (me.IPv6 != nil) {
me.IPv6.SetText(realip) me.IPv6.Set(realip)
} }
} else { } else {
me.ipmap[realip].ipv6 = false me.ipmap[realip].ipv6 = false
me.ipmap[realip].ipv4 = true me.ipmap[realip].ipv4 = true
if me.IPv4 != nil { if (me.IPv4 != nil) {
me.IPv4.SetText(realip) me.IPv4.Set(realip)
} }
} }
if IsReal(&ip.IP) { if (IsReal(&ip.IP)) {
log.Log(INFO, "\tIP is Real ", t, i.Index, i.Name, realip) log.Log(INFO, "\tIP is Real ", t, i.Index, i.Name, realip)
} else { } else {
log.Log(INFO, "\tIP is not Real", t, i.Index, i.Name, realip) log.Log(INFO, "\tIP is not Real", t, i.Index, i.Name, realip)
@ -215,10 +215,10 @@ func updateRealAAAA() {
var all4 []string var all4 []string
var all6 []string var all6 []string
for s, t := range me.ipmap { for s, t := range me.ipmap {
if t.ipv4 { if (t.ipv4) {
all4 = append(all4, s) all4 = append(all4, s)
log.Log(NET, "IPv4 =", s) log.Log(NET, "IPv4 =", s)
} else if t.ipv6 { } else if (t.ipv6) {
all6 = append(all6, s) all6 = append(all6, s)
log.Log(NET, "IPv6 =", s) log.Log(NET, "IPv6 =", s)
} else { } else {
@ -232,14 +232,14 @@ func updateRealAAAA() {
s4 := strings.Join(all4, "\n") s4 := strings.Join(all4, "\n")
s6 := strings.Join(all6, "\n") s6 := strings.Join(all6, "\n")
if me.IPv4.String() != s4 { if (me.IPv4.Get() != s4) {
log.Log(CHANGE, "IPv4 addresses have changed", s4) log.Log(CHANGE, "IPv4 addresses have changed", s4)
me.IPv4.SetText(s4) me.IPv4.Set(s4)
me.changed = true me.changed = true
} }
if me.IPv6.String() != s6 { if (me.IPv6.Get() != s6) {
log.Log(CHANGE, "IPv6 addresses have changed", s6) log.Log(CHANGE, "IPv6 addresses have changed", s6)
me.IPv6.SetText(s6) me.IPv6.Set(s6)
me.changed = true me.changed = true
} }
} }
@ -248,7 +248,7 @@ func updateRealAAAA() {
func deleteChanges() bool { func deleteChanges() bool {
var changed bool = false var changed bool = false
for i, t := range me.ifmap { for i, t := range me.ifmap {
if t.gone { if (t.gone) {
log.Log(CHANGE, "DELETE int =", i, "name =", t.name, t.iface) log.Log(CHANGE, "DELETE int =", i, "name =", t.name, t.iface)
delete(me.ifmap, i) delete(me.ifmap, i)
changed = true changed = true
@ -256,7 +256,7 @@ func deleteChanges() bool {
t.gone = true t.gone = true
} }
for s, t := range me.ipmap { for s, t := range me.ipmap {
if t.gone { if (t.gone) {
log.Log(CHANGE, "DELETE name =", s, "IPv4 =", t.ipv4) log.Log(CHANGE, "DELETE name =", s, "IPv4 =", t.ipv4)
log.Log(CHANGE, "DELETE name =", s, "IPv6 =", t.ipv6) log.Log(CHANGE, "DELETE name =", s, "IPv6 =", t.ipv6)
log.Log(CHANGE, "DELETE name =", s, "iface =", t.iface) log.Log(CHANGE, "DELETE name =", s, "iface =", t.iface)
@ -271,25 +271,19 @@ func deleteChanges() bool {
} }
func (ls *LinuxStatus) GetIPv6() []string { func (ls *LinuxStatus) GetIPv6() []string {
if !me.Ready() { if ! me.Ready() {return nil}
return nil tmp := me.workingIPv6.Get()
}
tmp := me.workingIPv6.String()
return strings.Split(tmp, "\n") return strings.Split(tmp, "\n")
} }
func (ls *LinuxStatus) GetIPv4() []string { func (ls *LinuxStatus) GetIPv4() []string {
if !me.Ready() { if ! me.Ready() {return nil}
return nil tmp := me.workingIPv4.Get()
}
tmp := me.workingIPv4.String()
return strings.Split(tmp, "\n") return strings.Split(tmp, "\n")
} }
func (ls *LinuxStatus) GetNameservers() []string { func (ls *LinuxStatus) GetNameservers() []string {
if !me.Ready() { if ! me.Ready() {return nil}
return nil tmp := me.resolver.Get()
}
tmp := me.resolver.String()
return strings.Split(tmp, "\n") return strings.Split(tmp, "\n")
} }

26
new.go
View File

@ -4,10 +4,10 @@ package linuxstatus
import ( import (
"go.wit.com/log" "go.wit.com/log"
"go.wit.com/lib/gadgets" "go.wit.com/gui/gadgets"
) )
func InitLinuxStatus() *LinuxStatus { func New() *LinuxStatus {
if me != nil { if me != nil {
log.Log(WARN, "You have done New() twice. You can only do this once") log.Log(WARN, "You have done New() twice. You can only do this once")
return me return me
@ -19,11 +19,21 @@ func InitLinuxStatus() *LinuxStatus {
me.ifmap = make(map[int]*IFtype) me.ifmap = make(map[int]*IFtype)
me.ipmap = make(map[string]*IPtype) me.ipmap = make(map[string]*IPtype)
log.Log(WARN, "Creating the Window")
me.window = gadgets.RawBasicWindow("OS Hostname Details")
me.window.Make()
draw(me)
me.ready = true
return me return me
} }
func (ls *LinuxStatus) InitWindow() {
if ! ls.Initialized() {
log.Log(WARN, "not initalized yet (no parent for the window?)")
return
}
if ls.window != nil {
log.Log(WARN, "You already have a window")
ls.ready = true
return
}
log.Log(WARN, "Creating the Window")
ls.window = gadgets.NewBasicWindow(ls.parent, "Linux OS Details")
ls.ready = true
}

61
old/bash.go Normal file
View File

@ -0,0 +1,61 @@
package main
import (
"io"
"os"
"os/exec"
"os/signal"
"syscall"
"github.com/creack/pty"
"golang.org/x/term"
"go.wit.com/log"
)
func test() error {
// Create arbitrary command.
c := exec.Command("bash")
// Start the command with a pty.
ptmx, err := pty.Start(c)
if err != nil {
return err
}
// Make sure to close the pty at the end.
defer func() { _ = ptmx.Close() }() // Best effort.
// Handle pty size.
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGWINCH)
go func() {
for range ch {
if err := pty.InheritSize(os.Stdin, ptmx); err != nil {
log.Println("error resizing pty: %s", err)
}
}
}()
ch <- syscall.SIGWINCH // Initial resize.
defer func() { signal.Stop(ch); close(ch) }() // Cleanup signals when done.
// Set stdin in raw mode.
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
defer func() { _ = term.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort.
// Copy stdin to the pty and the pty to stdout.
// NOTE: The goroutine will keep reading until the next keystroke before returning.
go func() { _, _ = io.Copy(ptmx, os.Stdin) }()
_, _ = io.Copy(os.Stdout, ptmx)
return nil
}
func mainBash() {
if err := test(); err != nil {
log.Error(err, "exit in mainBash()")
log.Exit(err)
}
}

32
old/dynamic-dns-update.go Normal file
View File

@ -0,0 +1,32 @@
package main
/*
https://pkg.go.dev/github.com/miekg/dns#section-readme
DYNAMIC UPDATES
Dynamic updates reuses the DNS message format, but renames three of the sections. Question is Zone, Answer is Prerequisite, Authority is Update, only the Additional is not renamed. See RFC 2136 for the gory details.
You can set a rather complex set of rules for the existence of absence of certain resource records or names in a zone to specify if resource records should be added or removed. The table from RFC 2136 supplemented with the Go DNS function shows which functions exist to specify the prerequisites.
3.2.4 - Table Of Metavalues Used In Prerequisite Section
CLASS TYPE RDATA Meaning Function
--------------------------------------------------------------
ANY ANY empty Name is in use dns.NameUsed
ANY rrset empty RRset exists (value indep) dns.RRsetUsed
NONE ANY empty Name is not in use dns.NameNotUsed
NONE rrset empty RRset does not exist dns.RRsetNotUsed
zone rrset rr RRset exists (value dep) dns.Used
The prerequisite section can also be left empty. If you have decided on the prerequisites you can tell what RRs should be added or deleted. The next table shows the options you have and what functions to call.
3.4.2.6 - Table Of Metavalues Used In Update Section
CLASS TYPE RDATA Meaning Function
---------------------------------------------------------------
ANY ANY empty Delete all RRsets from name dns.RemoveName
ANY rrset empty Delete an RRset dns.RemoveRRset
NONE rrset rr Delete an RR from RRset dns.Remove
zone rrset rr Add to an RRset dns.Insert
*/

81
old/fsnotify.go Normal file
View File

@ -0,0 +1,81 @@
package main
// Watches for changes to a directory. Works cross-platform
/*
import (
"go.wit.com/log"
"github.com/fsnotify/fsnotify"
)
// This would be a really dumb way to watch for new network interfaces
// since it then watches a linux only directory /sys/class/net for changes
func watchSysClassNet() {
// Create new watcher.
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Error(err, "watchSysClassNet() failed")
return
}
defer watcher.Close()
// Start listening for events.
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event)
if event.Has(fsnotify.Write) {
log.Println("modified file:", event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
// Add a path.
err = watcher.Add("/tmp")
if err != nil {
log.Error(err, "watchSysClassNet() watcher.Add() failed")
return
}
// Block main goroutine forever.
<-make(chan struct{})
}
func fsnotifyNetworkInterfaceChanges() error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
// Watch for network interface changes
err = watcher.Add("/sys/class/net")
if err != nil {
return err
}
for {
select {
case event := <-watcher.Events:
log.Println("fsnotifyNetworkInterfaceChanges() event =", event)
if event.Op&fsnotify.Create == fsnotify.Create {
// Do something on network interface creation
}
case err := <-watcher.Errors:
log.Println("fsnotifyNetworkInterfaceChanges() event err =", err)
return err
}
}
}
*/

34
old/nsupdate.go Normal file
View File

@ -0,0 +1,34 @@
// inspired from:
// https://github.com/mactsouk/opensource.com.git
// and
// https://coderwall.com/p/wohavg/creating-a-simple-tcp-server-in-go
package main
import (
)
// ./go-nsupdate \
// --tsig-algorithm=hmac-sha512 \
// --tsig-secret="OWh5/ZHIyaz7B8J9m9ZDqZ8448Pke0PTpkYbZmFcOf5a6rEzgmcwrG91u1BHi1/4us+mKKEobDPLw1x6sD+ZJw==" \
// -i eno2 farm001.lab.wit.com
/*
func nsupdate() {
var tsigSecret string
log.Log(NET, "nsupdate() START")
cmd := "go-nsupdate --tsig-algorithm=hmac-sha512"
tsigSecret = os.Getenv("TIG_SECRET")
cmd += " --tig-secret=\"" + tsigSecret + "\""
cmd += " -i wlo1 " + me.statusOS.GetHostname()
log.Log(NET, "nsupdate() RUN:", cmd)
for s, t := range me.ipmap {
if (t.IsReal()) {
if (t.ipv6) {
log.Log(NET, "nsupdate() found real AAAA =", s, "on iface", t.iface.Name)
}
}
}
}
*/

26
old/rtnetlink.go Normal file
View File

@ -0,0 +1,26 @@
package main
import (
"github.com/jsimonetti/rtnetlink"
"go.wit.com/log"
)
// List all interfaces
func Example_listLink() {
// Dial a connection to the rtnetlink socket
conn, err := rtnetlink.Dial(nil)
if err != nil {
log.Error(err, "Example_listLink() failed")
return
}
defer conn.Close()
// Request a list of interfaces
msg, err := conn.Link.List()
if err != nil {
log.Println(err)
}
log.Println("%#v", msg)
log.Println(SPEW, msg)
}

97
old/unix.go Normal file
View File

@ -0,0 +1,97 @@
// Various Linux/Unix'y things
// https://wiki.archlinux.org/title/Dynamic_DNS
package main
import (
"os"
"os/exec"
"net"
"bytes"
"fmt"
"strings"
"go.wit.com/log"
"go.wit.com/shell"
)
func CheckSuperuser() bool {
return os.Getuid() == 0
}
func Escalate() {
if os.Getuid() != 0 {
cmd := exec.Command("sudo", "./control-panel-dns") // TODO: get the actual path
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Error(err, "exit in Escalate()")
log.Exit(err)
}
}
}
// You need permission to do a zone transfer. Otherwise:
// dig +noall +answer +multiline lab.wit.com any
// dig +all +multiline fire.lab.wit.com # gives the zonefile header (ttl vals)
func DumpPublicDNSZone(zone string) {
entries, err := net.LookupHost(zone)
if err != nil {
panic(err)
}
for _, entry := range entries {
log.Println(entry)
}
}
func dumpIPs(host string) {
ips, err := net.LookupIP(host)
if err != nil {
log.Error(err, "dumpIPs() failed")
}
for _, ip := range ips {
log.Println(host, ip)
}
}
/*
check if ddclient is installed, working, and/or configured
https://github.com/ddclient/ddclient
*/
func ddclient() {
}
/*
check if ddupdate is installed, working, and/or configured
*/
func ddupdate() {
}
func run(s string) string {
cmdArgs := strings.Fields(s)
// Define the command you want to run
// cmd := exec.Command(cmdArgs)
cmd := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...)
// Create a buffer to capture the output
var out bytes.Buffer
// Set the output of the command to the buffer
cmd.Stdout = &out
// Run the command
err := cmd.Run()
if err != nil {
fmt.Println("Error running command:", err)
return ""
}
tmp := shell.Chomp(out.String())
// Output the results
log.Info("Command Output:", tmp)
return tmp
}

View File

@ -26,7 +26,7 @@ func GetProcessNameByPort(port int) string {
for _, line := range lines { for _, line := range lines {
fields := strings.Fields(line) fields := strings.Fields(line)
log.Log(PROC, "searchProcNet() portHex:", portHex) log.Log(PROC, "searchProcNet() portHex:", portHex)
if len(fields) > 9 { if (len(fields) > 9) {
log.Log(PROC, "searchProcNet() fields[9]", fields[9]) log.Log(PROC, "searchProcNet() fields[9]", fields[9])
} }
log.Log(PROC, "searchProcNet() lines:", line) log.Log(PROC, "searchProcNet() lines:", line)
@ -36,7 +36,7 @@ func GetProcessNameByPort(port int) string {
// Convert the hexadecimal string to an integer // Convert the hexadecimal string to an integer
value, _ := strconv.ParseInt(parts[1], 16, 64) value, _ := strconv.ParseInt(parts[1], 16, 64)
log.Log(PROC, "searchProcNet() value, port =", value, port, "parts[1] =", parts[1]) log.Log(PROC, "searchProcNet() value, port =", value, port, "parts[1] =", parts[1])
if port == int(value) { if (port == int(value)) {
log.Log(PROC, "searchProcNet() THIS IS THE LINE:", fields) log.Log(PROC, "searchProcNet() THIS IS THE LINE:", fields)
return fields[9] return fields[9]
} }

View File

@ -7,9 +7,8 @@ package linuxstatus
import ( import (
"net" "net"
"go.wit.com/gui/gui"
"go.wit.com/gui" "go.wit.com/gui/gadgets"
"go.wit.com/lib/gadgets"
) )
var me *LinuxStatus var me *LinuxStatus
@ -41,6 +40,7 @@ type LinuxStatus struct {
Interfaces *gui.Node Interfaces *gui.Node
speed *gadgets.OneLiner speed *gadgets.OneLiner
speedActual *gadgets.OneLiner speedActual *gadgets.OneLiner
} }
type IPtype struct { type IPtype struct {

View File

@ -1,9 +1,9 @@
package linuxstatus package linuxstatus
import ( import (
"time"
"sort" "sort"
"strings" "strings"
"time"
) )
// timeFunction takes a function as an argument and returns the execution time. // timeFunction takes a function as an argument and returns the execution time.
@ -31,8 +31,6 @@ func sortLines(input string) string {
} }
func (ls *LinuxStatus) SetSpeedActual(s string) { func (ls *LinuxStatus) SetSpeedActual(s string) {
if !ls.Ready() { if ! ls.Ready() {return}
return ls.speedActual.Set(s)
}
ls.speedActual.SetText(s)
} }

View File

@ -5,11 +5,11 @@
package linuxstatus package linuxstatus
import ( import (
"bytes"
"fmt"
"net"
"os" "os"
"os/exec" "os/exec"
"net"
"bytes"
"fmt"
"strings" "strings"
"go.wit.com/log" "go.wit.com/log"
@ -91,7 +91,7 @@ func run(s string) string {
// Trim leading and trailing whitespace from each line // Trim leading and trailing whitespace from each line
tmp := strings.TrimSpace(out.String()) tmp := strings.TrimSpace(out.String())
// Output the results // Output the results
log.Verbose("Command Output:", tmp) log.Info("Command Output:", tmp)
return tmp return tmp
} }

View File

@ -1,17 +1,14 @@
package linuxstatus package linuxstatus
import ( import (
"errors"
"fmt" "fmt"
"time" "time"
"errors"
"go.wit.com/log" "go.wit.com/log"
) )
func (ls *LinuxStatus) Update() { func (ls *LinuxStatus) Update() {
if ls == nil {
return
}
if ! ls.Ready() { if ! ls.Ready() {
log.Log(WARN, "can't update yet. ready is false") log.Log(WARN, "can't update yet. ready is false")
log.Error(errors.New("Update() is not ready yet")) log.Error(errors.New("Update() is not ready yet"))
@ -31,13 +28,13 @@ func (ls *LinuxStatus) setSpeed(duration time.Duration) {
log.Log(WARN, "can't actually warn") log.Log(WARN, "can't actually warn")
return return
} }
ls.speedActual.SetText(s) ls.speedActual.Set(s)
if duration > 500*time.Millisecond { if (duration > 500 * time.Millisecond ) {
ls.speed.SetText("SLOW") ls.speed.Set("SLOW")
} else if duration > 100*time.Millisecond { } else if (duration > 100 * time.Millisecond ) {
ls.speed.SetText("OK") ls.speed.Set("OK")
} else { } else {
ls.speed.SetText("FAST") ls.speed.Set("FAST")
} }
} }