Commit 9c017cba authored by inphi's avatar inphi

add proxyd ip to xff

parent 16e27bdf
......@@ -87,6 +87,7 @@ type Backend struct {
maxWSConns int
outOfServiceInterval time.Duration
stripTrailingXFF bool
proxydIP string
}
type BackendOpt func(b *Backend)
......@@ -149,6 +150,12 @@ func WithStrippedTrailingXFF() BackendOpt {
}
}
func WithProxydIP(ip string) BackendOpt {
return func(b *Backend) {
b.proxydIP = ip
}
}
func NewBackend(
name string,
rpcURL string,
......@@ -172,6 +179,10 @@ func NewBackend(
opt(backend)
}
if !backend.stripTrailingXFF && backend.proxydIP == "" {
log.Warn("proxied requests' XFF header will not contain the proxyd ip address")
}
return backend
}
......@@ -327,10 +338,12 @@ func (b *Backend) doForward(ctx context.Context, rpcReq *RPCReq) (*RPCRes, error
xForwardedFor := GetXForwardedFor(ctx)
if b.stripTrailingXFF {
ipList := strings.Split(xForwardedFor, ",")
ipList := strings.Split(xForwardedFor, ", ")
if len(ipList) > 0 {
xForwardedFor = ipList[0]
}
} else if b.proxydIP != "" {
xForwardedFor = fmt.Sprintf("%s, %s", xForwardedFor, b.proxydIP)
}
httpReq.Header.Set("content-type", "application/json")
......
......@@ -99,6 +99,7 @@ func Start(config *Config) error {
if cfg.StripTrailingXFF {
opts = append(opts, WithStrippedTrailingXFF())
}
opts = append(opts, WithProxydIP(os.Getenv("PROXYD_IP")))
back := NewBackend(name, rpcURL, wsURL, lim, opts...)
backendNames = append(backendNames, name)
backendsByName[name] = back
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment