Skip to content

Commit

Permalink
getlantern/lantern#2045 Added support for hooking into connections to…
Browse files Browse the repository at this point in the history
… server (to facilitate stat reporting)
  • Loading branch information
oxtoacart committed Dec 15, 2014
1 parent 94d5b93 commit 68e7d2e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ type Config struct {
// values indicating higher QOS.
QOS int

// OnDial: optional callback that gets invoked whenever we dial the server.
// The Conn and error returned from this callback will be used in lieu of
// the originals.
OnDial func(conn net.Conn, err error) (net.Conn, error)

// OnDialStats is an optional callback that will get called on every dial to
// the server to report stats on what was dialed and how long each step
// took.
Expand Down Expand Up @@ -105,7 +110,11 @@ func NewDialer(cfg *Config) *Dialer {
Dial: d.dialServer,
}
d.enproxyConfig = d.enproxyConfigWith(func(addr string) (net.Conn, error) {
return d.connPool.Get()
conn, err := d.connPool.Get()
if d.OnDial != nil {
conn, err = d.OnDial(conn, err)
}
return conn, err
})
return d
}
Expand Down
13 changes: 13 additions & 0 deletions fronted_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fronted

import (
"fmt"
"io/ioutil"
"net"
"net/http"
Expand Down Expand Up @@ -34,6 +35,18 @@ func TestBadEnproxyConn(t *testing.T) {
assert.Error(t, err, "Dialing using a non-existent host should have failed")
}

func TestReplaceBadOnDial(t *testing.T) {
d := NewDialer(&Config{
Host: "fallbacks.getiantem.org",
Port: 443,
OnDial: func(conn net.Conn, err error) (net.Conn, error) {
return nil, fmt.Errorf("Gotcha!")
},
})
_, err := d.Dial("tcp", "www.google.com")
assert.Error(t, err, "Dialing using a bad OnDial should fail")
}

func TestHttpClientWithBadEnproxyConn(t *testing.T) {
d := NewDialer(&Config{
Host: "localhost",
Expand Down

0 comments on commit 68e7d2e

Please sign in to comment.