forked from mushorg/go-dpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
45 lines (38 loc) · 978 Bytes
/
http.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
package classifiers
import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/hamidrezabstn/go-dpi/types"
"regexp"
"strings"
)
// HTTPClassifier struct
type HTTPClassifier struct{}
var httpVerbs = []string{
"OPTIONS",
"GET",
"HEAD",
"POST",
"PUT",
"DELETE",
"TRACE",
"CONNECT",
}
var regex *regexp.Regexp
func init() {
var regexStr = "^(" + strings.Join(httpVerbs, "|") + ") [^\\s]+ " +
"HTTP/[12](.[01])?\r\n(.*\r\n)*\r\n"
// regex should match the first line of all HTTP requests
regex, _ = regexp.Compile(regexStr)
}
// HeuristicClassify for HTTPClassifier
func (classifier HTTPClassifier) HeuristicClassify(flow *types.Flow) bool {
return checkFlowLayer(flow, layers.LayerTypeTCP, func(layer gopacket.Layer) bool {
payload := layer.LayerPayload()
return regex.Match(payload)
})
}
// GetProtocol returns the corresponding protocol
func (classifier HTTPClassifier) GetProtocol() types.Protocol {
return types.HTTP
}