Parse request from connection instead of buffer

This commit is contained in:
Andrey Semenov 2024-08-09 03:07:29 +05:00 committed by LiquidTheDangerous
parent dbee4964f8
commit 173b5da2a5
2 changed files with 15 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package packet
import (
"bufio"
"io"
"net"
"net/http"
"strings"
@ -56,12 +57,16 @@ func ParseUrl(raw []byte) {
}
func NewHttpPacket(raw []byte) (*HttpPacket, error) {
pkt := &HttpPacket{raw: raw}
pkt.parse()
return pkt, nil
func NewHttpPacketFromReader(rdr io.Reader) (*HttpPacket, error) {
sb := strings.Builder{}
tee := io.TeeReader(rdr, &sb)
p := &HttpPacket{}
err := parse(p, bufio.NewReader(tee))
if err != nil {
return nil, err
}
p.raw = []byte(sb.String())
return p, nil
}
func (p *HttpPacket) Raw() []byte {
@ -123,8 +128,7 @@ func (p *HttpPacket) Tidy() {
p.raw = []byte(result)
}
func (p *HttpPacket) parse() error {
reader := bufio.NewReader(strings.NewReader(string(p.raw)))
func parse(p *HttpPacket, reader *bufio.Reader) error {
request, err := http.ReadRequest(reader)
if err != nil {
return err
@ -152,6 +156,5 @@ func (p *HttpPacket) parse() error {
}
request.Body.Close()
return nil
}

View File

@ -59,17 +59,15 @@ func (pxy *Proxy) Start() {
}
go func() {
tmpBuf := make([]byte, pxy.bufferSize)
b, err := ReadBytes(conn.(*net.TCPConn), tmpBuf)
pkt, err := packet.NewHttpPacketFromReader(conn)
if err != nil {
return
}
log.Debug("[PROXY] Request from ", conn.RemoteAddr(), "\n\n", string(b))
log.Debug("[PROXY] Request from ", conn.RemoteAddr(), "\n\n", string(pkt.Raw()))
pkt, err := packet.NewHttpPacket(b)
if err != nil {
log.Debug("[PROXY] Error while parsing request: ", string(b))
log.Debug("[PROXY] Error while parsing request: ", string(pkt.Raw()))
conn.Close()
return
}