client.py 15 KB

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