aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/util.go
blob: 454912f4fecd0067c4fa2bc8f37236323598c10d (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main

import (
	"fmt"
	"io"
	"net"
	"net/http"
	"os"
	"strings"
)

// UnauthorizedIfNotLocal is a middleware that returns unauthorized if not
// being accessed from loopback, as a basic form of host authentication.
func UnauthorizedIfNotLocal(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if !isLocal(r.RemoteAddr) {
			http.Error(w, "Unauthorized", http.StatusUnauthorized)
			Logger.Printf("Unauthorized request from: %s for %s",
				r.RemoteAddr, r.RequestURI)
			return
		}
		h.ServeHTTP(w, r)
	})
}

var isLocalCIDR = "127.0.0.1/8"

func isLocal(addr string) bool {
	if strings.Contains(isLocalCIDR, ",") {
		for _, cidr := range strings.Split(isLocalCIDR, ",") {
			_, localNet, err := net.ParseCIDR(cidr)
			if err != nil {
				panic(err)
			}
			if localNet.Contains(net.ParseIP(strings.Split(addr, ":")[0])) {
				return true
			}
		}

		return false
	} else {
		_, localNet, err := net.ParseCIDR(isLocalCIDR)
		if err != nil {
			panic(err)
		}
		return localNet.Contains(net.ParseIP(strings.Split(addr, ":")[0]))
	}
}

var shareLink = ""

func getShareLink() string {
	if shareLink != "" {
		return shareLink
	}

	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, ":")

	if len(s) != 2 {
		return Listen
	}

	return s[1]
}

// ServeSelf tries to locate the currently running executable and serve
// it down to the client.
func ServeSelf(w http.ResponseWriter, r *http.Request) {
	s, err := os.Executable()
	if err != nil {
		Logger.Println("While trying to get my executable path: ", err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	fh, err := os.Open(s)
	if err != nil {
		Logger.Println("While opening my own executable for reading: ", err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	_, err = io.Copy(w, fh)
	fh.Close()
	return
}