This repository was archived by the owner on Apr 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoutput.go
202 lines (172 loc) · 5.34 KB
/
output.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
// Author: Liam Stanley <me@liamstanley.io>
// Docs: https://marill.liam.sh/
// Repo: https://github.com/lrstanley/marill
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"text/template"
"time"
"github.com/lrstanley/marill/utils"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/css"
"github.com/tdewolff/minify/html"
"github.com/tdewolff/minify/js"
)
// JSONOutput is the generated json that will be embedded in Angular.
type JSONOutput struct {
Version string
VersionFull string
GitRevision string
Out []*JSONTestResult
Successful int
Failed int
Success bool
HostFile string
TimeScanned string
ScanConfig ScanConfig
}
// Bytes returns a bytes array representation of JSONOutput.
func (j *JSONOutput) Bytes() []byte {
jsonBytes, err := json.Marshal(j)
if err != nil {
panic(err)
}
return jsonBytes
}
// String returns a string representation of JSONOutput.
func (j *JSONOutput) String() string {
jsonBytes, err := json.Marshal(j)
if err != nil {
panic(err)
}
return fmt.Sprintf("%s", jsonBytes)
}
// StringPretty returns a prettified/indented representation of JSONOutput.
func (j *JSONOutput) StringPretty() string {
jsonBytes, err := json.MarshalIndent(j, "", " ")
if err != nil {
panic(err)
}
return fmt.Sprintf("%s", jsonBytes)
}
// JSONTestResult is a wrapper around the test results, providing string
// representations of some errors and other items that during JSON conversion
// get converted to structs.
type JSONTestResult struct {
*TestResult
Assets []*JSONTestResource
ErrorString string // string representation of any errors
URLString string // string representation of the resulting URL.
}
type JSONTestResource struct {
URL string
Code int
ContentLength int64
Error string
Time *utils.TimerResult
ContentType string
}
func genJSONOutput(scan *Scan) (*JSONOutput, error) {
htmlConvertedResults := make([]*JSONTestResult, len(scan.results))
var hosts string
for i := 0; i < len(scan.results); i++ {
htmlConvertedResults[i] = &JSONTestResult{TestResult: scan.results[i]}
// Make the footprint of assets much smaller.
if htmlConvertedResults[i].Result.Assets != nil && len(htmlConvertedResults[i].Result.Assets) > 0 {
for j := 0; j < len(htmlConvertedResults[i].Result.Assets); j++ {
var errString string
err := htmlConvertedResults[i].Result.Assets[j].Error
if err != nil {
errString = htmlConvertedResults[i].Result.Assets[j].Error.Error()
}
htmlConvertedResults[i].Assets = append(htmlConvertedResults[i].Assets, &JSONTestResource{
URL: htmlConvertedResults[i].Result.Assets[j].URL,
Code: htmlConvertedResults[i].Result.Assets[j].Response.Code,
ContentLength: htmlConvertedResults[i].Result.Assets[j].Response.ContentLength,
Error: errString,
Time: htmlConvertedResults[i].Result.Assets[j].Time,
ContentType: htmlConvertedResults[i].Result.Assets[j].Response.Headers.Get("Content-Type"),
})
}
}
if htmlConvertedResults[i].Result.Error != nil {
htmlConvertedResults[i].ErrorString = htmlConvertedResults[i].Result.Error.Error()
// make it so errors are still true, but it doesn't bloat the json
htmlConvertedResults[i].Result.Error = errors.New(htmlConvertedResults[i].ErrorString)
}
if htmlConvertedResults[i].Result.Response.URL != nil {
htmlConvertedResults[i].URLString = htmlConvertedResults[i].Result.Response.URL.String()
}
// trim out some of the bulk here
if len(htmlConvertedResults[i].Result.Response.Body) > 200 {
htmlConvertedResults[i].Result.Response.Body = htmlConvertedResults[i].Result.Response.Body[0:200] + " [...]"
}
if htmlConvertedResults[i].Result.Request.IP != "" {
hosts += fmt.Sprintf("%s %s\n", htmlConvertedResults[i].Result.Request.IP, htmlConvertedResults[i].Result.Request.URL.Host)
}
}
jsonOut := &JSONOutput{
VersionFull: getVersion(),
Version: version,
GitRevision: commithash,
Out: htmlConvertedResults,
Successful: scan.successful,
Failed: scan.failed,
HostFile: strings.TrimRight(hosts, "\n"),
Success: true,
TimeScanned: time.Now().Format(time.RFC3339),
ScanConfig: conf.scan,
}
return jsonOut, nil
}
func genHTMLOutput(input *JSONOutput) ([]byte, error) {
m := minify.New()
m.AddFunc("text/css", css.Minify)
m.AddFunc("text/html", html.Minify)
m.AddFunc("text/javascript", js.Minify)
// above the necessary static files
htmlRawTmpl, err := Asset("data/html/index.html")
if err != nil {
return nil, err
}
jsRawTmpl, err := Asset("data/html/main.js")
if err != nil {
return nil, err
}
cssRawTmpl, err := Asset("data/html/main.css")
if err != nil {
return nil, err
}
// minify js and css
jsTmpl, err := m.String("text/javascript", string(jsRawTmpl))
if err != nil {
return nil, err
}
cssTmpl, err := m.String("text/css", string(cssRawTmpl))
if err != nil {
return nil, err
}
jsonStr := fmt.Sprintf("%s", input.String())
tmpl := template.New("html")
tmpl.Delims("{[", "]}")
tmpl = template.Must(tmpl.Parse(string(htmlRawTmpl)))
var buf bytes.Buffer
tmpl.Execute(&buf, struct {
JSON string
JS string
CSS string
}{
JSON: jsonStr,
JS: jsTmpl,
CSS: cssTmpl,
})
htmlBytes, err := m.Bytes("text/html", buf.Bytes())
if err != nil {
return nil, err
}
return htmlBytes, nil
}