SpoofDPI/packet/http.go

95 lines
1.8 KiB
Go
Raw Normal View History

2022-01-08 06:35:32 +00:00
package packet
2022-01-07 17:41:02 +00:00
import (
"strings"
)
type Http struct {
2022-01-07 18:34:26 +00:00
Raw *[]byte
Method string
Domain string
Version string
2022-01-07 17:41:02 +00:00
}
func NewHttp(raw *[]byte) Http {
2022-01-07 18:34:26 +00:00
method, domain, version := parse(raw)
return Http{
2022-01-07 18:34:26 +00:00
Raw: raw,
Method: method,
Domain: domain,
Version: version,
2022-01-07 18:01:54 +00:00
}
}
func (r *Http) IsValidMethod() bool {
2022-01-07 17:41:02 +00:00
if _, exists := getValidMethods()[r.Method]; exists {
return true
}
return false
}
func (r *Http) IsConnectMethod() bool {
2022-01-08 06:35:32 +00:00
return r.Method == "CONNECT"
2022-01-07 17:41:02 +00:00
}
2022-01-07 18:34:26 +00:00
func parse(raw *[]byte) (string, string, string) {
var firstLine string
for i := 0; i < len(*raw); i++ {
if (*raw)[i] == '\n' {
firstLine = string((*raw)[:i])
2022-01-07 17:41:02 +00:00
}
}
2022-01-07 18:34:26 +00:00
tokens := strings.Split(firstLine, " ")
method := strings.TrimSpace(tokens[0])
domain := strings.TrimSpace(tokens[1])
version := strings.TrimSpace(tokens[2])
2022-01-07 17:41:02 +00:00
domain = strings.Replace(domain, "http://", "", 1)
domain = strings.Replace(domain, "https://", "", 1)
domain = strings.Split(domain, ":")[0]
domain = strings.Split(domain, "/")[0]
2022-01-07 18:34:26 +00:00
return method, domain, version
2022-01-07 17:41:02 +00:00
}
func getValidMethods() map[string]struct{} {
return map[string]struct{}{
"DELETE": {},
"GET": {},
"HEAD": {},
"POST": {},
"PUT": {},
"CONNECT": {},
"OPTIONS": {},
"TRACE": {},
"COPY": {},
"LOCK": {},
"MKCOL": {},
"MOVE": {},
"PROPFIND": {},
"PROPPATCH": {},
"SEARCH": {},
"UNLOCK": {},
"BIND": {},
"REBIND": {},
"UNBIND": {},
"ACL": {},
"REPORT": {},
"MKACTIVITY": {},
"CHECKOUT": {},
"MERGE": {},
"M-SEARCH": {},
"NOTIFY": {},
"SUBSCRIBE": {},
"UNSUBSCRIBE": {},
"PATCH": {},
"PURGE": {},
"MKCALENDAR": {},
"LINK": {},
"UNLINK": {},
}
}