Skip to content

Commit

Permalink
added option to set headers for connect request;
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanfinn committed Jul 29, 2024
1 parent 2a633b7 commit ee0f8de
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions cffi_src/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ func getTlsClient(requestInput RequestInput, sessionId string, withSession bool)
options = append(options, tls_client.WithDefaultHeaders(requestInput.DefaultHeaders))
}

if requestInput.ConnectHeaders != nil && len(requestInput.ConnectHeaders) != 0 {
options = append(options, tls_client.WithConnectHeaders(requestInput.ConnectHeaders))
}

if requestInput.ServerNameOverwrite != nil && *requestInput.ServerNameOverwrite != "" {
options = append(options, tls_client.WithServerNameOverwrite(*requestInput.ServerNameOverwrite))
}
Expand Down
1 change: 1 addition & 0 deletions cffi_src/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type RequestInput struct {
HeaderOrder []string `json:"headerOrder"`
Headers map[string]string `json:"headers"`
DefaultHeaders map[string][]string `json:"defaultHeaders"`
ConnectHeaders map[string][]string `json:"connectHeaders"`
InsecureSkipVerify bool `json:"insecureSkipVerify"`
IsByteRequest bool `json:"isByteRequest"`
IsByteResponse bool `json:"isByteResponse"`
Expand Down
5 changes: 3 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func NewHttpClient(logger Logger, options ...HttpClientOption) (HttpClient, erro
badPinHandler: nil,
customRedirectFunc: nil,
defaultHeaders: make(http.Header),
connectHeaders: make(http.Header),
clientProfile: profiles.DefaultClientProfile,
timeout: time.Duration(DefaultTimeoutSeconds) * time.Second,
}
Expand Down Expand Up @@ -122,7 +123,7 @@ func buildFromConfig(logger Logger, config *httpClientConfig) (*http.Client, ban
dialer = newDirectDialer(config.timeout, config.localAddr, config.dialer)

if config.proxyUrl != "" {
proxyDialer, err := newConnectDialer(config.proxyUrl, config.timeout, config.localAddr, config.dialer, logger)
proxyDialer, err := newConnectDialer(config.proxyUrl, config.timeout, config.localAddr, config.dialer, config.connectHeaders, logger)
if err != nil {
return nil, nil, profiles.ClientProfile{}, err
}
Expand Down Expand Up @@ -233,7 +234,7 @@ func (c *httpClient) applyProxy() error {

if c.config.proxyUrl != "" {
c.logger.Debug("proxy url %s supplied - using proxy connect dialer", c.config.proxyUrl)
proxyDialer, err := newConnectDialer(c.config.proxyUrl, c.config.timeout, c.config.localAddr, c.config.dialer, c.logger)
proxyDialer, err := newConnectDialer(c.config.proxyUrl, c.config.timeout, c.config.localAddr, c.config.dialer, c.config.connectHeaders, c.logger)
if err != nil {
c.logger.Error("failed to create proxy connect dialer: %s", err.Error())
return err
Expand Down
8 changes: 8 additions & 0 deletions client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type httpClientConfig struct {
insecureSkipVerify bool
certificatePins map[string][]string
defaultHeaders http.Header
connectHeaders http.Header
badPinHandler BadPinHandlerFunc
proxyUrl string
serverNameOverwrite string
Expand Down Expand Up @@ -258,3 +259,10 @@ func WithBandwidthTracker() HttpClientOption {
config.enabledBandwidthTracker = true
}
}

// WithConnectHeaders configures a client to use the specified headers for the CONNECT request.
func WithConnectHeaders(headers http.Header) HttpClientOption {
return func(config *httpClientConfig) {
config.connectHeaders = headers
}
}
4 changes: 2 additions & 2 deletions connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type connectDialer struct {
// newConnectDialer creates a dialer to issue CONNECT requests and tunnel traffic via HTTP/S proxy.
// proxyUrlStr must provide Scheme and Host, may provide credentials and port.
// Example: https://username:[email protected]:443
func newConnectDialer(proxyUrlStr string, timeout time.Duration, localAddr *net.TCPAddr, configDialer net.Dialer, logger Logger) (proxy.ContextDialer, error) {
func newConnectDialer(proxyUrlStr string, timeout time.Duration, localAddr *net.TCPAddr, configDialer net.Dialer, connectHeaders http.Header, logger Logger) (proxy.ContextDialer, error) {
proxyUrl, err := url.Parse(proxyUrlStr)
if err != nil {
return nil, err
Expand Down Expand Up @@ -132,7 +132,7 @@ func newConnectDialer(proxyUrlStr string, timeout time.Duration, localAddr *net.
ProxyUrl: *proxyUrl,
Dialer: _dialer,
Timeout: timeout,
DefaultHeader: make(http.Header),
DefaultHeader: connectHeaders,
EnableH2ConnReuse: true,
}

Expand Down
3 changes: 2 additions & 1 deletion tests/header_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package tests

import (
"encoding/json"
"github.com/bogdanfinn/tls-client/profiles"
"io"
"testing"

"github.com/bogdanfinn/tls-client/profiles"

http "github.com/bogdanfinn/fhttp"
tls_client "github.com/bogdanfinn/tls-client"
"github.com/stretchr/testify/assert"
Expand Down

0 comments on commit ee0f8de

Please sign in to comment.