From b5df0732176136caa28f0b22b0e59d342d8e19e2 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 12 Apr 2018 15:06:53 +1000 Subject: [PATCH] Support different hosts for each network CircleCI's localhost hostname doesn't resolve for IPv6. --- client.go | 2 +- client_test.go | 16 +++++++++++----- cmd/torrent/main.go | 2 +- cmd/torrentfs/main.go | 7 ++++--- config.go | 13 ++++++++++++- fs/torrentfs_test.go | 10 ++++------ socket.go | 31 ++++++++++++++++++------------- 7 files changed, 51 insertions(+), 30 deletions(-) diff --git a/client.go b/client.go index 2b984b57..f0d21cac 100644 --- a/client.go +++ b/client.go @@ -225,7 +225,7 @@ func NewClient(cfg *Config) (cl *Client, err error) { } } - cl.conns, err = listenAll(cl.enabledPeerNetworks(), cl.config.ListenAddr) + cl.conns, err = listenAll(cl.enabledPeerNetworks(), cl.config.ListenHost, cl.config.ListenPort) if err != nil { return } diff --git a/client_test.go b/client_test.go index abeb953a..0d55a20b 100644 --- a/client_test.go +++ b/client_test.go @@ -9,6 +9,7 @@ import ( "net" "os" "path/filepath" + "strings" "sync" "testing" "time" @@ -31,7 +32,13 @@ import ( func TestingConfig() *Config { return &Config{ - ListenAddr: "localhost:0", + ListenHost: func(network string) string { + if strings.Contains(network, "4") { + return "127.0.0.1" + } else { + return "::1" + } + }, NoDHT: true, DataDir: tempDir(), DisableTrackers: true, @@ -540,7 +547,7 @@ func BenchmarkAddLargeTorrent(b *testing.B) { cfg := TestingConfig() cfg.DisableTCP = true cfg.DisableUTP = true - cfg.ListenAddr = "redonk" + cfg.ListenHost = func(string) string { return "redonk" } cl, err := NewClient(cfg) require.NoError(b, err) defer cl.Close() @@ -756,7 +763,7 @@ func TestAddTorrentPiecesNotAlreadyCompleted(t *testing.T) { func TestAddMetainfoWithNodes(t *testing.T) { cfg := TestingConfig() - cfg.ListenAddr = ":0" + cfg.ListenHost = func(string) string { return "" } cfg.NoDHT = false cfg.DhtStartingNodes = func() ([]dht.Addr, error) { return nil, nil } // For now, we want to just jam the nodes into the table, without @@ -1065,8 +1072,7 @@ func TestClientAddressInUse(t *testing.T) { if s != nil { defer s.Close() } - cfg := TestingConfig() - cfg.ListenAddr = ":50007" + cfg := TestingConfig().SetListenAddr(":50007") cl, err := NewClient(cfg) require.Error(t, err) require.Nil(t, cl) diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 52547590..2421412b 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -162,7 +162,7 @@ func main() { clientConfig.DefaultStorage = storage.NewMMap("") } if flags.Addr != nil { - clientConfig.ListenAddr = flags.Addr.String() + clientConfig.SetListenAddr(flags.Addr.String()) } if flags.UploadRate != -1 { clientConfig.UploadRateLimiter = rate.NewLimiter(rate.Limit(flags.UploadRate), 256<<10) diff --git a/cmd/torrentfs/main.go b/cmd/torrentfs/main.go index 4d9119da..3cc77812 100644 --- a/cmd/torrentfs/main.go +++ b/cmd/torrentfs/main.go @@ -86,12 +86,13 @@ func mainExitCode() int { defer fuse.Unmount(args.MountDir) // TODO: Think about the ramifications of exiting not due to a signal. defer conn.Close() - client, err := torrent.NewClient(&torrent.Config{ + cfg := torrent.Config{ DataDir: args.DownloadDir, DisableTrackers: args.DisableTrackers, - ListenAddr: args.ListenAddr.String(), NoUpload: true, // Ensure that downloads are responsive. - }) + } + cfg.SetListenAddr(args.ListenAddr.String()) + client, err := torrent.NewClient(&cfg) if err != nil { log.Print(err) return 1 diff --git a/config.go b/config.go index c2aec9e3..8b679c15 100644 --- a/config.go +++ b/config.go @@ -9,6 +9,8 @@ import ( "golang.org/x/time/rate" "github.com/anacrolix/dht" + "github.com/anacrolix/missinggo" + "github.com/anacrolix/missinggo/expect" "github.com/anacrolix/torrent/iplist" "github.com/anacrolix/torrent/storage" ) @@ -33,7 +35,8 @@ type Config struct { // The address to listen for new uTP and TCP bittorrent protocol // connections. DHT shares a UDP socket with uTP unless configured // otherwise. - ListenAddr string `long:"listen-addr" value-name:"HOST:PORT"` + ListenHost func(network string) string + ListenPort int NoDefaultPortForwarding bool // Don't announce to trackers. This only leaves DHT to discover peers. DisableTrackers bool `long:"disable-trackers"` @@ -112,6 +115,14 @@ type Config struct { PublicIp6 net.IP } +func (cfg *Config) SetListenAddr(addr string) *Config { + host, port, err := missinggo.ParseHostPort(addr) + expect.Nil(err) + cfg.ListenHost = func(string) string { return host } + cfg.ListenPort = port + return cfg +} + func (cfg *Config) setDefaults() { if cfg.HTTP == nil { cfg.HTTP = DefaultHTTPClient diff --git a/fs/torrentfs_test.go b/fs/torrentfs_test.go index 164a8fec..6f89aeea 100644 --- a/fs/torrentfs_test.go +++ b/fs/torrentfs_test.go @@ -165,13 +165,12 @@ func TestDownloadOnDemand(t *testing.T) { layout, err := newGreetingLayout() require.NoError(t, err) defer layout.Destroy() - seeder, err := torrent.NewClient(&torrent.Config{ + seeder, err := torrent.NewClient((&torrent.Config{ DataDir: layout.Completed, DisableTrackers: true, NoDHT: true, - ListenAddr: "localhost:0", Seed: true, - }) + }).SetListenAddr("localhost:0")) require.NoError(t, err) defer seeder.Close() testutil.ExportStatusWriter(seeder, "s") @@ -184,16 +183,15 @@ func TestDownloadOnDemand(t *testing.T) { <-seederTorrent.GotInfo() seederTorrent.VerifyData() }() - leecher, err := torrent.NewClient(&torrent.Config{ + leecher, err := torrent.NewClient((&torrent.Config{ DisableTrackers: true, NoDHT: true, - ListenAddr: "localhost:0", DisableTCP: true, DefaultStorage: storage.NewMMap(filepath.Join(layout.BaseDir, "download")), // This can be used to check if clients can connect to other clients // with the same ID. // PeerID: seeder.PeerID(), - }) + }).SetListenAddr("localhost:0")) require.NoError(t, err) testutil.ExportStatusWriter(leecher, "l") defer leecher.Close() diff --git a/socket.go b/socket.go index 7c14e3fd..fb70b280 100644 --- a/socket.go +++ b/socket.go @@ -61,26 +61,31 @@ func setPort(addr string, port int) string { return net.JoinHostPort(host, strconv.FormatInt(int64(port), 10)) } -func listenAll(networks []string, addr string) ([]socket, error) { +func listenAll(networks []string, getHost func(string) string, port int) ([]socket, error) { if len(networks) == 0 { return nil, nil } + var nahs []networkAndHost + for _, n := range networks { + nahs = append(nahs, networkAndHost{n, getHost(n)}) + } for { - ss, retry, err := listenAllRetry(networks, addr) + ss, retry, err := listenAllRetry(nahs, port) if !retry { return ss, err } } } -func listenAllRetry(networks []string, addr string) (ss []socket, retry bool, err error) { - _, port, err := missinggo.ParseHostPort(addr) - if err != nil { - err = fmt.Errorf("error parsing addr: %s", err) - return - } - ss = make([]socket, 1, len(networks)) - ss[0], err = listen(networks[0], addr) +type networkAndHost struct { + Network string + Host string +} + +func listenAllRetry(nahs []networkAndHost, port int) (ss []socket, retry bool, err error) { + ss = make([]socket, 1, len(nahs)) + portStr := strconv.FormatInt(int64(port), 10) + ss[0], err = listen(nahs[0].Network, net.JoinHostPort(nahs[0].Host, portStr)) if err != nil { return nil, false, fmt.Errorf("first listen: %s", err) } @@ -92,9 +97,9 @@ func listenAllRetry(networks []string, addr string) (ss []socket, retry bool, er ss = nil } }() - restAddr := setPort(addr, missinggo.AddrPort(ss[0].Addr())) - for _, n := range networks[1:] { - s, err := listen(n, restAddr) + portStr = strconv.FormatInt(int64(missinggo.AddrPort(ss[0].Addr())), 10) + for _, nah := range nahs[1:] { + s, err := listen(nah.Network, net.JoinHostPort(nah.Host, portStr)) if err != nil { return ss, missinggo.IsAddrInUse(err) && port == 0,