client.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. #!/usr/bin/env python3
  2. import random # For simulation, not cryptography!
  3. import math
  4. import sys
  5. import logging
  6. import network
  7. import dirauth
  8. import relay
  9. import nacl.hash
  10. class VanillaCreatedExtendedHandler:
  11. """A handler for VanillaCreatedCircuitCell and
  12. VanillaExtendedCircuitCell cells."""
  13. def __init__(self, channelmgr, ntor, expecteddesc):
  14. self.channelmgr = channelmgr
  15. self.ntor = ntor
  16. self.expecteddesc = expecteddesc
  17. self.onionkey = expecteddesc.descdict['onionkey']
  18. self.idkey = expecteddesc.descdict['idkey']
  19. def received_cell(self, circhandler, cell):
  20. secret = self.ntor.verify(cell.ntor_reply, self.onionkey, self.idkey)
  21. enckey = nacl.hash.sha256(secret + b'upstream')
  22. deckey = nacl.hash.sha256(secret + b'downstream')
  23. circhandler.add_crypt_layer(enckey, deckey)
  24. if len(circhandler.circuit_descs) == 0:
  25. # This was a VanillaCreatedCircuitCell
  26. circhandler.replace_celltype_handler(
  27. relay.VanillaCreatedCircuitCell, None)
  28. else:
  29. # This was a VanillaExtendedCircuitCell
  30. circhandler.replace_celltype_handler(
  31. relay.VanillaExtendedCircuitCell, None)
  32. circhandler.circuit_descs.append(self.expecteddesc)
  33. # Are we done building the circuit?
  34. if len(circhandler.circuit_descs) == 3:
  35. # Yes!
  36. return
  37. nexthop = None
  38. while nexthop is None:
  39. nexthop = self.channelmgr.relaypicker.pick_weighted_relay()
  40. if nexthop.descdict['addr'] in \
  41. [ desc.descdict['addr'] \
  42. for desc in circhandler.circuit_descs ]:
  43. nexthop = None
  44. # Construct the VanillaExtendCircuitCell
  45. ntor = relay.NTor(self.channelmgr.perfstats)
  46. ntor_request = ntor.request()
  47. circextendmsg = relay.VanillaExtendCircuitCell(
  48. nexthop.descdict['addr'], ntor_request)
  49. # Set up the reply handler
  50. circhandler.replace_celltype_handler(
  51. relay.VanillaExtendedCircuitCell,
  52. VanillaCreatedExtendedHandler(self.channelmgr, ntor, nexthop))
  53. # Send the cell
  54. circhandler.send_cell(circextendmsg)
  55. class TelescopingCreatedHandler:
  56. """A handler for TelescopingCreatedCircuitCell cells; this will only always
  57. communicate with the client's guard."""
  58. def __init__(self, channelmgr, ntor):
  59. self.channelmgr = channelmgr
  60. self.ntor = ntor
  61. if type(self.channelmgr.guard) is dirauth.RelayDescriptor:
  62. guardd = self.channelmgr.guard.descdict
  63. else:
  64. guardd = self.channelmgr.guard.snipdict
  65. self.onionkey = guardd["onionkey"]
  66. self.idkey = guardd["idkey"]
  67. def received_cell(self, circhandler, cell):
  68. logging.debug("Received cell in TelescopingCreatedHandler")
  69. secret = self.ntor.verify(cell.ntor_reply, self.onionkey, self.idkey)
  70. enckey = nacl.hash.sha256(secret + b'upstream')
  71. deckey = nacl.hash.sha256(secret + b'downstream')
  72. circhandler.add_crypt_layer(enckey, deckey)
  73. circhandler.replace_celltype_handler(relay.TelescopingCreatedCircuitCell, None)
  74. circhandler.circuit_descs.append(self.channelmgr.guard)
  75. nexthopidx = None
  76. while nexthopidx is None:
  77. nexthopidx = self.channelmgr.relaypicker.pick_weighted_relay_index()
  78. #print("WARNING: Unimplemented! Need to check if this idx is in the list of circhandlers idxs")
  79. # TODO verify we don't need to do the above
  80. # Construct the TelescopingExtendCircuitCell
  81. ntor = relay.NTor(self.channelmgr.perfstats)
  82. ntor_request = ntor.request()
  83. circextendmsg = relay.TelescopingExtendCircuitCell(
  84. nexthopidx, ntor_request)
  85. # Set up the reply handler
  86. circhandler.replace_celltype_handler(
  87. relay.TelescopingExtendedCircuitCell,
  88. TelescopingExtendedHandler(self.channelmgr, ntor))
  89. # Send the cell
  90. circhandler.send_cell(circextendmsg)
  91. class TelescopingExtendedHandler:
  92. """A handler for TelescopingExtendedCircuitCell cells."""
  93. def __init__(self, channelmgr, ntor):
  94. self.channelmgr = channelmgr
  95. self.ntor = ntor
  96. def received_cell(self, circhandler, cell):
  97. logging.debug("Received cell in TelescopingExtendedHandler")
  98. # Validate the SNIP
  99. dirauth.SNIP.verify(cell.snip, self.channelmgr.consensus,
  100. network.thenetwork.dirauthkeys()[0],
  101. self.channelmgr.perfstats)
  102. onionkey = cell.snip.snipdict['onionkey']
  103. idkey = cell.snip.snipdict['idkey']
  104. secret = self.ntor.verify(cell.ntor_reply, onionkey, idkey)
  105. enckey = nacl.hash.sha256(secret + b'upstream')
  106. deckey = nacl.hash.sha256(secret + b'downstream')
  107. circhandler.add_crypt_layer(enckey, deckey)
  108. circhandler.replace_celltype_handler(
  109. relay.TelescopingExtendedCircuitCell, None)
  110. circhandler.circuit_descs.append(cell.snip)
  111. # Are we done building the circuit?
  112. logging.warning("we may need another circhandler structure for snips")
  113. if len(circhandler.circuit_descs) == 3:
  114. # Yes!
  115. return
  116. nexthopidx = self.channelmgr.relaypicker.pick_weighted_relay_index()
  117. # Construct the VanillaExtendCircuitCell
  118. ntor = relay.NTor(self.channelmgr.perfstats)
  119. ntor_request = ntor.request()
  120. circextendmsg = relay.TelescopingExtendCircuitCell(
  121. nexthopidx, ntor_request)
  122. # Set up the reply handler
  123. circhandler.replace_celltype_handler(
  124. relay.TelescopingExtendedCircuitCell,
  125. TelescopingExtendedHandler(self.channelmgr, ntor))
  126. # Send the cell
  127. circhandler.send_cell(circextendmsg)
  128. class SinglePassCreatedHandler:
  129. """A handler for SinglePassCreatedCircuitCell cells."""
  130. def __init__(self, channelmgr, ntor, client_key):
  131. self.channelmgr = channelmgr
  132. self.ntor = ntor
  133. self.client_key = client_key
  134. def received_cell(self, circhandler, cell):
  135. # We should only get one relay.SinglePassCreatedCircuitCell per
  136. # circuit
  137. circhandler.replace_celltype_handler(relay.SinglePassCreatedCircuitCell, None)
  138. # The circuit always starts with the guard
  139. circhandler.circuit_descs.append(self.channelmgr.guard)
  140. # Process each layer of the message
  141. blinding_keys = []
  142. while cell is not None:
  143. lasthop = circhandler.circuit_descs[-1]
  144. if type(lasthop) is dirauth.RelayDescriptor:
  145. lasthopd = lasthop.descdict
  146. else:
  147. lasthopd = lasthop.snipdict
  148. onionkey = lasthopd["onionkey"]
  149. idkey = lasthopd["idkey"]
  150. pathselkey = lasthopd["pathselkey"]
  151. if cell.enc is None:
  152. secret = self.ntor.verify(cell.ntor_reply, onionkey, idkey)
  153. enckey = nacl.hash.sha256(secret + b'upstream')
  154. deckey = nacl.hash.sha256(secret + b'downstream')
  155. circhandler.add_crypt_layer(enckey, deckey)
  156. cell = None
  157. else:
  158. secret = self.ntor.verify(cell.ntor_reply, onionkey,
  159. idkey, b'circuit')
  160. enckey = nacl.hash.sha256(secret + b'upstream')
  161. deckey = nacl.hash.sha256(secret + b'downstream')
  162. createdkey = nacl.hash.sha256(secret + b'created')
  163. circhandler.add_crypt_layer(enckey, deckey)
  164. (snip, vrfout, nextlayer) = cell.enc.decrypt(createdkey)
  165. # Check the signature on the SNIP
  166. dirauth.SNIP.verify(snip, self.channelmgr.consensus,
  167. network.thenetwork.dirauthkeys()[0],
  168. self.channelmgr.perfstats)
  169. # TODO: compute the index, check the VRF, ensure the
  170. # SNIP is the correct one
  171. pathsel_rand, next_blindkey = relay.Sphinx.client(
  172. self.client_key, blinding_keys,
  173. onionkey, b'pathsel',
  174. nextlayer is None, self.channelmgr.perfstats)
  175. if nextlayer is not None:
  176. blinding_keys.append(next_blindkey)
  177. index = int.from_bytes(relay.VRF.check_output(pathselkey,
  178. pathsel_rand, vrfout,
  179. self.channelmgr.perfstats)[:4],
  180. 'big', signed=False)
  181. indexrange = snip.snipdict["range"]
  182. if index < indexrange[0] or index >= indexrange[1]:
  183. logging.error("Incorrect SNIP received")
  184. circhandler.circuit_descs.append(snip)
  185. cell = nextlayer
  186. class ClientChannelManager(relay.ChannelManager):
  187. """The subclass of ChannelManager for clients."""
  188. def __init__(self, myaddr, dirauthaddrs, perfstats):
  189. super().__init__(myaddr, dirauthaddrs, perfstats)
  190. self.guardaddr = None
  191. self.guard = None
  192. def get_consensus_from_fallbackrelay(self):
  193. """Download a fresh consensus from a random fallbackrelay."""
  194. fb = network.thenetwork.getfallbackrelay()
  195. logging.debug("Chose fallback %s", fb)
  196. if network.thenetwork.womode == network.WOMode.VANILLA:
  197. if self.consensus is not None and \
  198. len(self.consensus.consdict['relays']) > 0:
  199. self.send_msg(relay.RelayGetConsensusDiffMsg(), fb.netaddr)
  200. else:
  201. self.send_msg(relay.RelayGetConsensusMsg(), fb.netaddr)
  202. else:
  203. self.send_msg(relay.RelayGetConsensusMsg(), fb.netaddr)
  204. def ensure_guard_vanilla(self):
  205. """Ensure that we have a channel to a guard (Vanilla Onion
  206. Routing version)."""
  207. while True:
  208. if self.guardaddr is None:
  209. # Pick a guard from the consensus
  210. self.guard = self.relaypicker.pick_weighted_relay()
  211. self.guardaddr = self.guard.descdict['addr']
  212. self.test_guard_connection()
  213. if self.guardaddr is not None:
  214. break
  215. logging.debug('chose guard=%s', self.guardaddr)
  216. def test_guard_connection(self):
  217. # Connect to the guard
  218. try:
  219. self.get_channel_to(self.guardaddr)
  220. except network.NetNoServer:
  221. # Our guard is gone
  222. self.guardaddr = None
  223. self.guard = None
  224. def ensure_guard_walking_onions(self):
  225. """Ensure we have a channel to a guard (Walking Onions version).
  226. For the first implementation, we assume an out-of-band mechanism
  227. that just simply hands us a guard; we don't count the number of
  228. operations or bandwidth as this operation in practice occurs
  229. infrequently."""
  230. while True:
  231. if self.guardaddr is None:
  232. #randomly sample a guard
  233. logging.warning("Unimplemented! guard should be selected from any relays.")
  234. self.guard = self.relaypicker.pick_weighted_relay()
  235. # here, we have a SNIP instead of a relay descriptor
  236. self.guardaddr = self.guard.snipdict['addr']
  237. self.test_guard_connection()
  238. if self.guardaddr is not None:
  239. break
  240. # Ensure we have the current descriptor for the guard
  241. # Note that self.guard may be a RelayDescriptor or a SNIP,
  242. # depending on how we got it
  243. if type(self.guard) is dirauth.RelayDescriptor:
  244. guardepoch = self.guard.descdict["epoch"]
  245. else:
  246. guardepoch = self.guard.snipdict["epoch"]
  247. if guardepoch != network.thenetwork.getepoch():
  248. guardchannel = self.get_channel_to(self.guardaddr)
  249. guardchannel.send_msg(relay.RelayGetDescMsg())
  250. logging.debug('chose guard=%s', self.guardaddr)
  251. def ensure_guard(self):
  252. """Ensure that we have a channel to a guard."""
  253. if network.thenetwork.womode == network.WOMode.VANILLA:
  254. self.ensure_guard_vanilla()
  255. return
  256. # At this point, we are either in Telescoping or Single-Pass mode
  257. self.ensure_guard_walking_onions()
  258. def new_circuit_vanilla(self):
  259. """Create a new circuit from this client. (Vanilla Onion Routing
  260. version)"""
  261. # Get our channel to the guard
  262. guardchannel = self.get_channel_to(self.guardaddr)
  263. # Allocate a new circuit id on it
  264. circid, circhandler = guardchannel.new_circuit()
  265. # Construct the VanillaCreateCircuitMsg
  266. ntor = relay.NTor(self.perfstats)
  267. ntor_request = ntor.request()
  268. circcreatemsg = relay.VanillaCreateCircuitMsg(circid, ntor_request)
  269. # Set up the reply handler
  270. circhandler.replace_celltype_handler(
  271. relay.VanillaCreatedCircuitCell,
  272. VanillaCreatedExtendedHandler(self, ntor, self.guard))
  273. # Send the message
  274. guardchannel.send_msg(circcreatemsg)
  275. return circhandler
  276. def new_circuit_telescoping(self):
  277. """Create a new circuit from this client (Telescoping Walking Onions
  278. version). If an error occurs and the circuit is deleted from the guard
  279. channel, return None, otherwise, return the circuit handler."""
  280. # Get our channel to the guard
  281. guardchannel = self.get_channel_to(self.guardaddr)
  282. # Allocate a new circuit id on it
  283. circid, circhandler = guardchannel.new_circuit()
  284. # Construct the TelescopingCreateCircuitMsg
  285. ntor = relay.NTor(self.perfstats)
  286. ntor_request = ntor.request()
  287. circcreatemsg = relay.TelescopingCreateCircuitMsg(circid, ntor_request)
  288. # Set up the reply handler
  289. circhandler.replace_celltype_handler(
  290. relay.TelescopingCreatedCircuitCell,
  291. TelescopingCreatedHandler(self, ntor))
  292. # Send the message
  293. guardchannel.send_msg(circcreatemsg)
  294. # Check to make sure the circuit is open before sending it- if there
  295. # was an error when establishing it, the circuit could already be
  296. # closed.
  297. if not guardchannel.is_circuit_open(circid):
  298. logging.debug("Circuit was already closed, not sending bytes. circid: " + str(circid))
  299. return None
  300. # In Telescoping Walking Onions, it should never happen that the
  301. # guard and exit are the same node, as the
  302. # TelescopingExtendedHandler takes care to not pick an index for
  303. # the exit that matches the guard's range. So this test should
  304. # never trigger. In Single-Pass Walking Onions, however, the
  305. # equivalent test is needed here (but should just log a debug,
  306. # not an error, since the client cannot control the index value
  307. # selected for the exit.
  308. guard = circhandler.circuit_descs[0]
  309. if type(guard) is dirauth.RelayDescriptor:
  310. guardd = guard.descdict
  311. else:
  312. guardd = guard.snipdict
  313. if guardd["addr"] == circhandler.circuit_descs[2].snipdict["addr"]:
  314. logging.error("CIRCUIT IN A LOOP")
  315. circhandler.close()
  316. circhandler = None
  317. return circhandler
  318. def new_circuit_singlepass(self):
  319. """Create a new circuit from this client (Single-Pass Walking Onions
  320. version). If an error occurs and the circuit is deleted from the guard
  321. channel, return None, otherwise, return the circuit handler."""
  322. # Get our channel to the guard
  323. guardchannel = self.get_channel_to(self.guardaddr)
  324. # Allocate a new circuit id on it
  325. circid, circhandler = guardchannel.new_circuit()
  326. # first, create the path-selection key used for Sphinx
  327. client_pathsel_key = nacl.public.PrivateKey.generate()
  328. self.perfstats.keygens += 1
  329. # Construct the SinglePassCreateCircuitMsg
  330. ntor = relay.NTor(self.perfstats)
  331. ntor_request = ntor.request()
  332. circcreatemsg = relay.SinglePassCreateCircuitMsg(circid, ntor_request,
  333. client_pathsel_key.public_key)
  334. # Set up the reply handler
  335. circhandler.replace_celltype_handler(
  336. relay.SinglePassCreatedCircuitCell,
  337. SinglePassCreatedHandler(self, ntor, client_pathsel_key))
  338. # Send the message
  339. guardchannel.send_msg(circcreatemsg)
  340. # Check to make sure the circuit is open before sending it- if there
  341. # was an error when establishing it, the circuit could already be
  342. # closed.
  343. if not guardchannel.is_circuit_open(circid):
  344. logging.debug("Circuit was already closed, not sending bytes. circid: " + str(circid))
  345. return None
  346. # In Single-Pass Walking Onions, we need to check whether the
  347. # circuit got into a loop (guard equals exit); each node will
  348. # refuse to extend to itself, so this is the only possible loop
  349. # in a circuit of length 3
  350. guard = circhandler.circuit_descs[0]
  351. if type(guard) is dirauth.RelayDescriptor:
  352. guardd = guard.descdict
  353. else:
  354. guardd = guard.snipdict
  355. if guardd["addr"] == circhandler.circuit_descs[2].snipdict["addr"]:
  356. logging.debug("circuit in a loop")
  357. circhandler.close()
  358. circhandler = None
  359. return circhandler
  360. def new_circuit(self):
  361. """Create a new circuit from this client."""
  362. circhandler = None
  363. # If an error occured, circhandler will still be None, so we should
  364. # try again.
  365. while circhandler is None:
  366. if network.thenetwork.womode == network.WOMode.VANILLA:
  367. circhandler = self.new_circuit_vanilla()
  368. elif network.thenetwork.womode == network.WOMode.TELESCOPING:
  369. circhandler = self.new_circuit_telescoping()
  370. elif network.thenetwork.womode == network.WOMode.SINGLEPASS:
  371. circhandler = self.new_circuit_singlepass()
  372. return circhandler
  373. def received_msg(self, msg, peeraddr, channel):
  374. """Callback when a NetMsg not specific to a circuit is
  375. received."""
  376. logging.debug("Client %s received msg %s from %s" % (self.myaddr, msg, peeraddr))
  377. if isinstance(msg, relay.RelayConsensusMsg) or \
  378. isinstance(msg, relay.RelayConsensusDiffMsg):
  379. self.relaypicker = dirauth.Consensus.verify(msg.consensus,
  380. network.thenetwork.dirauthkeys(), self.perfstats)
  381. self.consensus = msg.consensus
  382. elif isinstance(msg, relay.RelayDescMsg):
  383. dirauth.RelayDescriptor.verify(msg.desc, self.perfstats)
  384. self.guard = msg.desc
  385. else:
  386. return super().received_msg(msg, peeraddr, channel)
  387. def received_cell(self, circid, cell, peeraddr, channel):
  388. """Callback with a circuit-specific cell is received."""
  389. logging.debug("Client %s received cell on circ %d: %s from %s" % (self.myaddr, circid, cell, peeraddr))
  390. if isinstance(msg, relay.CloseCell):
  391. logging.debug("Log: Client received close cell; closing circuit")
  392. # TODO close cell
  393. return super().received_cell(circid, cell, peeraddr, channel)
  394. class Client:
  395. """A class representing a Tor client."""
  396. def __init__(self, dirauthaddrs):
  397. # Get a network address for client-side use only (do not bind it
  398. # to the network)
  399. self.netaddr = network.NetAddr()
  400. self.perfstats = network.PerfStats(network.EntType.CLIENT)
  401. self.perfstats.name = "Client at %s" % self.netaddr
  402. self.perfstats.is_bootstrapping = True
  403. self.channelmgr = ClientChannelManager(self.netaddr, dirauthaddrs,
  404. self.perfstats)
  405. # Register for epoch tick notifications
  406. network.thenetwork.wantepochticks(self, True)
  407. def terminate(self):
  408. """Quit this client."""
  409. # Stop listening for epoch ticks
  410. network.thenetwork.wantepochticks(self, False)
  411. # Close relay connections
  412. self.channelmgr.terminate()
  413. def get_consensus(self):
  414. """Fetch a new consensus."""
  415. # We're going to want a new consensus from our guard. In order
  416. # to get that, we'll need a channel to our guard. In order to
  417. # get that, we'll need a guard address. In order to get that,
  418. # we'll need a consensus (uh, oh; in that case, fetch the
  419. # consensus from a fallback relay).
  420. guardaddr = self.channelmgr.guardaddr
  421. guardchannel = None
  422. if guardaddr is not None:
  423. try:
  424. guardchannel = self.channelmgr.get_channel_to(guardaddr)
  425. except network.NetNoServer:
  426. guardaddr = None
  427. if guardchannel is None:
  428. logging.debug("In bootstrapping mode")
  429. self.channelmgr.get_consensus_from_fallbackrelay()
  430. logging.debug('client consensus=%s', self.channelmgr.consensus)
  431. return
  432. if network.thenetwork.womode == network.WOMode.VANILLA:
  433. if self.channelmgr.consensus is not None and len(self.channelmgr.consensus.consdict['relays']) > 0:
  434. guardchannel.send_msg(relay.RelayGetConsensusDiffMsg())
  435. logging.debug('got consensus diff, client consensus=%s', self.channelmgr.consensus)
  436. return
  437. # At this point, we are in one of the following scenarios:
  438. # 1. This is a walking onions protocol, and the client fetches the
  439. # complete consensus each epoch
  440. # 2. This is Vanilla Onion Routing and the client doesn't have a
  441. # consensus and needs to bootstrap it.
  442. guardchannel.send_msg(relay.RelayGetConsensusMsg())
  443. logging.debug('client consensus=%s', self.channelmgr.consensus)
  444. def newepoch(self, epoch):
  445. """Callback that fires at the start of each epoch"""
  446. # We'll need a new consensus
  447. self.get_consensus()
  448. # If we don't have a guard, pick one and make a channel to it
  449. self.channelmgr.ensure_guard()
  450. if __name__ == '__main__':
  451. perfstats = network.PerfStats(network.EntType.NONE)
  452. totsent = 0
  453. totrecv = 0
  454. dirasent = 0
  455. dirarecv = 0
  456. relaysent = 0
  457. relayrecv = 0
  458. clisent = 0
  459. clirecv = 0
  460. if len(sys.argv) < 3:
  461. print("Must pass in network mode and snip auth mode!")
  462. print("Network options are vanilla, telescoping, or single-pass.")
  463. print("SNIP auth options are merkle or threshold.")
  464. sys.exit(0)
  465. logging.basicConfig(level=logging.DEBUG)
  466. womode = network.WOMode[sys.argv[1].upper()]
  467. snipauthmode = network.SNIPAuthMode[sys.argv[2].upper()]
  468. network.thenetwork.set_wo_style(womode, snipauthmode)
  469. # Initialize the (non-cryptographic) random seed
  470. random.seed(1)
  471. # Start some dirauths
  472. numdirauths = 9
  473. dirauthaddrs = []
  474. dirauths = []
  475. for i in range(numdirauths):
  476. dira = dirauth.DirAuth(i, numdirauths)
  477. dirauths.append(dira)
  478. dirauthaddrs.append(dira.netaddr)
  479. # Start some relays
  480. numrelays = 100
  481. relays = []
  482. for i in range(numrelays):
  483. # Relay bandwidths (at least the ones fast enough to get used)
  484. # in the live Tor network (as of Dec 2019) are well approximated
  485. # by (200000-(200000-25000)/3*log10(x)) where x is a
  486. # uniform integer in [1,2500]
  487. x = random.randint(1,2500)
  488. bw = int(200000-(200000-25000)/3*math.log10(x))
  489. relays.append(relay.Relay(dirauthaddrs, bw, 0))
  490. # The fallback relays are a hardcoded list of about 5% of the
  491. # relays, used by clients for bootstrapping
  492. numfallbackrelays = int(numrelays * 0.05) + 1
  493. fallbackrelays = random.sample(relays, numfallbackrelays)
  494. for r in fallbackrelays:
  495. r.set_is_fallbackrelay()
  496. network.thenetwork.setfallbackrelays(fallbackrelays)
  497. # Tick the epoch
  498. network.thenetwork.nextepoch()
  499. dirauth.Consensus.verify(dirauth.DirAuth.consensus, network.thenetwork.dirauthkeys(), perfstats)
  500. print('ticked; epoch=', network.thenetwork.getepoch())
  501. relays[3].channelmgr.send_msg(relay.RelayRandomHopMsg(30), relays[5].netaddr)
  502. # See what channels exist and do a consistency check
  503. for r in relays:
  504. print("%s: %s" % (r.netaddr, [ str(k) for k in r.channelmgr.channels.keys()]))
  505. raddr = r.netaddr
  506. for ad, ch in r.channelmgr.channels.items():
  507. if ch.peer.channelmgr.myaddr != ad:
  508. print('address mismatch:', raddr, ad, ch.peer.channelmgr.myaddr)
  509. if ch.peer.channelmgr.channels[raddr].peer is not ch:
  510. print('asymmetry:', raddr, ad, ch, ch.peer.channelmgr.channels[raddr].peer)
  511. # Start some clients
  512. numclients = 1
  513. clients = []
  514. for i in range(numclients):
  515. clients.append(Client(dirauthaddrs))
  516. # Tick the epoch
  517. network.thenetwork.nextepoch()
  518. # See what channels exist and do a consistency check
  519. for c in clients:
  520. print("%s: %s" % (c.netaddr, [ str(k) for k in c.channelmgr.channels.keys()]))
  521. caddr = c.netaddr
  522. for ad, ch in c.channelmgr.channels.items():
  523. if ch.peer.channelmgr.myaddr != ad:
  524. print('address mismatch:', caddr, ad, ch.peer.channelmgr.myaddr)
  525. if ch.peer.channelmgr.channels[caddr].peer is not ch:
  526. print('asymmetry:', caddr, ad, ch, ch.peer.channelmgr.channels[caddr].peer)
  527. # Pick a bunch of bw-weighted random relays and look at the
  528. # distribution
  529. for i in range(100):
  530. r = relays[0].channelmgr.relaypicker.pick_weighted_relay()
  531. if network.thenetwork.womode == network.WOMode.VANILLA:
  532. print("relay",r.descdict["addr"])
  533. else:
  534. print("relay",r.snipdict["addr"])
  535. relays[3].terminate()
  536. relaysent += relays[3].perfstats.bytes_sent
  537. relayrecv += relays[3].perfstats.bytes_received
  538. del relays[3]
  539. # Tick the epoch
  540. network.thenetwork.nextepoch()
  541. circs = []
  542. for i in range(20):
  543. circ = clients[0].channelmgr.new_circuit()
  544. if circ is None:
  545. sys.exit("ERR: Client unable to create circuits")
  546. circs.append(circ)
  547. circ.send_cell(relay.StringCell("hello world circuit %d" % i))
  548. # Tick the epoch
  549. network.thenetwork.nextepoch()
  550. # See what channels exist and do a consistency check
  551. for r in relays:
  552. print("%s: %s" % (r.netaddr, [ str(k) + str([ck for ck in r.channelmgr.channels[k].circuithandlers.keys()]) for k in r.channelmgr.channels.keys()]))
  553. raddr = r.netaddr
  554. for ad, ch in r.channelmgr.channels.items():
  555. if ch.peer.channelmgr.myaddr != ad:
  556. print('address mismatch:', raddr, ad, ch.peer.channelmgr.myaddr)
  557. if ch.peer.channelmgr.channels[raddr].peer is not ch:
  558. print('asymmetry:', raddr, ad, ch, ch.peer.channelmgr.channels[raddr].peer)
  559. # See what channels exist and do a consistency check
  560. for c in clients:
  561. print("%s: %s" % (c.netaddr, [ str(k) + str([ck for ck in c.channelmgr.channels[k].circuithandlers.keys()]) for k in c.channelmgr.channels.keys()]))
  562. caddr = c.netaddr
  563. for ad, ch in c.channelmgr.channels.items():
  564. if ch.peer.channelmgr.myaddr != ad:
  565. print('address mismatch:', caddr, ad, ch.peer.channelmgr.myaddr)
  566. if ch.peer.channelmgr.channels[caddr].peer is not ch:
  567. print('asymmetry:', caddr, ad, ch, ch.peer.channelmgr.channels[caddr].peer)
  568. if ch.circuithandlers.keys() != \
  569. ch.peer.channelmgr.channels[caddr].circuithandlers.keys():
  570. print('circuit asymmetry:', caddr, ad, ch.peer.channelmgr.myaddr)
  571. for c in circs:
  572. c.close()
  573. for d in dirauths:
  574. print(d.perfstats)
  575. dirasent += d.perfstats.bytes_sent
  576. dirarecv += d.perfstats.bytes_received
  577. print("DirAuths sent=%s recv=%s" % (dirasent, dirarecv))
  578. totsent += dirasent
  579. totrecv += dirarecv
  580. for r in relays:
  581. print(r.perfstats)
  582. relaysent += r.perfstats.bytes_sent
  583. relayrecv += r.perfstats.bytes_received
  584. print("Relays sent=%s recv=%s" % (relaysent, relayrecv))
  585. totsent += relaysent
  586. totrecv += relayrecv
  587. for c in clients:
  588. print(c.perfstats)
  589. clisent += c.perfstats.bytes_sent
  590. clirecv += c.perfstats.bytes_received
  591. print("Client sent=%s recv=%s" % (clisent, clirecv))
  592. totsent += clisent
  593. totrecv += clirecv
  594. print("Total sent=%s recv=%s" % (totsent, totrecv))