TorNet.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #!/usr/bin/python
  2. #
  3. # Copyright 2011 Nick Mathewson, Michael Stone
  4. #
  5. # You may do anything with this work that copyright law would normally
  6. # restrict, so long as you retain the above notice(s) and this license
  7. # in all redistributed copies and derived works. There is no warranty.
  8. from __future__ import with_statement
  9. import cgitb
  10. cgitb.enable(format="plain")
  11. import os
  12. import signal
  13. import subprocess
  14. import sys
  15. import re
  16. import errno
  17. import time
  18. import chutney.Templating
  19. def mkdir_p(d):
  20. try:
  21. os.makedirs(d)
  22. except OSError, e:
  23. if e.errno == errno.EEXIST:
  24. return
  25. raise
  26. class Node(object):
  27. ########
  28. # Users are expected to call these:
  29. def __init__(self, parent=None, **kwargs):
  30. self._parent = parent
  31. self.env = self._createEnviron(parent, kwargs)
  32. def getN(self, N):
  33. return [ Node(self) for _ in xrange(N) ]
  34. def specialize(self, **kwargs):
  35. return Node(parent=self, **kwargs)
  36. def expand(self, pat, includePath=(".",)):
  37. return chutney.Templating.Template(pat, includePath).format(self.env)
  38. #######
  39. # Users are NOT expected to call these:
  40. def _getTorrcFname(self):
  41. return self.expand("${torrc_fname}")
  42. def _createTorrcFile(self, checkOnly=False):
  43. fn_out = self._getTorrcFname()
  44. torrc_template = self._getTorrcTemplate()
  45. output = torrc_template.format(self.env)
  46. if checkOnly:
  47. return
  48. with open(fn_out, 'w') as f:
  49. f.write(output)
  50. def _getTorrcTemplate(self):
  51. template_path = self.env['torrc_template_path']
  52. return chutney.Templating.Template("$${include:$torrc}",
  53. includePath=template_path)
  54. def _getFreeVars(self):
  55. template = self._getTorrcTemplate()
  56. return template.freevars(self.env)
  57. def _createEnviron(self, parent, argdict):
  58. if parent:
  59. parentenv = parent.env
  60. else:
  61. parentenv = self._getDefaultEnviron()
  62. return TorEnviron(parentenv, **argdict)
  63. def _getDefaultEnviron(self):
  64. return _BASE_ENVIRON
  65. def _checkConfig(self, net):
  66. self._createTorrcFile(checkOnly=True)
  67. def _preConfig(self, net):
  68. self._makeDataDir()
  69. if self.env['authority']:
  70. self._genAuthorityKey()
  71. if self.env['relay']:
  72. self._genRouterKey()
  73. def _config(self, net):
  74. self._createTorrcFile()
  75. #self._createScripts()
  76. def _postConfig(self, net):
  77. #self.net.addNode(self)
  78. pass
  79. def _setnodenum(self, num):
  80. self.env['nodenum'] = num
  81. def _makeDataDir(self):
  82. datadir = self.env['dir']
  83. mkdir_p(os.path.join(datadir, 'keys'))
  84. def _genAuthorityKey(self):
  85. datadir = self.env['dir']
  86. tor_gencert = self.env['tor_gencert']
  87. lifetime = self.env['auth_cert_lifetime']
  88. idfile = os.path.join(datadir,'keys',"authority_identity_key")
  89. skfile = os.path.join(datadir,'keys',"authority_signing_key")
  90. certfile = os.path.join(datadir,'keys',"authority_certificate")
  91. addr = self.expand("${ip}:${dirport}")
  92. passphrase = self.env['auth_passphrase']
  93. if all(os.path.exists(f) for f in [idfile, skfile, certfile]):
  94. return
  95. cmdline = [
  96. tor_gencert,
  97. '--create-identity-key',
  98. '--passphrase-fd', '0',
  99. '-i', idfile,
  100. '-s', skfile,
  101. '-c', certfile,
  102. '-m', str(lifetime),
  103. '-a', addr]
  104. print "Creating identity key %s for %s with %s"%(
  105. idfile,self.env['nick']," ".join(cmdline))
  106. p = subprocess.Popen(cmdline, stdin=subprocess.PIPE)
  107. p.communicate(passphrase+"\n")
  108. assert p.returncode == 0 #XXXX BAD!
  109. def _genRouterKey(self):
  110. datadir = self.env['dir']
  111. tor = self.env['tor']
  112. idfile = os.path.join(datadir,'keys',"identity_key")
  113. cmdline = [
  114. tor,
  115. "--quiet",
  116. "--list-fingerprint",
  117. "--orport", "1",
  118. "--dirserver",
  119. "xyzzy 127.0.0.1:1 ffffffffffffffffffffffffffffffffffffffff",
  120. "--datadirectory", datadir ]
  121. p = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
  122. stdout, stderr = p.communicate()
  123. fingerprint = "".join(stdout.split()[1:])
  124. assert re.match(r'^[A-F0-9]{40}$', fingerprint)
  125. self.env['fingerprint'] = fingerprint
  126. def _getDirServerLine(self):
  127. if not self.env['authority']:
  128. return ""
  129. datadir = self.env['dir']
  130. certfile = os.path.join(datadir,'keys',"authority_certificate")
  131. v3id = None
  132. with open(certfile, 'r') as f:
  133. for line in f:
  134. if line.startswith("fingerprint"):
  135. v3id = line.split()[1].strip()
  136. break
  137. assert v3id is not None
  138. return "DirServer %s v3ident=%s orport=%s %s %s:%s %s\n" %(
  139. self.env['nick'], v3id, self.env['orport'],
  140. self.env['dirserver_flags'], self.env['ip'], self.env['dirport'],
  141. self.env['fingerprint'])
  142. ##### Controlling a node. This should probably get split into its
  143. # own class. XXXX
  144. def getPid(self):
  145. pidfile = os.path.join(self.env['dir'], 'pid')
  146. if not os.path.exists(pidfile):
  147. return None
  148. with open(pidfile, 'r') as f:
  149. return int(f.read())
  150. def isRunning(self, pid=None):
  151. if pid is None:
  152. pid = self.getPid()
  153. if pid is None:
  154. return False
  155. try:
  156. os.kill(pid, 0) # "kill 0" == "are you there?"
  157. except OSError, e:
  158. if e.errno == errno.ESRCH:
  159. return False
  160. raise
  161. # okay, so the process exists. Say "True" for now.
  162. # XXXX check if this is really tor!
  163. return True
  164. def check(self, listRunning=True, listNonRunning=False):
  165. pid = self.getPid()
  166. running = self.isRunning(pid)
  167. nick = self.env['nick']
  168. dir = self.env['dir']
  169. if running:
  170. if listRunning:
  171. print "%s is running with PID %s"%(nick,pid)
  172. return True
  173. elif os.path.exists(os.path.join(dir, "core.%s"%pid)):
  174. if listNonRunning:
  175. print "%s seems to have crashed, and left core file core.%s"%(
  176. nick,pid)
  177. return False
  178. else:
  179. if listNonRunning:
  180. print "%s is stopped"%nick
  181. return False
  182. def hup(self):
  183. pid = self.getPid()
  184. running = self.isRunning()
  185. nick = self.env['nick']
  186. if self.isRunning():
  187. print "Sending sighup to %s"%nick
  188. os.kill(pid, signal.SIGHUP)
  189. return True
  190. else:
  191. print "%s is not running"%nick
  192. return False
  193. def start(self):
  194. if self.isRunning():
  195. print "%s is already running"%self.env['nick']
  196. return
  197. torrc = self._getTorrcFname()
  198. cmdline = [
  199. self.env['tor'],
  200. "--quiet",
  201. "-f", torrc,
  202. ]
  203. p = subprocess.Popen(cmdline)
  204. # XXXX this requires that RunAsDaemon is set.
  205. p.wait()
  206. if p.returncode != 0:
  207. print "Couldn't launch %s (%s): %s"%(self.env['nick'],
  208. " ".join(cmdline),
  209. p.returncode)
  210. return False
  211. return True
  212. def stop(self, sig=signal.SIGINT):
  213. pid = self.getPid()
  214. if not self.isRunning(pid):
  215. print "%s is not running"%self.env['nick']
  216. return
  217. os.kill(pid, sig)
  218. DEFAULTS = {
  219. 'authority' : False,
  220. 'relay' : False,
  221. 'connlimit' : 60,
  222. 'net_base_dir' : 'net',
  223. 'tor' : 'tor',
  224. 'auth_cert_lifetime' : 12,
  225. 'ip' : '127.0.0.1',
  226. 'dirserver_flags' : 'no-v2',
  227. 'privnet_dir' : '.',
  228. 'torrc_fname' : '${dir}/torrc',
  229. 'orport_base' : 6000,
  230. 'dirport_base' : 7000,
  231. 'controlport_base' : 8000,
  232. 'socksport_base' : 9000,
  233. 'dirservers' : "Dirserver bleargh bad torrc file!",
  234. 'core' : True,
  235. }
  236. class TorEnviron(chutney.Templating.Environ):
  237. def __init__(self,parent=None,**kwargs):
  238. chutney.Templating.Environ.__init__(self, parent=parent, **kwargs)
  239. def _get_orport(self, my):
  240. return my['orport_base']+my['nodenum']
  241. def _get_controlport(self, my):
  242. return my['controlport_base']+my['nodenum']
  243. def _get_socksport(self, my):
  244. return my['socksport_base']+my['nodenum']
  245. def _get_dirport(self, my):
  246. return my['dirport_base']+my['nodenum']
  247. def _get_dir(self, my):
  248. return os.path.abspath(os.path.join(my['net_base_dir'],
  249. "nodes",
  250. "%03d%s"%(my['nodenum'], my['tag'])))
  251. def _get_nick(self, my):
  252. return "test%03d%s"%(my['nodenum'], my['tag'])
  253. def _get_tor_gencert(self, my):
  254. return my['tor']+"-gencert"
  255. def _get_auth_passphrase(self, my):
  256. return self['nick'] # OMG TEH SECURE!
  257. def _get_torrc_template_path(self, my):
  258. return [ os.path.join(my['privnet_dir'], 'torrc_templates') ]
  259. class Network(object):
  260. def __init__(self,defaultEnviron):
  261. self._nodes = []
  262. self._dfltEnv = defaultEnviron
  263. self._nextnodenum = 0
  264. def _addNode(self, n):
  265. n._setnodenum(self._nextnodenum)
  266. self._nextnodenum += 1
  267. self._nodes.append(n)
  268. def _checkConfig(self):
  269. for n in self._nodes:
  270. n._checkConfig(self)
  271. def configure(self):
  272. network = self
  273. dirserverlines = []
  274. self._checkConfig()
  275. # XXX don't change node names or types or count if anything is
  276. # XXX running!
  277. for n in self._nodes:
  278. n._preConfig(network)
  279. dirserverlines.append(n._getDirServerLine())
  280. self._dfltEnv['dirservers'] = "".join(dirserverlines)
  281. for n in self._nodes:
  282. n._config(network)
  283. for n in self._nodes:
  284. n._postConfig(network)
  285. def status(self):
  286. statuses = [n.check() for n in self._nodes]
  287. n_ok = len([x for x in statuses if x])
  288. print "%d/%d nodes are running"%(n_ok,len(self._nodes))
  289. def restart(self):
  290. self.stop()
  291. self.start()
  292. def start(self):
  293. print "Starting nodes"
  294. return all([n.start() for n in self._nodes])
  295. def hup(self):
  296. print "Sending SIGHUP to nodes"
  297. return all([n.hup() for n in self._nodes])
  298. def stop(self):
  299. for sig, desc in [(signal.SIGINT, "SIGINT"),
  300. (signal.SIGINT, "another SIGINT"),
  301. (signal.SIGKILL, "SIGKILL")]:
  302. print "Sending %s to nodes"%desc
  303. for n in self._nodes:
  304. if n.isRunning():
  305. n.stop(sig=sig)
  306. print "Waiting for nodes to finish."
  307. for n in xrange(15):
  308. time.sleep(1)
  309. if all(not n.isRunning() for n in self._nodes):
  310. return
  311. sys.stdout.write(".")
  312. sys.stdout.flush()
  313. for n in self._nodes:
  314. n.check(listNonRunning=False)
  315. def ConfigureNodes(nodelist):
  316. network = _THE_NETWORK
  317. for n in nodelist:
  318. network._addNode(n)
  319. def runConfigFile(verb, f):
  320. global _BASE_ENVIRON
  321. global _THE_NETWORK
  322. _BASE_ENVIRON = TorEnviron(chutney.Templating.Environ(**DEFAULTS))
  323. _THE_NETWORK = Network(_BASE_ENVIRON)
  324. _GLOBALS = dict(_BASE_ENVIRON= _BASE_ENVIRON,
  325. Node=Node,
  326. ConfigureNodes=ConfigureNodes,
  327. _THE_NETWORK=_THE_NETWORK)
  328. exec f in _GLOBALS
  329. network = _GLOBALS['_THE_NETWORK']
  330. if not hasattr(network, verb):
  331. print "I don't know how to %s. Known commands are: %s" % (
  332. verb, " ".join(x for x in dir(network) if not x.startswith("_")))
  333. return
  334. getattr(network,verb)()
  335. if __name__ == '__main__':
  336. if len(sys.argv) < 3:
  337. print "Syntax: chutney {command} {networkfile}"
  338. sys.exit(1)
  339. f = open(sys.argv[2])
  340. runConfigFile(sys.argv[1], f)