SpoofDPI/packet/http.go

107 lines
2.0 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"
)
2022-01-10 19:27:12 +00:00
var validMethod = 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": {},
}
2022-01-08 15:09:01 +00:00
type HttpPacket struct {
2022-01-11 17:15:45 +00:00
raw []byte
method string
domain string
version string
2022-01-07 17:41:02 +00:00
}
2022-01-10 19:27:12 +00:00
func NewHttpPacket(raw []byte) HttpPacket {
2022-01-07 18:34:26 +00:00
method, domain, version := parse(raw)
2022-01-11 17:15:45 +00:00
2022-01-08 15:09:01 +00:00
return HttpPacket{
2022-01-11 17:15:45 +00:00
raw: raw,
method: method,
domain: domain,
version: version,
2022-01-07 18:01:54 +00:00
}
}
2022-01-11 17:15:45 +00:00
func (p *HttpPacket) Raw() []byte {
return p.raw
}
func (p *HttpPacket) Method() string {
return p.method
}
func (p *HttpPacket) Domain() string {
return p.domain
}
func (p *HttpPacket) Version() string {
return p.version
}
func (p *HttpPacket) IsValidMethod() bool {
if _, exists := validMethod[p.Method()]; exists {
2022-01-07 17:41:02 +00:00
return true
}
return false
}
2022-01-11 17:15:45 +00:00
func (p *HttpPacket) IsConnectMethod() bool {
return p.Method() == "CONNECT"
2022-01-07 17:41:02 +00:00
}
2022-01-10 19:27:12 +00:00
func parse(raw []byte) (string, string, string) {
2022-01-07 18:34:26 +00:00
var firstLine string
2022-01-10 19:27:12 +00:00
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
}