Skip to content
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

changes for compatability testing with js-ipfs #75

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions p2p/host/routed/routed.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (rh *RoutedHost) Connect(ctx context.Context, pi pstore.PeerInfo) error {
if len(rh.Network().ConnsToPeer(pi.ID)) > 0 {
return nil
}
log.Error("not already connected to: ", pi.ID)

// if we were given some addresses, keep + use them.
if len(pi.Addrs) > 0 {
Expand Down
4 changes: 4 additions & 0 deletions p2p/net/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ func (c *singleConn) Write(buf []byte) (int, error) {
return c.maconn.Write(buf)
}

func (c *singleConn) SetRemotePeer(p peer.ID) {
c.remote = p
}

// ID returns the ID of a given Conn.
func ID(c Conn) string {
l := fmt.Sprintf("%s/%s", c.LocalMultiaddr(), c.LocalPeer().Pretty())
Expand Down
2 changes: 2 additions & 0 deletions p2p/net/conn/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type PeerConn interface {
RemotePeer() peer.ID
RemotePublicKey() ic.PubKey
RemoteMultiaddr() ma.Multiaddr

SetRemotePeer(peer.ID)
}

// Conn is a generic message-based Peer-to-Peer connection.
Expand Down
4 changes: 4 additions & 0 deletions p2p/net/conn/secure_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,7 @@ func (c *secureConn) Write(buf []byte) (int, error) {
func (c *secureConn) ReleaseMsg(m []byte) {
c.secure.ReadWriter().ReleaseMsg(m)
}

func (c *secureConn) SetRemotePeer(p peer.ID) {
c.insecure.SetRemotePeer(p)
}
4 changes: 4 additions & 0 deletions p2p/net/mock/mock_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,7 @@ func (c *conn) RemotePeer() peer.ID {
func (c *conn) RemotePublicKey() ic.PubKey {
return c.remotePubKey
}

func (c *conn) SetRemotePeer(p peer.ID) {
c.remote = p
}
5 changes: 5 additions & 0 deletions p2p/net/swarm/swarm_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func (c *Conn) Close() error {
return c.StreamConn().Close()
}

func (c *Conn) SetRemotePeer(p peer.ID) {
c.StreamConn().AddGroup(p)
c.RawConn().SetRemotePeer(p)
}

func wrapConn(psc *ps.Conn) (*Conn, error) {
// grab the underlying connection.
if _, ok := psc.NetConn().(conn.Conn); !ok {
Expand Down
22 changes: 22 additions & 0 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

semver "github.com/coreos/go-semver/semver"
ggio "github.com/gogo/protobuf/io"
ci "github.com/ipfs/go-libp2p-crypto"
peer "github.com/ipfs/go-libp2p-peer"
pstore "github.com/ipfs/go-libp2p-peerstore"
host "github.com/ipfs/go-libp2p/p2p/host"
mstream "github.com/ipfs/go-libp2p/p2p/metrics/stream"
Expand Down Expand Up @@ -154,6 +156,14 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c inet.Conn) {
// "public" address, at least in relation to us.
mes.ObservedAddr = c.RemoteMultiaddr().Bytes()

pubk := ids.Host.Peerstore().PubKey(ids.Host.ID())
data, err := pubk.Bytes()
if err != nil {
log.Error("failed to marshal public key: ", err)
}

mes.PublicKey = data

// set listen addrs, get our latest addrs from Host.
laddrs := ids.Host.Addrs()
mes.ListenAddrs = make([][]byte, len(laddrs))
Expand Down Expand Up @@ -190,6 +200,18 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c inet.Conn) {
lmaddrs = append(lmaddrs, maddr)
}

pubk, err := ci.UnmarshalPublicKey(mes.GetPublicKey())
if err != nil {
log.Error("couldnt parse pubkey: ", err)
} else {
pid, err := peer.IDFromPublicKey(pubk)
if err != nil {
log.Error("couldnt get id from pubkey: ", err)
} else {
c.SetRemotePeer(pid)
}
}

// if the address reported by the connection roughly matches their annoucned
// listener addresses, its likely to be an external NAT address
if HasConsistentTransport(c.RemoteMultiaddr(), lmaddrs) {
Expand Down