-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
safebrowsing.go
78 lines (64 loc) · 2.09 KB
/
safebrowsing.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
// Copyright (c) Liam Stanley <[email protected]>. All rights reserved. Use
// of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package main
import (
"context"
"os"
"time"
"github.com/flosch/pongo2"
"github.com/google/safebrowsing"
)
var safeBrowser *safebrowsing.SafeBrowser
func init() {
pongo2.RegisterFilter("threatdefinition", safeTypeToStringFilter)
}
func initSafeBrowsing() {
if conf.SafeBrowsing.APIKey == "" {
return
}
debug.Println("safebrowsing support enabled, initializing")
// Validate the part of the config that we can.
if conf.SafeBrowsing.UpdatePeriod < 30*time.Minute {
// Minimum 30m.
conf.SafeBrowsing.UpdatePeriod = 30 * time.Minute
}
if conf.SafeBrowsing.UpdatePeriod > 168*time.Hour {
// Maximum 7 days.
conf.SafeBrowsing.UpdatePeriod = 168 * time.Minute
}
var err error
safeBrowser, err = safebrowsing.NewSafeBrowser(safebrowsing.Config{
APIKey: conf.SafeBrowsing.APIKey,
DBPath: conf.SafeBrowsing.DBPath,
UpdatePeriod: conf.SafeBrowsing.UpdatePeriod,
RequestTimeout: 15 * time.Second,
Logger: os.Stdout,
})
if err != nil {
debug.Fatalf("error initializing google safebrowsing: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
if err = safeBrowser.WaitUntilReady(ctx); err != nil {
debug.Fatalf("error initializing google safebrowsing: %v", err)
}
}
func safeTypeToString(t safebrowsing.ThreatType) string {
switch t {
case safebrowsing.ThreatType_Malware:
return "Site is known for hosting malware"
case safebrowsing.ThreatType_PotentiallyHarmfulApplication:
return "Site provides potentially harmful applications"
case safebrowsing.ThreatType_SocialEngineering:
return "Site is known for social engineering"
case safebrowsing.ThreatType_UnwantedSoftware:
return "Site provides unwanted software"
}
return "Unknown threat"
}
func safeTypeToStringFilter(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
input := in.Integer()
t := safebrowsing.ThreatType(input)
return pongo2.AsValue(safeTypeToString(t)), nil
}