diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2021-08-11 23:44:53 -0400 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2021-08-11 23:47:53 -0400 |
| commit | 3156765985f2e7ad7b533c893de9d5ce56aab0ee (patch) | |
| tree | c7f2e08892ce675ee5f0ec30e57043ea1e1ef5df /cmd/web/windows.go | |
| parent | 705fc2f44be5528b07897cd2f020f429024cddf0 (diff) | |
| download | steam-export-3156765985f2e7ad7b533c893de9d5ce56aab0ee.tar.gz steam-export-3156765985f2e7ad7b533c893de9d5ce56aab0ee.tar.xz | |
Add a Windows specific function to scrape the route table for the IP address from the default route.
Diffstat (limited to 'cmd/web/windows.go')
| -rw-r--r-- | cmd/web/windows.go | 76 |
1 files changed, 76 insertions, 0 deletions
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") +} |
