client.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. # Compute the index, check the VRF, ensure the SNIP is
  170. # 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. try:
  178. index = int.from_bytes(relay.VRF.check_output(pathselkey,
  179. pathsel_rand, vrfout,
  180. self.channelmgr.perfstats)[:4],
  181. 'big', signed=False)
  182. except ValueError as e:
  183. circhandler.close()
  184. raise ValueError(str(e.args) + str(lasthopd))
  185. indexrange = snip.snipdict["range"]
  186. if index < indexrange[0] or index >= indexrange[1]:
  187. logging.error("Incorrect SNIP received")
  188. circhandler.circuit_descs.append(snip)
  189. cell = nextlayer
  190. class ClientChannelManager(relay.ChannelManager):
  191. """The subclass of ChannelManager for clients."""
  192. def __init__(self, myaddr, dirauthaddrs, perfstats):
  193. super().__init__(myaddr, dirauthaddrs, perfstats)
  194. self.guardaddr = None
  195. self.guard = None
  196. def get_consensus_from_fallbackrelay(self):
  197. """Download a fresh consensus from a random fallbackrelay."""
  198. fb = network.thenetwork.getfallbackrelay()
  199. logging.debug("Chose fallback %s", fb)
  200. if network.thenetwork.womode == network.WOMode.VANILLA:
  201. if self.consensus is not None and \
  202. len(self.consensus.consdict['relays']) > 0:
  203. self.send_msg(relay.RelayGetConsensusDiffMsg(), fb.netaddr)
  204. else:
  205. self.send_msg(relay.RelayGetConsensusMsg(), fb.netaddr)
  206. else:
  207. self.send_msg(relay.RelayGetConsensusMsg(), fb.netaddr)
  208. def ensure_guard_vanilla(self):
  209. """Ensure that we have a channel to a guard (Vanilla Onion
  210. Routing version)."""
  211. while True:
  212. if self.guardaddr is None:
  213. # Pick a guard from the consensus
  214. self.guard = self.relaypicker.pick_weighted_relay()
  215. self.guardaddr = self.guard.descdict['addr']
  216. self.test_guard_connection()
  217. if self.guardaddr is not None:
  218. break
  219. logging.debug('chose guard=%s', self.guardaddr)
  220. def test_guard_connection(self):
  221. # Connect to the guard
  222. try:
  223. self.get_channel_to(self.guardaddr)
  224. except network.NetNoServer:
  225. # Our guard is gone
  226. self.guardaddr = None
  227. self.guard = None
  228. def ensure_guard_walking_onions(self):
  229. """Ensure we have a channel to a guard (Walking Onions version).
  230. For the first implementation, we assume an out-of-band mechanism
  231. that just simply hands us a guard; we don't count the number of
  232. operations or bandwidth as this operation in practice occurs
  233. infrequently."""
  234. while True:
  235. if self.guardaddr is None:
  236. #randomly sample a guard
  237. logging.warning("Unimplemented! guard should be selected from any relays.")
  238. self.guard = self.relaypicker.pick_weighted_relay()
  239. # here, we have a SNIP instead of a relay descriptor
  240. self.guardaddr = self.guard.snipdict['addr']
  241. self.test_guard_connection()
  242. if self.guardaddr is not None:
  243. break
  244. # Ensure we have the current descriptor for the guard
  245. # Note that self.guard may be a RelayDescriptor or a SNIP,
  246. # depending on how we got it
  247. if type(self.guard) is dirauth.RelayDescriptor:
  248. guardepoch = self.guard.descdict["epoch"]
  249. else:
  250. guardepoch = self.guard.snipdict["epoch"]
  251. if guardepoch != network.thenetwork.getepoch():
  252. guardchannel = self.get_channel_to(self.guardaddr)
  253. guardchannel.send_msg(relay.RelayGetDescMsg())
  254. logging.debug('chose guard=%s', self.guardaddr)
  255. def ensure_guard(self):
  256. """Ensure that we have a channel to a guard."""
  257. if network.thenetwork.womode == network.WOMode.VANILLA:
  258. self.ensure_guard_vanilla()
  259. return
  260. # At this point, we are either in Telescoping or Single-Pass mode
  261. self.ensure_guard_walking_onions()
  262. def new_circuit_vanilla(self):
  263. """Create a new circuit from this client. (Vanilla Onion Routing
  264. version)"""
  265. # Get our channel to the guard
  266. guardchannel = self.get_channel_to(self.guardaddr)
  267. # Allocate a new circuit id on it
  268. circid, circhandler = guardchannel.new_circuit()
  269. # Construct the VanillaCreateCircuitMsg
  270. ntor = relay.NTor(self.perfstats)
  271. ntor_request = ntor.request()
  272. circcreatemsg = relay.VanillaCreateCircuitMsg(circid, ntor_request)
  273. # Set up the reply handler
  274. circhandler.replace_celltype_handler(
  275. relay.VanillaCreatedCircuitCell,
  276. VanillaCreatedExtendedHandler(self, ntor, self.guard))
  277. # Send the message
  278. guardchannel.send_msg(circcreatemsg)
  279. return circhandler
  280. def new_circuit_telescoping(self):
  281. """Create a new circuit from this client (Telescoping Walking Onions
  282. version). If an error occurs and the circuit is deleted from the guard
  283. channel, return None, otherwise, return the circuit handler."""
  284. # Get our channel to the guard
  285. guardchannel = self.get_channel_to(self.guardaddr)
  286. # Allocate a new circuit id on it
  287. circid, circhandler = guardchannel.new_circuit()
  288. # Construct the TelescopingCreateCircuitMsg
  289. ntor = relay.NTor(self.perfstats)
  290. ntor_request = ntor.request()
  291. circcreatemsg = relay.TelescopingCreateCircuitMsg(circid, ntor_request)
  292. # Set up the reply handler
  293. circhandler.replace_celltype_handler(
  294. relay.TelescopingCreatedCircuitCell,
  295. TelescopingCreatedHandler(self, ntor))
  296. # Send the message
  297. guardchannel.send_msg(circcreatemsg)
  298. # Check to make sure the circuit is open before sending it- if there
  299. # was an error when establishing it, the circuit could already be
  300. # closed.
  301. if not guardchannel.is_circuit_open(circid):
  302. logging.debug("Circuit was already closed, not sending bytes. circid: " + str(circid))
  303. return None
  304. guard = circhandler.circuit_descs[0]
  305. if type(guard) is dirauth.RelayDescriptor:
  306. guardd = guard.descdict
  307. else:
  308. guardd = guard.snipdict
  309. if guardd["addr"] == circhandler.circuit_descs[2].snipdict["addr"]:
  310. logging.debug("circuit in a loop")
  311. circhandler.close()
  312. circhandler = None
  313. return circhandler
  314. def new_circuit_singlepass(self):
  315. """Create a new circuit from this client (Single-Pass Walking Onions
  316. version). If an error occurs and the circuit is deleted from the guard
  317. channel, return None, otherwise, return the circuit handler."""
  318. # Get our channel to the guard
  319. guardchannel = self.get_channel_to(self.guardaddr)
  320. # Allocate a new circuit id on it
  321. circid, circhandler = guardchannel.new_circuit()
  322. # first, create the path-selection key used for Sphinx
  323. client_pathsel_key = nacl.public.PrivateKey.generate()
  324. self.perfstats.keygens += 1
  325. # Construct the SinglePassCreateCircuitMsg
  326. ntor = relay.NTor(self.perfstats)
  327. ntor_request = ntor.request()
  328. circcreatemsg = relay.SinglePassCreateCircuitMsg(circid, ntor_request,
  329. client_pathsel_key.public_key)
  330. # Set up the reply handler
  331. circhandler.replace_celltype_handler(
  332. relay.SinglePassCreatedCircuitCell,
  333. SinglePassCreatedHandler(self, ntor, client_pathsel_key))
  334. # Send the message
  335. guardchannel.send_msg(circcreatemsg)
  336. # Check to make sure the circuit is open before sending it- if there
  337. # was an error when establishing it, the circuit could already be
  338. # closed.
  339. if not guardchannel.is_circuit_open(circid):
  340. logging.debug("Circuit was already closed, not sending bytes. circid: " + str(circid))
  341. return None
  342. # In Single-Pass Walking Onions, we need to check whether the
  343. # circuit got into a loop (guard equals exit); each node will
  344. # refuse to extend to itself, so this is the only possible loop
  345. # in a circuit of length 3
  346. guard = circhandler.circuit_descs[0]
  347. if type(guard) is dirauth.RelayDescriptor:
  348. guardd = guard.descdict
  349. else:
  350. guardd = guard.snipdict
  351. if guardd["addr"] == circhandler.circuit_descs[2].snipdict["addr"]:
  352. logging.debug("circuit in a loop")
  353. circhandler.close()
  354. circhandler = None
  355. return circhandler
  356. def new_circuit(self):
  357. """Create a new circuit from this client."""
  358. circhandler = None
  359. # If an error occured, circhandler will still be None, so we should
  360. # try again.
  361. while circhandler is None:
  362. if network.thenetwork.womode == network.WOMode.VANILLA:
  363. circhandler = self.new_circuit_vanilla()
  364. elif network.thenetwork.womode == network.WOMode.TELESCOPING:
  365. circhandler = self.new_circuit_telescoping()
  366. elif network.thenetwork.womode == network.WOMode.SINGLEPASS:
  367. circhandler = self.new_circuit_singlepass()
  368. return circhandler
  369. def received_msg(self, msg, peeraddr, channel):
  370. """Callback when a NetMsg not specific to a circuit is
  371. received."""
  372. logging.debug("Client %s received msg %s from %s" % (self.myaddr, msg, peeraddr))
  373. if isinstance(msg, relay.RelayConsensusMsg) or \
  374. isinstance(msg, relay.RelayConsensusDiffMsg):
  375. self.relaypicker = dirauth.Consensus.verify(msg.consensus,
  376. network.thenetwork.dirauthkeys(), self.perfstats)
  377. self.consensus = msg.consensus
  378. elif isinstance(msg, relay.RelayDescMsg):
  379. dirauth.RelayDescriptor.verify(msg.desc, self.perfstats)
  380. self.guard = msg.desc
  381. else:
  382. return super().received_msg(msg, peeraddr, channel)
  383. def received_cell(self, circid, cell, peeraddr, channel):
  384. """Callback with a circuit-specific cell is received."""
  385. logging.debug("Client %s received cell on circ %d: %s from %s" % (self.myaddr, circid, cell, peeraddr))
  386. if isinstance(msg, relay.CloseCell):
  387. logging.debug("Log: Client received close cell; closing circuit")
  388. # TODO close cell
  389. return super().received_cell(circid, cell, peeraddr, channel)
  390. class Client:
  391. """A class representing a Tor client."""
  392. def __init__(self, dirauthaddrs):
  393. # Get a network address for client-side use only (do not bind it
  394. # to the network)
  395. self.netaddr = network.NetAddr()
  396. self.perfstats = network.PerfStats(network.EntType.CLIENT)
  397. self.perfstats.name = "Client at %s" % self.netaddr
  398. self.perfstats.is_bootstrapping = True
  399. self.channelmgr = ClientChannelManager(self.netaddr, dirauthaddrs,
  400. self.perfstats)
  401. # Register for epoch tick notifications
  402. network.thenetwork.wantepochticks(self, True)
  403. def terminate(self):
  404. """Quit this client."""
  405. # Stop listening for epoch ticks
  406. network.thenetwork.wantepochticks(self, False)
  407. # Close relay connections
  408. self.channelmgr.terminate()
  409. def get_consensus(self):
  410. """Fetch a new consensus."""
  411. # We're going to want a new consensus from our guard. In order
  412. # to get that, we'll need a channel to our guard. In order to
  413. # get that, we'll need a guard address. In order to get that,
  414. # we'll need a consensus (uh, oh; in that case, fetch the
  415. # consensus from a fallback relay).
  416. guardaddr = self.channelmgr.guardaddr
  417. guardchannel = None
  418. if guardaddr is not None:
  419. try:
  420. guardchannel = self.channelmgr.get_channel_to(guardaddr)
  421. except network.NetNoServer:
  422. guardaddr = None
  423. if guardchannel is None:
  424. logging.debug("In bootstrapping mode")
  425. self.channelmgr.get_consensus_from_fallbackrelay()
  426. logging.debug('client consensus=%s', self.channelmgr.consensus)
  427. return
  428. if network.thenetwork.womode == network.WOMode.VANILLA:
  429. if self.channelmgr.consensus is not None and len(self.channelmgr.consensus.consdict['relays']) > 0:
  430. guardchannel.send_msg(relay.RelayGetConsensusDiffMsg())
  431. logging.debug('got consensus diff, client consensus=%s', self.channelmgr.consensus)
  432. return
  433. # At this point, we are in one of the following scenarios:
  434. # 1. This is a walking onions protocol, and the client fetches the
  435. # complete consensus each epoch
  436. # 2. This is Vanilla Onion Routing and the client doesn't have a
  437. # consensus and needs to bootstrap it.
  438. guardchannel.send_msg(relay.RelayGetConsensusMsg())
  439. logging.debug('client consensus=%s', self.channelmgr.consensus)
  440. def newepoch(self, epoch):
  441. """Callback that fires at the start of each epoch"""
  442. # We'll need a new consensus
  443. self.get_consensus()
  444. # If we don't have a guard, pick one and make a channel to it
  445. self.channelmgr.ensure_guard()
  446. if __name__ == '__main__':
  447. perfstats = network.PerfStats(network.EntType.NONE)
  448. totsent = 0
  449. totrecv = 0
  450. dirasent = 0
  451. dirarecv = 0
  452. relaysent = 0
  453. relayrecv = 0
  454. clisent = 0
  455. clirecv = 0
  456. if len(sys.argv) < 3:
  457. print("Must pass in network mode and snip auth mode!")
  458. print("Network options are vanilla, telescoping, or single-pass.")
  459. print("SNIP auth options are merkle or threshold.")
  460. sys.exit(0)
  461. logging.basicConfig(level=logging.DEBUG)
  462. womode = network.WOMode[sys.argv[1].upper()]
  463. snipauthmode = network.SNIPAuthMode[sys.argv[2].upper()]
  464. network.thenetwork.set_wo_style(womode, snipauthmode)
  465. # Initialize the (non-cryptographic) random seed
  466. random.seed(1)
  467. # Start some dirauths
  468. numdirauths = 9
  469. dirauthaddrs = []
  470. dirauths = []
  471. for i in range(numdirauths):
  472. dira = dirauth.DirAuth(i, numdirauths)
  473. dirauths.append(dira)
  474. dirauthaddrs.append(dira.netaddr)
  475. # Start some relays
  476. numrelays = 100
  477. relays = []
  478. for i in range(numrelays):
  479. # Relay bandwidths (at least the ones fast enough to get used)
  480. # in the live Tor network (as of Dec 2019) are well approximated
  481. # by (200000-(200000-25000)/3*log10(x)) where x is a
  482. # uniform integer in [1,2500]
  483. x = random.randint(1,2500)
  484. bw = int(200000-(200000-25000)/3*math.log10(x))
  485. relays.append(relay.Relay(dirauthaddrs, bw, 0))
  486. # The fallback relays are a hardcoded list of about 5% of the
  487. # relays, used by clients for bootstrapping
  488. numfallbackrelays = int(numrelays * 0.05) + 1
  489. fallbackrelays = random.sample(relays, numfallbackrelays)
  490. for r in fallbackrelays:
  491. r.set_is_fallbackrelay()
  492. network.thenetwork.setfallbackrelays(fallbackrelays)
  493. # Tick the epoch
  494. network.thenetwork.nextepoch()
  495. dirauth.Consensus.verify(dirauth.DirAuth.consensus, network.thenetwork.dirauthkeys(), perfstats)
  496. print('ticked; epoch=', network.thenetwork.getepoch())
  497. relays[3].channelmgr.send_msg(relay.RelayRandomHopMsg(30), relays[5].netaddr)
  498. # See what channels exist and do a consistency check
  499. for r in relays:
  500. print("%s: %s" % (r.netaddr, [ str(k) for k in r.channelmgr.channels.keys()]))
  501. raddr = r.netaddr
  502. for ad, ch in r.channelmgr.channels.items():
  503. if ch.peer.channelmgr.myaddr != ad:
  504. print('address mismatch:', raddr, ad, ch.peer.channelmgr.myaddr)
  505. if ch.peer.channelmgr.channels[raddr].peer is not ch:
  506. print('asymmetry:', raddr, ad, ch, ch.peer.channelmgr.channels[raddr].peer)
  507. # Start some clients
  508. numclients = 1
  509. clients = []
  510. for i in range(numclients):
  511. clients.append(Client(dirauthaddrs))
  512. # Tick the epoch
  513. network.thenetwork.nextepoch()
  514. # See what channels exist and do a consistency check
  515. for c in clients:
  516. print("%s: %s" % (c.netaddr, [ str(k) for k in c.channelmgr.channels.keys()]))
  517. caddr = c.netaddr
  518. for ad, ch in c.channelmgr.channels.items():
  519. if ch.peer.channelmgr.myaddr != ad:
  520. print('address mismatch:', caddr, ad, ch.peer.channelmgr.myaddr)
  521. if ch.peer.channelmgr.channels[caddr].peer is not ch:
  522. print('asymmetry:', caddr, ad, ch, ch.peer.channelmgr.channels[caddr].peer)
  523. # Pick a bunch of bw-weighted random relays and look at the
  524. # distribution
  525. for i in range(100):
  526. r = relays[0].channelmgr.relaypicker.pick_weighted_relay()
  527. if network.thenetwork.womode == network.WOMode.VANILLA:
  528. print("relay",r.descdict["addr"])
  529. else:
  530. print("relay",r.snipdict["addr"])
  531. relays[3].terminate()
  532. relaysent += relays[3].perfstats.bytes_sent
  533. relayrecv += relays[3].perfstats.bytes_received
  534. del relays[3]
  535. # Tick the epoch
  536. network.thenetwork.nextepoch()
  537. circs = []
  538. for i in range(20):
  539. circ = clients[0].channelmgr.new_circuit()
  540. if circ is None:
  541. sys.exit("ERR: Client unable to create circuits")
  542. circs.append(circ)
  543. circ.send_cell(relay.StringCell("hello world circuit %d" % i))
  544. # Tick the epoch
  545. network.thenetwork.nextepoch()
  546. # See what channels exist and do a consistency check
  547. for r in relays:
  548. 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()]))
  549. raddr = r.netaddr
  550. for ad, ch in r.channelmgr.channels.items():
  551. if ch.peer.channelmgr.myaddr != ad:
  552. print('address mismatch:', raddr, ad, ch.peer.channelmgr.myaddr)
  553. if ch.peer.channelmgr.channels[raddr].peer is not ch:
  554. print('asymmetry:', raddr, ad, ch, ch.peer.channelmgr.channels[raddr].peer)
  555. # See what channels exist and do a consistency check
  556. for c in clients:
  557. 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()]))
  558. caddr = c.netaddr
  559. for ad, ch in c.channelmgr.channels.items():
  560. if ch.peer.channelmgr.myaddr != ad:
  561. print('address mismatch:', caddr, ad, ch.peer.channelmgr.myaddr)
  562. if ch.peer.channelmgr.channels[caddr].peer is not ch:
  563. print('asymmetry:', caddr, ad, ch, ch.peer.channelmgr.channels[caddr].peer)
  564. if ch.circuithandlers.keys() != \
  565. ch.peer.channelmgr.channels[caddr].circuithandlers.keys():
  566. print('circuit asymmetry:', caddr, ad, ch.peer.channelmgr.myaddr)
  567. for c in circs:
  568. c.close()
  569. for d in dirauths:
  570. print(d.perfstats)
  571. dirasent += d.perfstats.bytes_sent
  572. dirarecv += d.perfstats.bytes_received
  573. print("DirAuths sent=%s recv=%s" % (dirasent, dirarecv))
  574. totsent += dirasent
  575. totrecv += dirarecv
  576. for r in relays:
  577. print(r.perfstats)
  578. relaysent += r.perfstats.bytes_sent
  579. relayrecv += r.perfstats.bytes_received
  580. print("Relays sent=%s recv=%s" % (relaysent, relayrecv))
  581. totsent += relaysent
  582. totrecv += relayrecv
  583. for c in clients:
  584. print(c.perfstats)
  585. clisent += c.perfstats.bytes_sent
  586. clirecv += c.perfstats.bytes_received
  587. print("Client sent=%s recv=%s" % (clisent, clirecv))
  588. totsent += clisent
  589. totrecv += clirecv
  590. print("Total sent=%s recv=%s" % (totsent, totrecv))