Config.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.3.2"
  8. self.rev = 351
  9. self.argv = argv
  10. self.action = None
  11. self.createParser()
  12. self.createArguments()
  13. def createParser(self):
  14. # Create parser
  15. self.parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  16. self.parser.register('type', 'bool', self.strToBool)
  17. self.subparsers = self.parser.add_subparsers(title="Action to perform", dest="action")
  18. def __str__(self):
  19. return str(self.arguments).replace("Namespace", "Config") # Using argparse str output
  20. # Convert string to bool
  21. def strToBool(self, v):
  22. return v.lower() in ("yes", "true", "t", "1")
  23. # Create command line arguments
  24. def createArguments(self):
  25. # Platform specific
  26. if sys.platform.startswith("win"):
  27. coffeescript = "type %s | tools\\coffee\\coffee.cmd"
  28. else:
  29. coffeescript = None
  30. use_openssl = True
  31. # Main
  32. action = self.subparsers.add_parser("main", help='Start UiServer and FileServer (default)')
  33. # SiteCreate
  34. action = self.subparsers.add_parser("siteCreate", help='Create a new site')
  35. # SiteSign
  36. action = self.subparsers.add_parser("siteSign", help='Update and sign content.json: address [privatekey]')
  37. action.add_argument('address', help='Site to sign')
  38. action.add_argument('privatekey', help='Private key (default: ask on execute)', nargs='?')
  39. action.add_argument('--inner_path', help='File you want to sign (default: content.json)',
  40. default="content.json", metavar="inner_path")
  41. action.add_argument('--publish', help='Publish site after the signing', action='store_true')
  42. # SitePublish
  43. action = self.subparsers.add_parser("sitePublish", help='Publish site to other peers: address')
  44. action.add_argument('address', help='Site to publish')
  45. action.add_argument('peer_ip', help='Peer ip to publish (default: random peers ip from tracker)',
  46. default=None, nargs='?')
  47. action.add_argument('peer_port', help='Peer port to publish (default: random peer port from tracker)',
  48. default=15441, nargs='?')
  49. action.add_argument('--inner_path', help='Content.json you want to publish (default: content.json)',
  50. default="content.json", metavar="inner_path")
  51. # SiteVerify
  52. action = self.subparsers.add_parser("siteVerify", help='Verify site files using sha512: address')
  53. action.add_argument('address', help='Site to verify')
  54. # dbRebuild
  55. action = self.subparsers.add_parser("dbRebuild", help='Rebuild site database cache')
  56. action.add_argument('address', help='Site to rebuild')
  57. # dbQuery
  58. action = self.subparsers.add_parser("dbQuery", help='Query site sql cache')
  59. action.add_argument('address', help='Site to query')
  60. action.add_argument('query', help='Sql query')
  61. # PeerPing
  62. action = self.subparsers.add_parser("peerPing", help='Send Ping command to peer')
  63. action.add_argument('peer_ip', help='Peer ip')
  64. action.add_argument('peer_port', help='Peer port', nargs='?')
  65. # PeerGetFile
  66. action = self.subparsers.add_parser("peerGetFile", help='Request and print a file content from peer')
  67. action.add_argument('peer_ip', help='Peer ip')
  68. action.add_argument('peer_port', help='Peer port')
  69. action.add_argument('site', help='Site address')
  70. action.add_argument('filename', help='File name to request')
  71. action.add_argument('--benchmark', help='Request file 10x then displays the total time', action='store_true')
  72. # PeerCmd
  73. action = self.subparsers.add_parser("peerCmd", help='Request and print a file content from peer')
  74. action.add_argument('peer_ip', help='Peer ip')
  75. action.add_argument('peer_port', help='Peer port')
  76. action.add_argument('cmd', help='Command to execute')
  77. action.add_argument('parameters', help='Parameters to command', nargs='?')
  78. # CryptSign
  79. action = self.subparsers.add_parser("cryptSign", help='Sign message using Bitcoin private key')
  80. action.add_argument('message', help='Message to sign')
  81. action.add_argument('privatekey', help='Private key')
  82. # Config parameters
  83. self.parser.add_argument('--debug', help='Debug mode', action='store_true')
  84. self.parser.add_argument('--debug_socket', help='Debug socket connections', action='store_true')
  85. self.parser.add_argument('--config_file', help='Path of config file', default="zeronet.conf", metavar="path")
  86. self.parser.add_argument('--data_dir', help='Path of data directory', default="data", metavar="path")
  87. self.parser.add_argument('--log_dir', help='Path of logging directory', default="log", metavar="path")
  88. self.parser.add_argument('--ui_ip', help='Web interface bind address', default="127.0.0.1", metavar='ip')
  89. self.parser.add_argument('--ui_port', help='Web interface bind port', default=43110, type=int, metavar='port')
  90. self.parser.add_argument('--ui_restrict', help='Restrict web access', default=False, metavar='ip', nargs='*')
  91. self.parser.add_argument('--open_browser', help='Open homepage in web browser automatically',
  92. nargs='?', const="default_browser", metavar='browser_name')
  93. self.parser.add_argument('--homepage', help='Web interface Homepage', default='1EU1tbG9oC1A8jz2ouVwGZyQ5asrNsE4Vr',
  94. metavar='address')
  95. self.parser.add_argument('--size_limit', help='Default site size limit in MB', default=10, metavar='size')
  96. self.parser.add_argument('--fileserver_ip', help='FileServer bind address', default="*", metavar='ip')
  97. self.parser.add_argument('--fileserver_port', help='FileServer bind port', default=15441, type=int, metavar='port')
  98. self.parser.add_argument('--disable_udp', help='Disable UDP connections', action='store_true')
  99. self.parser.add_argument('--proxy', help='Socks proxy address', metavar='ip:port')
  100. self.parser.add_argument('--ip_external', help='External ip (tested on start if None)', metavar='ip')
  101. self.parser.add_argument('--use_openssl', help='Use OpenSSL liblary for speedup',
  102. type='bool', choices=[True, False], default=use_openssl)
  103. self.parser.add_argument('--disable_encryption', help='Disable connection encryption', action='store_true')
  104. self.parser.add_argument('--disable_sslcompression', help='Disable SSL compression to save memory',
  105. type='bool', choices=[True, False], default=True)
  106. self.parser.add_argument('--use_tempfiles', help='Use temporary files when downloading (experimental)',
  107. type='bool', choices=[True, False], default=False)
  108. self.parser.add_argument('--stream_downloads', help='Stream download directly to files (experimental)',
  109. type='bool', choices=[True, False], default=False)
  110. self.parser.add_argument("--msgpack_purepython", help='Use less memory, but a bit more CPU power',
  111. type='bool', choices=[True, False], default=False)
  112. self.parser.add_argument('--coffeescript_compiler', help='Coffeescript compiler for developing', default=coffeescript,
  113. metavar='executable_path')
  114. self.parser.add_argument('--version', action='version', version='ZeroNet %s r%s' % (self.version, self.rev))
  115. return self.parser
  116. # Find arguments specificed for current action
  117. def getActionArguments(self):
  118. back = {}
  119. arguments = self.parser._subparsers._group_actions[0].choices[self.action]._actions[1:] # First is --version
  120. for argument in arguments:
  121. back[argument.dest] = getattr(self, argument.dest)
  122. return back
  123. # Try to find action from argv
  124. def getAction(self, argv):
  125. actions = [action.choices.keys() for action in self.parser._actions if action.dest == "action"][0] # Valid actions
  126. found_action = False
  127. for action in actions: # See if any in argv
  128. if action in argv:
  129. found_action = action
  130. break
  131. return found_action
  132. # Move plugin parameters to end of argument list
  133. def moveUnknownToEnd(self, argv, default_action):
  134. valid_actions = sum([action.option_strings for action in self.parser._actions], [])
  135. valid_parameters = []
  136. plugin_parameters = []
  137. plugin = False
  138. for arg in argv:
  139. if arg.startswith("--"):
  140. if arg not in valid_actions:
  141. plugin = True
  142. else:
  143. plugin = False
  144. elif arg == default_action:
  145. plugin = False
  146. if plugin:
  147. plugin_parameters.append(arg)
  148. else:
  149. valid_parameters.append(arg)
  150. return valid_parameters + plugin_parameters
  151. # Parse arguments from config file and command line
  152. def parse(self, silent=False, parse_config=True):
  153. if silent: # Don't display messages or quit on unknown parameter
  154. original_print_message = self.parser._print_message
  155. original_exit = self.parser.exit
  156. def silent(parser, function_name):
  157. parser.exited = True
  158. return None
  159. self.parser.exited = False
  160. self.parser._print_message = lambda *args, **kwargs: silent(self.parser, "_print_message")
  161. self.parser.exit = lambda *args, **kwargs: silent(self.parser, "exit")
  162. argv = self.argv[:] # Copy command line arguments
  163. if parse_config:
  164. argv = self.parseConfig(argv) # Add arguments from config file
  165. self.parseCommandline(argv, silent) # Parse argv
  166. self.setAttributes()
  167. if silent: # Restore original functions
  168. if self.parser.exited and self.action == "main": # Argument parsing halted, don't start ZeroNet with main action
  169. self.action = None
  170. self.parser._print_message = original_print_message
  171. self.parser.exit = original_exit
  172. # Parse command line arguments
  173. def parseCommandline(self, argv, silent=False):
  174. # Find out if action is specificed on start
  175. action = self.getAction(argv)
  176. if not action:
  177. argv.append("main")
  178. action = "main"
  179. argv = self.moveUnknownToEnd(argv, action)
  180. if silent:
  181. res = self.parser.parse_known_args(argv[1:])
  182. if res:
  183. self.arguments = res[0]
  184. else:
  185. self.arguments = {}
  186. else:
  187. self.arguments = self.parser.parse_args(argv[1:])
  188. # Parse config file
  189. def parseConfig(self, argv):
  190. # Find config file path from parameters
  191. config_file = "zeronet.conf"
  192. if "--config_file" in argv:
  193. config_file = argv[argv.index("--config_file") + 1]
  194. # Load config file
  195. if os.path.isfile(config_file):
  196. config = ConfigParser.ConfigParser(allow_no_value=True)
  197. config.read(config_file)
  198. for section in config.sections():
  199. for key, val in config.items(section):
  200. if section != "global": # If not global prefix key with section
  201. key = section + "_" + key
  202. if val:
  203. argv.insert(1, val)
  204. argv.insert(1, "--%s" % key)
  205. return argv
  206. # Expose arguments as class attributes
  207. def setAttributes(self):
  208. # Set attributes from arguments
  209. if self.arguments:
  210. args = vars(self.arguments)
  211. for key, val in args.items():
  212. setattr(self, key, val)
  213. def loadPlugins(self):
  214. from Plugin import PluginManager
  215. @PluginManager.acceptPlugins
  216. class ConfigPlugin(object):
  217. def __init__(self, config):
  218. self.parser = config.parser
  219. self.createArguments()
  220. def createArguments(self):
  221. pass
  222. ConfigPlugin(self)
  223. config = Config(sys.argv)