-
Notifications
You must be signed in to change notification settings - Fork 6
/
scan.go
487 lines (428 loc) · 12.3 KB
/
scan.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//go:generate go-bindata views static/...
package main
import (
"bytes"
"crypto/tls"
"database/sql"
"encoding/json"
"errors"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"mime"
"net"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"time"
"golang.org/x/crypto/acme/autocert"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/jamesog/scan/internal/sqlite"
"github.com/jamesog/scan/pkg/scan"
)
var (
// Flag variables
authDisabled bool
credsFile string
dataDir string
httpsAddr string
verbose bool
// HTML templates
tmpl *template.Template
)
type storage interface {
LoadData(filter sqlite.SQLFilter) ([]scan.IPInfo, error)
ResultData(ip, fs, ls string) (scan.Data, error)
SaveData(results []scan.Result, now time.Time) (int64, error)
LoadSubmission(filter sqlite.SQLFilter) (scan.Submission, error)
SaveSubmission(host string, job *int64, now time.Time) error
LoadTracerouteIPs() (map[string]struct{}, error)
LoadTraceroute(dest string) (string, error)
SaveTraceroute(dest, trace string) error
LoadJobs(filter sqlite.SQLFilter) ([]scan.Job, error)
LoadJobSubmission() (scan.Submission, error)
SaveJob(cidr, ports, proto, user string) (int64, error)
UpdateJob(id string, count int64) error
LoadUsers() ([]string, error)
LoadGroups() ([]string, error)
UserExists(email string) (bool, error)
SaveUser(email string) error
DeleteUser(email string) error
SaveAudit(ts time.Time, user, event, info string) error
}
type indexData struct {
NotAuth string
Errors []string
Authenticated bool
User User
URI string
AllResults bool
Submission scan.Submission
scan.Data
}
type App struct {
db storage
}
// Handler for GET /
func (app *App) index(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 := indexData{URI: r.RequestURI}
if flash := session.Flashes("unauth_flash"); len(flash) > 0 {
data.NotAuth = flash[0].(string)
w.WriteHeader(http.StatusUnauthorized)
session.Save(r, w)
}
tmpl.ExecuteTemplate(w, "index", data)
return
}
v := session.Values["user"]
switch v := v.(type) {
case string:
user.Email = v
case User:
user = v
}
}
q := r.URL.Query()
ip := q.Get("ip")
firstSeen := q.Get("firstseen")
lastSeen := q.Get("lastseen")
_, allResults := q["all"]
results, err := app.db.ResultData(ip, firstSeen, lastSeen)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sub, err := app.db.LoadSubmission(sqlite.SQLFilter{})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := indexData{
Authenticated: true,
User: user,
URI: r.URL.Path,
AllResults: allResults,
Submission: sub,
Data: results,
}
tmpl.ExecuteTemplate(w, "index", data)
}
func (app *App) saveResults(w http.ResponseWriter, r *http.Request, now time.Time) (int64, error) {
if r.Header.Get("Content-Type") != "application/json" {
w.WriteHeader(http.StatusUnsupportedMediaType)
return 0, errors.New("invalid Content-Type")
}
res := new([]scan.Result)
err := json.NewDecoder(r.Body).Decode(&res)
if err != nil {
return 0, err
}
count, err := app.db.SaveData(*res, now)
if err != nil {
return 0, err
}
return count, nil
}
// Handler for POST /results
func (app *App) recvResults(w http.ResponseWriter, r *http.Request) {
now := time.Now().UTC().Truncate(time.Second)
_, err := app.saveResults(w, r, now)
if err != nil {
log.Println("recvResults: error saving results:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
ip = r.RemoteAddr
}
err = app.db.SaveSubmission(ip, nil, now)
if err != nil {
log.Println("recvResults: error saving submission:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Update metrics with latest data
results, err := app.db.ResultData("", "", "")
if err != nil {
log.Printf("saveResults: error fetching results for metrics update: %v\n", err)
} else {
gaugeSubmission.Set(float64(now.Unix()))
gaugeTotal.Set(float64(results.Total))
gaugeLatest.Set(float64(results.Latest))
gaugeNew.Set(float64(results.New))
}
}
// Handler for POST /traceroute
func (app *App) recvTraceroute(w http.ResponseWriter, r *http.Request) {
dest := r.FormValue("dest")
f, _, err := r.FormFile("traceroute")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
trace, err := ioutil.ReadAll(f)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = app.db.SaveTraceroute(dest, string(trace))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Location", path.Join(r.URL.Path, dest))
w.WriteHeader(http.StatusCreated)
}
// Handler for GET /traceroute/{ip}
func (app *App) traceroute(w http.ResponseWriter, r *http.Request) {
ip := chi.URLParam(r, "ip")
path, err := app.db.LoadTraceroute(ip)
switch {
case errors.Is(err, sql.ErrNoRows):
http.Error(w, "Traceroute not found", http.StatusNotFound)
return
case err != nil:
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
io.WriteString(w, path)
}
// redirectHTTPS is a middleware for redirecting non-HTTPS requests to HTTPS
func redirectHTTPS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, httpsPort, err := net.SplitHostPort(httpsAddr)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if r.TLS == nil {
url := r.URL
url.Scheme = "https"
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
url.Host = r.Host
} else {
url.Host = host
}
if httpsPort != "443" {
url.Host = net.JoinHostPort(url.Host, httpsPort)
}
http.Redirect(w, r, url.String(), http.StatusMovedPermanently)
return
}
next.ServeHTTP(w, r)
})
}
type assetMap map[string]asset
var assets assetMap
// loadAssetsFromDir gets all assets whose parent directory is "name" and
// returns a map of the asset path to the asset function.
func loadAssetsFromDir(name string) assetMap {
assets = make(assetMap)
for b := range _bindata {
if strings.HasPrefix(b, name+"/") {
a, err := _bindata[b]()
if err != nil {
log.Printf("Failed to load asset %s: %v", b, err)
}
assets[b] = *a
}
}
return assets
}
// staticHandler returns a static asset from the map generated by
// loadAssetsFromDir.
func staticHandler(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/")
if a, ok := assets[path]; ok {
ct := mime.TypeByExtension(filepath.Ext(a.info.Name()))
if ct == "" {
ct = http.DetectContentType(a.bytes)
}
w.Header().Set("Content-Type", ct)
b := bytes.NewReader(a.bytes)
http.ServeContent(w, r, a.info.Name(), a.info.ModTime(), b)
}
http.NotFound(w, r)
}
func (app *App) setupRouter(middlewares ...func(http.Handler) http.Handler) *chi.Mux {
r := chi.NewRouter()
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
for _, mw := range middlewares {
r.Use(mw)
}
assets = loadAssetsFromDir("static")
r.Get("/", app.index)
r.Route("/admin", func(r chi.Router) {
r.Get("/", app.adminHandler)
r.Post("/", app.adminHandler)
})
r.Get("/auth", app.authHandler)
r.Route("/job", func(r chi.Router) {
r.Get("/", app.newJob)
r.Post("/", app.newJob)
})
r.Get("/jobs", app.jobs)
r.Get("/login", app.loginHandler)
r.Get("/logout", app.logoutHandler)
r.Post("/results", app.recvResults)
r.Put("/results/{id}", app.recvJobResults)
r.Get("/static/*", staticHandler)
r.Post("/traceroute", app.recvTraceroute)
r.Get("/traceroute/{ip}", app.traceroute)
return r
}
func setupTemplates() {
funcMap := template.FuncMap{
"join": func(sep string, s []string) string {
return strings.Join(s, sep)
},
}
tmpl = template.New("").Funcs(funcMap)
views, err := AssetDir("views")
if err != nil {
log.Fatal(err)
}
for _, file := range views {
b, err := Asset("views/" + file)
if err != nil {
log.Println(err)
continue
}
t := tmpl.New(filepath.Base(file))
template.Must(t.Parse(string(b)))
}
}
func main() {
flag.BoolVar(&authDisabled, "no-auth", false, "Disable authentication")
flag.StringVar(&credsFile, "credentials", "client_secret.json",
"OAuth 2.0 credentials `file`\n"+
"Relative paths are taken as relative to -data.dir")
flag.StringVar(&dataDir, "data.dir", ".", "Data directory `path`")
httpAddr := flag.String("http.addr", ":80", "HTTP `address`:port")
flag.StringVar(&httpsAddr, "https.addr", ":443", "HTTPS `address`:port")
metricsAddr := flag.String("metrics.addr", "localhost:3000", "Metrics `address`:port")
metricsTLS := flag.Bool("metrics.tls", false, "Enable AutoTLS for metrics, if -tls enabled\n"+
"This is useful when exposing metrics on a public interface")
enableTLS := flag.Bool("tls", false, "Enable AutoTLS")
tlsHostname := flag.String("tls.hostname", "", "(Optional) Restrict AutoTLS to `hostname`")
flag.BoolVar(&verbose, "v", false, "Enable verbose logging")
flag.Parse()
// Disable TLS on metrics if TLS wasn't generally enabled as autocert
// isn't set up.
if !*enableTLS && *metricsTLS {
log.Println("Info: Disabling -metrics.tls as -tls was not enabled")
*metricsTLS = false
}
if !filepath.IsAbs(credsFile) {
credsFile = filepath.Join(dataDir, credsFile)
}
if !authDisabled {
oauthConfig()
}
db, err := sqlite.Open(filepath.Join(dataDir, sqlite.DefaultDBFile))
if err != nil {
log.Fatalf("failed to open database: %v", err)
}
app := &App{db: db}
setupTemplates()
var middlewares []func(http.Handler) http.Handler
if authDisabled {
fmt.Fprintf(os.Stderr, "%sAuthentication Disabled%s\n", "\033[31m", "\033[0m")
}
var m *autocert.Manager
if *enableTLS {
m = &autocert.Manager{
Cache: autocert.DirCache(filepath.Join(dataDir, ".cache")),
Prompt: autocert.AcceptTOS,
}
if *tlsHostname != "" {
m.HostPolicy = autocert.HostWhitelist(*tlsHostname)
}
middlewares = append(middlewares, m.HTTPHandler, redirectHTTPS)
}
r := app.setupRouter(middlewares...)
// Common http.Server timeout values
readTimeout := 5 * time.Second
writeTimeout := 5 * time.Second
idleTimeout := 120 * time.Second
httpSrv := &http.Server{
Addr: *httpAddr,
Handler: r,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
IdleTimeout: idleTimeout,
}
metricsMux := chi.NewRouter()
metricsMux.Use(middleware.RealIP)
metricsMux.Use(middleware.Logger)
if *metricsTLS {
metricsMux.Use(redirectHTTPS)
}
metricsMux.Handle("/metrics", app.metrics())
metricsSrv := &http.Server{
Addr: *metricsAddr,
Handler: metricsMux,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
IdleTimeout: idleTimeout,
}
if !*metricsTLS {
log.Println("Metrics HTTP server starting on", metricsSrv.Addr)
go func() { log.Fatal(metricsSrv.ListenAndServe()) }()
}
if *enableTLS {
tlsConfig := &tls.Config{
GetCertificate: m.GetCertificate,
PreferServerCipherSuites: true,
CurvePreferences: []tls.CurveID{
tls.CurveP256,
tls.X25519,
},
MinVersion: tls.VersionTLS12,
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
},
}
httpsSrv := &http.Server{
Addr: httpsAddr,
Handler: r,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
IdleTimeout: idleTimeout,
TLSConfig: tlsConfig,
}
if *metricsTLS {
metricsSrv.Addr = *metricsAddr
metricsSrv.Handler = metricsMux
metricsSrv.TLSConfig = tlsConfig
log.Println("Metrics HTTPS server starting on", metricsSrv.Addr)
go func() { log.Fatal(metricsSrv.ListenAndServeTLS("", "")) }()
}
log.Println("HTTPS server starting on", httpsSrv.Addr)
go func() { log.Fatal(httpsSrv.ListenAndServeTLS("", "")) }()
}
log.Println("HTTP server starting on", httpSrv.Addr)
log.Fatal(httpSrv.ListenAndServe())
}