-
Notifications
You must be signed in to change notification settings - Fork 939
/
Copy pathclient.go
74 lines (59 loc) · 1.83 KB
/
client.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
package safebrowsing
import (
"emperror.dev/errors"
"encoding/json"
"github.com/google/safebrowsing"
"github.com/jonas747/yagpdb/common/backgroundworkers"
"io/ioutil"
"net/http"
"strings"
)
// CheckString checks a string against google safebrowsing for threats
// if the safebrowser is running on this process then it will perform the check instantly
// otherwise it will make a api request towards the safebrowsing proxy server (or return an error)
func CheckString(input string) (*safebrowsing.URLThreat, error) {
if SafeBrowser != nil {
return performLocalLookup(input)
}
return performRemoteLookup(input)
}
func performLocalLookup(input string) (*safebrowsing.URLThreat, error) {
logger.Debug("performing local lookup")
result, err := serverPerformLookup(input)
if err != nil {
return nil, err
}
return findThreatInResult(result), nil
}
func performRemoteLookup(input string) (*safebrowsing.URLThreat, error) {
logger.Debug("performing remote lookup")
bodyR := strings.NewReader(input)
req, err := http.NewRequest("POST", "http://"+backgroundworkers.HTTPAddr.GetString()+"/safebroswing/checkmessage", bodyR)
if err != nil {
return nil, errors.WithMessage(err, "NewRequest")
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.WithMessage(err, "httpclient.Do")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.WithMessage(err, "ioutil.ReadAll")
}
var result [][]safebrowsing.URLThreat
err = json.Unmarshal(body, &result)
if err != nil {
return nil, errors.WithMessage(err, "json.Unmarshal")
}
return findThreatInResult(result), nil
}
func findThreatInResult(result [][]safebrowsing.URLThreat) *safebrowsing.URLThreat {
for _, list := range result {
for _, threat := range list {
t := threat
return &t
}
}
return nil
}