Config.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import argparse
  2. import sys
  3. import os
  4. import ConfigParser
  5. class Config(object):
  6. def __init__(self, argv):
  7. self.version = "0.5.0"
  8. self.rev = 1700
  9. self.argv = argv
  10. self.action = None
  11. self.config_file = "zeronet.conf"
  12. self.createParser()
  13. self.createArguments()
  14. def createParser(self):
  15. # Create parser
  16. self.parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  17. self.parser.register('type', 'bool', self.strToBool)
  18. self.subparsers = self.parser.add_subparsers(title="Action to perform", dest="action")
  19. def __str__(self):
  20. return str(self.arguments).replace("Namespace", "Config") # Using argparse str output
  21. # Convert string to bool
  22. def strToBool(self, v):
  23. return v.lower() in ("yes", "true", "t", "1")
  24. # Create command line arguments
  25. def createArguments(self):
  26. trackers = [
  27. "zero://boot3rdez4rzn36x.onion:15441",
  28. "zero://boot.zeronet.io#f36ca555bee6ba216b14d10f38c16f7769ff064e0e37d887603548cc2e64191d:15441",
  29. "udp://tracker.coppersurfer.tk:6969",
  30. "udp://tracker.leechers-paradise.org:6969",
  31. "udp://9.rarbg.com:2710",
  32. "http://tracker.tordb.ml:6881/announce",
  33. "http://explodie.org:6969/announce",
  34. "http://tracker1.wasabii.com.tw:6969/announce"
  35. ]
  36. # Platform specific
  37. if sys.platform.startswith("win"):
  38. coffeescript = "type %s | tools\\coffee\\coffee.cmd"
  39. else:
  40. coffeescript = None
  41. use_openssl = True
  42. # Main
  43. action = self.subparsers.add_parser("main", help='Start UiServer and FileServer (default)')
  44. # SiteCreate
  45. action = self.subparsers.add_parser("siteCreate", help='Create a new site')
  46. # SiteNeedFile
  47. action = self.subparsers.add_parser("siteNeedFile", help='Get a file from site')
  48. action.add_argument('address', help='Site address')
  49. action.add_argument('inner_path', help='File inner path')
  50. # SiteDownload
  51. action = self.subparsers.add_parser("siteDownload", help='Download a new site')
  52. action.add_argument('address', help='Site address')
  53. # SiteSign
  54. action = self.subparsers.add_parser("siteSign", help='Update and sign content.json: address [privatekey]')
  55. action.add_argument('address', help='Site to sign')
  56. action.add_argument('privatekey', help='Private key (default: ask on execute)', nargs='?')
  57. action.add_argument('--inner_path', help='File you want to sign (default: content.json)',
  58. default="content.json", metavar="inner_path")
  59. action.add_argument('--publish', help='Publish site after the signing', action='store_true')
  60. # SitePublish
  61. action = self.subparsers.add_parser("sitePublish", help='Publish site to other peers: address')
  62. action.add_argument('address', help='Site to publish')
  63. action.add_argument('peer_ip', help='Peer ip to publish (default: random peers ip from tracker)',
  64. default=None, nargs='?')
  65. action.add_argument('peer_port', help='Peer port to publish (default: random peer port from tracker)',
  66. default=15441, nargs='?')
  67. action.add_argument('--inner_path', help='Content.json you want to publish (default: content.json)',
  68. default="content.json", metavar="inner_path")
  69. # SiteVerify
  70. action = self.subparsers.add_parser("siteVerify", help='Verify site files using sha512: address')
  71. action.add_argument('address', help='Site to verify')
  72. # dbRebuild
  73. action = self.subparsers.add_parser("dbRebuild", help='Rebuild site database cache')
  74. action.add_argument('address', help='Site to rebuild')
  75. # dbQuery
  76. action = self.subparsers.add_parser("dbQuery", help='Query site sql cache')
  77. action.add_argument('address', help='Site to query')
  78. action.add_argument('query', help='Sql query')
  79. # PeerPing
  80. action = self.subparsers.add_parser("peerPing", help='Send Ping command to peer')
  81. action.add_argument('peer_ip', help='Peer ip')
  82. action.add_argument('peer_port', help='Peer port', nargs='?')
  83. # PeerGetFile
  84. action = self.subparsers.add_parser("peerGetFile", help='Request and print a file content from peer')
  85. action.add_argument('peer_ip', help='Peer ip')
  86. action.add_argument('peer_port', help='Peer port')
  87. action.add_argument('site', help='Site address')
  88. action.add_argument('filename', help='File name to request')
  89. action.add_argument('--benchmark', help='Request file 10x then displays the total time', action='store_true')
  90. # PeerCmd
  91. action = self.subparsers.add_parser("peerCmd", help='Request and print a file content from peer')
  92. action.add_argument('peer_ip', help='Peer ip')
  93. action.add_argument('peer_port', help='Peer port')
  94. action.add_argument('cmd', help='Command to execute')
  95. action.add_argument('parameters', help='Parameters to command', nargs='?')
  96. # CryptSign
  97. action = self.subparsers.add_parser("cryptSign", help='Sign message using Bitcoin private key')
  98. action.add_argument('message', help='Message to sign')
  99. action.add_argument('privatekey', help='Private key')
  100. # Config parameters
  101. self.parser.add_argument('--verbose', help='More detailed logging', action='store_true')
  102. self.parser.add_argument('--debug', help='Debug mode', action='store_true')
  103. self.parser.add_argument('--debug_socket', help='Debug socket connections', action='store_true')
  104. self.parser.add_argument('--debug_gevent', help='Debug gevent functions', action='store_true')
  105. self.parser.add_argument('--batch', help="Batch mode (No interactive input for commands)", action='store_true')
  106. self.parser.add_argument('--config_file', help='Path of config file', default="zeronet.conf", metavar="path")
  107. self.parser.add_argument('--data_dir', help='Path of data directory', default="data", metavar="path")
  108. self.parser.add_argument('--log_dir', help='Path of logging directory', default="log", metavar="path")
  109. self.parser.add_argument('--ui_ip', help='Web interface bind address', default="127.0.0.1", metavar='ip')
  110. self.parser.add_argument('--ui_port', help='Web interface bind port', default=43110, type=int, metavar='port')
  111. self.parser.add_argument('--ui_restrict', help='Restrict web access', default=False, metavar='ip', nargs='*')
  112. self.parser.add_argument('--open_browser', help='Open homepage in web browser automatically',
  113. nargs='?', const="default_browser", metavar='browser_name')
  114. self.parser.add_argument('--homepage', help='Web interface Homepage', default='1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D',
  115. metavar='address')
  116. self.parser.add_argument('--size_limit', help='Default site size limit in MB', default=10, type=int, metavar='size')
  117. self.parser.add_argument('--connected_limit', help='Max connected peer per site', default=10, type=int, metavar='connected_limit')
  118. self.parser.add_argument('--fileserver_ip', help='FileServer bind address', default="*", metavar='ip')
  119. self.parser.add_argument('--fileserver_port', help='FileServer bind port', default=15441, type=int, metavar='port')
  120. self.parser.add_argument('--disable_udp', help='Disable UDP connections', action='store_true')
  121. self.parser.add_argument('--proxy', help='Socks proxy address', metavar='ip:port')
  122. self.parser.add_argument('--ip_external', help='Set reported external ip (tested on start if None)', metavar='ip')
  123. self.parser.add_argument('--trackers', help='Bootstraping torrent trackers', default=trackers, metavar='protocol://address', nargs='*')
  124. self.parser.add_argument('--trackers_file', help='Load torrent trackers dynamically from a file', default=False, metavar='path')
  125. self.parser.add_argument('--use_openssl', help='Use OpenSSL liblary for speedup',
  126. type='bool', choices=[True, False], default=use_openssl)
  127. self.parser.add_argument('--disable_db', help='Disable database updating', action='store_true')
  128. self.parser.add_argument('--disable_encryption', help='Disable connection encryption', action='store_true')
  129. self.parser.add_argument('--disable_sslcompression', help='Disable SSL compression to save memory',
  130. type='bool', choices=[True, False], default=True)
  131. self.parser.add_argument('--keep_ssl_cert', help='Disable new SSL cert generation on startup', action='store_true')
  132. self.parser.add_argument('--max_files_opened', help='Change maximum opened files allowed by OS to this value on startup',
  133. default=2048, type=int, metavar='limit')
  134. self.parser.add_argument('--use_tempfiles', help='Use temporary files when downloading (experimental)',
  135. type='bool', choices=[True, False], default=False)
  136. self.parser.add_argument('--stream_downloads', help='Stream download directly to files (experimental)',
  137. type='bool', choices=[True, False], default=False)
  138. self.parser.add_argument("--msgpack_purepython", help='Use less memory, but a bit more CPU power',
  139. type='bool', choices=[True, False], default=True)
  140. self.parser.add_argument('--coffeescript_compiler', help='Coffeescript compiler for developing', default=coffeescript,
  141. metavar='executable_path')
  142. self.parser.add_argument('--tor', help='enable: Use only for Tor peers, always: Use Tor for every connection', choices=["disable", "enable", "always"], default='enable')
  143. self.parser.add_argument('--tor_controller', help='Tor controller address', metavar='ip:port', default='127.0.0.1:9051')
  144. self.parser.add_argument('--tor_proxy', help='Tor proxy address', metavar='ip:port', default='127.0.0.1:9050')
  145. self.parser.add_argument('--version', action='version', version='ZeroNet %s r%s' % (self.version, self.rev))
  146. return self.parser
  147. def loadTrackersFile(self):
  148. self.trackers = []
  149. for tracker in open(self.trackers_file):
  150. if "://" in tracker:
  151. self.trackers.append(tracker.strip())
  152. # Find arguments specified for current action
  153. def getActionArguments(self):
  154. back = {}
  155. arguments = self.parser._subparsers._group_actions[0].choices[self.action]._actions[1:] # First is --version
  156. for argument in arguments:
  157. back[argument.dest] = getattr(self, argument.dest)
  158. return back
  159. # Try to find action from argv
  160. def getAction(self, argv):
  161. actions = [action.choices.keys() for action in self.parser._actions if action.dest == "action"][0] # Valid actions
  162. found_action = False
  163. for action in actions: # See if any in argv
  164. if action in argv:
  165. found_action = action
  166. break
  167. return found_action
  168. # Move plugin parameters to end of argument list
  169. def moveUnknownToEnd(self, argv, default_action):
  170. valid_actions = sum([action.option_strings for action in self.parser._actions], [])
  171. valid_parameters = []
  172. plugin_parameters = []
  173. plugin = False
  174. for arg in argv:
  175. if arg.startswith("--"):
  176. if arg not in valid_actions:
  177. plugin = True
  178. else:
  179. plugin = False
  180. elif arg == default_action:
  181. plugin = False
  182. if plugin:
  183. plugin_parameters.append(arg)
  184. else:
  185. valid_parameters.append(arg)
  186. return valid_parameters + plugin_parameters
  187. # Parse arguments from config file and command line
  188. def parse(self, silent=False, parse_config=True):
  189. if silent: # Don't display messages or quit on unknown parameter
  190. original_print_message = self.parser._print_message
  191. original_exit = self.parser.exit
  192. def silencer(parser, function_name):
  193. parser.exited = True
  194. return None
  195. self.parser.exited = False
  196. self.parser._print_message = lambda *args, **kwargs: silencer(self.parser, "_print_message")
  197. self.parser.exit = lambda *args, **kwargs: silencer(self.parser, "exit")
  198. argv = self.argv[:] # Copy command line arguments
  199. if parse_config:
  200. argv = self.parseConfig(argv) # Add arguments from config file
  201. self.parseCommandline(argv, silent) # Parse argv
  202. self.setAttributes()
  203. if silent: # Restore original functions
  204. if self.parser.exited and self.action == "main": # Argument parsing halted, don't start ZeroNet with main action
  205. self.action = None
  206. self.parser._print_message = original_print_message
  207. self.parser.exit = original_exit
  208. # Parse command line arguments
  209. def parseCommandline(self, argv, silent=False):
  210. # Find out if action is specificed on start
  211. action = self.getAction(argv)
  212. if not action:
  213. argv.append("main")
  214. action = "main"
  215. argv = self.moveUnknownToEnd(argv, action)
  216. if silent:
  217. res = self.parser.parse_known_args(argv[1:])
  218. if res:
  219. self.arguments = res[0]
  220. else:
  221. self.arguments = {}
  222. else:
  223. self.arguments = self.parser.parse_args(argv[1:])
  224. # Parse config file
  225. def parseConfig(self, argv):
  226. # Find config file path from parameters
  227. if "--config_file" in argv:
  228. self.config_file = argv[argv.index("--config_file") + 1]
  229. # Load config file
  230. if os.path.isfile(self.config_file):
  231. config = ConfigParser.ConfigParser(allow_no_value=True)
  232. config.read(self.config_file)
  233. for section in config.sections():
  234. for key, val in config.items(section):
  235. if section != "global": # If not global prefix key with section
  236. key = section + "_" + key
  237. if val:
  238. for line in val.strip().split("\n"): # Allow multi-line values
  239. argv.insert(1, line)
  240. argv.insert(1, "--%s" % key)
  241. return argv
  242. # Expose arguments as class attributes
  243. def setAttributes(self):
  244. # Set attributes from arguments
  245. if self.arguments:
  246. args = vars(self.arguments)
  247. for key, val in args.items():
  248. setattr(self, key, val)
  249. def loadPlugins(self):
  250. from Plugin import PluginManager
  251. @PluginManager.acceptPlugins
  252. class ConfigPlugin(object):
  253. def __init__(self, config):
  254. self.parser = config.parser
  255. self.createArguments()
  256. def createArguments(self):
  257. pass
  258. ConfigPlugin(self)
  259. def saveValue(self, key, value):
  260. if not os.path.isfile(self.config_file):
  261. content = ""
  262. else:
  263. content = open(self.config_file).read()
  264. lines = content.splitlines()
  265. global_line_i = None
  266. key_line_i = None
  267. i = 0
  268. for line in lines:
  269. if line.strip() == "[global]":
  270. global_line_i = i
  271. if line.startswith(key + " = "):
  272. key_line_i = i
  273. i += 1
  274. if value is None: # Delete line
  275. if key_line_i:
  276. del lines[key_line_i]
  277. else: # Add / update
  278. new_line = "%s = %s" % (key, str(value).replace("\n", "").replace("\r", ""))
  279. if key_line_i: # Already in the config, change the line
  280. lines[key_line_i] = new_line
  281. elif global_line_i is None: # No global section yet, append to end of file
  282. lines.append("[global]")
  283. lines.append(new_line)
  284. else: # Has global section, append the line after it
  285. lines.insert(global_line_i + 1, new_line)
  286. open(self.config_file, "w").write("\n".join(lines))
  287. config = Config(sys.argv)