-
Notifications
You must be signed in to change notification settings - Fork 6
/
job.go
190 lines (166 loc) · 4.24 KB
/
job.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
package main
import (
"log"
"net"
"net/http"
"strconv"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/jamesog/scan/internal/sqlite"
"github.com/jamesog/scan/pkg/scan"
"github.com/prometheus/client_golang/prometheus"
)
type jobData struct {
indexData
JobID []string
Jobs []scan.Job
}
// Handler for GET and POST /job
func (app *App) newJob(w http.ResponseWriter, r *http.Request) {
var user User
if !authDisabled {
session, err := store.Get(r, "user")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, ok := session.Values["user"]; !ok {
data := jobData{indexData: indexData{URI: r.RequestURI}}
tmpl.ExecuteTemplate(w, "job", data)
return
}
v := session.Values["user"]
switch v := v.(type) {
case string:
user.Email = v
case User:
user = v
}
}
var jobID []string
var errors []string
if r.Method == "POST" {
err := r.ParseForm()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
f := r.Form
cidr := f.Get("cidr")
ports := f.Get("ports")
proto := f["proto"]
if cidr == "" {
errors = append(errors, "CIDR")
}
if ports == "" {
errors = append(errors, "Ports")
}
if len(proto) == 0 {
errors = append(errors, "Protocol")
}
// If we have form parameters, save the data as a new job.
// Multiple protocols can be submitted. These are saved as separate jobs.
if len(errors) == 0 {
for i := range proto {
id, err := app.db.SaveJob(cidr, ports, proto[i], user.Email)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
jobID = append(jobID, strconv.FormatInt(id, 10))
}
}
}
jobs, err := app.db.LoadJobs(sqlite.SQLFilter{})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sub, err := app.db.LoadJobSubmission()
if err != nil {
log.Println("newJob: couldn't load submissions:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Fetch result numbers for display in the navbar
// Errors aren't fatal here, we can just display 0 results if something
// goes wrong
results, _ := app.db.ResultData("", "", "")
data := jobData{
indexData: indexData{
Errors: errors,
Authenticated: true,
User: user,
URI: r.URL.Path,
Submission: sub,
Data: results,
},
JobID: jobID,
Jobs: jobs,
}
tmpl.ExecuteTemplate(w, "job", data)
}
// Handler for GET /jobs
func (app *App) jobs(w http.ResponseWriter, r *http.Request) {
jobs, err := app.db.LoadJobs(sqlite.SQLFilter{
Where: []string{"received IS NULL"},
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, err.Error())
}
render.JSON(w, r, jobs)
}
// Handler for PUT /results/{id}
func (app *App) recvJobResults(w http.ResponseWriter, r *http.Request) {
job := chi.URLParam(r, "id")
// Check if the job ID is valid
jobs, err := app.db.LoadJobs(sqlite.SQLFilter{
Where: []string{"rowid=?"},
Values: []interface{}{job},
})
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if len(jobs) == 0 {
http.Error(w, "Job does not exist", http.StatusBadRequest)
return
}
if !jobs[0].Received.IsZero() {
http.Error(w, "Job already submitted", http.StatusBadRequest)
return
}
now := time.Now().UTC()
// Insert the results as normal
count, err := app.saveResults(w, r, now)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Update the job
err = app.db.UpdateJob(job, count)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
ip = r.RemoteAddr
}
id, _ := strconv.ParseInt(job, 10, 64)
err = app.db.SaveSubmission(ip, &id, now)
if err != nil {
log.Println("recvJobResults: error saving submission:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Finally, update metrics
gaugeJobSubmission.Set(float64(now.Unix()))
gaugeJobs.With(prometheus.Labels{
"id": strconv.FormatInt(id, 10),
"submitted": strconv.FormatInt(time.Now().Unix(), 10),
"received": strconv.FormatInt(time.Now().Unix(), 10),
}).Set(float64(count))
}