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

Changed: add possibility to set timeout, dial and transport #26

Closed
wants to merge 5 commits into from
Closed
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
27 changes: 22 additions & 5 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/pkg/errors"
)

var errClientCertRequested = errors.New("tls: client cert authentication unsupported")
var ErrClientCertRequested = errors.New("tls: client cert authentication unsupported")

// defaultTimeout is the default value for reading from local connections.
// By default we have no timeout.
Expand All @@ -28,6 +28,8 @@ const defaultTimeout = 0
const dialTimeout = 30 * time.Second
const tlsHandshakeTimeout = 10 * time.Second

type DialFunc func(string, string) (net.Conn, error)

// Proxy is a structure with the proxy server configuration and current state.
type Proxy struct {
// addr is the address the proxy listens to.
Expand All @@ -42,7 +44,7 @@ type Proxy struct {

// dial is a function for creating net.Conn. Can be useful to override in
// unit-tests.
dial func(string, string) (net.Conn, error)
dial DialFunc

// timeout is the remote connection's read/write timeout.
timeout time.Duration
Expand Down Expand Up @@ -80,7 +82,7 @@ func NewProxy(config Config) *Proxy {
// http.Transport.RoundTrip method failed. In this case
// we'll receive the error and will be able to add the host
// to invalidTLSHosts.
return nil, errClientCertRequested
return nil, ErrClientCertRequested
},
},
},
Expand All @@ -102,6 +104,21 @@ func NewProxy(config Config) *Proxy {
return proxy
}

// SetTimeout set Timeout of the proxy (Deadline for processing the request).
func (p *Proxy) SetTimeout(timeout time.Duration) {
p.timeout = timeout
}

// SetTransport set RoundTripper of the proxy.
func (p *Proxy) SetTransport(transport *http.Transport) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please explain what's the need for exposing transport?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The need is to be able to control the transport configuration (dialer, proxy, etc.). I need to modify the DNS resolver to avoid service discovery in a k8s cluster.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just thinking that it'd be better to route transport through the proxy.dial, the current approach when the transport is independent looks pretty strange.

It will also effectively solve this issue: #24 without any config changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p.transport = transport
}

// SetDial set Dialer of the proxy.
func (p *Proxy) SetDial(dial DialFunc) {
p.dial = dial
}

// Addr returns the address this proxy listens to.
func (p *Proxy) Addr() (addr net.Addr) {
return p.addr
Expand Down Expand Up @@ -297,7 +314,7 @@ func (p *Proxy) handleRequest(ctx *Context) (err error) {
res = proxyutil.NewErrorResponse(session.req, err)

if strings.Contains(err.Error(), "x509: ") ||
treussart marked this conversation as resolved.
Show resolved Hide resolved
strings.Contains(err.Error(), errClientCertRequested.Error()) {
errors.Is(err, ErrClientCertRequested) {
log.Printf("id=%s: adding %s to invalid TLS hosts due to: %v", session.ID(), session.req.Host, err)
p.invalidTLSHostsMu.Lock()
p.invalidTLSHosts[session.req.Host] = true
Expand Down Expand Up @@ -413,7 +430,7 @@ func (p *Proxy) handleTunnel(session *Session) (err error) {
// http.Transport.RoundTrip method failed. In this case we'll
// receive the error and will be able to add the host to
// invalidTLSHosts.
return nil, errClientCertRequested
return nil, ErrClientCertRequested
}

tlsConn := tls.Client(conn, &tls.Config{
Expand Down