mirror of
https://github.com/vacp2p/mix.git
synced 2026-01-09 03:28:16 -05:00
fix: ensure destination and exit nodes are not selected as part of SURBs path (#85)
This commit is contained in:
@@ -363,6 +363,7 @@ proc buildSurbs(
|
|||||||
mixProto: MixProtocol,
|
mixProto: MixProtocol,
|
||||||
incoming: AsyncQueue[seq[byte]],
|
incoming: AsyncQueue[seq[byte]],
|
||||||
numSurbs: uint8,
|
numSurbs: uint8,
|
||||||
|
destPeerId: PeerId,
|
||||||
exitPeerId: PeerId,
|
exitPeerId: PeerId,
|
||||||
): Result[seq[SURB], string] =
|
): Result[seq[SURB], string] =
|
||||||
var response: seq[SURB]
|
var response: seq[SURB]
|
||||||
@@ -379,23 +380,16 @@ proc buildSurbs(
|
|||||||
hmacDrbgGenerate(mixProto.rng[], id)
|
hmacDrbgGenerate(mixProto.rng[], id)
|
||||||
|
|
||||||
# Select L mix nodes at random
|
# Select L mix nodes at random
|
||||||
let numMixNodes = mixProto.pubNodeInfo.len
|
|
||||||
|
|
||||||
if mixProto.pubNodeInfo.len < L:
|
if mixProto.pubNodeInfo.len < L:
|
||||||
return err("No. of public mix nodes less than path length")
|
return err("No. of public mix nodes less than path length")
|
||||||
|
|
||||||
var
|
# Remove exit and dest node from nodes to consider for surbs
|
||||||
pubNodeInfoKeys = toSeq(mixProto.pubNodeInfo.keys)
|
var pubNodeInfoKeys =
|
||||||
randPeerId: PeerId
|
mixProto.pubNodeInfo.keys.toSeq().filterIt(it != exitPeerId and it != destPeerId)
|
||||||
availableIndices = toSeq(0 ..< numMixNodes)
|
var availableIndices = toSeq(0 ..< pubNodeInfoKeys.len)
|
||||||
|
|
||||||
# Remove exit node from nodes to consider for surbs
|
|
||||||
let index = pubNodeInfoKeys.find(exitPeerId)
|
|
||||||
if index != -1:
|
|
||||||
availableIndices.del(index)
|
|
||||||
else:
|
|
||||||
return err("could not find exit node")
|
|
||||||
|
|
||||||
|
# Select L mix nodes at random
|
||||||
var i = 0
|
var i = 0
|
||||||
while i < L:
|
while i < L:
|
||||||
let (multiAddr, mixPubKey, delayMillisec) =
|
let (multiAddr, mixPubKey, delayMillisec) =
|
||||||
@@ -403,7 +397,7 @@ proc buildSurbs(
|
|||||||
let randomIndexPosition = cryptoRandomInt(availableIndices.len).valueOr:
|
let randomIndexPosition = cryptoRandomInt(availableIndices.len).valueOr:
|
||||||
return err("failed to generate random num: " & error)
|
return err("failed to generate random num: " & error)
|
||||||
let selectedIndex = availableIndices[randomIndexPosition]
|
let selectedIndex = availableIndices[randomIndexPosition]
|
||||||
randPeerId = pubNodeInfoKeys[selectedIndex]
|
let randPeerId = pubNodeInfoKeys[selectedIndex]
|
||||||
availableIndices.del(randomIndexPosition)
|
availableIndices.del(randomIndexPosition)
|
||||||
debug "Selected mix node for surbs: ", indexInPath = i, peerId = randPeerId
|
debug "Selected mix node for surbs: ", indexInPath = i, peerId = randPeerId
|
||||||
let mixPubInfo = getMixPubInfo(mixProto.pubNodeInfo.getOrDefault(randPeerId))
|
let mixPubInfo = getMixPubInfo(mixProto.pubNodeInfo.getOrDefault(randPeerId))
|
||||||
@@ -448,9 +442,10 @@ proc prepareMsgWithSurbs(
|
|||||||
incoming: AsyncQueue[seq[byte]],
|
incoming: AsyncQueue[seq[byte]],
|
||||||
msg: seq[byte],
|
msg: seq[byte],
|
||||||
numSurbs: uint8 = 0,
|
numSurbs: uint8 = 0,
|
||||||
|
destPeerId: PeerId,
|
||||||
exitPeerId: PeerId,
|
exitPeerId: PeerId,
|
||||||
): Result[seq[byte], string] =
|
): Result[seq[byte], string] =
|
||||||
let surbs = mixProto.buildSurbs(incoming, numSurbs, exitPeerId).valueOr:
|
let surbs = mixProto.buildSurbs(incoming, numSurbs, destPeerId, exitPeerId).valueOr:
|
||||||
return err(error)
|
return err(error)
|
||||||
|
|
||||||
let serialized = ?serializeMessageWithSURBs(msg, surbs)
|
let serialized = ?serializeMessageWithSURBs(msg, surbs)
|
||||||
@@ -601,10 +596,10 @@ proc anonymizeLocalProtocolSend*(
|
|||||||
mix_messages_error.inc(labelValues = ["Entry", "LOW_MIX_POOL"])
|
mix_messages_error.inc(labelValues = ["Entry", "LOW_MIX_POOL"])
|
||||||
return
|
return
|
||||||
|
|
||||||
var
|
# Skip the destination peer
|
||||||
pubNodeInfoKeys = toSeq(mixProto.pubNodeInfo.keys)
|
var pubNodeInfoKeys =
|
||||||
randPeerId: PeerId
|
mixProto.pubNodeInfo.keys.toSeq().filterIt(it != destination.peerId)
|
||||||
availableIndices = toSeq(0 ..< numMixNodes)
|
var availableIndices = toSeq(0 ..< pubNodeInfoKeys.len)
|
||||||
|
|
||||||
var i = 0
|
var i = 0
|
||||||
while i < L:
|
while i < L:
|
||||||
@@ -613,12 +608,9 @@ proc anonymizeLocalProtocolSend*(
|
|||||||
mix_messages_error.inc(labelValues = ["Entry", "NON_RECOVERABLE"])
|
mix_messages_error.inc(labelValues = ["Entry", "NON_RECOVERABLE"])
|
||||||
return
|
return
|
||||||
let selectedIndex = availableIndices[randomIndexPosition]
|
let selectedIndex = availableIndices[randomIndexPosition]
|
||||||
randPeerId = pubNodeInfoKeys[selectedIndex]
|
let randPeerId = pubNodeInfoKeys[selectedIndex]
|
||||||
availableIndices.del(randomIndexPosition)
|
availableIndices.del(randomIndexPosition)
|
||||||
|
|
||||||
# Skip the destination peer
|
|
||||||
if randPeerId == destination.peerId:
|
|
||||||
continue
|
|
||||||
# Last hop will be the exit node that will forward the request
|
# Last hop will be the exit node that will forward the request
|
||||||
if i == L - 1:
|
if i == L - 1:
|
||||||
exitPeerId = randPeerId
|
exitPeerId = randPeerId
|
||||||
@@ -660,7 +652,9 @@ proc anonymizeLocalProtocolSend*(
|
|||||||
return
|
return
|
||||||
let destHop = Hop.init(destAddrBytes)
|
let destHop = Hop.init(destAddrBytes)
|
||||||
|
|
||||||
let msgWithSurbs = mixProto.prepareMsgWithSurbs(incoming, msg, numSurbs, exitPeerId).valueOr:
|
let msgWithSurbs = mixProto.prepareMsgWithSurbs(
|
||||||
|
incoming, msg, numSurbs, destination.peerId, exitPeerId
|
||||||
|
).valueOr:
|
||||||
error "Could not prepend SURBs", err = error
|
error "Could not prepend SURBs", err = error
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user