-
Notifications
You must be signed in to change notification settings - Fork 13
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
http_proxy: support multi-addresses listening #654
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9faaeec
listener: support multi-addresses listening
Choraden d3880cb
http_server: use forwarder's custom Listener
Choraden e1a3c27
http_server: extend config with optional addresses
Choraden 7428e79
http_server: log all addresses the server is listening on
Choraden db02647
http_proxy: support listening on optional addresses
Choraden 74f5e2f
http_proxy: log all addresses the proxy is listening on
Choraden d9ac6a1
bind: add `--optional-addresses` flag
Choraden 4b67218
e2e: add optional addresses flag test
Choraden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,13 @@ import ( | |
"context" | ||
"crypto/tls" | ||
"net" | ||
"sync" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/saucelabs/forwarder/log" | ||
"github.com/saucelabs/forwarder/ratelimit" | ||
"go.uber.org/multierr" | ||
) | ||
|
||
type DialConfig struct { | ||
|
@@ -87,25 +89,64 @@ type ListenerCallbacks interface { | |
// OnAccept is called when a new connection is successfully accepted. | ||
OnAccept(net.Conn) | ||
|
||
// OnBindError is called when a listener fails to bind to an address. | ||
OnBindError(address string, err error) | ||
|
||
// OnTLSHandshakeError is called after a TLS handshake errors out. | ||
OnTLSHandshakeError(*tls.Conn, error) | ||
} | ||
|
||
// Listener is a multi-address listener with TLS support, rate limiting and callbacks. | ||
// The Listener must successfully bind on Address, but it may fail to bind on OptionalAddresses. | ||
type Listener struct { | ||
Address string | ||
OptionalAddresses []string | ||
Log log.Logger | ||
TLSConfig *tls.Config | ||
TLSHandshakeTimeout time.Duration | ||
ReadLimit int64 | ||
WriteLimit int64 | ||
Callbacks ListenerCallbacks | ||
|
||
listener net.Listener | ||
listeners []net.Listener | ||
acceptCh chan acceptResult | ||
wg sync.WaitGroup | ||
closeCh chan struct{} | ||
closeOnce sync.Once | ||
} | ||
|
||
type acceptResult struct { | ||
c net.Conn | ||
err error | ||
} | ||
|
||
// Listen starts listening on the provided addresses. | ||
// The method should be called only once. | ||
func (l *Listener) Listen() error { | ||
ll, err := Listen("tcp", l.Address) | ||
l.acceptCh = make(chan acceptResult) | ||
l.closeCh = make(chan struct{}) | ||
|
||
if err := l.listen(l.Address); err != nil { | ||
return err | ||
} | ||
|
||
// OptionalAddresses may fail to bind. | ||
for _, addr := range l.OptionalAddresses { | ||
if err := l.listen(addr); err != nil { | ||
l.Log.Errorf("failed to listen on %s: %v", addr, err) | ||
continue | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (l *Listener) listen(addr string) error { | ||
ll, err := Listen("tcp", addr) | ||
if err != nil { | ||
if l.Callbacks != nil { | ||
l.Callbacks.OnBindError(addr, err) | ||
} | ||
return err | ||
} | ||
|
||
|
@@ -116,13 +157,42 @@ func (l *Listener) Listen() error { | |
ll = ratelimit.NewListener(ll, wl, rl) | ||
} | ||
|
||
l.listener = ll | ||
l.listeners = append(l.listeners, ll) | ||
l.wg.Add(1) | ||
go l.acceptLoop(ll) | ||
|
||
return nil | ||
} | ||
|
||
func (l *Listener) acceptLoop(ll net.Listener) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd use |
||
defer l.wg.Done() | ||
for { | ||
c, err := ll.Accept() | ||
select { | ||
case l.acceptCh <- acceptResult{c, err}: | ||
case <-l.closeCh: | ||
if c != nil { | ||
if cerr := c.Close(); cerr != nil { | ||
l.Log.Errorf("failed to close connection: %v", cerr) | ||
} | ||
} | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (l *Listener) Accept() (net.Conn, error) { | ||
for { | ||
c, err := l.listener.Accept() | ||
var ( | ||
c net.Conn | ||
err error | ||
) | ||
select { | ||
case <-l.closeCh: | ||
return nil, net.ErrClosed | ||
case res := <-l.acceptCh: | ||
c, err = res.c, res.err | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -137,9 +207,9 @@ func (l *Listener) Accept() (net.Conn, error) { | |
|
||
tc, err := l.withTLS(c) | ||
if err != nil { | ||
l.Log.Errorf("Failed to perform TLS handshake: %v", err) | ||
l.Log.Errorf("failed to perform TLS handshake: %v", err) | ||
if cerr := tc.Close(); cerr != nil { | ||
l.Log.Errorf("Failed to close TLS connection: %v", cerr) | ||
l.Log.Errorf("failed to close connection: %v", cerr) | ||
} | ||
continue | ||
} | ||
|
@@ -169,9 +239,32 @@ func (l *Listener) withTLS(conn net.Conn) (*tls.Conn, error) { | |
} | ||
|
||
func (l *Listener) Addr() net.Addr { | ||
return l.listener.Addr() | ||
if len(l.listeners) == 0 { | ||
return &net.IPAddr{} | ||
} | ||
|
||
return l.listeners[0].Addr() | ||
} | ||
|
||
func (l *Listener) Addrs() []net.Addr { | ||
addrs := make([]net.Addr, 0, len(l.listeners)) | ||
for _, ll := range l.listeners { | ||
addrs = append(addrs, ll.Addr()) | ||
} | ||
return addrs | ||
} | ||
|
||
func (l *Listener) Close() error { | ||
return l.listener.Close() | ||
l.closeOnce.Do(func() { close(l.closeCh) }) | ||
|
||
var merr error | ||
for _, ll := range l.listeners { | ||
if err := ll.Close(); err != nil { | ||
merr = multierr.Append(merr, err) | ||
} | ||
} | ||
|
||
l.wg.Wait() | ||
|
||
return merr | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should go before TLS things.