summaryrefslogtreecommitdiff
path: root/main.go
blob: 1d4d4df261e836da699a112e7577344dc8161230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// This creates a simple hello world window
package main

import 	(
	"log"
	"net"
	"github.com/fsnotify/fsnotify"
	"git.wit.org/wit/gui"
	arg "github.com/alexflint/go-arg"
)

var me Host

func main() {
	arg.MustParse(&args)
	// fmt.Println(args.Foo, args.Bar, args.User)
	log.Println("Toolkit = ", args.Toolkit)

	me.ips = make(map[string]*IPtype)

	// 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)
			}
		}
	}
}