relay.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #!/usr/bin/env python3
  2. import random # For simulation, not cryptography!
  3. import math
  4. import nacl.utils
  5. import nacl.signing
  6. import nacl.public
  7. import network
  8. import dirauth
  9. class RelayNetMsg(network.NetMsg):
  10. """The subclass of NetMsg for messages between relays and either
  11. relays or clients."""
  12. class RelayGetConsensusMsg(RelayNetMsg):
  13. """The subclass of RelayNetMsg for fetching the consensus."""
  14. class RelayConsensusMsg(RelayNetMsg):
  15. """The subclass of RelayNetMsg for returning the consensus."""
  16. def __init__(self, consensus):
  17. self.consensus = consensus
  18. class RelayRandomHopMsg(RelayNetMsg):
  19. """A message used for testing, that hops from relay to relay
  20. randomly until its TTL expires."""
  21. def __init__(self, ttl):
  22. self.ttl = ttl
  23. def __str__(self):
  24. return "RandomHop TTL=%d" % self.ttl
  25. class VanillaCreateCircuitMsg(RelayNetMsg):
  26. """The message for requesting circuit creation in Vanilla Onion
  27. Routing."""
  28. def __init__(self, circid, ntor_request):
  29. self.circid = circid
  30. self.ntor_request = ntor_request
  31. class VanillaCreatedCircuitMsg(RelayNetMsg):
  32. """The message for responding to circuit creation in Vanilla Onion
  33. Routing."""
  34. def __init__(self, circid, ntor_response):
  35. self.circid = circid
  36. self.ntor_response = ntor_response
  37. class CircuitCellMsg(RelayNetMsg):
  38. """Send a message tagged with a circuit id."""
  39. def __init__(self, circuitid, cell):
  40. self.circid = circuitid
  41. self.cell = cell
  42. def __str__(self):
  43. return "C%d:%s" % (self.circid, self.cell)
  44. class RelayFallbackTerminationError(Exception):
  45. """An exception raised when someone tries to terminate a fallback
  46. relay."""
  47. class CircuitHandler:
  48. """A class for managing sending and receiving encrypted cells on a
  49. particular circuit."""
  50. def __init__(self, channel, circid):
  51. self.channel = channel
  52. self.circid = circid
  53. self.send_cell = self.channel_send_cell
  54. self.received_cell = self.channel_received_cell
  55. def channel_send_cell(self, cell):
  56. """Send a cell on this circuit."""
  57. self.channel.send_msg(CircuitCellMsg(self.circid, cell))
  58. def channel_received_cell(self, cell, peeraddr, peer):
  59. """A cell has been received on this circuit. Forward it to the
  60. channel's received_cell callback."""
  61. self.channel.cellhandler.received_cell(self.circid, cell, peeraddr, peer)
  62. class Channel(network.Connection):
  63. """A class representing a channel between a relay and either a
  64. client or a relay, transporting cells from various circuits."""
  65. def __init__(self):
  66. super().__init__()
  67. # The CellRelay managing this Channel
  68. self.cellhandler = None
  69. # The Channel at the other end
  70. self.peer = None
  71. # The function to call when the connection closes
  72. self.closer = lambda: 0
  73. # The next circuit id to use on this channel. The party that
  74. # opened the channel uses even numbers; the receiving party uses
  75. # odd numbers.
  76. self.next_circid = None
  77. # A map for CircuitHandlers to use for each open circuit on the
  78. # channel
  79. self.circuithandlers = dict()
  80. def closed(self):
  81. self.closer()
  82. self.peer = None
  83. def close(self):
  84. if self.peer is not None and self.peer is not self:
  85. self.peer.closed()
  86. self.closed()
  87. def new_circuit(self):
  88. """Allocate a new circuit on this channel, returning the new
  89. circuit's id."""
  90. circid = self.next_circid
  91. self.next_circid += 2
  92. self.circuithandlers[circid] = CircuitHandler(self, circid)
  93. return circid
  94. def new_circuit_with_circid(self, circid):
  95. """Allocate a new circuit on this channel, with the circuit id
  96. received from our peer."""
  97. self.circuithandlers[circid] = CircuitHandler(self, circid)
  98. def send_cell(self, circid, cell):
  99. """Send the given message on the given circuit, encrypting or
  100. decrypting as needed."""
  101. self.circuithandlers[circid].send_cell(cell)
  102. def send_raw_cell(self, circid, cell):
  103. """Send the given message, tagged for the given circuit id. No
  104. encryption or decryption is done."""
  105. self.send_msg(CircuitCellMsg(self.circid, self.cell))
  106. def send_msg(self, msg):
  107. """Send the given NetMsg on the channel."""
  108. self.peer.received(self.cellhandler.myaddr, msg)
  109. def received(self, peeraddr, msg):
  110. """Callback when a message is received from the network."""
  111. if isinstance(msg, CircuitCellMsg):
  112. circid, cell = msg.circid, msg.cell
  113. self.circuithandlers[circid].received_cell(cell, peeraddr, self.peer)
  114. else:
  115. self.cellhandler.received_msg(msg, peeraddr, self.peer)
  116. class CellHandler:
  117. """The class that manages the channels to other relays and clients.
  118. Relays and clients both use subclasses of this class to both create
  119. on-demand channels to relays, to gracefully handle the closing of
  120. channels, and to handle commands received over the channels."""
  121. def __init__(self, myaddr, dirauthaddrs, perfstats):
  122. # A dictionary of Channels to other hosts, indexed by NetAddr
  123. self.channels = dict()
  124. self.myaddr = myaddr
  125. self.dirauthaddrs = dirauthaddrs
  126. self.consensus = None
  127. self.perfstats = perfstats
  128. def terminate(self):
  129. """Close all connections we're managing."""
  130. while self.channels:
  131. channelitems = iter(self.channels.items())
  132. addr, channel = next(channelitems)
  133. print('closing channel', addr, channel)
  134. channel.close()
  135. def add_channel(self, channel, peeraddr):
  136. """Add the given channel to the list of channels we are
  137. managing. If we are already managing a channel to the same
  138. peer, close it first."""
  139. if peeraddr in self.channels:
  140. self.channels[peeraddr].close()
  141. channel.cellhandler = self
  142. self.channels[peeraddr] = channel
  143. channel.closer = lambda: self.channels.pop(peeraddr)
  144. def get_channel_to(self, addr):
  145. """Get the Channel connected to the given NetAddr, creating one
  146. if none exists right now."""
  147. if addr in self.channels:
  148. return self.channels[addr]
  149. # Create the new channel
  150. newchannel = network.thenetwork.connect(self.myaddr, addr, \
  151. self.perfstats)
  152. self.channels[addr] = newchannel
  153. newchannel.closer = lambda: self.channels.pop(addr)
  154. newchannel.cellhandler = self
  155. return newchannel
  156. def received_msg(self, msg, peeraddr, peer):
  157. """Callback when a NetMsg not specific to a circuit is
  158. received."""
  159. print("CellHandler: Node %s received msg %s from %s" % (self.myaddr, msg, peeraddr))
  160. def received_cell(self, circid, cell, peeraddr, peer):
  161. """Callback with a circuit-specific cell is received."""
  162. print("CellHandler: Node %s received cell on circ %d: %s from %s" % (self.myaddr, circid, cell, peeraddr))
  163. def send_msg(self, msg, peeraddr):
  164. """Send a message to the peer with the given address."""
  165. channel = self.get_channel_to(peeraddr)
  166. channel.send_msg(msg)
  167. def send_cell(self, circid, cell, peeraddr):
  168. """Send a cell on the given circuit to the peer with the given
  169. address."""
  170. channel = self.get_channel_to(peeraddr)
  171. channel.send_cell(circid, cell)
  172. class CellRelay(CellHandler):
  173. """The subclass of CellHandler for relays."""
  174. def __init__(self, myaddr, dirauthaddrs, perfstats):
  175. super().__init__(myaddr, dirauthaddrs, perfstats)
  176. def get_consensus(self):
  177. """Download a fresh consensus from a random dirauth."""
  178. a = random.choice(self.dirauthaddrs)
  179. c = network.thenetwork.connect(self, a, self.perfstats)
  180. self.consensus = c.getconsensus()
  181. dirauth.Consensus.verify(self.consensus, \
  182. network.thenetwork.dirauthkeys(), self.perfstats)
  183. c.close()
  184. def received_msg(self, msg, peeraddr, peer):
  185. """Callback when a NetMsg not specific to a circuit is
  186. received."""
  187. print("CellRelay: Node %s received msg %s from %s" % (self.myaddr, msg, peeraddr))
  188. if isinstance(msg, RelayRandomHopMsg):
  189. if msg.ttl > 0:
  190. # Pick a random next hop from the consensus
  191. nexthop = random.choice(self.consensus.consdict['relays'])
  192. nextaddr = nexthop.descdict['addr']
  193. self.send_msg(RelayRandomHopMsg(msg.ttl-1), nextaddr)
  194. elif isinstance(msg, RelayGetConsensusMsg):
  195. self.send_msg(RelayConsensusMsg(self.consensus), peeraddr)
  196. else:
  197. return super().received_msg(msg, peeraddr, peer)
  198. def received_cell(self, circid, cell, peeraddr, peer):
  199. """Callback with a circuit-specific cell is received."""
  200. print("CellRelay: Node %s received cell on circ %d: %s from %s" % (self.myaddr, circid, cell, peeraddr))
  201. return super().received_cell(circid, cell, peeraddr, peer)
  202. class Relay(network.Server):
  203. """The class representing an onion relay."""
  204. def __init__(self, dirauthaddrs, bw, flags):
  205. # Gather performance statistics
  206. self.perfstats = network.PerfStats(network.EntType.RELAY)
  207. self.perfstats.is_bootstrapping = True
  208. # Create the identity and onion keys
  209. self.idkey = nacl.signing.SigningKey.generate()
  210. self.onionkey = nacl.public.PrivateKey.generate()
  211. self.perfstats.keygens += 2
  212. self.name = self.idkey.verify_key.encode(encoder=nacl.encoding.HexEncoder).decode("ascii")
  213. # Bind to the network to get a network address
  214. self.netaddr = network.thenetwork.bind(self)
  215. self.perfstats.name = "Relay at %s" % self.netaddr
  216. # Our bandwidth and flags
  217. self.bw = bw
  218. self.flags = flags
  219. # Register for epoch change notification
  220. network.thenetwork.wantepochticks(self, True, end=True)
  221. network.thenetwork.wantepochticks(self, True)
  222. # Create the CellRelay connection manager
  223. self.cellhandler = CellRelay(self.netaddr, dirauthaddrs, self.perfstats)
  224. # Initially, we're not a fallback relay
  225. self.is_fallbackrelay = False
  226. self.uploaddesc()
  227. def terminate(self):
  228. """Stop this relay."""
  229. if self.is_fallbackrelay:
  230. # Fallback relays must not (for now) terminate
  231. raise RelayFallbackTerminationError(self)
  232. # Stop listening for epoch ticks
  233. network.thenetwork.wantepochticks(self, False, end=True)
  234. network.thenetwork.wantepochticks(self, False)
  235. # Tell the dirauths we're going away
  236. self.uploaddesc(False)
  237. # Close connections to other relays
  238. self.cellhandler.terminate()
  239. # Stop listening to our own bound port
  240. self.close()
  241. def set_is_fallbackrelay(self, isfallback = True):
  242. """Set this relay to be a fallback relay (or unset if passed
  243. False)."""
  244. self.is_fallbackrelay = isfallback
  245. def epoch_ending(self, epoch):
  246. # Download the new consensus, which will have been created
  247. # already since the dirauths' epoch_ending callbacks happened
  248. # before the relays'.
  249. self.cellhandler.get_consensus()
  250. def newepoch(self, epoch):
  251. self.uploaddesc()
  252. def uploaddesc(self, upload=True):
  253. # Upload the descriptor for the epoch to come, or delete a
  254. # previous upload if upload=False
  255. descdict = dict();
  256. descdict["epoch"] = network.thenetwork.getepoch() + 1
  257. descdict["idkey"] = self.idkey.verify_key
  258. descdict["onionkey"] = self.onionkey.public_key
  259. descdict["addr"] = self.netaddr
  260. descdict["bw"] = self.bw
  261. descdict["flags"] = self.flags
  262. desc = dirauth.RelayDescriptor(descdict)
  263. desc.sign(self.idkey, self.perfstats)
  264. dirauth.RelayDescriptor.verify(desc, self.perfstats)
  265. if upload:
  266. descmsg = dirauth.DirAuthUploadDescMsg(desc)
  267. else:
  268. # Note that this relies on signatures being deterministic;
  269. # otherwise we'd need to save the descriptor we uploaded
  270. # before so we could tell the airauths to delete the exact
  271. # one
  272. descmsg = dirauth.DirAuthDelDescMsg(desc)
  273. # Upload them
  274. for a in self.cellhandler.dirauthaddrs:
  275. c = network.thenetwork.connect(self, a, self.perfstats)
  276. c.sendmsg(descmsg)
  277. c.close()
  278. def connected(self, peer):
  279. """Callback invoked when someone (client or relay) connects to
  280. us. Create a pair of linked Channels and return the peer half
  281. to the peer."""
  282. # Create the linked pair
  283. if peer is self.netaddr:
  284. # A self-loop? We'll allow it.
  285. peerchannel = Channel()
  286. peerchannel.peer = peerchannel
  287. peerchannel.next_circid = 2
  288. return peerchannel
  289. peerchannel = Channel()
  290. ourchannel = Channel()
  291. peerchannel.peer = ourchannel
  292. peerchannel.next_circid = 2
  293. ourchannel.peer = peerchannel
  294. ourchannel.next_circid = 1
  295. # Add our channel to the CellRelay
  296. self.cellhandler.add_channel(ourchannel, peer)
  297. return peerchannel
  298. if __name__ == '__main__':
  299. perfstats = network.PerfStats(network.EntType.NONE)
  300. # Start some dirauths
  301. numdirauths = 9
  302. dirauthaddrs = []
  303. for i in range(numdirauths):
  304. dira = dirauth.DirAuth(i, numdirauths)
  305. dirauthaddrs.append(dira.netaddr)
  306. # Start some relays
  307. numrelays = 10
  308. relays = []
  309. for i in range(numrelays):
  310. # Relay bandwidths (at least the ones fast enough to get used)
  311. # in the live Tor network (as of Dec 2019) are well approximated
  312. # by (200000-(200000-25000)/3*log10(x)) where x is a
  313. # uniform integer in [1,2500]
  314. x = random.randint(1,2500)
  315. bw = int(200000-(200000-25000)/3*math.log10(x))
  316. relays.append(Relay(dirauthaddrs, bw, 0))
  317. # The fallback relays are a hardcoded list of about 5% of the
  318. # relays, used by clients for bootstrapping
  319. numfallbackrelays = int(numrelays * 0.05) + 1
  320. fallbackrelays = random.sample(relays, numfallbackrelays)
  321. for r in fallbackrelays:
  322. r.set_is_fallbackrelay()
  323. network.thenetwork.setfallbackrelays(fallbackrelays)
  324. # Tick the epoch
  325. network.thenetwork.nextepoch()
  326. dirauth.Consensus.verify(dirauth.DirAuth.consensus, \
  327. network.thenetwork.dirauthkeys(), perfstats)
  328. print('ticked; epoch=', network.thenetwork.getepoch())
  329. relays[3].cellhandler.send_msg(RelayRandomHopMsg(30), relays[5].netaddr)
  330. # See what channels exist and do a consistency check
  331. for r in relays:
  332. print("%s: %s" % (r.netaddr, [ str(k) for k in r.cellhandler.channels.keys()]))
  333. raddr = r.netaddr
  334. for ad, ch in r.cellhandler.channels.items():
  335. if ch.peer.cellhandler.myaddr != ad:
  336. print('address mismatch:', raddr, ad, ch.peer.cellhandler.myaddr)
  337. if ch.peer.cellhandler.channels[raddr].peer is not ch:
  338. print('asymmetry:', raddr, ad, ch, ch.peer.cellhandler.channels[raddr].peer)
  339. # Stop some relays
  340. relays[3].terminate()
  341. del relays[3]
  342. relays[5].terminate()
  343. del relays[5]
  344. relays[7].terminate()
  345. del relays[7]
  346. # Tick the epoch
  347. network.thenetwork.nextepoch()
  348. print(dirauth.DirAuth.consensus)
  349. # See what channels exist and do a consistency check
  350. for r in relays:
  351. print("%s: %s" % (r.netaddr, [ str(k) for k in r.cellhandler.channels.keys()]))
  352. raddr = r.netaddr
  353. for ad, ch in r.cellhandler.channels.items():
  354. if ch.peer.cellhandler.myaddr != ad:
  355. print('address mismatch:', raddr, ad, ch.peer.cellhandler.myaddr)
  356. if ch.peer.cellhandler.channels[raddr].peer is not ch:
  357. print('asymmetry:', raddr, ad, ch, ch.peer.cellhandler.channels[raddr].peer)
  358. channel = relays[3].cellhandler.get_channel_to(relays[5].netaddr)
  359. circid = channel.new_circuit()
  360. peerchannel = relays[5].cellhandler.get_channel_to(relays[3].netaddr)
  361. peerchannel.new_circuit_with_circid(circid)
  362. relays[3].cellhandler.send_cell(circid, network.StringNetMsg("test"), relays[5].netaddr)