aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/windows.go
blob: d7e32d2cb5e7fd1a880a8fabaaeea6c56c9a9481 (plain) (blame)
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
// +build windows

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`

// BrowserCommand returns a []string containing the command that will start the
// browser for a given url specific to the OS
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")
}