1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #!/usr/bin/env python3
- import nacl.utils
- import nacl.signing
- import nacl.public
- import network
- import dirauth
- class Relay(network.Server):
- """The class representing an onion relay."""
- def __init__(self, dirauthaddrs, bw, flags):
- self.consensus = None
- self.dirauthaddrs = dirauthaddrs
- # Create the identity and onion keys
- self.idkey = nacl.signing.SigningKey.generate()
- self.onionkey = nacl.public.PrivateKey.generate()
- # Bind to the network to get a network address
- self.netaddr = network.thenetwork.bind(self)
- # Our bandwidth and flags
- self.bw = bw
- self.flags = flags
- # Register for epoch change notification
- network.thenetwork.wantepochticks(self, True)
- self.uploaddesc()
- def newepoch(self, epoch):
- self.uploaddesc()
- def uploaddesc(self):
- # Upload the descriptor for the epoch to come
- descdict = dict();
- descdict["epoch"] = network.thenetwork.getepoch() + 1
- descdict["idkey"] = self.idkey.verify_key
- descdict["onionkey"] = self.onionkey.public_key
- descdict["addr"] = self.netaddr
- descdict["bw"] = self.bw
- descdict["flags"] = self.flags
- desc = dirauth.RelayDescriptor(descdict)
- desc.sign(self.idkey)
- desc.verify()
- descmsg = dirauth.DirAuthUploadDescMsg(desc)
- # Upload them
- for a in self.dirauthaddrs:
- c = network.thenetwork.connect(self, a)
- c.sendmsg(descmsg)
- c.close()
- if __name__ == '__main__':
- # Start some dirauths
- numdirauths = 9
- dirauthaddrs = []
- for i in range(numdirauths):
- dira = dirauth.DirAuth(i, numdirauths)
- dirauthaddrs.append(network.thenetwork.bind(dira))
- # Start some relays
- numrelays = 10
- for i in range(numrelays):
- Relay(dirauthaddrs, 500, 0)
|