Skip to content

Commit

Permalink
fix: post body
Browse files Browse the repository at this point in the history
  • Loading branch information
hax0r31337 committed Sep 9, 2024
1 parent c892ff3 commit 886eb7c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 27 deletions.
44 changes: 39 additions & 5 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package http
import (
"bytes"
"crypto/rand"
"io"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -38,6 +39,15 @@ func (h *HttpPayload) getHttp1Payload() []byte {

r := h.NumRequests

var body []byte
if h.Request.Body != nil {
var err error
body, err = getRequestBodyBytes(h.Request)
if err != nil {
log.Fatal(err)
}
}

for r > 0 {
if r == 1 {
h.Request.Header.Set("Connection", "close")
Expand All @@ -49,6 +59,10 @@ func (h *HttpPayload) getHttp1Payload() []byte {
log.Fatal(err)
}

if body != nil {
h.Request.Body = io.NopCloser(bytes.NewReader(body))
}

r--
}

Expand All @@ -72,19 +86,24 @@ func (h *HttpPayload) getHttp2Payload() []byte {
}

func (h *HttpPayload) GetPayload(alpn string) []byte {
h.payloadLock.Lock()
defer h.payloadLock.Unlock()

switch alpn {
case ALPN_HTTP1:
if h.cachedHttp1Payload == nil {
h.cachedHttp1Payload = h.getHttp1Payload()
h.payloadLock.Lock()
if h.cachedHttp1Payload == nil {
h.cachedHttp1Payload = h.getHttp1Payload()
}
h.payloadLock.Unlock()
}

return h.cachedHttp1Payload
case ALPN_HTTP2:
if h.cachedHttp2Payload == nil {
h.cachedHttp2Payload = h.getHttp2Payload()
h.payloadLock.Lock()
if h.cachedHttp2Payload == nil {
h.cachedHttp2Payload = h.getHttp2Payload()
}
h.payloadLock.Unlock()
}

return h.cachedHttp2Payload
Expand Down Expand Up @@ -125,3 +144,18 @@ func ProbeRedirects(req *http.Request) error {

return nil
}

func getRequestBodyBytes(req *http.Request) ([]byte, error) {
if req.Body == nil {
return nil, nil
}

body, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}

req.Body = io.NopCloser(bytes.NewReader(body))

return body, nil
}
39 changes: 19 additions & 20 deletions http/http2.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,19 @@ func writeRequestToFramer(framer *http2.Framer, req *http.Request, requests uint

endStream := req.Body == nil || req.Body == http.NoBody

var body []byte
if !endStream {
body, err = getRequestBodyBytes(req)
if err != nil {
return err
}
}

for i := range requests {
writeHeadersToFramer(framer, i*2+1, endStream, headers)

if !endStream {
writeBodyToFramer(framer, i*2+1, req)
writeBodyToFramer(framer, i*2+1, body)
}
}

Expand Down Expand Up @@ -80,28 +88,19 @@ func writeHeadersToFramer(framer *http2.Framer, streamId uint32, endStream bool,
}
}

func writeBodyToFramer(framer *http2.Framer, streamId uint32, req *http.Request) {
hasEndStream := false
defer func() {
if !hasEndStream {
framer.WriteData(streamId, true, nil)
}
}()
func writeBodyToFramer(framer *http2.Framer, streamId uint32, body []byte) {
var buf []byte

buf := make([]byte, MAX_FRAME_SIZE)

for {
n, err := req.Body.Read(buf)
if err != nil {
break
for len(body) > 0 {
if len(body) > MAX_FRAME_SIZE {
buf = body[:MAX_FRAME_SIZE]
body = body[MAX_FRAME_SIZE:]
} else {
buf = body
body = nil
}

endStream := n != MAX_FRAME_SIZE
framer.WriteData(streamId, endStream, buf[:n])
if endStream {
hasEndStream = true
break
}
framer.WriteData(streamId, len(body) == 0, buf)
}
}

Expand Down
5 changes: 3 additions & 2 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ func main() {
dialLock.Lock()
sourcePort++
addr := addrs[uint16(sourcePort)%uint16(len(addrs))]
sp := sourcePort
dialLock.Unlock()

if tlsConfig != nil {
h := egressguy.NewReliableReaderHandler()
Expand All @@ -206,8 +208,7 @@ func main() {
handler = h
}

conn, err := egressguy.NewTcpConn(eg, src, addr, sourcePort, port, handler)
dialLock.Unlock()
conn, err := egressguy.NewTcpConn(eg, src, addr, sp, port, handler)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 886eb7c

Please sign in to comment.