From 3156765985f2e7ad7b533c893de9d5ce56aab0ee Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Wed, 11 Aug 2021 23:44:53 -0400 Subject: Add a Windows specific function to scrape the route table for the IP address from the default route. --- cmd/web/gethostip.go | 36 +++++++++++++++++++++++++ cmd/web/util.go | 33 ----------------------- cmd/web/windows.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 33 deletions(-) create mode 100644 cmd/web/gethostip.go diff --git a/cmd/web/gethostip.go b/cmd/web/gethostip.go new file mode 100644 index 0000000..3788ac1 --- /dev/null +++ b/cmd/web/gethostip.go @@ -0,0 +1,36 @@ +// +build dragonfly freebsd linux nacl netbsd openbsd solaris darwin + +package main + +// GetHostIP attempts to guess the IP address of the current machine and +// returns that. Simply bails at the first non sane looking IP and returns it. +// Not ideal but it should work well enough most of the time +func GetHostIP() string { + iFaces, err := net.Interfaces() + if err != nil { + return "127.0.0.1" + } + + // RFC 3927 + _, ipv4LinkLocal, _ := net.ParseCIDR("169.254.0.0/16") + + for _, iFace := range iFaces { + addrs, err := iFace.Addrs() + if err != nil { + continue + } + + for _, a := range addrs { + n, ok := a.(*net.IPNet) + if !ok { + continue + } + + if n.IP.To4() != nil && !n.IP.IsLoopback() && !ipv4LinkLocal.Contains(n.IP.To4()) { + return n.IP.String() + } + } + } + + return "127.0.0.1" +} diff --git a/cmd/web/util.go b/cmd/web/util.go index 454912f..294079c 100644 --- a/cmd/web/util.go +++ b/cmd/web/util.go @@ -57,39 +57,6 @@ func getShareLink() string { return fmt.Sprintf("http://%s:%s/", GetHostIP(), getPort()) } -// GetHostIP attempts to guess the IP address of the current machine and -// returns that. Simply bails at the first non sane looking IP and returns it. -// Not ideal but it should work well enough most of the time -func GetHostIP() string { - iFaces, err := net.Interfaces() - if err != nil { - return "127.0.0.1" - } - - // RFC 3927 - _, ipv4LinkLocal, _ := net.ParseCIDR("169.254.0.0/16") - - for _, iFace := range iFaces { - addrs, err := iFace.Addrs() - if err != nil { - continue - } - - for _, a := range addrs { - n, ok := a.(*net.IPNet) - if !ok { - continue - } - - if n.IP.To4() != nil && !n.IP.IsLoopback() && !ipv4LinkLocal.Contains(n.IP.To4()) { - return n.IP.String() - } - } - } - - return "127.0.0.1" -} - func getPort() string { s := strings.Split(Listen, ":") diff --git a/cmd/web/windows.go b/cmd/web/windows.go index 76491f1..d7e32d2 100644 --- a/cmd/web/windows.go +++ b/cmd/web/windows.go @@ -2,6 +2,16 @@ package main +import ( + "bufio" + "bytes" + "errors" + "fmt" + "io" + "os/exec" + "regexp" +) + // DefaultLib path for the operating system in question var DefaultLib string = `C:\Program Files (x86)\Steam\steamapps` @@ -10,3 +20,69 @@ var DefaultLib string = `C:\Program Files (x86)\Steam\steamapps` func BrowserCommand(url string) []string { return []string{"cmd", "/c", "start", url} } + +func GetHostIP() string { + s, err := GetHostIPFromRoutes() + if err != nil { + panic(err) + } + return s +} + +// GetHostIPFromRoutes is a terrible little function to scrape up the +// information about the route table and return the IP address associated with +// the default route +func GetHostIPFromRoutes() (string, error) { + spaceRe := regexp.MustCompile(" *") + + c := exec.Command("route", "print", "-4") + + b, err := c.Output() + if err != nil { + return "", err + } + + rdr := bufio.NewReader(bytes.NewBuffer(b)) + + net := false + for { + l, err := rdr.ReadBytes('\n') + if err == io.EOF { + break + } else if err != nil { + return "", err + } + + fmt.Println(string(l)) + + if bytes.HasPrefix(l, []byte("Network Destination")) && !net { + net = true + continue + } + + if net && bytes.HasPrefix(l, []byte("=")) { + break + } + + if net == false { + continue + } + + fmt.Println("Before: ", string(l)) + + l = spaceRe.ReplaceAll(l, []byte{' '}) + l = bytes.TrimPrefix(l, []byte{' '}) + + fields := bytes.Split(l, []byte(" ")) + + if len(fields) < 5 { + return "", errors.New("Less than five fields") + } + + if bytes.Equal(fields[0], []byte("0.0.0.0")) { + return string(fields[3]), nil + } + } + + return "", errors.New("Unable to parse IP from route table") +} -- cgit v1.2.3