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
|
2022-03-05 02:51:17 +00:00
|
|
|
port string
|
2022-01-15 16:44:00 +00:00
|
|
|
path string
|
2022-01-11 17:15:45 +00:00
|
|
|
version string
|
2022-01-07 17:41:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 02:51:17 +00:00
|
|
|
func ParseUrl(raw []byte) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-06 08:44:11 +00:00
|
|
|
func NewHttpPacket(raw []byte) (HttpPacket, error){
|
|
|
|
method, domain, port, path, version, err := parse(raw)
|
|
|
|
if err != nil {
|
|
|
|
return HttpPacket{}, err
|
|
|
|
}
|
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,
|
2022-03-05 02:51:17 +00:00
|
|
|
port: port,
|
2022-01-15 16:44:00 +00:00
|
|
|
path: path,
|
2022-01-11 17:15:45 +00:00
|
|
|
version: version,
|
2022-03-06 08:44:11 +00:00
|
|
|
}, nil
|
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
|
|
|
|
}
|
2022-01-15 16:44:00 +00:00
|
|
|
|
2022-01-11 17:15:45 +00:00
|
|
|
func (p *HttpPacket) Domain() string {
|
|
|
|
return p.domain
|
|
|
|
}
|
2022-01-15 16:44:00 +00:00
|
|
|
|
2022-03-05 02:51:17 +00:00
|
|
|
func (p *HttpPacket) Port() string {
|
|
|
|
return p.port
|
|
|
|
}
|
|
|
|
|
2022-01-11 17:15:45 +00:00
|
|
|
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-15 16:44:00 +00:00
|
|
|
func (p *HttpPacket) Tidy() {
|
2022-01-14 21:40:57 +00:00
|
|
|
s := string(p.raw)
|
|
|
|
|
2022-03-04 23:49:28 +00:00
|
|
|
lines := strings.Split(s, "\r\n")
|
2022-01-15 16:44:00 +00:00
|
|
|
|
|
|
|
lines[0] = p.method + " " + p.path + " " + p.version
|
|
|
|
|
2022-01-14 21:40:57 +00:00
|
|
|
for i := 0; i < len(lines); i++ {
|
|
|
|
if strings.HasPrefix(lines[i], "Proxy-Connection") {
|
|
|
|
lines[i] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result := ""
|
2022-03-04 23:49:28 +00:00
|
|
|
|
2022-01-14 21:40:57 +00:00
|
|
|
for i := 0; i < len(lines); i++ {
|
|
|
|
if lines[i] == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-03-04 23:49:28 +00:00
|
|
|
result += lines[i] + "\r\n"
|
2022-01-14 21:40:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 23:49:28 +00:00
|
|
|
result += "\r\n"
|
|
|
|
|
2022-01-14 21:40:57 +00:00
|
|
|
p.raw = []byte(result)
|
|
|
|
}
|
|
|
|
|
2022-03-06 08:44:11 +00:00
|
|
|
func parse(raw []byte) (string, string, string, string, string, error) {
|
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++ {
|
2022-03-06 08:44:11 +00:00
|
|
|
if (raw)[i] == '\r' {
|
2022-01-10 19:27:12 +00:00
|
|
|
firstLine = string((raw)[:i])
|
2022-01-15 16:44:00 +00:00
|
|
|
break
|
2022-01-07 17:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 18:34:26 +00:00
|
|
|
tokens := strings.Split(firstLine, " ")
|
|
|
|
|
2022-03-06 08:44:11 +00:00
|
|
|
method := tokens[0]
|
|
|
|
url := tokens[1]
|
|
|
|
version := tokens[2]
|
2022-01-07 17:41:02 +00:00
|
|
|
|
2022-01-15 16:44:00 +00:00
|
|
|
url = strings.Replace(url, "http://", "", 1)
|
|
|
|
url = strings.Replace(url, "https://", "", 1)
|
|
|
|
|
2022-03-06 08:44:11 +00:00
|
|
|
domain := ""
|
|
|
|
port := ""
|
2022-01-15 16:44:00 +00:00
|
|
|
for i := 0; i < len(url); i++ {
|
|
|
|
if url[i] == ':' {
|
|
|
|
domain = url[:i]
|
2022-03-05 02:51:17 +00:00
|
|
|
port = url[i:]
|
2022-01-15 16:44:00 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if url[i] == '/' {
|
|
|
|
domain = url[:i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
path := "/"
|
|
|
|
for i := 0; i < len(url); i++ {
|
|
|
|
if url[i] == '/' {
|
|
|
|
path = url[i:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 17:41:02 +00:00
|
|
|
|
2022-03-06 08:44:11 +00:00
|
|
|
return method, domain, port, path, version, nil
|
2022-01-07 17:41:02 +00:00
|
|
|
}
|