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

View File

@ -59,17 +59,15 @@ func (pxy *Proxy) Start() {
} }
go func() { go func() {
tmpBuf := make([]byte, pxy.bufferSize) pkt, err := packet.NewHttpPacketFromReader(conn)
b, err := ReadBytes(conn.(*net.TCPConn), tmpBuf)
if err != nil { if err != nil {
return 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 { if err != nil {
log.Debug("[PROXY] Error while parsing request: ", string(b)) log.Debug("[PROXY] Error while parsing request: ", string(pkt.Raw()))
conn.Close() conn.Close()
return return
} }