2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
|
|
|
# Copyright (c) 2022 Status Research & Development GmbH
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
## `Identify <https://docs.libp2p.io/concepts/protocols/#identify>`_ and
|
|
|
|
## `Push Identify <https://docs.libp2p.io/concepts/protocols/#identify-push>`_ implementation
|
2019-08-28 02:30:53 +00:00
|
|
|
|
2022-08-03 11:33:19 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2022-03-14 08:39:30 +00:00
|
|
|
import std/[sequtils, options, strutils, sugar]
|
2022-09-22 19:55:59 +00:00
|
|
|
import stew/results
|
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,
|
2020-06-19 17:29:43 +00:00
|
|
|
../stream/connection,
|
2020-07-01 06:25:09 +00:00
|
|
|
../peerid,
|
2019-12-10 20:50:35 +00:00
|
|
|
../crypto/crypto,
|
2019-09-08 07:43:33 +00:00
|
|
|
../multiaddress,
|
2020-03-23 06:03:36 +00:00
|
|
|
../protocols/protocol,
|
2021-05-21 16:27:01 +00:00
|
|
|
../utility,
|
|
|
|
../errors
|
2019-08-28 02:30:53 +00:00
|
|
|
|
2019-09-10 02:15:52 +00:00
|
|
|
logScope:
|
2020-12-01 17:34:27 +00:00
|
|
|
topics = "libp2p 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
|
|
|
|
|
|
|
type
|
2021-05-21 16:27:01 +00:00
|
|
|
IdentifyError* = object of LPError
|
2020-11-02 20:35:26 +00:00
|
|
|
IdentityNoMatchError* = object of IdentifyError
|
|
|
|
IdentityInvalidMsgError* = object of IdentifyError
|
2021-05-21 16:27:01 +00:00
|
|
|
IdentifyNoPubKeyError* = object of IdentifyError
|
2019-08-28 02:30:53 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
IdentifyInfo* {.public.} = object
|
2021-12-16 10:05:20 +00:00
|
|
|
pubkey*: Option[PublicKey]
|
2021-09-08 09:07:46 +00:00
|
|
|
peerId*: PeerId
|
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]
|
2022-03-14 08:39:30 +00:00
|
|
|
signedPeerRecord*: Option[Envelope]
|
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
|
2022-03-14 08:39:30 +00:00
|
|
|
sendSignedPeerRecord*: bool
|
2019-08-28 02:30:53 +00:00
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
IdentifyPushHandler* = proc (
|
|
|
|
peer: PeerId,
|
|
|
|
newInfo: IdentifyInfo):
|
|
|
|
Future[void]
|
2022-07-01 18:19:57 +00:00
|
|
|
{.gcsafe, raises: [Defect], public.}
|
2021-09-08 09:07:46 +00:00
|
|
|
|
2021-06-14 17:08:47 +00:00
|
|
|
IdentifyPush* = ref object of LPProtocol
|
2021-09-08 09:07:46 +00:00
|
|
|
identifyHandler: IdentifyPushHandler
|
2021-06-14 17:08:47 +00:00
|
|
|
|
2022-03-14 08:39:30 +00:00
|
|
|
chronicles.expandIt(IdentifyInfo):
|
|
|
|
pubkey = ($it.pubkey).shortLog
|
|
|
|
addresses = it.addrs.map(x => $x).join(",")
|
|
|
|
protocols = it.protos.map(x => $x).join(",")
|
|
|
|
observable_address =
|
|
|
|
if it.observedAddr.isSome(): $it.observedAddr.get()
|
|
|
|
else: "None"
|
|
|
|
proto_version = it.protoVersion.get("None")
|
|
|
|
agent_version = it.agentVersion.get("None")
|
|
|
|
signedPeerRecord =
|
|
|
|
# The SPR contains the same data as the identify message
|
|
|
|
# would be cumbersome to log
|
|
|
|
if iinfo.signedPeerRecord.isSome(): "Some"
|
|
|
|
else: "None"
|
|
|
|
|
2022-09-22 19:55:59 +00:00
|
|
|
proc encodeMsg(peerInfo: PeerInfo, observedAddr: Opt[MultiAddress], sendSpr: bool): ProtoBuffer
|
2022-03-14 08:39:30 +00:00
|
|
|
{.raises: [Defect].} =
|
2019-08-30 05:16:30 +00:00
|
|
|
result = initProtoBuffer()
|
2021-05-21 16:27:01 +00:00
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
let pkey = peerInfo.publicKey
|
|
|
|
|
|
|
|
result.write(1, pkey.getBytes().get())
|
2019-08-30 05:16:30 +00:00
|
|
|
for ma in peerInfo.addrs:
|
2020-07-13 12:43:07 +00:00
|
|
|
result.write(2, ma.data.buffer)
|
2019-08-31 19:07:00 +00:00
|
|
|
for proto in peerInfo.protocols:
|
2020-07-13 12:43:07 +00:00
|
|
|
result.write(3, proto)
|
2022-09-22 19:55:59 +00:00
|
|
|
if observedAddr.isSome:
|
|
|
|
result.write(4, observedAddr.get().data.buffer)
|
2019-08-28 19:02:55 +00:00
|
|
|
let protoVersion = ProtoVersion
|
2020-07-13 12:43:07 +00:00
|
|
|
result.write(5, protoVersion)
|
2020-11-25 13:42:02 +00:00
|
|
|
let agentVersion = if peerInfo.agentVersion.len <= 0:
|
|
|
|
AgentVersion
|
|
|
|
else:
|
|
|
|
peerInfo.agentVersion
|
2020-07-13 12:43:07 +00:00
|
|
|
result.write(6, agentVersion)
|
2022-03-14 08:39:30 +00:00
|
|
|
|
|
|
|
## Optionally populate signedPeerRecord field.
|
|
|
|
## See https://github.com/libp2p/go-libp2p/blob/ddf96ce1cfa9e19564feb9bd3e8269958bbc0aba/p2p/protocol/identify/pb/identify.proto for reference.
|
2022-04-13 07:35:28 +00:00
|
|
|
if sendSpr:
|
|
|
|
let sprBuff = peerInfo.signedPeerRecord.envelope.encode()
|
2022-03-14 08:39:30 +00:00
|
|
|
if sprBuff.isOk():
|
|
|
|
result.write(8, sprBuff.get())
|
|
|
|
|
2019-08-28 19:02:55 +00:00
|
|
|
result.finish()
|
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
proc decodeMsg*(buf: seq[byte]): Option[IdentifyInfo] =
|
|
|
|
var
|
|
|
|
iinfo: IdentifyInfo
|
2021-12-16 10:05:20 +00:00
|
|
|
pubkey: PublicKey
|
2020-07-15 08:25:39 +00:00
|
|
|
oaddr: MultiAddress
|
|
|
|
protoVersion: string
|
|
|
|
agentVersion: string
|
2022-03-14 08:39:30 +00:00
|
|
|
signedPeerRecord: SignedPeerRecord
|
2020-07-13 12:43:07 +00:00
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
var pb = initProtoBuffer(buf)
|
2019-08-28 19:02:55 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
let r1 = pb.getField(1, pubkey)
|
2020-07-15 08:25:39 +00:00
|
|
|
let r2 = pb.getRepeatedField(2, iinfo.addrs)
|
|
|
|
let r3 = pb.getRepeatedField(3, iinfo.protos)
|
|
|
|
let r4 = pb.getField(4, oaddr)
|
|
|
|
let r5 = pb.getField(5, protoVersion)
|
|
|
|
let r6 = pb.getField(6, agentVersion)
|
|
|
|
|
2022-03-14 08:39:30 +00:00
|
|
|
let r8 = pb.getField(8, signedPeerRecord)
|
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
let res = r1.isOk() and r2.isOk() and r3.isOk() and
|
2022-03-14 08:39:30 +00:00
|
|
|
r4.isOk() and r5.isOk() and r6.isOk() and
|
|
|
|
r8.isOk()
|
2020-07-15 08:25:39 +00:00
|
|
|
|
|
|
|
if res:
|
|
|
|
if r1.get():
|
2021-12-16 10:05:20 +00:00
|
|
|
iinfo.pubkey = some(pubkey)
|
2020-07-15 08:25:39 +00:00
|
|
|
if r4.get():
|
|
|
|
iinfo.observedAddr = some(oaddr)
|
|
|
|
if r5.get():
|
|
|
|
iinfo.protoVersion = some(protoVersion)
|
|
|
|
if r6.get():
|
|
|
|
iinfo.agentVersion = some(agentVersion)
|
2022-03-14 08:39:30 +00:00
|
|
|
if r8.get() and r1.get():
|
|
|
|
if iinfo.pubkey.get() == signedPeerRecord.envelope.publicKey:
|
|
|
|
iinfo.signedPeerRecord = some(signedPeerRecord.envelope)
|
|
|
|
debug "decodeMsg: decoded identify", iinfo
|
2020-07-15 08:25:39 +00:00
|
|
|
some(iinfo)
|
|
|
|
else:
|
|
|
|
trace "decodeMsg: failed to decode received message"
|
|
|
|
none[IdentifyInfo]()
|
2019-08-28 19:02:55 +00:00
|
|
|
|
2022-03-14 08:39:30 +00:00
|
|
|
proc new*(
|
|
|
|
T: typedesc[Identify],
|
|
|
|
peerInfo: PeerInfo,
|
|
|
|
sendSignedPeerRecord = false
|
|
|
|
): T =
|
|
|
|
let identify = T(
|
|
|
|
peerInfo: peerInfo,
|
|
|
|
sendSignedPeerRecord: sendSignedPeerRecord
|
|
|
|
)
|
2021-06-07 07:32:08 +00:00
|
|
|
identify.init()
|
|
|
|
identify
|
|
|
|
|
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.} =
|
2020-06-08 23:42:27 +00:00
|
|
|
try:
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "handling identify request", conn
|
2022-03-14 08:39:30 +00:00
|
|
|
var pb = encodeMsg(p.peerInfo, conn.observedAddr, p.sendSignedPeerRecord)
|
2020-06-29 15:15:31 +00:00
|
|
|
await conn.writeLp(pb.buffer)
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2020-06-08 23:42:27 +00:00
|
|
|
except CatchableError as exc:
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "exception in identify handler", exc = exc.msg, conn
|
2020-09-24 05:30:19 +00:00
|
|
|
finally:
|
|
|
|
trace "exiting identify handler", conn
|
|
|
|
await conn.closeWithEOF()
|
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,
|
2021-09-08 09:07:46 +00:00
|
|
|
remotePeerId: PeerId): Future[IdentifyInfo] {.async, gcsafe.} =
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "initiating identify", conn
|
2020-05-08 20:58:23 +00:00
|
|
|
var message = await conn.readLp(64*1024)
|
2019-08-28 19:02:55 +00:00
|
|
|
if len(message) == 0:
|
2020-09-06 08:31:47 +00:00
|
|
|
trace "identify: Empty message received!", conn
|
2020-07-15 08:25:39 +00:00
|
|
|
raise newException(IdentityInvalidMsgError, "Empty message received!")
|
2019-09-06 00:16:21 +00:00
|
|
|
|
2020-07-15 08:25:39 +00:00
|
|
|
let infoOpt = decodeMsg(message)
|
|
|
|
if infoOpt.isNone():
|
|
|
|
raise newException(IdentityInvalidMsgError, "Incorrect message received!")
|
|
|
|
result = infoOpt.get()
|
2019-09-11 16:14:20 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
if result.pubkey.isSome:
|
|
|
|
let peer = PeerId.init(result.pubkey.get())
|
2020-07-01 06:25:09 +00:00
|
|
|
if peer.isErr:
|
|
|
|
raise newException(IdentityInvalidMsgError, $peer.error)
|
|
|
|
else:
|
2021-09-08 09:07:46 +00:00
|
|
|
result.peerId = peer.get()
|
|
|
|
if peer.get() != remotePeerId:
|
2020-07-01 06:25:09 +00:00
|
|
|
trace "Peer ids don't match",
|
2020-09-06 08:31:47 +00:00
|
|
|
remote = peer,
|
2021-09-08 09:07:46 +00:00
|
|
|
local = remotePeerId
|
2020-07-01 06:25:09 +00:00
|
|
|
|
|
|
|
raise newException(IdentityNoMatchError, "Peer ids don't match")
|
2021-09-08 09:07:46 +00:00
|
|
|
else:
|
|
|
|
raise newException(IdentityInvalidMsgError, "No pubkey in identify")
|
2019-10-04 21:20:19 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc new*(T: typedesc[IdentifyPush], handler: IdentifyPushHandler = nil): T {.public.} =
|
|
|
|
## Create a IdentifyPush protocol. `handler` will be called every time
|
|
|
|
## a peer sends us new `PeerInfo`
|
2021-09-08 09:07:46 +00:00
|
|
|
let identifypush = T(identifyHandler: handler)
|
2021-06-14 17:08:47 +00:00
|
|
|
identifypush.init()
|
|
|
|
identifypush
|
|
|
|
|
|
|
|
proc init*(p: IdentifyPush) =
|
|
|
|
proc handle(conn: Connection, proto: string) {.async, gcsafe, closure.} =
|
|
|
|
trace "handling identify push", conn
|
|
|
|
try:
|
|
|
|
var message = await conn.readLp(64*1024)
|
|
|
|
|
|
|
|
let infoOpt = decodeMsg(message)
|
|
|
|
if infoOpt.isNone():
|
|
|
|
raise newException(IdentityInvalidMsgError, "Incorrect message received!")
|
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
var indentInfo = infoOpt.get()
|
2021-06-14 17:08:47 +00:00
|
|
|
|
2021-12-16 10:05:20 +00:00
|
|
|
if indentInfo.pubkey.isSome:
|
|
|
|
let receivedPeerId = PeerId.init(indentInfo.pubkey.get()).tryGet()
|
2021-09-08 09:07:46 +00:00
|
|
|
if receivedPeerId != conn.peerId:
|
2021-06-14 17:08:47 +00:00
|
|
|
raise newException(IdentityNoMatchError, "Peer ids don't match")
|
2021-09-08 09:07:46 +00:00
|
|
|
indentInfo.peerId = receivedPeerId
|
2021-06-14 17:08:47 +00:00
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
trace "triggering peer event", peerInfo = conn.peerId
|
|
|
|
if not isNil(p.identifyHandler):
|
|
|
|
await p.identifyHandler(conn.peerId, indentInfo)
|
2021-06-14 17:08:47 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
info "exception in identify push handler", exc = exc.msg, conn
|
|
|
|
finally:
|
|
|
|
trace "exiting identify push handler", conn
|
|
|
|
await conn.closeWithEOF()
|
|
|
|
|
|
|
|
p.handler = handle
|
|
|
|
p.codec = IdentifyPushCodec
|
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
proc push*(p: IdentifyPush, peerInfo: PeerInfo, conn: Connection) {.async, public.} =
|
|
|
|
## Send new `peerInfo`s to a connection
|
2022-03-14 08:39:30 +00:00
|
|
|
var pb = encodeMsg(peerInfo, conn.observedAddr, true)
|
2019-08-30 05:16:30 +00:00
|
|
|
await conn.writeLp(pb.buffer)
|