-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathagent_http.go
167 lines (159 loc) · 4.02 KB
/
agent_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"html/template"
"net/http"
"time"
"golang.org/x/oauth2"
)
const timeFmt = "Mon Jan 2 15:04:05"
var htmlStatusTemplate = `
<!doctype html>
<html>
<style>
.styled-table {
border-collapse: collapse;
margin: 25px 0;
font-size: 0.9em;
font-family: sans-serif;
min-width: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
}
.styled-table thead tr {
background-color: #009879;
color: #ffffff;
text-align: left;
}
.styled-table th,
.styled-table td {
padding: 12px 15px;
}
.styled-table tbody tr {
border-bottom: 1px solid #dddddd;
}
.styled-table tbody tr:nth-of-type(even) {
background-color: #f3f3f3;
}
.styled-table tbody tr:last-of-type {
border-bottom: 2px solid #009879;
}
.styled-table tbody tr.active-row {
font-weight: bold;
color: #009879;
}
</style>
<head>
<meta charset="utf-8">
<title>Wiresteward Agent Status</title>
</head>
<body>
<h2 class="text-center">Wiresteward Agent Status</h2>
<div>
<h3 class="text-left">Token Info</h2>
{{ if .TokenMissing }}
<p>No token found, please hit Renew button below.</p>
{{else}}
{{ if .TokenActive }}
<p><b style="color:green;">Active</b><b> until: {{.TokenExpiry}}</b></p>
{{else}}
<p><b style="color:red;">Expired</b><b> since: {{.TokenExpiry}}</b></p>
<p>Please use renew button bellow.</p>
{{end}}
{{end}}
<i>Fetches a new token before renewing leases.</i>
<form action="/renew" method="get">
<button type="submit">Renew</button>
</form>
</div>
<div>
<h3 class="text-left">Routes Info</h2>
<i>Last update at: {{.Time}}.</i>
<form action="/" method="get">
<button type="submit">Refresh</button>
</form>
<table class="styled-table">
<caption>Route Map</caption>
<thead>
<tr>
{{range .RouteTableHeaders}}
<th>{{.}}</th>
{{end}}
</tr>
</thead>
<tbody>
{{range .Routes}}<tr>
<td>{{.Device}}</td>
<td>{{.Dst}}</td>
<td>{{.GW}}</td>
{{ if .IsHealthChecked }}
{{ if .Healthy }}
<td style="color:green;">ok</td>
{{else}}
<td style="color:red;">unhealthy</td>
{{end}}
{{else}}
<td>N/A</td>
{{end}}
{{end}}</tr>
</tbody>
</table>
</div>
</body>
</html>
`
type httpRoute struct {
Device string
Dst string
GW string
IsHealthChecked bool
Healthy bool
}
type httpStatus struct {
Time string
TokenMissing bool
TokenActive bool
TokenExpiry string
RouteTableHeaders []string
Routes []httpRoute
}
func statusHTTPWriter(w http.ResponseWriter, r *http.Request, deviceManagers []*DeviceManager, token *oauth2.Token) {
status := httpStatus{
Time: time.Now().Format(timeFmt),
TokenMissing: true,
TokenActive: true,
}
if token != nil {
status.TokenMissing = false
if token.Expiry.Before(time.Now()) {
status.TokenActive = false
}
status.TokenExpiry = token.Expiry.Format(timeFmt)
}
status.RouteTableHeaders = []string{"Device", "Subnet", "Gateway", "Health"}
routes := []httpRoute{}
for _, dm := range deviceManagers {
if dm.config != nil {
for _, ip := range dm.config.AllowedIPs {
r := httpRoute{
Device: dm.Name(),
Dst: ip.String(),
GW: dm.config.LocalAddress.String(),
IsHealthChecked: dm.isHealthChecked(),
Healthy: dm.healthCheck.healthy,
}
routes = append(routes, r)
}
}
}
status.Routes = routes
tmpl, err := template.New("status").Parse(htmlStatusTemplate)
if err != nil {
logger.Errorf("Failed to parse template: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
err = tmpl.Execute(w, status)
if err != nil {
logger.Errorf("Failed to write template: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
}
}