a bunch more dns stuff

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-02-09 09:07:00 -06:00
parent 00082af773
commit d00a8f5cd3
4 changed files with 144 additions and 0 deletions

View File

@ -11,3 +11,6 @@ build:
update:
GO111MODULE="off" go get -v -u -x .
clean:
rm control-panel-dns

31
gui.go
View File

@ -3,8 +3,11 @@ package main
import (
"os"
"os/user"
"log"
"net"
"git.wit.org/wit/gui"
"github.com/davecgh/go-spew/spew"
)
// This initializes the first window
@ -31,6 +34,8 @@ func initGUI() {
func addDemoTab(window *gui.Node, title string) {
var newNode, g, g2, tb *gui.Node
var err error
var name string
newNode = window.NewTab(title)
log.Println("addDemoTab() newNode.Dump")
@ -53,6 +58,32 @@ func addDemoTab(window *gui.Node, title string) {
s := tb.GetText()
log.Println("text =", s)
}
g2.NewButton("hello", func () {
log.Println("world")
scanInterfaces()
})
g2.NewButton("os.Hostname()", func () {
name, err = os.Hostname()
log.Println("name =", name, err)
})
g2.NewButton("os.User()", func () {
user, _ := user.Current()
spew.Dump(user)
log.Println("os.Getuid =", os.Getuid())
})
g2.NewButton("Escalate()", func () {
Escalate()
})
g2.NewButton("LookupAddr(<raw ipv6>) == fire from /etc/hosts", func () {
host, err := net.LookupAddr("2600:1700:afd5:6000:b26e:bfff:fe80:3c52")
if err != nil {
return
}
log.Println("host =", host)
})
g2.NewButton("DumpPublicDNSZone(apple.com)", func () {
DumpPublicDNSZone("apple.com")
})
}
func myDefaultExit(n *gui.Node) {

69
main.go
View File

@ -3,6 +3,8 @@ package main
import (
"log"
"net"
"github.com/fsnotify/fsnotify"
"git.wit.org/wit/gui"
arg "github.com/alexflint/go-arg"
)
@ -13,5 +15,72 @@ func main() {
log.Println("Toolkit = ", args.Toolkit)
// gui.InitPlugins([]string{"andlabs"})
scanInterfaces()
watchNetworkInterfaces()
go inotifyNetworkInterfaceChanges()
gui.Main(initGUI)
}
func watchNetworkInterfaces() {
// Get list of network interfaces
interfaces, _ := net.Interfaces()
// Set up a notification channel
notification := make(chan net.Interface)
// Start goroutine to watch for changes
go func() {
for {
// Check for changes in each interface
for _, i := range interfaces {
if status := i.Flags & net.FlagUp; status != 0 {
notification <- i
log.Println("something on i =", i)
}
}
}
}()
}
func inotifyNetworkInterfaceChanges() 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("inotifyNetworkInterfaceChanges() event =", event)
if event.Op&fsnotify.Create == fsnotify.Create {
// Do something on network interface creation
}
case err := <-watcher.Errors:
return err
}
}
}
func scanInterfaces() {
ifaces, _ := net.Interfaces()
for _, i := range ifaces {
log.Println(i)
addrs, _ := i.Addrs()
for _, addr := range addrs {
log.Println("\taddr =", addr)
switch v := addr.(type) {
case *net.IPNet:
log.Println("\t\taddr.(type) = *net.IPNet")
default:
log.Println("\t\taddr.(type) =", v)
}
}
}
}

41
unix.go Normal file
View File

@ -0,0 +1,41 @@
// This creates a simple hello world window
package main
import (
"os"
"os/exec"
"log"
"net"
// "git.wit.org/wit/gui"
// "github.com/davecgh/go-spew/spew"
)
func CheckSuperuser() bool {
return os.Getuid() == 0
}
func Escalate() {
if os.Getuid() != 0 {
cmd := exec.Command("sudo", "./control-panel-dns")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
}
// You need permission to do a zone transfer. Otherwise:
// dig +noall +answer +multiline lab.wit.org any
// dig +all +multiline fire.lab.wit.org # 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)
}
}