Skip to content

Commit

Permalink
Copy more attributes from the request
Browse files Browse the repository at this point in the history
  • Loading branch information
reakaleek committed Nov 22, 2024
1 parent a0f5ec3 commit 544293e
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"bytes"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"strings"
)

func handler(targets []string, w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -39,6 +41,7 @@ func forwardRequest(target string, r *http.Request, ch chan *http.Response) {
}
// Append the original request's path to the target URL
url.Path = r.URL.Path
url.RawQuery = r.URL.RawQuery // Include the query string

// Create a new request with the same method and URL
req, err := http.NewRequest(r.Method, url.String(), bytes.NewBuffer(bodyBytes)) // Use the copied body here
Expand All @@ -48,6 +51,17 @@ func forwardRequest(target string, r *http.Request, ch chan *http.Response) {
return
}

// Copy the Host header
req.Host = r.Host

// Copy the X-Forwarded-For header (if it exists) and append the client's IP
if clientIP, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
if prior, ok := r.Header["X-Forwarded-For"]; ok {
clientIP = strings.Join(prior, ", ") + ", " + clientIP
}
req.Header.Set("X-Forwarded-For", clientIP)
}

// Copy the headers from the original request
for key, values := range r.Header {
for _, value := range values {
Expand Down

0 comments on commit 544293e

Please sign in to comment.