-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhttp.go
105 lines (89 loc) · 2.26 KB
/
http.go
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
package main
import (
"bufio"
"bytes"
"log"
"net"
"net/http"
"regexp"
"strings"
)
func checkNetwork(r *http.Request) bool {
remote := strings.Split(r.RemoteAddr, ":")[0]
remoteIP := net.ParseIP(remote)
// Is in network
if !hassioNetwork.Contains(remoteIP) {
log.Printf("Access not allow from %s", remote)
return false
}
return true
}
func apiPing(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
w.WriteHeader(http.StatusOK)
}
func apiLogs(w http.ResponseWriter, r *http.Request) {
if !checkNetwork(r) {
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
if r.Method != "GET" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
log.Printf("Access to logs from %s", r.RemoteAddr)
err := supervisorLogs(w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Return the content
w.Header().Add("Content-Type", "text/plain")
}
type statusData struct {
SupervisorConnected bool
SupervisorResponse bool
SupervisorState string
Supported bool
Unsupported []string
Healthy bool
Unhealthy []string
Logs string
}
func statusIndex(w http.ResponseWriter, r *http.Request) {
pingData := supervisorPing()
data := statusData{
SupervisorConnected: pingData.Connected,
SupervisorState: pingData.State,
}
if data.SupervisorConnected {
supervisorInfo, err := getSupervisorInfo()
if err == nil {
data.SupervisorResponse = true
data.Healthy = supervisorInfo.Healthy
data.Supported = supervisorInfo.Supported
resolutionInfo, err := getResolutionInfo()
if err == nil {
data.Unhealthy = resolutionInfo.Unhealthy
data.Unsupported = resolutionInfo.Unsupported
}
}
}
// Set logs
if data.SupervisorState != "" || !data.SupervisorConnected {
var buf bytes.Buffer
var re = regexp.MustCompile(`\[\d+m`)
logWriter := bufio.NewWriter(&buf)
err := supervisorLogs(logWriter)
if err != nil {
data.Logs = err.Error()
} else {
data.Logs = re.ReplaceAllLiteralString(buf.String(), "")
}
}
// Render Website
indexTemplate.Execute(w, data)
}