client.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #!/usr/bin/env python3
  2. import random # For simulation, not cryptography!
  3. import math
  4. import network
  5. import dirauth
  6. import relay
  7. import nacl.hash
  8. class VanillaCreatedExtendedHandler:
  9. """A handler for VanillaCreatedCircuitCell and
  10. VanillaExtendedCircuitCell cells."""
  11. def __init__(self, channelmgr, ntor, expecteddesc):
  12. self.channelmgr = channelmgr
  13. self.ntor = ntor
  14. self.expecteddesc = expecteddesc
  15. self.onionkey = expecteddesc.descdict['onionkey']
  16. self.idkey = expecteddesc.descdict['idkey']
  17. def received_cell(self, circhandler, cell):
  18. secret = self.ntor.verify(cell.ntor_reply, self.onionkey, self.idkey)
  19. enckey = nacl.hash.sha256(secret + b'upstream')
  20. deckey = nacl.hash.sha256(secret + b'downstream')
  21. circhandler.add_crypt_layer(enckey, deckey)
  22. if len(circhandler.circuit_descs) == 0:
  23. # This was a VanillaCreatedCircuitCell
  24. circhandler.replace_celltype_handler(
  25. relay.VanillaCreatedCircuitCell, None)
  26. else:
  27. # This was a VanillaExtendedCircuitCell
  28. circhandler.replace_celltype_handler(
  29. relay.VanillaExtendedCircuitCell, None)
  30. circhandler.circuit_descs.append(self.expecteddesc)
  31. # Are we done building the circuit?
  32. if len(circhandler.circuit_descs) == 3:
  33. # Yes!
  34. return
  35. nexthop = None
  36. while nexthop is None:
  37. nexthop = self.channelmgr.relaypicker.pick_weighted_relay()
  38. if nexthop.descdict['addr'] in \
  39. [ desc.descdict['addr'] \
  40. for desc in circhandler.circuit_descs ]:
  41. nexthop = None
  42. # Construct the VanillaExtendCircuitCell
  43. ntor = relay.NTor(self.channelmgr.perfstats)
  44. ntor_request = ntor.request()
  45. circextendmsg = relay.VanillaExtendCircuitCell(
  46. nexthop.descdict['addr'], ntor_request)
  47. # Set up the reply handler
  48. circhandler.replace_celltype_handler(
  49. relay.VanillaExtendedCircuitCell,
  50. VanillaCreatedExtendedHandler(self.channelmgr, ntor, nexthop))
  51. # Send the cell
  52. circhandler.send_cell(circextendmsg)
  53. class ClientChannelManager(relay.ChannelManager):
  54. """The subclass of ChannelManager for clients."""
  55. def __init__(self, myaddr, dirauthaddrs, perfstats):
  56. super().__init__(myaddr, dirauthaddrs, perfstats)
  57. self.guardaddr = None
  58. self.guard = None
  59. def get_consensus_from_fallbackrelay(self):
  60. """Download a fresh consensus from a random fallbackrelay."""
  61. fb = random.choice(network.thenetwork.getfallbackrelays())
  62. if network.thenetwork.womode == network.WOMode.VANILLA:
  63. if self.consensus is not None and \
  64. len(self.consensus.consdict['relays']) > 0:
  65. self.send_msg(relay.RelayGetConsensusDiffMsg(), fb.netaddr)
  66. else:
  67. self.send_msg(relay.RelayGetConsensusMsg(), fb.netaddr)
  68. else:
  69. self.send_msg(relay.RelayGetConsensusMsg(), fb.netaddr)
  70. def ensure_guard_vanilla(self):
  71. """Ensure that we have a channel to a guard (Vanilla Onion
  72. Routing version)."""
  73. while True:
  74. if self.guardaddr is None:
  75. # Pick a guard from the consensus
  76. self.guard = self.relaypicker.pick_weighted_relay()
  77. self.guardaddr = self.guard.descdict['addr']
  78. # Connect to the guard
  79. try:
  80. self.get_channel_to(self.guardaddr)
  81. except network.NetNoServer:
  82. # Our guard is gone
  83. self.guardaddr = None
  84. self.guard = None
  85. if self.guardaddr is not None:
  86. break
  87. print('chose guard=', self.guardaddr)
  88. def ensure_guard(self):
  89. """Ensure that we have a channel to a guard."""
  90. if network.thenetwork.womode == network.WOMode.VANILLA:
  91. self.ensure_guard_vanilla()
  92. def new_circuit_vanilla(self):
  93. """Create a new circuit from this client. (Vanilla Onion Routing
  94. version)"""
  95. # Get our channel to the guard
  96. guardchannel = self.get_channel_to(self.guardaddr)
  97. # Allocate a new circuit id on it
  98. circid, circhandler = guardchannel.new_circuit()
  99. # Construct the VanillaCreateCircuitMsg
  100. ntor = relay.NTor(self.perfstats)
  101. ntor_request = ntor.request()
  102. circcreatemsg = relay.VanillaCreateCircuitMsg(circid, ntor_request)
  103. # Set up the reply handler
  104. circhandler.replace_celltype_handler(
  105. relay.VanillaCreatedCircuitCell,
  106. VanillaCreatedExtendedHandler(self, ntor, self.guard))
  107. # Send the message
  108. guardchannel.send_msg(circcreatemsg)
  109. return circhandler
  110. def new_circuit(self):
  111. """Create a new circuit from this client."""
  112. if network.thenetwork.womode == network.WOMode.VANILLA:
  113. return self.new_circuit_vanilla()
  114. def received_msg(self, msg, peeraddr, channel):
  115. """Callback when a NetMsg not specific to a circuit is
  116. received."""
  117. print("Client %s received msg %s from %s" % (self.myaddr, msg, peeraddr))
  118. if isinstance(msg, relay.RelayConsensusMsg) or \
  119. isinstance(msg, relay.RelayConsensusDiffMsg):
  120. self.relaypicker = dirauth.Consensus.verify(msg.consensus,
  121. network.thenetwork.dirauthkeys(), self.perfstats)
  122. self.consensus = msg.consensus
  123. else:
  124. return super().received_msg(msg, peeraddr, channel)
  125. def received_cell(self, circid, cell, peeraddr, channel):
  126. """Callback with a circuit-specific cell is received."""
  127. print("Client %s received cell on circ %d: %s from %s" % (self.myaddr, circid, cell, peeraddr))
  128. return super().received_cell(circid, cell, peeraddr, channel)
  129. class Client:
  130. """A class representing a Tor client."""
  131. def __init__(self, dirauthaddrs):
  132. # Get a network address for client-side use only (do not bind it
  133. # to the network)
  134. self.netaddr = network.NetAddr()
  135. self.perfstats = network.PerfStats(network.EntType.CLIENT)
  136. self.perfstats.name = "Client at %s" % self.netaddr
  137. self.perfstats.is_bootstrapping = True
  138. self.channelmgr = ClientChannelManager(self.netaddr, dirauthaddrs, self.perfstats)
  139. # Register for epoch tick notifications
  140. network.thenetwork.wantepochticks(self, True)
  141. def terminate(self):
  142. """Quit this client."""
  143. # Stop listening for epoch ticks
  144. network.thenetwork.wantepochticks(self, False)
  145. # Close relay connections
  146. self.channelmgr.terminate()
  147. def get_consensus(self):
  148. """Fetch a new consensus."""
  149. # We're going to want a new consensus from our guard. In order
  150. # to get that, we'll need a channel to our guard. In order to
  151. # get that, we'll need a guard address. In order to get that,
  152. # we'll need a consensus (uh, oh; in that case, fetch the
  153. # consensus from a fallback relay).
  154. guardaddr = self.channelmgr.guardaddr
  155. guardchannel = None
  156. if guardaddr is not None:
  157. try:
  158. guardchannel = self.channelmgr.get_channel_to(guardaddr)
  159. except network.NetNoServer:
  160. guardaddr = None
  161. if guardchannel is None:
  162. self.channelmgr.get_consensus_from_fallbackrelay()
  163. else:
  164. if self.channelmgr.consensus is not None and \
  165. len(self.channelmgr.consensus.consdict['relays']) > 0:
  166. guardchannel.send_msg(relay.RelayGetConsensusDiffMsg())
  167. else:
  168. guardchannel.send_msg(relay.RelayGetConsensusMsg())
  169. print('client consensus=', self.channelmgr.consensus)
  170. def newepoch(self, epoch):
  171. """Callback that fires at the start of each epoch"""
  172. # We'll need a new consensus
  173. self.get_consensus()
  174. # If we don't have a guard, pick one and make a channel to it
  175. self.channelmgr.ensure_guard()
  176. if __name__ == '__main__':
  177. perfstats = network.PerfStats(network.EntType.NONE)
  178. totsent = 0
  179. totrecv = 0
  180. dirasent = 0
  181. dirarecv = 0
  182. relaysent = 0
  183. relayrecv = 0
  184. clisent = 0
  185. clirecv = 0
  186. network.thenetwork.set_wo_style(network.WOMode.TELESCOPING,
  187. network.SNIPAuthMode.THRESHSIG)
  188. # Start some dirauths
  189. numdirauths = 9
  190. dirauthaddrs = []
  191. dirauths = []
  192. for i in range(numdirauths):
  193. dira = dirauth.DirAuth(i, numdirauths)
  194. dirauths.append(dira)
  195. dirauthaddrs.append(dira.netaddr)
  196. # Start some relays
  197. numrelays = 10
  198. relays = []
  199. for i in range(numrelays):
  200. # Relay bandwidths (at least the ones fast enough to get used)
  201. # in the live Tor network (as of Dec 2019) are well approximated
  202. # by (200000-(200000-25000)/3*log10(x)) where x is a
  203. # uniform integer in [1,2500]
  204. x = random.randint(1,2500)
  205. bw = int(200000-(200000-25000)/3*math.log10(x))
  206. relays.append(relay.Relay(dirauthaddrs, bw, 0))
  207. # The fallback relays are a hardcoded list of about 5% of the
  208. # relays, used by clients for bootstrapping
  209. numfallbackrelays = int(numrelays * 0.05) + 1
  210. fallbackrelays = random.sample(relays, numfallbackrelays)
  211. for r in fallbackrelays:
  212. r.set_is_fallbackrelay()
  213. network.thenetwork.setfallbackrelays(fallbackrelays)
  214. # Tick the epoch
  215. network.thenetwork.nextepoch()
  216. dirauth.Consensus.verify(dirauth.DirAuth.consensus, network.thenetwork.dirauthkeys(), perfstats)
  217. print('ticked; epoch=', network.thenetwork.getepoch())
  218. relays[3].channelmgr.send_msg(relay.RelayRandomHopMsg(30), relays[5].netaddr)
  219. # See what channels exist and do a consistency check
  220. for r in relays:
  221. print("%s: %s" % (r.netaddr, [ str(k) for k in r.channelmgr.channels.keys()]))
  222. raddr = r.netaddr
  223. for ad, ch in r.channelmgr.channels.items():
  224. if ch.peer.channelmgr.myaddr != ad:
  225. print('address mismatch:', raddr, ad, ch.peer.channelmgr.myaddr)
  226. if ch.peer.channelmgr.channels[raddr].peer is not ch:
  227. print('asymmetry:', raddr, ad, ch, ch.peer.channelmgr.channels[raddr].peer)
  228. # Start some clients
  229. numclients = 1
  230. clients = []
  231. for i in range(numclients):
  232. clients.append(Client(dirauthaddrs))
  233. # Tick the epoch
  234. network.thenetwork.nextepoch()
  235. # See what channels exist and do a consistency check
  236. for c in clients:
  237. print("%s: %s" % (c.netaddr, [ str(k) for k in c.channelmgr.channels.keys()]))
  238. caddr = c.netaddr
  239. for ad, ch in c.channelmgr.channels.items():
  240. if ch.peer.channelmgr.myaddr != ad:
  241. print('address mismatch:', caddr, ad, ch.peer.channelmgr.myaddr)
  242. if ch.peer.channelmgr.channels[caddr].peer is not ch:
  243. print('asymmetry:', caddr, ad, ch, ch.peer.channelmgr.channels[caddr].peer)
  244. # Pick a bunch of bw-weighted random relays and look at the
  245. # distribution
  246. for i in range(100):
  247. r = relays[0].channelmgr.relaypicker.pick_weighted_relay()
  248. if network.thenetwork.womode == network.WOMode.VANILLA:
  249. print("relay",r.descdict["addr"])
  250. else:
  251. print("relay",r.snipdict["addr"])
  252. relays[3].terminate()
  253. relaysent += relays[3].perfstats.bytes_sent
  254. relayrecv += relays[3].perfstats.bytes_received
  255. del relays[3]
  256. # Tick the epoch
  257. network.thenetwork.nextepoch()
  258. circs = []
  259. for i in range(20):
  260. circ = clients[0].channelmgr.new_circuit()
  261. circs.append(circ)
  262. circ.send_cell(relay.StringCell("hello world circuit %d" % i))
  263. # Tick the epoch
  264. network.thenetwork.nextepoch()
  265. # See what channels exist and do a consistency check
  266. for r in relays:
  267. 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()]))
  268. raddr = r.netaddr
  269. for ad, ch in r.channelmgr.channels.items():
  270. if ch.peer.channelmgr.myaddr != ad:
  271. print('address mismatch:', raddr, ad, ch.peer.channelmgr.myaddr)
  272. if ch.peer.channelmgr.channels[raddr].peer is not ch:
  273. print('asymmetry:', raddr, ad, ch, ch.peer.channelmgr.channels[raddr].peer)
  274. # See what channels exist and do a consistency check
  275. for c in clients:
  276. 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()]))
  277. caddr = c.netaddr
  278. for ad, ch in c.channelmgr.channels.items():
  279. if ch.peer.channelmgr.myaddr != ad:
  280. print('address mismatch:', caddr, ad, ch.peer.channelmgr.myaddr)
  281. if ch.peer.channelmgr.channels[caddr].peer is not ch:
  282. print('asymmetry:', caddr, ad, ch, ch.peer.channelmgr.channels[caddr].peer)
  283. if ch.circuithandlers.keys() != \
  284. ch.peer.channelmgr.channels[caddr].circuithandlers.keys():
  285. print('circuit asymmetry:', caddr, ad, ch.peer.channelmgr.myaddr)
  286. for c in circs:
  287. c.close()
  288. for d in dirauths:
  289. print(d.perfstats)
  290. dirasent += d.perfstats.bytes_sent
  291. dirarecv += d.perfstats.bytes_received
  292. print("DirAuths sent=%s recv=%s" % (dirasent, dirarecv))
  293. totsent += dirasent
  294. totrecv += dirarecv
  295. for r in relays:
  296. print(r.perfstats)
  297. relaysent += r.perfstats.bytes_sent
  298. relayrecv += r.perfstats.bytes_received
  299. print("Relays sent=%s recv=%s" % (relaysent, relayrecv))
  300. totsent += relaysent
  301. totrecv += relayrecv
  302. for c in clients:
  303. print(c.perfstats)
  304. clisent += c.perfstats.bytes_sent
  305. clirecv += c.perfstats.bytes_received
  306. print("Client sent=%s recv=%s" % (clisent, clirecv))
  307. totsent += clisent
  308. totrecv += clirecv
  309. print("Total sent=%s recv=%s" % (totsent, totrecv))