update packet parsing

This commit is contained in:
xvzc 2022-05-09 10:57:34 +09:00
parent 9f742582c5
commit 729008083e
2 changed files with 26 additions and 48 deletions

View File

@ -108,7 +108,7 @@ func (lConn *Conn) HandleHttp(p *packet.HttpPacket) {
// Create connection to server // Create connection to server
var port = ":80" var port = ":80"
if p.Port() != "" { if p.Port() != "" {
port = p.Port() port = ":" + p.Port()
} }
rConn, err := Dial("tcp", ip + port) rConn, err := Dial("tcp", ip + port)
@ -154,7 +154,7 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) {
// Create a connection to the requested server // Create a connection to the requested server
var port = ":443" var port = ":443"
if p.Port() != "" { if p.Port() != "" {
port = p.Port() port = ":" + p.Port()
} }
rConn, err := Dial("tcp", ip + port) rConn, err := Dial("tcp", ip + port)

View File

@ -1,7 +1,9 @@
package packet package packet
import ( import (
"errors" "bufio"
"net"
"net/http"
"strings" "strings"
) )
@ -122,58 +124,34 @@ func (p *HttpPacket) Tidy() {
} }
func (p *HttpPacket )parse() error { func (p *HttpPacket )parse() error {
var firstLine string reader := bufio.NewReader(strings.NewReader(string(p.raw)))
for i := 0; i < len(p.raw); i++ { request, err := http.ReadRequest(reader)
if (p.raw)[i] == '\r' { if err != nil {
firstLine = string(p.raw[:i]) return err
break
}
} }
tokens := strings.Split(firstLine, " ") p.domain, p.port, err = net.SplitHostPort(request.Host)
if err != nil {
if (len(tokens) < 3) { p.domain = request.Host
return errors.New("Error parsing http request") p.port = ""
} }
p.method = tokens[0] p.method = request.Method
url := tokens[1] p.version = request.Proto
p.version = tokens[2] p.path = request.URL.Path
if strings.HasPrefix(url, "http://") { if request.URL.RawQuery != "" {
url = strings.Replace(url, "http://", "", 1) p.path += "?" + request.URL.RawQuery
} }
if strings.HasPrefix(url, "https://") { if request.URL.RawFragment != "" {
url = strings.Replace(url, "https://", "", 1) p.path += "#" + request.URL.RawFragment
}
if p.path == "" {
p.path = "/"
} }
domain := "" request.Body.Close()
port := ""
for i := 0; i < len(url); i++ {
if url[i] == ':' {
domain = url[:i]
port = url[i:]
break
}
if url[i] == '/' {
domain = url[:i]
break
}
}
p.domain = domain
p.port = port
path := "/"
for i := 0; i < len(url); i++ {
if url[i] == '/' {
path = url[i:]
break
}
}
p.path = path
return nil return nil
} }