-
Notifications
You must be signed in to change notification settings - Fork 1
/
html.go
252 lines (215 loc) · 4.65 KB
/
html.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"fmt"
"io"
"os"
"strings"
"text/template"
"github.com/pkg/errors"
)
type htmlvars struct {
Name string
Contract string
Grade string
Cryptcheck string
Mozilla string
Redir string
DefKey string
DefSig string
DefCA string
IsExpired string
Issues string
Protocols string
PFS string
OCSP string
HSTS string
Sweet32 string
}
func grade(val string) string {
switch val {
case "A+":
fallthrough
case "A":
return green(val)
case "A-":
return yellow(val)
case "B+":
fallthrough
case "B":
fallthrough
case "B-":
return orange(val)
case "C+":
fallthrough
case "C":
fallthrough
case "C-":
fallthrough
case "D+":
fallthrough
case "D":
fallthrough
case "D-":
fallthrough
case "E+":
fallthrough
case "E":
fallthrough
case "E-":
fallthrough
case "F":
return red(val)
}
return white(" ")
}
func booleanT(val bool) string {
if val {
return white("TRUE")
}
return red("FALSE")
}
func booleanF(val bool) string {
if val {
return red("TRUE")
}
return white("FALSE")
}
func proto(val string) string {
switch val {
case "TLSv1.2":
return green(val)
case "TLSv1.1,TLSv1.2":
fallthrough
case "TLSv1.0,TLSv1.1,TLSv1.2":
return yellow(val)
case "SSLv3.0,TLSv1.0":
return red(val)
}
return white(val)
}
const (
hstsNone = -1 // red
hstsLow = 86400 * 90 // orange
hstsGood = 86400 * 180 // yellow
// green
)
func hsts(age int64) string {
if age == hstsNone {
return red("NO")
}
if age >= 0 && age < hstsLow {
return orange(fmt.Sprintf("%d", age))
}
if age >= hstsLow && age < hstsGood {
return yellow(fmt.Sprintf("%d", age))
}
return green(fmt.Sprintf("%d", age))
}
func servertype(t int) string {
if t == TypeHTTPSok {
return green("HTTPS")
} else if t == TypeHTTPSnok {
return orange("MIXED")
} else {
return red("HTTP")
}
}
func (r *TLSReport) ToHTML(w io.Writer, tmpl string) error {
var (
err error
)
debug("ToHTML\n")
debug("tmpl=%s\n", tmpl)
t := template.Must(template.New("html-report").Parse(tmpl))
debug("t=%v\n", t)
debug("Sites=%#v\n", r.Sites)
Sites := []htmlvars{}
for _, s := range r.Sites {
h := htmlvars{
Name: text(s.Name),
Contract: text(s.Contract),
Grade: grade(s.Grade),
Cryptcheck: grade(s.CryptCheck),
Mozilla: grade(s.Mozilla),
Redir: servertype(s.Type),
DefKey: booleanT(s.DefKey),
DefSig: booleanT(s.DefSig),
DefCA: booleanT(s.DefCA),
IsExpired: booleanF(s.IsExpired),
Issues: booleanF(s.PathIssues),
Protocols: proto(s.Protocols),
PFS: booleanT(s.PFS),
OCSP: booleanT(s.OCSP),
HSTS: hsts(s.HSTS),
Sweet32: booleanF(s.Sweet32),
}
Sites = append(Sites, h)
debug("h=%#v\n", h)
}
htmlVars := struct {
Date string
Version string
Sites []htmlvars
}{makeDate(), r.SSLLabs, Sites}
err = t.ExecuteTemplate(w, "html-report", htmlVars)
return errors.Wrap(err, "can not write HTML file")
}
func WriteHTML(fh *os.File, final *TLSReport, cntrs, https map[string]int) error {
var err error
debug("WriteHTML")
if final == nil {
return fmt.Errorf("nil final")
}
if len(final.Sites) == 0 {
return fmt.Errorf("empty final")
}
debug("tmpls=%v\n", tmpls)
if err = final.ToHTML(fh, tmpls["templ.html"]); err != nil {
return errors.Wrap(err, "Can not write HTML")
}
// Generate colour map
//cm := final.ColourMap()
if fSummary != "" {
fn := fSummary + "-" + makeDate() + ".html"
verbose("HTML summary: %s\n", fn)
fh = checkOutput(fn)
err = writeHTMLSummary(fh, cntrs, https)
}
return err
}
func red(str string) string {
var buf strings.Builder
t, _ := template.New("red").Parse(`<td class=xl64 align=center>{{.}}</td>`)
t.Execute(&buf, str)
return buf.String()
}
func yellow(str string) string {
var buf strings.Builder
t, _ := template.New("yellow").Parse(`<td class=xl631 align=center>{{.}}</td>`)
t.Execute(&buf, str)
return buf.String()
}
func orange(str string) string {
var buf strings.Builder
t, _ := template.New("orange").Parse(`<td class=xl63 align=center>{{.}}</td>`)
t.Execute(&buf, str)
return buf.String()
}
func green(str string) string {
var buf strings.Builder
t, _ := template.New("green").Parse(`<td class=xl65 align=center>{{.}}</td>`)
t.Execute(&buf, str)
return buf.String()
}
func white(str string) string {
var buf strings.Builder
t, _ := template.New("white").Parse(`<td class=xl661 align=center>{{.}}</td>`)
t.Execute(&buf, str)
return buf.String()
}
func text(str string) string {
var buf strings.Builder
t, _ := template.New("white").Parse(`<td height=21 style='height:16.0pt'>{{.}}</td>`)
t.Execute(&buf, str)
return buf.String()
}