-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathclassify.go
266 lines (225 loc) · 6.83 KB
/
classify.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
package main
import (
"bytes"
"encoding/json"
"image"
"io"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"github.com/andybalholm/dhash"
"github.com/klauspost/compress/gzip"
"golang.org/x/net/html"
"golang.org/x/net/html/charset"
)
type classificationResponse struct {
URL string `json:"url,omitempty"`
Text string `json:"text,omitempty"`
Categories map[string]int `json:"categories,omitempty"`
Rules map[string]int `json:"rules,omitempty"`
Error string `json:"error,omitempty"`
LogLine []string `json:"logLine,omitempty"`
ScoreAnalysis map[string]map[string]ruleScore `json:"scoreAnalysis,omitempty"`
}
// handleClassification responds to an HTTP request with a url parameter, and
// responds with a JSON object describing how the page would be classified.
func handleClassification(w http.ResponseWriter, r *http.Request) {
conf := getConfig()
var result classificationResponse
url := r.FormValue("url")
result.URL = url
if url == "" {
http.Error(w, "The URL to classify must be supplied as an HTTP form parameter named 'url'.", 400)
return
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
result.Error = err.Error()
ServeJSON(w, r, result)
log.Printf("Classifier: error creating request for %s: %v", url, err)
return
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_7_10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.6045.53 Safari/537.36")
resp, err := http.DefaultClient.Do(req)
if err != nil {
result.Error = err.Error()
ServeJSON(w, r, result)
log.Printf("Classifier: error fetching %s: %v", url, err)
return
}
defer resp.Body.Close()
// If the http Client followed redirects, use the final URL, not the one initially specified.
req = resp.Request
result.URL = req.URL.String()
if resp.StatusCode != 200 {
result.Error = resp.Status
ServeJSON(w, r, result)
log.Printf("Classifier: bad HTTP status fetching %s: %s", result.URL, resp.Status)
return
}
reqACLs := conf.ACLs.requestACLs(req, "")
respACLs := conf.ACLs.responseACLs(resp)
acls := unionACLSets(reqACLs, respACLs)
tally := conf.URLRules.MatchingRules(req.URL)
scores := conf.categoryScores(tally)
content, err := ioutil.ReadAll(&io.LimitedReader{
R: resp.Body,
N: int64(conf.MaxContentScanSize),
})
if err != nil {
result.Error = err.Error()
ServeJSON(w, r, result)
log.Printf("Classifier: error reading response body from %s: %v", result.URL, err)
return
}
thisRule, _ := conf.ChooseACLCategoryAction(acls, scores, conf.Threshold, "allow", "block", "block-invisible", "hash-image", "phrase-scan")
modified := false
scoresNeedUpdate := false
switch thisRule.Action {
case "phrase-scan":
contentType := resp.Header.Get("Content-Type")
_, cs, _ := charset.DetermineEncoding(content, contentType)
var doc *html.Node
if strings.Contains(contentType, "html") {
modified = conf.pruneContent(req.URL, &content, cs, &doc)
}
conf.scanContent(content, contentType, cs, tally)
scoresNeedUpdate = true
case "hash-image":
img, _, err := image.Decode(bytes.NewReader(content))
if err != nil {
result.Error = resp.Status
ServeJSON(w, r, result)
log.Printf("Classifier: error decoding image from %v: %v", req.URL, err)
return
}
hash := dhash.New(img)
for _, h := range conf.ImageHashes {
distance := dhash.Distance(hash, h.Hash)
if distance <= h.Threshold || h.Threshold == -1 && distance <= conf.DhashThreshold {
tally[simpleRule{imageHash, h.String()}]++
scoresNeedUpdate = true
}
}
}
if scoresNeedUpdate {
scores = conf.categoryScores(tally)
}
for _, c := range conf.ClassifierIgnoredCategories {
delete(scores, c)
}
for k, v := range scores {
if v < conf.Threshold || conf.Categories[k].action == ACL {
delete(scores, k)
}
}
result.Categories = scores
logLine := logAccess(req, resp, int64(len(content)), modified, "", tally, scores, ACLActionRule{Action: "classify"}, "", nil, nil, nil)
switch r.URL.Path {
case "/classify/verbose":
result.LogLine = logLine
case "/classify/analyze-score":
sa := make(map[string]map[string]ruleScore)
for _, c := range conf.Categories {
rs := make(map[string]ruleScore)
c.score(tally, conf, rs)
if len(rs) > 0 {
sa[c.name] = rs
}
}
result.ScoreAnalysis = sa
}
ServeJSON(w, r, result)
}
// handleClassifyText is like handleClassify, but it takes the text to be
// classified from the "text" parameter instead of fetching a URL.
func handleClassifyText(w http.ResponseWriter, r *http.Request) {
conf := getConfig()
var result classificationResponse
text := r.FormValue("text")
result.Text = text
if text == "" {
http.Error(w, "The text to classify must be supplied as an HTTP form parameter named 'text'.", 400)
return
}
tally := make(map[rule]int)
conf.scanContent([]byte(text), "text/plain", "utf-8", tally)
scores := conf.categoryScores(tally)
for _, c := range conf.ClassifierIgnoredCategories {
delete(scores, c)
}
result.Categories = scores
if r.URL.Path == "/classify-text/verbose" {
result.Rules = make(map[string]int)
for r, n := range tally {
result.Rules[r.String()] = n
}
}
ServeJSON(w, r, result)
}
// ServeJSON converts v to JSON and sends it on w.
func ServeJSON(w http.ResponseWriter, r *http.Request, v interface{}) {
data, err := json.Marshal(v)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
if len(data) > 1000 && strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
gzw := gzip.NewWriter(w)
defer gzw.Close()
gzw.Write(data)
} else {
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
w.Write(data)
}
}
func parseTally(input string) (tally map[rule]int, rest string, err error) {
tally = make(map[rule]int)
rest = input
for {
var r rule
var n int
r, rest, err = parseCompoundRule(rest)
if err != nil {
return nil, rest, err
}
n, rest, err = integer(rest)
if err != nil {
return nil, rest, err
}
tally[r] = n
_, rest, err = tag(",")(rest)
if err != nil {
break
}
}
return tally, rest, nil
}
func handleAnalyzeTally(w http.ResponseWriter, r *http.Request) {
var result classificationResponse
tally, rest, err := parseTally(r.FormValue("tally"))
if rest != "" || err != nil {
result.Error = "invalid tally syntax"
ServeJSON(w, r, result)
return
}
result.Rules = make(map[string]int)
for r, n := range tally {
result.Rules[r.String()] = n
}
conf := getConfig()
sa := make(map[string]map[string]ruleScore)
for _, c := range conf.Categories {
rs := make(map[string]ruleScore)
c.score(tally, conf, rs)
if len(rs) > 0 {
sa[c.name] = rs
}
}
result.ScoreAnalysis = sa
ServeJSON(w, r, result)
}