add IsRealIP() and IsIPv6()

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-02-12 15:35:56 -06:00
parent 32f47de62a
commit ea88b7049a
6 changed files with 177 additions and 65 deletions

View File

@ -1,4 +1,5 @@
run: build run: build
reset
./control-panel-dns ./control-panel-dns
build-release: build-release:
@ -21,3 +22,5 @@ deb:
cd debian && make cd debian && make
-wit mirrors -wit mirrors
netlink:
GO111MODULE="off" go get -v -u github.com/vishvananda/netlink

2
gui.go
View File

@ -63,6 +63,8 @@ func addDNSTab(window *gui.Node, title string) {
} }
g2.NewButton("hello", func () { g2.NewButton("hello", func () {
log.Println("world") log.Println("world")
})
g2.NewButton("scanInterfaces()", func () {
scanInterfaces() scanInterfaces()
}) })
g2.NewButton("os.Hostname()", func () { g2.NewButton("os.Hostname()", func () {

37
he-ipv6-tunnel.sh Executable file
View File

@ -0,0 +1,37 @@
#!/bin/bash -x
## Tunnel ID: 818143
# Creation Date:Feb 12, 2023
# Description:
# IPv6 Tunnel Endpoints
# Server IPv4 Address:184.105.253.14
# Server IPv6 Address:2001:470:1f10:2a::1/64
# Client IPv4 Address:74.87.91.117
# Client IPv6 Address:2001:470:1f10:2a::2/64
# Routed IPv6 Prefixes
# Routed /64:2001:470:1f11:2a::/64
# Routed /48:Assign /48
# DNS Resolvers
# Anycast IPv6 Caching Nameserver:2001:470:20::2
# Anycast IPv4 Caching Nameserver:74.82.42.42
# DNS over HTTPS / DNS over TLS:ordns.he.net
# rDNS DelegationsEdit
# rDNS Delegated NS1:
# rDNS Delegated NS2:
# rDNS Delegated NS3:
# rDNS Delegated NS4:
# rDNS Delegated NS5:
# ifconfig sit0 up
# ifconfig sit0 inet6 tunnel ::184.105.253.14
# ifconfig sit1 up
# ifconfig sit1 inet6 add 2001:470:1f10:2a::2/64
# route -A inet6 add ::/0 dev sit1
modprobe ipv6
ip tunnel add he-ipv6 mode sit remote 184.105.253.14 local 74.87.91.117 ttl 255
ip link set he-ipv6 up
ip addr add 2001:470:1f10:2a::2/64 dev he-ipv6
ip route add ::/0 dev he-ipv6
ip -f inet6 addr

32
lookupAAAA.go Normal file
View File

@ -0,0 +1,32 @@
package main
/*
import "log"
import "github.com/miekg/dns"
import "git.wit.org/jcarr/dnssecsocket"
import "github.com/davecgh/go-spew/spew"
// import "github.com/Showmax/go-fqdn"
func lookupAAAA(hostname string) string {
// lookup the IP address from DNS
dnsRR := dnssecsocket.Dnstrace(hostname, "AAAA")
spew.Dump(dnsRR)
if (dnsRR == nil) {
return "BROKEN"
}
ipaddr := dns.Field(dnsRR, 1)
log.Println("ipaddr", ipaddr)
return ipaddr
}
*/
/*
func main() {
hostname := "check.lab.wit.org"
// 2604:bbc0:2:248:5054:f0ff:fe00:156
lookupAAAA(hostname)
}
*/

67
main.go
View File

@ -3,8 +3,8 @@ package main
import ( import (
"log" "log"
"net" // "net"
"github.com/fsnotify/fsnotify" // "github.com/fsnotify/fsnotify"
"git.wit.org/wit/gui" "git.wit.org/wit/gui"
arg "github.com/alexflint/go-arg" arg "github.com/alexflint/go-arg"
) )
@ -25,66 +25,3 @@ func main() {
go inotifyNetworkInterfaceChanges() go inotifyNetworkInterfaceChanges()
gui.Main(initGUI) 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)
}
}
}
}

101
net.go Normal file
View File

@ -0,0 +1,101 @@
// This creates a simple hello world window
package main
import (
"log"
"net"
"strings"
"github.com/fsnotify/fsnotify"
// "git.wit.org/wit/gui"
"github.com/davecgh/go-spew/spew"
)
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 IsIPv6(address string) bool {
return strings.Count(address, ":") >= 2
}
func scanInterfaces() {
ifaces, _ := net.Interfaces()
spew.Dump(ifaces)
for _, i := range ifaces {
addrs, _ := i.Addrs()
log.Println("addrs = ", i)
spew.Dump(addrs)
for _, addr := range addrs {
log.Println("\taddr =", addr)
spew.Dump(addr)
ips, _ := net.LookupIP(addr.String())
log.Println("\tLookupIP(addr) =", ips)
switch v := addr.(type) {
case *net.IPNet:
log.Println("\t\taddr.(type) = *net.IPNet")
log.Println("\t\taddr.(type) =", v)
ip := v.IP
// spew.Dump(ip)
log.Println("\t\taddr.IP =", ip)
if (IsIPv6(ip.String())) {
log.Println("\t\tIP is IPv6")
} else {
log.Println("\t\tIP is IPv4")
}
log.Println("\t\tIP is IsPrivate() =", ip.IsPrivate())
log.Println("\t\tIP is IsLoopback() =", ip.IsLoopback())
log.Println("\t\tIP is IsLinkLocalUnicast() =", ip.IsLinkLocalUnicast())
if (ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast()) {
log.Println("\t\tIP is Real = false")
} else {
log.Println("\t\tIP is Real = true")
}
// log.Println("\t\tIP is () =", ip.())
default:
log.Println("\t\taddr.(type) = NO IDEA WHAT TO DO HERE v =", v)
}
}
}
}