2022-08-01 12:31:22 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-08-01 12:31:22 +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.
|
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2022-08-01 12:31:22 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
import macros
|
|
|
|
import stew/[objects, results]
|
2022-09-15 07:43:40 +00:00
|
|
|
import ../../../peerinfo, ../../../signed_envelope
|
2022-08-01 12:31:22 +00:00
|
|
|
|
|
|
|
# Circuit Relay V1 Message
|
|
|
|
|
|
|
|
type
|
|
|
|
RelayType* {.pure.} = enum
|
|
|
|
Hop = 1
|
|
|
|
Stop = 2
|
|
|
|
Status = 3
|
|
|
|
CanHop = 4
|
|
|
|
|
|
|
|
StatusV1* {.pure.} = enum
|
|
|
|
Success = 100
|
|
|
|
HopSrcAddrTooLong = 220
|
|
|
|
HopDstAddrTooLong = 221
|
|
|
|
HopSrcMultiaddrInvalid = 250
|
|
|
|
HopDstMultiaddrInvalid = 251
|
|
|
|
HopNoConnToDst = 260
|
|
|
|
HopCantDialDst = 261
|
|
|
|
HopCantOpenDstStream = 262
|
|
|
|
HopCantSpeakRelay = 270
|
|
|
|
HopCantRelayToSelf = 280
|
|
|
|
StopSrcAddrTooLong = 320
|
|
|
|
StopDstAddrTooLong = 321
|
|
|
|
StopSrcMultiaddrInvalid = 350
|
|
|
|
StopDstMultiaddrInvalid = 351
|
|
|
|
StopRelayRefused = 390
|
|
|
|
MalformedMessage = 400
|
|
|
|
|
|
|
|
RelayPeer* = object
|
|
|
|
peerId*: PeerId
|
|
|
|
addrs*: seq[MultiAddress]
|
|
|
|
|
|
|
|
RelayMessage* = object
|
2023-06-28 14:44:58 +00:00
|
|
|
msgType*: Opt[RelayType]
|
|
|
|
srcPeer*: Opt[RelayPeer]
|
|
|
|
dstPeer*: Opt[RelayPeer]
|
|
|
|
status*: Opt[StatusV1]
|
2022-08-01 12:31:22 +00:00
|
|
|
|
|
|
|
proc encode*(msg: RelayMessage): ProtoBuffer =
|
|
|
|
result = initProtoBuffer()
|
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
msg.msgType.withValue(typ):
|
|
|
|
result.write(1, typ.ord.uint)
|
|
|
|
msg.srcPeer.withValue(srcPeer):
|
2022-08-01 12:31:22 +00:00
|
|
|
var peer = initProtoBuffer()
|
2023-06-28 14:44:58 +00:00
|
|
|
peer.write(1, srcPeer.peerId)
|
|
|
|
for ma in srcPeer.addrs:
|
2022-08-01 12:31:22 +00:00
|
|
|
peer.write(2, ma.data.buffer)
|
|
|
|
peer.finish()
|
|
|
|
result.write(2, peer.buffer)
|
2023-06-28 14:44:58 +00:00
|
|
|
msg.dstPeer.withValue(dstPeer):
|
2022-08-01 12:31:22 +00:00
|
|
|
var peer = initProtoBuffer()
|
2023-06-28 14:44:58 +00:00
|
|
|
peer.write(1, dstPeer.peerId)
|
|
|
|
for ma in dstPeer.addrs:
|
2022-08-01 12:31:22 +00:00
|
|
|
peer.write(2, ma.data.buffer)
|
|
|
|
peer.finish()
|
|
|
|
result.write(3, peer.buffer)
|
2023-06-28 14:44:58 +00:00
|
|
|
msg.status.withValue(status):
|
|
|
|
result.write(4, status.ord.uint)
|
2022-08-01 12:31:22 +00:00
|
|
|
|
|
|
|
result.finish()
|
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
proc decode*(_: typedesc[RelayMessage], buf: seq[byte]): Opt[RelayMessage] =
|
2022-08-01 12:31:22 +00:00
|
|
|
var
|
|
|
|
rMsg: RelayMessage
|
|
|
|
msgTypeOrd: uint32
|
|
|
|
src: RelayPeer
|
|
|
|
dst: RelayPeer
|
|
|
|
statusOrd: uint32
|
|
|
|
pbSrc: ProtoBuffer
|
|
|
|
pbDst: ProtoBuffer
|
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
let pb = initProtoBuffer(buf)
|
2022-08-01 12:31:22 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
if ?pb.getField(1, msgTypeOrd).toOpt():
|
|
|
|
if msgTypeOrd.int notin RelayType:
|
|
|
|
return Opt.none(RelayMessage)
|
|
|
|
rMsg.msgType = Opt.some(RelayType(msgTypeOrd))
|
2022-08-01 12:31:22 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
if ?pb.getField(2, pbSrc).toOpt():
|
|
|
|
discard ?pbSrc.getField(1, src.peerId).toOpt()
|
|
|
|
discard ?pbSrc.getRepeatedField(2, src.addrs).toOpt()
|
|
|
|
rMsg.srcPeer = Opt.some(src)
|
2022-08-01 12:31:22 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
if ?pb.getField(3, pbDst).toOpt():
|
|
|
|
discard ?pbDst.getField(1, dst.peerId).toOpt()
|
|
|
|
discard ?pbDst.getRepeatedField(2, dst.addrs).toOpt()
|
|
|
|
rMsg.dstPeer = Opt.some(dst)
|
2022-08-01 12:31:22 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
if ?pb.getField(4, statusOrd).toOpt():
|
2023-05-18 08:24:17 +00:00
|
|
|
var status: StatusV1
|
|
|
|
if not checkedEnumAssign(status, statusOrd):
|
2023-06-28 14:44:58 +00:00
|
|
|
return Opt.none(RelayMessage)
|
|
|
|
rMsg.status = Opt.some(status)
|
|
|
|
Opt.some(rMsg)
|
2022-08-01 12:31:22 +00:00
|
|
|
|
|
|
|
# Voucher
|
|
|
|
|
|
|
|
type Voucher* = object
|
|
|
|
relayPeerId*: PeerId # peer ID of the relay
|
|
|
|
reservingPeerId*: PeerId # peer ID of the reserving peer
|
|
|
|
expiration*: uint64 # UNIX UTC expiration time for the reservation
|
|
|
|
|
|
|
|
proc decode*(_: typedesc[Voucher], buf: seq[byte]): Result[Voucher, ProtoError] =
|
|
|
|
let pb = initProtoBuffer(buf)
|
|
|
|
var v = Voucher()
|
|
|
|
|
|
|
|
?pb.getRequiredField(1, v.relayPeerId)
|
|
|
|
?pb.getRequiredField(2, v.reservingPeerId)
|
|
|
|
?pb.getRequiredField(3, v.expiration)
|
|
|
|
|
|
|
|
ok(v)
|
|
|
|
|
|
|
|
proc encode*(v: Voucher): seq[byte] =
|
|
|
|
var pb = initProtoBuffer()
|
|
|
|
|
|
|
|
pb.write(1, v.relayPeerId)
|
|
|
|
pb.write(2, v.reservingPeerId)
|
|
|
|
pb.write(3, v.expiration)
|
|
|
|
|
|
|
|
pb.finish()
|
|
|
|
pb.buffer
|
|
|
|
|
|
|
|
proc init*(
|
|
|
|
T: typedesc[Voucher],
|
|
|
|
relayPeerId: PeerId,
|
|
|
|
reservingPeerId: PeerId,
|
|
|
|
expiration: uint64,
|
|
|
|
): T =
|
|
|
|
T(
|
|
|
|
relayPeerId = relayPeerId, reservingPeerId = reservingPeerId, expiration: expiration
|
|
|
|
)
|
|
|
|
|
|
|
|
type SignedVoucher* = SignedPayload[Voucher]
|
|
|
|
|
|
|
|
proc payloadDomain*(_: typedesc[Voucher]): string =
|
|
|
|
"libp2p-relay-rsvp"
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2022-08-01 12:31:22 +00:00
|
|
|
proc payloadType*(_: typedesc[Voucher]): seq[byte] =
|
|
|
|
@[(byte) 0x03, (byte) 0x02]
|
|
|
|
|
|
|
|
proc checkValid*(spr: SignedVoucher): Result[void, EnvelopeError] =
|
|
|
|
if not spr.data.relayPeerId.match(spr.envelope.publicKey):
|
|
|
|
err(EnvelopeInvalidSignature)
|
|
|
|
else:
|
|
|
|
ok()
|
|
|
|
|
|
|
|
# Circuit Relay V2 Hop Message
|
|
|
|
|
|
|
|
type
|
|
|
|
Peer* = object
|
|
|
|
peerId*: PeerId
|
|
|
|
addrs*: seq[MultiAddress]
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2022-08-01 12:31:22 +00:00
|
|
|
Reservation* = object
|
|
|
|
expire*: uint64 # required, Unix expiration time (UTC)
|
|
|
|
addrs*: seq[MultiAddress] # relay address for reserving peer
|
2023-06-28 14:44:58 +00:00
|
|
|
svoucher*: Opt[seq[byte]] # optional, reservation voucher
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2022-08-01 12:31:22 +00:00
|
|
|
Limit* = object
|
|
|
|
duration*: uint32 # seconds
|
|
|
|
data*: uint64 # bytes
|
|
|
|
|
|
|
|
StatusV2* = enum
|
|
|
|
Ok = 100
|
|
|
|
ReservationRefused = 200
|
|
|
|
ResourceLimitExceeded = 201
|
|
|
|
PermissionDenied = 202
|
|
|
|
ConnectionFailed = 203
|
|
|
|
NoReservation = 204
|
|
|
|
MalformedMessage = 400
|
|
|
|
UnexpectedMessage = 401
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2022-08-01 12:31:22 +00:00
|
|
|
HopMessageType* {.pure.} = enum
|
|
|
|
Reserve = 0
|
|
|
|
Connect = 1
|
|
|
|
Status = 2
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2022-08-01 12:31:22 +00:00
|
|
|
HopMessage* = object
|
|
|
|
msgType*: HopMessageType
|
2023-06-28 14:44:58 +00:00
|
|
|
peer*: Opt[Peer]
|
|
|
|
reservation*: Opt[Reservation]
|
2022-08-01 12:31:22 +00:00
|
|
|
limit*: Limit
|
2023-06-28 14:44:58 +00:00
|
|
|
status*: Opt[StatusV2]
|
2022-08-01 12:31:22 +00:00
|
|
|
|
|
|
|
proc encode*(msg: HopMessage): ProtoBuffer =
|
|
|
|
var pb = initProtoBuffer()
|
|
|
|
|
|
|
|
pb.write(1, msg.msgType.ord.uint)
|
2023-06-28 14:44:58 +00:00
|
|
|
msg.peer.withValue(peer):
|
2022-08-01 12:31:22 +00:00
|
|
|
var ppb = initProtoBuffer()
|
2023-06-28 14:44:58 +00:00
|
|
|
ppb.write(1, peer.peerId)
|
|
|
|
for ma in peer.addrs:
|
2022-08-01 12:31:22 +00:00
|
|
|
ppb.write(2, ma.data.buffer)
|
|
|
|
ppb.finish()
|
|
|
|
pb.write(2, ppb.buffer)
|
2023-06-28 14:44:58 +00:00
|
|
|
msg.reservation.withValue(rsrv):
|
2022-08-01 12:31:22 +00:00
|
|
|
var rpb = initProtoBuffer()
|
|
|
|
rpb.write(1, rsrv.expire)
|
|
|
|
for ma in rsrv.addrs:
|
|
|
|
rpb.write(2, ma.data.buffer)
|
2023-06-28 14:44:58 +00:00
|
|
|
rsrv.svoucher.withValue(vouch):
|
|
|
|
rpb.write(3, vouch)
|
2022-08-01 12:31:22 +00:00
|
|
|
rpb.finish()
|
|
|
|
pb.write(3, rpb.buffer)
|
|
|
|
if msg.limit.duration > 0 or msg.limit.data > 0:
|
|
|
|
var lpb = initProtoBuffer()
|
|
|
|
if msg.limit.duration > 0:
|
|
|
|
lpb.write(1, msg.limit.duration)
|
|
|
|
if msg.limit.data > 0:
|
|
|
|
lpb.write(2, msg.limit.data)
|
|
|
|
lpb.finish()
|
|
|
|
pb.write(4, lpb.buffer)
|
2023-06-28 14:44:58 +00:00
|
|
|
msg.status.withValue(status):
|
|
|
|
pb.write(5, status.ord.uint)
|
2022-08-01 12:31:22 +00:00
|
|
|
|
|
|
|
pb.finish()
|
|
|
|
pb
|
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
proc decode*(_: typedesc[HopMessage], buf: seq[byte]): Opt[HopMessage] =
|
|
|
|
var msg: HopMessage
|
|
|
|
let pb = initProtoBuffer(buf)
|
2022-08-01 12:31:22 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
var msgTypeOrd: uint32
|
|
|
|
?pb.getRequiredField(1, msgTypeOrd).toOpt()
|
2022-08-01 12:31:22 +00:00
|
|
|
if not checkedEnumAssign(msg.msgType, msgTypeOrd):
|
2023-06-28 14:44:58 +00:00
|
|
|
return Opt.none(HopMessage)
|
|
|
|
|
|
|
|
var pbPeer: ProtoBuffer
|
|
|
|
if ?pb.getField(2, pbPeer).toOpt():
|
|
|
|
var peer: Peer
|
|
|
|
?pbPeer.getRequiredField(1, peer.peerId).toOpt()
|
|
|
|
discard ?pbPeer.getRepeatedField(2, peer.addrs).toOpt()
|
|
|
|
msg.peer = Opt.some(peer)
|
|
|
|
|
|
|
|
var pbReservation: ProtoBuffer
|
|
|
|
if ?pb.getField(3, pbReservation).toOpt():
|
|
|
|
var
|
|
|
|
svoucher: seq[byte]
|
|
|
|
reservation: Reservation
|
|
|
|
if ?pbReservation.getField(3, svoucher).toOpt():
|
|
|
|
reservation.svoucher = Opt.some(svoucher)
|
|
|
|
?pbReservation.getRequiredField(1, reservation.expire).toOpt()
|
|
|
|
discard ?pbReservation.getRepeatedField(2, reservation.addrs).toOpt()
|
|
|
|
msg.reservation = Opt.some(reservation)
|
|
|
|
|
|
|
|
var pbLimit: ProtoBuffer
|
|
|
|
if ?pb.getField(4, pbLimit).toOpt():
|
|
|
|
discard ?pbLimit.getField(1, msg.limit.duration).toOpt()
|
|
|
|
discard ?pbLimit.getField(2, msg.limit.data).toOpt()
|
|
|
|
|
|
|
|
var statusOrd: uint32
|
|
|
|
if ?pb.getField(5, statusOrd).toOpt():
|
2023-05-18 08:24:17 +00:00
|
|
|
var status: StatusV2
|
|
|
|
if not checkedEnumAssign(status, statusOrd):
|
2023-06-28 14:44:58 +00:00
|
|
|
return Opt.none(HopMessage)
|
|
|
|
msg.status = Opt.some(status)
|
|
|
|
Opt.some(msg)
|
2022-08-01 12:31:22 +00:00
|
|
|
|
|
|
|
# Circuit Relay V2 Stop Message
|
|
|
|
|
|
|
|
type
|
|
|
|
StopMessageType* {.pure.} = enum
|
|
|
|
Connect = 0
|
|
|
|
Status = 1
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2022-08-01 12:31:22 +00:00
|
|
|
StopMessage* = object
|
|
|
|
msgType*: StopMessageType
|
2023-06-28 14:44:58 +00:00
|
|
|
peer*: Opt[Peer]
|
2022-08-01 12:31:22 +00:00
|
|
|
limit*: Limit
|
2023-06-28 14:44:58 +00:00
|
|
|
status*: Opt[StatusV2]
|
2022-08-01 12:31:22 +00:00
|
|
|
|
|
|
|
proc encode*(msg: StopMessage): ProtoBuffer =
|
|
|
|
var pb = initProtoBuffer()
|
|
|
|
|
|
|
|
pb.write(1, msg.msgType.ord.uint)
|
2023-06-28 14:44:58 +00:00
|
|
|
msg.peer.withValue(peer):
|
2022-08-01 12:31:22 +00:00
|
|
|
var ppb = initProtoBuffer()
|
2023-06-28 14:44:58 +00:00
|
|
|
ppb.write(1, peer.peerId)
|
|
|
|
for ma in peer.addrs:
|
2022-08-01 12:31:22 +00:00
|
|
|
ppb.write(2, ma.data.buffer)
|
|
|
|
ppb.finish()
|
|
|
|
pb.write(2, ppb.buffer)
|
|
|
|
if msg.limit.duration > 0 or msg.limit.data > 0:
|
|
|
|
var lpb = initProtoBuffer()
|
|
|
|
if msg.limit.duration > 0:
|
|
|
|
lpb.write(1, msg.limit.duration)
|
|
|
|
if msg.limit.data > 0:
|
|
|
|
lpb.write(2, msg.limit.data)
|
|
|
|
lpb.finish()
|
|
|
|
pb.write(3, lpb.buffer)
|
2023-06-28 14:44:58 +00:00
|
|
|
msg.status.withValue(status):
|
|
|
|
pb.write(4, status.ord.uint)
|
2022-08-01 12:31:22 +00:00
|
|
|
|
|
|
|
pb.finish()
|
|
|
|
pb
|
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
proc decode*(_: typedesc[StopMessage], buf: seq[byte]): Opt[StopMessage] =
|
|
|
|
var msg: StopMessage
|
|
|
|
|
|
|
|
let pb = initProtoBuffer(buf)
|
|
|
|
|
|
|
|
var msgTypeOrd: uint32
|
|
|
|
?pb.getRequiredField(1, msgTypeOrd).toOpt()
|
|
|
|
if msgTypeOrd.int notin StopMessageType:
|
|
|
|
return Opt.none(StopMessage)
|
2022-08-01 12:31:22 +00:00
|
|
|
msg.msgType = StopMessageType(msgTypeOrd)
|
2023-06-28 14:44:58 +00:00
|
|
|
|
|
|
|
var pbPeer: ProtoBuffer
|
|
|
|
if ?pb.getField(2, pbPeer).toOpt():
|
|
|
|
var peer: Peer
|
|
|
|
?pbPeer.getRequiredField(1, peer.peerId).toOpt()
|
|
|
|
discard ?pbPeer.getRepeatedField(2, peer.addrs).toOpt()
|
|
|
|
msg.peer = Opt.some(peer)
|
|
|
|
|
|
|
|
var pbLimit: ProtoBuffer
|
|
|
|
if ?pb.getField(3, pbLimit).toOpt():
|
|
|
|
discard ?pbLimit.getField(1, msg.limit.duration).toOpt()
|
|
|
|
discard ?pbLimit.getField(2, msg.limit.data).toOpt()
|
|
|
|
|
|
|
|
var statusOrd: uint32
|
|
|
|
if ?pb.getField(4, statusOrd).toOpt():
|
2023-05-18 08:24:17 +00:00
|
|
|
var status: StatusV2
|
|
|
|
if not checkedEnumAssign(status, statusOrd):
|
2023-06-28 14:44:58 +00:00
|
|
|
return Opt.none(StopMessage)
|
|
|
|
msg.status = Opt.some(status)
|
|
|
|
Opt.some(msg)
|