1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-14 01:35:23 +08:00

swarm + handshake: better observed addr check

The check needed knowledge of the _listen_ addresses,
not just the interface addresses. Also, the handshake now
sends out all the addresses we accumulate about ourselves.
(this may be bad in the long run, but useful now to test)
This commit is contained in:
Juan Batiz-Benet
2014-11-05 04:01:32 -08:00
parent 0135e3ebbe
commit 4989dcafed
7 changed files with 105 additions and 137 deletions

View File

@ -4,16 +4,13 @@ import (
"errors"
"io"
"math/rand"
"net"
"os"
"path/filepath"
"reflect"
"strings"
"time"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir"
)
@ -111,57 +108,3 @@ func GetenvBool(name string) bool {
v := strings.ToLower(os.Getenv(name))
return v == "true" || v == "t" || v == "1"
}
// IsLoopbackAddr returns whether or not the ip portion of the passed in multiaddr
// string is a loopback address
func IsLoopbackAddr(addr string) bool {
loops := []string{"/ip4/127.0.0.1", "/ip6/::1"}
for _, loop := range loops {
if strings.HasPrefix(addr, loop) {
return true
}
}
return false
}
// GetLocalAddresses returns a list of ip addresses associated with
// the local machine
func GetLocalAddresses() ([]ma.Multiaddr, error) {
// Enumerate interfaces on this machine
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
var maddrs []ma.Multiaddr
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
log.Warningf("Skipping addr: %s", err)
continue
}
// Check each address and convert to a multiaddr
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
// Build multiaddr
maddr, err := manet.FromIP(v.IP)
if err != nil {
log.Errorf("maddr parsing error: %s", err)
continue
}
// Dont list loopback addresses
if IsLoopbackAddr(maddr.String()) {
continue
}
maddrs = append(maddrs, maddr)
default:
// Not sure if any other types will show up here
log.Errorf("Got '%s' type = '%s'", v, reflect.TypeOf(v))
}
}
}
return maddrs, nil
}