2019-08-28 02:30:53 +00:00
|
|
|
## Nim-LibP2P
|
2019-09-24 17:48:23 +00:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-08-28 02:30:53 +00:00
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2019-12-04 04:44:54 +00:00
|
|
|
import options
|
2019-09-09 17:33:32 +00:00
|
|
|
import chronos, chronicles
|
2019-12-10 20:50:35 +00:00
|
|
|
import ../protobuf/minprotobuf,
|
2019-09-08 07:43:33 +00:00
|
|
|
../peerinfo,
|
|
|
|
../connection,
|
2019-12-10 20:50:35 +00:00
|
|
|
../peer,
|
|
|
|
../crypto/crypto,
|
2019-09-08 07:43:33 +00:00
|
|
|
../multiaddress,
|
2020-03-23 06:03:36 +00:00
|
|
|
../protocols/protocol,
|
|
|
|
../utility
|
2019-08-28 02:30:53 +00:00
|
|
|
|
2019-09-10 02:15:52 +00:00
|
|
|
logScope:
|
2020-03-27 14:25:52 +00:00
|
|
|
topic = "Identify"
|
2019-09-10 02:15:52 +00:00
|
|
|
|
2019-10-04 16:04:21 +00:00
|
|
|
const
|
|
|
|
IdentifyCodec* = "/ipfs/id/1.0.0"
|
|
|
|
IdentifyPushCodec* = "/ipfs/id/push/1.0.0"
|
|
|
|
ProtoVersion* = "ipfs/0.1.0"
|
|
|
|
AgentVersion* = "nim-libp2p/0.0.1"
|
2019-08-28 02:30:53 +00:00
|
|
|
|
2019-08-30 15:28:07 +00:00
|
|
|
#TODO: implment push identify, leaving out for now as it is not essential
|
|
|
|
|
2019-08-28 02:30:53 +00:00
|
|
|
type
|
2019-09-04 17:36:07 +00:00
|
|
|
IdentityNoMatchError* = object of CatchableError
|
2019-09-06 00:16:21 +00:00
|
|
|
IdentityInvalidMsgError* = object of CatchableError
|
2019-08-28 02:30:53 +00:00
|
|
|
|
2019-08-30 05:16:30 +00:00
|
|
|
IdentifyInfo* = object
|
2019-09-11 16:14:20 +00:00
|
|
|
pubKey*: Option[PublicKey]
|
2019-08-30 05:16:30 +00:00
|
|
|
addrs*: seq[MultiAddress]
|
2019-09-11 16:14:20 +00:00
|
|
|
observedAddr*: Option[MultiAddress]
|
|
|
|
protoVersion*: Option[string]
|
|
|
|
agentVersion*: Option[string]
|
2019-08-31 19:07:00 +00:00
|
|
|
protos*: seq[string]
|
2019-08-28 02:30:53 +00:00
|
|
|
|
2019-08-30 05:16:30 +00:00
|
|
|
Identify* = ref object of LPProtocol
|
2019-09-03 20:39:28 +00:00
|
|
|
peerInfo*: PeerInfo
|
2019-08-28 02:30:53 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc encodeMsg*(peerInfo: PeerInfo, observedAddrs: Multiaddress): ProtoBuffer =
|
2019-08-30 05:16:30 +00:00
|
|
|
result = initProtoBuffer()
|
2019-08-28 19:02:55 +00:00
|
|
|
|
2019-12-07 16:36:39 +00:00
|
|
|
result.write(initProtoField(1, peerInfo.publicKey.get().getBytes()))
|
2019-09-11 16:14:20 +00:00
|
|
|
|
2019-08-30 05:16:30 +00:00
|
|
|
for ma in peerInfo.addrs:
|
2019-08-28 19:02:55 +00:00
|
|
|
result.write(initProtoField(2, ma.data.buffer))
|
|
|
|
|
2019-08-31 19:07:00 +00:00
|
|
|
for proto in peerInfo.protocols:
|
|
|
|
result.write(initProtoField(3, proto))
|
2019-08-28 19:02:55 +00:00
|
|
|
|
|
|
|
result.write(initProtoField(4, observedAddrs.data.buffer))
|
|
|
|
|
|
|
|
let protoVersion = ProtoVersion
|
|
|
|
result.write(initProtoField(5, protoVersion))
|
|
|
|
|
|
|
|
let agentVersion = AgentVersion
|
|
|
|
result.write(initProtoField(6, agentVersion))
|
|
|
|
result.finish()
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc decodeMsg*(buf: seq[byte]): IdentifyInfo =
|
2019-08-30 05:16:30 +00:00
|
|
|
var pb = initProtoBuffer(buf)
|
|
|
|
|
2019-09-11 16:14:20 +00:00
|
|
|
result.pubKey = none(PublicKey)
|
|
|
|
var pubKey: PublicKey
|
|
|
|
if pb.getValue(1, pubKey) > 0:
|
2020-03-23 06:03:36 +00:00
|
|
|
trace "read public key from message", pubKey = ($pubKey).shortLog
|
2019-09-11 16:14:20 +00:00
|
|
|
result.pubKey = some(pubKey)
|
2019-08-30 05:16:30 +00:00
|
|
|
|
2019-08-28 19:02:55 +00:00
|
|
|
result.addrs = newSeq[MultiAddress]()
|
|
|
|
var address = newSeq[byte]()
|
2019-09-10 02:15:52 +00:00
|
|
|
while pb.getBytes(2, address) > 0:
|
2019-08-28 19:02:55 +00:00
|
|
|
if len(address) != 0:
|
|
|
|
var copyaddr = address
|
2019-10-04 21:20:19 +00:00
|
|
|
var ma = MultiAddress.init(copyaddr)
|
|
|
|
result.addrs.add(ma)
|
|
|
|
trace "read address bytes from message", address = ma
|
2019-08-28 19:02:55 +00:00
|
|
|
address.setLen(0)
|
|
|
|
|
|
|
|
var proto = ""
|
|
|
|
while pb.getString(3, proto) > 0:
|
2019-10-04 21:20:19 +00:00
|
|
|
trace "read proto from message", proto = proto
|
2019-08-31 19:07:00 +00:00
|
|
|
result.protos.add(proto)
|
2019-09-01 21:52:02 +00:00
|
|
|
proto = ""
|
2019-12-10 20:50:35 +00:00
|
|
|
|
2019-08-28 19:02:55 +00:00
|
|
|
var observableAddr = newSeq[byte]()
|
|
|
|
if pb.getBytes(4, observableAddr) > 0: # attempt to read the observed addr
|
2019-10-04 21:20:19 +00:00
|
|
|
var ma = MultiAddress.init(observableAddr)
|
|
|
|
trace "read observedAddr from message", address = ma
|
|
|
|
result.observedAddr = some(ma)
|
2019-09-11 16:14:20 +00:00
|
|
|
|
|
|
|
var protoVersion = ""
|
|
|
|
if pb.getString(5, protoVersion) > 0:
|
2019-10-04 21:20:19 +00:00
|
|
|
trace "read protoVersion from message", protoVersion = protoVersion
|
2019-09-11 16:14:20 +00:00
|
|
|
result.protoVersion = some(protoVersion)
|
2019-08-28 19:02:55 +00:00
|
|
|
|
2019-09-11 16:14:20 +00:00
|
|
|
var agentVersion = ""
|
|
|
|
if pb.getString(6, agentVersion) > 0:
|
2019-10-04 21:20:19 +00:00
|
|
|
trace "read agentVersion from message", agentVersion = agentVersion
|
2019-09-11 16:14:20 +00:00
|
|
|
result.agentVersion = some(protoVersion)
|
2019-08-28 19:02:55 +00:00
|
|
|
|
2019-09-03 20:39:28 +00:00
|
|
|
proc newIdentify*(peerInfo: PeerInfo): Identify =
|
|
|
|
new result
|
|
|
|
result.peerInfo = peerInfo
|
|
|
|
result.init()
|
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
method init*(p: Identify) =
|
2019-09-02 20:45:52 +00:00
|
|
|
proc handle(conn: Connection, proto: string) {.async, gcsafe, closure.} =
|
2019-09-23 21:51:28 +00:00
|
|
|
trace "handling identify request"
|
2019-08-30 23:45:57 +00:00
|
|
|
var pb = encodeMsg(p.peerInfo, await conn.getObservedAddrs())
|
|
|
|
await conn.writeLp(pb.buffer)
|
2019-12-04 04:44:54 +00:00
|
|
|
# await conn.close() #TODO: investigate why this breaks
|
2019-08-30 23:45:57 +00:00
|
|
|
|
|
|
|
p.handler = handle
|
2019-09-06 00:16:21 +00:00
|
|
|
p.codec = IdentifyCodec
|
2019-08-30 23:45:57 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
proc identify*(p: Identify,
|
|
|
|
conn: Connection,
|
|
|
|
remotePeerInfo: PeerInfo): Future[IdentifyInfo] {.async, gcsafe.} =
|
2020-03-27 14:25:52 +00:00
|
|
|
trace "initiating identify"
|
2019-08-28 19:02:55 +00:00
|
|
|
var message = await conn.readLp()
|
|
|
|
if len(message) == 0:
|
2019-09-23 21:51:28 +00:00
|
|
|
trace "identify: Invalid or empty message received!"
|
2019-10-04 21:20:19 +00:00
|
|
|
raise newException(IdentityInvalidMsgError,
|
2019-09-06 00:16:21 +00:00
|
|
|
"Invalid or empty message received!")
|
|
|
|
|
2019-08-30 05:16:30 +00:00
|
|
|
result = decodeMsg(message)
|
2019-09-11 16:14:20 +00:00
|
|
|
|
2019-12-10 20:50:35 +00:00
|
|
|
if not isNil(remotePeerInfo) and result.pubKey.isSome:
|
2019-10-04 21:20:19 +00:00
|
|
|
let peer = PeerID.init(result.pubKey.get())
|
|
|
|
|
|
|
|
# do a string comaprison of the ids,
|
2019-12-10 20:50:35 +00:00
|
|
|
# because that is the only thing we
|
2019-12-07 16:36:39 +00:00
|
|
|
# have in most cases
|
2019-12-10 20:50:35 +00:00
|
|
|
if peer != remotePeerInfo.peerId:
|
2019-10-04 21:20:19 +00:00
|
|
|
trace "Peer ids don't match",
|
|
|
|
remote = peer.pretty(),
|
2019-12-17 05:24:03 +00:00
|
|
|
local = remotePeerInfo.id
|
2019-10-04 21:20:19 +00:00
|
|
|
|
|
|
|
raise newException(IdentityNoMatchError,
|
|
|
|
"Peer ids don't match")
|
|
|
|
|
|
|
|
trace "Identify for remote peer succeded"
|
2019-08-28 19:02:55 +00:00
|
|
|
|
2019-08-30 05:16:30 +00:00
|
|
|
proc push*(p: Identify, conn: Connection) {.async.} =
|
|
|
|
await conn.write(IdentifyPushCodec)
|
|
|
|
var pb = encodeMsg(p.peerInfo, await conn.getObservedAddrs())
|
|
|
|
await conn.writeLp(pb.buffer)
|