nim-libp2p/libp2p/protocols/identify.nim

150 lines
4.4 KiB
Nim
Raw Normal View History

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.
import options
2019-09-09 17:33:32 +00:00
import chronos, chronicles
import ../protobuf/minprotobuf,
2019-09-08 07:43:33 +00:00
../peerinfo,
../stream/connection,
../peerid,
../crypto/crypto,
2019-09-08 07:43:33 +00:00
../multiaddress,
../protocols/protocol,
../utility
2019-08-28 02:30:53 +00:00
2019-09-10 02:15:52 +00:00
logScope:
topics = "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
#TODO: implement push identify, leaving out for now as it is not essential
2019-08-30 15:28:07 +00:00
2019-08-28 02:30:53 +00:00
type
IdentityNoMatchError* = object of CatchableError
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]
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
proc encodeMsg*(peerInfo: PeerInfo, observedAddr: Multiaddress): ProtoBuffer =
2019-08-30 05:16:30 +00:00
result = initProtoBuffer()
result.write(1, peerInfo.publicKey.get().getBytes().tryGet())
2019-09-11 16:14:20 +00:00
2019-08-30 05:16:30 +00:00
for ma in peerInfo.addrs:
result.write(2, ma.data.buffer)
for proto in peerInfo.protocols:
result.write(3, proto)
result.write(4, observedAddr.data.buffer)
let protoVersion = ProtoVersion
result.write(5, protoVersion)
let agentVersion = AgentVersion
result.write(6, agentVersion)
result.finish()
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
var pubKey: PublicKey
if pb.getField(1, pubKey):
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
if pb.getRepeatedField(2, result.addrs):
trace "read addresses from message", addresses = result.addrs
if pb.getRepeatedField(3, result.protos):
trace "read protos from message", protocols = result.protos
var observableAddr: MultiAddress
if pb.getField(4, observableAddr):
trace "read observableAddr from message", address = observableAddr
result.observedAddr = some(observableAddr)
2019-09-11 16:14:20 +00:00
var protoVersion = ""
if pb.getField(5, protoVersion):
trace "read protoVersion from message", protoVersion = protoVersion
2019-09-11 16:14:20 +00:00
result.protoVersion = some(protoVersion)
2019-09-11 16:14:20 +00:00
var agentVersion = ""
if pb.getField(6, agentVersion):
trace "read agentVersion from message", agentVersion = agentVersion
2020-06-09 18:42:52 +00:00
result.agentVersion = some(agentVersion)
2019-09-03 20:39:28 +00:00
proc newIdentify*(peerInfo: PeerInfo): Identify =
new result
result.peerInfo = peerInfo
result.init()
method init*(p: Identify) =
2019-09-02 20:45:52 +00:00
proc handle(conn: Connection, proto: string) {.async, gcsafe, closure.} =
try:
defer:
trace "exiting identify handler", oid = conn.oid
await conn.close()
trace "handling identify request", oid = conn.oid
var pb = encodeMsg(p.peerInfo, conn.observedAddr)
await conn.writeLp(pb.buffer)
except CancelledError as exc:
raise exc
except CatchableError as exc:
trace "exception in identify handler", exc = exc.msg
2019-08-30 23:45:57 +00:00
p.handler = handle
p.codec = IdentifyCodec
2019-08-30 23:45:57 +00:00
proc identify*(p: Identify,
conn: Connection,
remotePeerInfo: PeerInfo): Future[IdentifyInfo] {.async, gcsafe.} =
2020-06-21 09:14:19 +00:00
trace "initiating identify", peer = $conn
2020-05-08 20:58:23 +00:00
var message = await conn.readLp(64*1024)
if len(message) == 0:
trace "identify: Invalid or empty message received!"
raise newException(IdentityInvalidMsgError,
"Invalid or empty message received!")
2019-08-30 05:16:30 +00:00
result = decodeMsg(message)
2019-09-11 16:14:20 +00:00
if not isNil(remotePeerInfo) and result.pubKey.isSome:
let peer = PeerID.init(result.pubKey.get())
if peer.isErr:
raise newException(IdentityInvalidMsgError, $peer.error)
else:
# do a string comaprison of the ids,
# because that is the only thing we
# have in most cases
if peer.get() != remotePeerInfo.peerId:
trace "Peer ids don't match",
remote = peer.get().pretty(),
local = remotePeerInfo.id
raise newException(IdentityNoMatchError, "Peer ids don't match")
2019-08-30 05:16:30 +00:00
proc push*(p: Identify, conn: Connection) {.async.} =
await conn.write(IdentifyPushCodec)
var pb = encodeMsg(p.peerInfo, conn.observedAddr)
2019-08-30 05:16:30 +00:00
await conn.writeLp(pb.buffer)