-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathguess.go
More file actions
35 lines (31 loc) · 781 Bytes
/
guess.go
File metadata and controls
35 lines (31 loc) · 781 Bytes
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
package main
import (
"bytes"
)
var httpMethods = map[string]bool{
"GET": true, "POST": true, "PUT": true, "DELETE": true, "HEAD": true,
"TRACE": true, "OPTIONS": true, "PATCH": true,
}
// guessProtocol 根据TCP数据判断其协议类型
func guessProtocol(payload []byte) (protocol string) {
if isHttpRequestData(payload) {
return HttpType
}
return UnknownType
}
// isHttpRequestData 判断是否符合HTTP/1.x
func isHttpRequestData(payload []byte) bool {
// see https://stackoverflow.com/questions/25047905/http-request-minimum-size-in-bytes
if len(payload) < 26 {
return false
}
idx := bytes.IndexByte(payload, byte(' '))
if idx < 0 {
return false
}
method := string(payload[:idx])
if ok := httpMethods[method]; !ok {
return false
}
return true
}