Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(proxyd): ability to add additional headers to backend requests #8134

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions proxyd/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type Backend struct {
wsURL string
authUsername string
authPassword string
headers map[string]string
client *LimitedHTTPClient
dialer *websocket.Dialer
maxRetries int
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 12 additions & 11 deletions proxyd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
12 changes: 12 additions & 0 deletions proxyd/proxyd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down