error out if attempting connection to loopback

This commit is contained in:
Jeromy 2014-11-03 00:28:07 +00:00
parent cb2fb9cfe6
commit 094baf6fbf
2 changed files with 12 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import (
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
peer "github.com/jbenet/go-ipfs/peer"
"github.com/jbenet/go-ipfs/util"
)
// Dial connects to a particular peer, over a given network
@ -23,6 +24,10 @@ func (d *Dialer) Dial(ctx context.Context, network string, remote peer.Peer) (Co
return nil, fmt.Errorf("No remote address for network %s", network)
}
if util.IsLoopbackAddr(raddr.String()) {
return nil, fmt.Errorf("Attempted to connect to loopback address: %s", raddr)
}
remote, err := d.Peerstore.Add(remote)
if err != nil {
log.Errorf("Error putting peer into peerstore: %s", remote)

View File

@ -113,7 +113,13 @@ func GetenvBool(name string) bool {
}
func IsLoopbackAddr(addr string) bool {
return addr == "/ip4/127.0.0.1" || addr == "/ip6/::1"
loops := []string{"/ip4/127.0.0.1", "/ip6/::1", "/ip4/0.0.0.0"}
for _, loop := range loops {
if strings.HasPrefix(addr, loop) {
return true
}
}
return false
}
func GetLocalAddresses() ([]ma.Multiaddr, error) {