Commit 9c017cba authored by inphi's avatar inphi

add proxyd ip to xff

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