fix: restore x-forwarded headers
This commit is contained in:
parent
254effa8e5
commit
f3ec9005f7
3 changed files with 81 additions and 8 deletions
80
forwarding.go
Normal file
80
forwarding.go
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
package traefik_umami_feeder
|
||||
|
||||
// Copied source from
|
||||
// https://github.com/kzmake/traefik-plugin-forward-request/blob/master/util.go
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
xForwardedProto = "x-forwarded-proto"
|
||||
xForwardedFor = "x-forwarded-for"
|
||||
xForwardedHost = "x-forwarded-host"
|
||||
xForwardedPort = "x-forwarded-port"
|
||||
xForwardedURI = "x-forwarded-uri"
|
||||
xForwardedMethod = "x-forwarded-method"
|
||||
)
|
||||
|
||||
func copyHeaders(dst, src http.Header, headersToCopy []string) {
|
||||
for _, key := range headersToCopy {
|
||||
if values := src.Values(key); len(values) > 0 {
|
||||
dst[key] = values
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func writeXForwardedHeaders(dst http.Header, req *http.Request) {
|
||||
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
|
||||
if values := req.Header.Values(xForwardedFor); len(values) > 0 {
|
||||
clientIP = strings.Join(values, ", ") + ", " + clientIP
|
||||
}
|
||||
dst.Set(xForwardedFor, clientIP)
|
||||
}
|
||||
|
||||
xfm := req.Header.Get(xForwardedMethod)
|
||||
switch {
|
||||
case xfm != "":
|
||||
dst.Set(xForwardedMethod, xfm)
|
||||
case req.Method != "":
|
||||
dst.Set(xForwardedMethod, req.Method)
|
||||
default:
|
||||
dst.Del(xForwardedMethod)
|
||||
}
|
||||
|
||||
xfp := req.Header.Get(xForwardedProto)
|
||||
switch {
|
||||
case xfp != "":
|
||||
dst.Set(xForwardedProto, xfp)
|
||||
case req.TLS != nil:
|
||||
dst.Set(xForwardedProto, "https")
|
||||
default:
|
||||
dst.Set(xForwardedProto, "http")
|
||||
}
|
||||
|
||||
if xfp := req.Header.Get(xForwardedPort); xfp != "" {
|
||||
dst.Set(xForwardedPort, xfp)
|
||||
}
|
||||
|
||||
xfh := req.Header.Get(xForwardedHost)
|
||||
switch {
|
||||
case xfh != "":
|
||||
dst.Set(xForwardedHost, xfh)
|
||||
case req.Host != "":
|
||||
dst.Set(xForwardedHost, req.Host)
|
||||
default:
|
||||
dst.Del(xForwardedHost)
|
||||
}
|
||||
|
||||
xfu := req.Header.Get(xForwardedURI)
|
||||
switch {
|
||||
case xfu != "":
|
||||
dst.Set(xForwardedURI, xfu)
|
||||
case req.URL.RequestURI() != "":
|
||||
dst.Set(xForwardedURI, req.URL.RequestURI())
|
||||
default:
|
||||
dst.Del(xForwardedURI)
|
||||
}
|
||||
}
|
||||
|
|
@ -98,11 +98,3 @@ func parseAcceptLanguage(acceptLanguage string) string {
|
|||
}
|
||||
return matches[0][1]
|
||||
}
|
||||
|
||||
func copyHeaders(dst, src http.Header, headersToCopy []string) {
|
||||
for _, key := range headersToCopy {
|
||||
if values := src.Values(key); len(values) > 0 {
|
||||
dst[key] = values
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,6 +59,7 @@ func (h *UmamiFeeder) submitToFeed(req *http.Request) {
|
|||
|
||||
var headers = make(http.Header)
|
||||
copyHeaders(headers, req.Header, headersToCopy)
|
||||
writeXForwardedHeaders(headers, req)
|
||||
|
||||
payload := &UmamiPayload{body: body, headers: headers}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue