mirror of
https://github.com/libp2p/go-libp2p.git
synced 2025-09-26 20:21:26 +08:00
some fixes to make things work with js a little better
This commit is contained in:
@@ -128,6 +128,10 @@ func (c *singleConn) Write(buf []byte) (int, error) {
|
||||
return c.maconn.Write(buf)
|
||||
}
|
||||
|
||||
func (c *singleConn) SetRemotePeer(id peer.ID) {
|
||||
c.remote = id
|
||||
}
|
||||
|
||||
// ID returns the ID of a given Conn.
|
||||
func ID(c Conn) string {
|
||||
l := fmt.Sprintf("%s/%s", c.LocalMultiaddr(), c.LocalPeer().Pretty())
|
||||
|
@@ -24,6 +24,7 @@ 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.
|
||||
|
@@ -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(id peer.ID) {
|
||||
c.insecure.SetRemotePeer(id)
|
||||
}
|
||||
|
@@ -147,3 +147,7 @@ func (c *conn) RemotePeer() peer.ID {
|
||||
func (c *conn) RemotePublicKey() ic.PubKey {
|
||||
return c.remotePubKey
|
||||
}
|
||||
|
||||
func (c *conn) SetRemotePeer(id peer.ID) {
|
||||
c.remote = id
|
||||
}
|
||||
|
@@ -91,6 +91,10 @@ func (c *Conn) Close() error {
|
||||
return c.StreamConn().Close()
|
||||
}
|
||||
|
||||
func (c *Conn) SetRemotePeer(id peer.ID) {
|
||||
c.RawConn().SetRemotePeer(id)
|
||||
}
|
||||
|
||||
func wrapConn(psc *ps.Conn) (*Conn, error) {
|
||||
// grab the underlying connection.
|
||||
if _, ok := psc.NetConn().(conn.Conn); !ok {
|
||||
|
@@ -1,11 +1,14 @@
|
||||
package identify
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
semver "github.com/coreos/go-semver/semver"
|
||||
ggio "github.com/gogo/protobuf/io"
|
||||
ci "github.com/ipfs/go-libp2p-crypto"
|
||||
"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"
|
||||
@@ -150,6 +153,16 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c inet.Conn) {
|
||||
mes.Protocols[i] = string(p)
|
||||
}
|
||||
|
||||
if c.RemotePeer() == "" {
|
||||
pubk := ids.Host.Peerstore().PubKey(ids.Host.ID())
|
||||
data, err := pubk.Bytes()
|
||||
if err != nil {
|
||||
log.Error("marshaling our public key: ", err)
|
||||
} else {
|
||||
mes.PublicKey = data
|
||||
}
|
||||
}
|
||||
|
||||
// observed address so other side is informed of their
|
||||
// "public" address, at least in relation to us.
|
||||
mes.ObservedAddr = c.RemoteMultiaddr().Bytes()
|
||||
@@ -169,9 +182,35 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c inet.Conn) {
|
||||
mes.AgentVersion = &av
|
||||
}
|
||||
|
||||
func checkPubKeyID(mes *pb.Identify, c inet.Conn) error {
|
||||
if len(mes.PublicKey) == 0 {
|
||||
return fmt.Errorf("remote peer has no ID and no public key was sent")
|
||||
}
|
||||
|
||||
pubk, err := ci.UnmarshalPublicKey(mes.PublicKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pid, err := peer.IDFromPublicKey(pubk)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.SetRemotePeer(pid)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ids *IDService) consumeMessage(mes *pb.Identify, c inet.Conn) {
|
||||
p := c.RemotePeer()
|
||||
|
||||
if p == "" {
|
||||
err := checkPubKeyID(mes, c)
|
||||
if err != nil {
|
||||
log.Warning("no peerID set: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
// mes.Protocols
|
||||
|
||||
// mes.ObservedAddr
|
||||
|
Reference in New Issue
Block a user