compare peer string ids, rather than keys

This commit is contained in:
Dmitriy Ryajov 2019-10-04 15:20:19 -06:00
parent 3194e3ac5b
commit 018da65708
3 changed files with 50 additions and 30 deletions

View File

@ -3,23 +3,24 @@ when not(compileOption("threads")):
import tables, options, sequtils, algorithm, strformat, os, strutils import tables, options, sequtils, algorithm, strformat, os, strutils
import chronos import chronos
import ../libp2p/switch, import ../libp2p/[switch,
../libp2p/multistream, multistream,
../libp2p/crypto/crypto, crypto/crypto,
../libp2p/protocols/identify, protocols/identify,
../libp2p/connection, connection,
../libp2p/transports/[transport, tcptransport], transports/transport,
../libp2p/multiaddress, transports/tcptransport,
../libp2p/peerinfo, multiaddress,
../libp2p/peer, peerinfo,
../libp2p/protocols/protocol, peer,
../libp2p/protocols/secure/secure, protocols/protocol,
../libp2p/protocols/secure/secio, protocols/secure/secure,
../libp2p/protocols/pubsub/pubsub, protocols/secure/secio,
../libp2p/protocols/pubsub/floodsub, protocols/pubsub/pubsub,
../libp2p/muxers/muxer, protocols/pubsub/floodsub,
../libp2p/muxers/mplex/mplex, muxers/muxer,
../libp2p/muxers/mplex/types muxers/mplex/mplex,
muxers/mplex/types]
const ChatCodec = "/nim-libp2p/chat/1.0.0" const ChatCodec = "/nim-libp2p/chat/1.0.0"
const DefaultAddr = "/ip4/127.0.0.1/tcp/55505" const DefaultAddr = "/ip4/127.0.0.1/tcp/55505"
@ -127,7 +128,7 @@ proc writeAndPrint(p: ChatProto) {.async, gcsafe.} =
if line.startsWith("/") and "ipfs" in line: if line.startsWith("/") and "ipfs" in line:
await p.dialPeer(line) await p.dialPeer(line)
except: except:
echo &"unable to dial {line}" echo &"unable to dial remote peer {line}"
# echo getCurrentExceptionMsg() # echo getCurrentExceptionMsg()
proc readWriteLoop(p: ChatProto) {.async, gcsafe.} = proc readWriteLoop(p: ChatProto) {.async, gcsafe.} =
@ -191,7 +192,11 @@ proc serveThread(customData: CustomData) {.async.} =
var muxers = [(MplexCodec, mplexProvider)].toTable() var muxers = [(MplexCodec, mplexProvider)].toTable()
var identify = newIdentify(peerInfo) var identify = newIdentify(peerInfo)
var secureManagers = [(SecioCodec, Secure(newSecio(seckey)))].toTable() var secureManagers = [(SecioCodec, Secure(newSecio(seckey)))].toTable()
var switch = newSwitch(peerInfo, transports, identify, muxers, secureManagers = secureManagers) var switch = newSwitch(peerInfo,
transports,
identify,
muxers,
secureManagers = secureManagers)
var chatProto = newChatProto(switch, transp) var chatProto = newChatProto(switch, transp)
switch.mount(chatProto) switch.mount(chatProto)

View File

