From 7175c46a00e9f0e6e2a8d5176adfd3bab83f62d2 Mon Sep 17 00:00:00 2001 From: Danyal Prout Date: Sat, 11 Nov 2023 03:52:09 -0600 Subject: [PATCH] feat(proxyd): ability to add additional headers to backend requests (#8134) --- proxyd/proxyd/backend.go | 11 +++++++++++ proxyd/proxyd/config.go | 23 ++++++++++++----------- proxyd/proxyd/proxyd.go | 12 ++++++++++++ 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/proxyd/proxyd/backend.go b/proxyd/proxyd/backend.go index 6c699f0..87ba52e 100644 --- a/proxyd/proxyd/backend.go +++ b/proxyd/proxyd/backend.go @@ -139,6 +139,7 @@ type Backend struct { wsURL string authUsername string authPassword string + headers map[string]string client *LimitedHTTPClient dialer *websocket.Dialer maxRetries int @@ -170,6 +171,12 @@ func WithBasicAuth(username, password string) BackendOpt { } } +func WithHeaders(headers map[string]string) BackendOpt { + return func(b *Backend) { + b.headers = headers + } +} + func WithTimeout(timeout time.Duration) BackendOpt { return func(b *Backend) { b.client.Timeout = timeout @@ -535,6 +542,10 @@ func (b *Backend) doForward(ctx context.Context, rpcReqs []*RPCReq, isBatch bool httpReq.Header.Set("content-type", "application/json") httpReq.Header.Set("X-Forwarded-For", xForwardedFor) + for name, value := range b.headers { + httpReq.Header.Set(name, value) + } + start := time.Now() httpRes, err := b.client.DoLimited(httpReq) if err != nil { diff --git a/proxyd/proxyd/config.go b/proxyd/proxyd/config.go index 63f557c..df75a7d 100644 --- a/proxyd/proxyd/config.go +++ b/proxyd/proxyd/config.go @@ -83,17 +83,18 @@ type BackendOptions struct { } type BackendConfig struct { - Username string `toml:"username"` - Password string `toml:"password"` - RPCURL string `toml:"rpc_url"` - WSURL string `toml:"ws_url"` - WSPort int `toml:"ws_port"` - MaxRPS int `toml:"max_rps"` - MaxWSConns int `toml:"max_ws_conns"` - CAFile string `toml:"ca_file"` - ClientCertFile string `toml:"client_cert_file"` - ClientKeyFile string `toml:"client_key_file"` - StripTrailingXFF bool `toml:"strip_trailing_xff"` + Username string `toml:"username"` + Password string `toml:"password"` + RPCURL string `toml:"rpc_url"` + WSURL string `toml:"ws_url"` + WSPort int `toml:"ws_port"` + MaxRPS int `toml:"max_rps"` + MaxWSConns int `toml:"max_ws_conns"` + CAFile string `toml:"ca_file"` + ClientCertFile string `toml:"client_cert_file"` + ClientKeyFile string `toml:"client_key_file"` + StripTrailingXFF bool `toml:"strip_trailing_xff"` + Headers map[string]string `toml:"headers"` ConsensusSkipPeerCountCheck bool `toml:"consensus_skip_peer_count"` ConsensusForcedCandidate bool `toml:"consensus_forced_candidate"` diff --git a/proxyd/proxyd/proxyd.go b/proxyd/proxyd/proxyd.go index 84051ab..fe6aae1 100644 --- a/proxyd/proxyd/proxyd.go +++ b/proxyd/proxyd/proxyd.go @@ -130,6 +130,18 @@ func Start(config *Config) (*Server, func(), error) { } opts = append(opts, WithBasicAuth(cfg.Username, passwordVal)) } + + headers := map[string]string{} + for headerName, headerValue := range cfg.Headers { + headerValue, err := ReadFromEnvOrConfig(headerValue) + if err != nil { + return nil, nil, err + } + + headers[headerName] = headerValue + } + opts = append(opts, WithHeaders(headers)) + tlsConfig, err := configureBackendTLS(cfg) if err != nil { return nil, nil, err