forked from nimbusec-oss/api-frontend-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
102 lines (88 loc) · 2.47 KB
/
handler.go
File metadata and controls
102 lines (88 loc) · 2.47 KB
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
package main
import (
"encoding/json"
"log"
"net/http"
"sort"
"time"
)
// Dashboard returns shows the current produced report
func (ctl *Controller) Dashboard(w http.ResponseWriter, r *http.Request) {
today := time.Now().Format("2006-01-02")
rd := map[int]*DomainReport{}
issues, err := ctl.nimbusec.GetIssues()
if err != nil {
http.Error(w, err.Error(), http.StatusGatewayTimeout)
return
}
domains, err := ctl.nimbusec.FindDomains("")
if err != nil {
http.Error(w, err.Error(), http.StatusGatewayTimeout)
return
}
for _, domain := range domains {
rd[domain.Id] = &DomainReport{
URL: domain.Name,
}
}
for _, issue := range issues {
//log.Printf("processing DomainID %d --> rd %+v --> issue %+v\n", issue.DomainID, rd[issue.DomainID], issue)
domainid := issue.DomainID
category := issue.Category
severity := issue.Severity
if rd[domainid] == nil {
log.Printf("no domain for issue %+v\n", issue)
continue
}
switch category {
case "webshell":
rd[domainid].Webshell = severity
case "malware":
rd[domainid].Malware = severity
case "tls":
rd[domainid].TLS = severity
case "application":
rd[domainid].Application = severity
case "text":
rd[domainid].Text = severity
case "reputation":
rd[domainid].Reputation = severity
case "configuration":
rd[domainid].Configuration = severity
}
}
// sort domains by name
reports := make(ReportList, 0, len(rd))
for _, value := range rd {
reports = append(reports, value)
}
sort.Sort(reports)
ctl.reports = reports
err = ctl.tpl.ExecuteTemplate(w, "report.html", V{
"reports": ctl.reports,
"today": today,
"reportlogo": ctl.reportlogo,
})
if err != nil {
log.Printf("%+v", err)
}
}
// SaveReport returns the report data in json format which can be saved and used in other apps or opened in this one again
func (ctl *Controller) SaveReport(w http.ResponseWriter, r *http.Request) {
JSON(w, r, http.StatusOK, ctl.reports)
}
// JSON encodes v as json and writes it to the ResponseWriter.
func JSON(w http.ResponseWriter, r *http.Request, status int, v interface{}) error {
w.Header().Add("content-type", "application/json; charset=utf-8")
w.WriteHeader(status)
if v == nil {
return nil
}
return json.NewEncoder(w).Encode(v)
}
// Error sends an JSON error response back to the client and logs the error.
func Error(w http.ResponseWriter, r *http.Request, status int, err error) error {
return JSON(w, r, status, map[string]string{
"error": err.Error(),
})
}