@ -70,6 +70,7 @@ proc decodeMsg*(buf: seq[byte]): IdentifyInfo =
result.pubKey = none(PublicKey) result.pubKey = none(PublicKey)
var pubKey: PublicKey var pubKey: PublicKey
if pb.getValue(1, pubKey) > 0: if pb.getValue(1, pubKey) > 0:
trace "read public key from message", pubKey = pubKey
result.pubKey = some(pubKey) result.pubKey = some(pubKey)
result.addrs = newSeq[MultiAddress]() result.addrs = newSeq[MultiAddress]()
@ -77,24 +78,31 @@ proc decodeMsg*(buf: seq[byte]): IdentifyInfo =
while pb.getBytes(2, address) > 0: while pb.getBytes(2, address) > 0:
if len(address) != 0: if len(address) != 0:
var copyaddr = address var copyaddr = address
result.addrs.add(MultiAddress.init(copyaddr)) var ma = MultiAddress.init(copyaddr)
result.addrs.add(ma)
trace "read address bytes from message", address = ma
address.setLen(0) address.setLen(0)
var proto = "" var proto = ""
while pb.getString(3, proto) > 0: while pb.getString(3, proto) > 0:
trace "read proto from message", proto = proto
result.protos.add(proto) result.protos.add(proto)
proto = "" proto = ""
var observableAddr = newSeq[byte]() var observableAddr = newSeq[byte]()
if pb.getBytes(4, observableAddr) > 0: # attempt to read the observed addr if pb.getBytes(4, observableAddr) > 0: # attempt to read the observed addr
result.observedAddr = some(MultiAddress.init(observableAddr)) var ma = MultiAddress.init(observableAddr)
trace "read observedAddr from message", address = ma
result.observedAddr = some(ma)
var protoVersion = "" var protoVersion = ""
if pb.getString(5, protoVersion) > 0: if pb.getString(5, protoVersion) > 0:
trace "read protoVersion from message", protoVersion = protoVersion
result.protoVersion = some(protoVersion) result.protoVersion = some(protoVersion)
var agentVersion = "" var agentVersion = ""
if pb.getString(6, agentVersion) > 0: if pb.getString(6, agentVersion) > 0:
trace "read agentVersion from message", agentVersion = agentVersion
result.agentVersion = some(protoVersion) result.agentVersion = some(protoVersion)
proc newIdentify*(peerInfo: PeerInfo): Identify = proc newIdentify*(peerInfo: PeerInfo): Identify =
@ -118,18 +126,25 @@ proc identify*(p: Identify,
var message = await conn.readLp() var message = await conn.readLp()
if len(message) == 0: if len(message) == 0:
trace "identify: Invalid or empty message received!" trace "identify: Invalid or empty message received!"
raise newException(IdentityInvalidMsgError, raise newException(IdentityInvalidMsgError,
"Invalid or empty message received!") "Invalid or empty message received!")
result = decodeMsg(message) result = decodeMsg(message)
trace "Identify for remote peer succeded"
if remotePeerInfo.peerId.isSome and if remotePeerInfo.peerId.isSome and result.pubKey.isSome:
result.pubKey.isSome and let peer = PeerID.init(result.pubKey.get())
result.pubKey != remotePeerInfo.peerId.get().publicKey:
trace "identify: Peer's remote public key doesn't match" # do a string comaprison of the ids,
raise newException(IdentityNoMatchError, # because that is the only thing we have in most cases
"Peer's remote public key doesn't match") if peer.pretty() != remotePeerInfo.peerId.get().pretty():
trace "Peer ids don't match",
remote = peer.pretty(),
local = remotePeerInfo.peerId.get().pretty()
raise newException(IdentityNoMatchError,
"Peer ids don't match")
trace "Identify for remote peer succeded"
proc push*(p: Identify, conn: Connection) {.async.} = proc push*(p: Identify, conn: Connection) {.async.} =
await conn.write(IdentifyPushCodec) await conn.write(IdentifyPushCodec)

View File

@ -67,7 +67,7 @@ method listen*(t: TcpTransport,
# always get the resolved address in case we're bound to 0.0.0.0:0 # always get the resolved address in case we're bound to 0.0.0.0:0
t.ma = t.server.sock.getLocalAddress().toMultiAddr() t.ma = t.server.sock.getLocalAddress().toMultiAddr()
result = t.server.join() result = t.server.join()
trace "started node on", address = t.ma debug "started node on", address = t.ma
method dial*(t: TcpTransport, method dial*(t: TcpTransport,
address: MultiAddress): address: MultiAddress):