Config.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. import argparse
  2. import sys
  3. import os
  4. import locale
  5. import re
  6. import ConfigParser
  7. class Config(object):
  8. def __init__(self, argv):
  9. self.version = "0.6.3"
  10. self.rev = 3495
  11. self.argv = argv
  12. self.action = None
  13. self.config_file = "zeronet.conf"
  14. self.createParser()
  15. self.createArguments()
  16. def createParser(self):
  17. # Create parser
  18. self.parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  19. self.parser.register('type', 'bool', self.strToBool)
  20. self.subparsers = self.parser.add_subparsers(title="Action to perform", dest="action")
  21. def __str__(self):
  22. return str(self.arguments).replace("Namespace", "Config") # Using argparse str output
  23. # Convert string to bool
  24. def strToBool(self, v):
  25. return v.lower() in ("yes", "true", "t", "1")
  26. # Create command line arguments
  27. def createArguments(self):
  28. trackers = [
  29. "zero://boot3rdez4rzn36x.onion:15441",
  30. "zero://zero.booth.moe#f36ca555bee6ba216b14d10f38c16f7769ff064e0e37d887603548cc2e64191d:443", # US/NY
  31. "udp://tracker.coppersurfer.tk:6969", # DE
  32. "udp://tracker.leechers-paradise.org:6969", # NL
  33. "udp://104.238.198.186:8000", # US/LA
  34. "http://tracker.swateam.org.uk:2710/announce", # US/NY
  35. "http://open.acgnxtracker.com:80/announce", # DE
  36. "http://retracker.mgts.by:80/announce" # BY
  37. ]
  38. # Platform specific
  39. if sys.platform.startswith("win"):
  40. coffeescript = "type %s | tools\\coffee\\coffee.cmd"
  41. else:
  42. coffeescript = None
  43. try:
  44. language, enc = locale.getdefaultlocale()
  45. language = language.split("_")[0]
  46. except Exception:
  47. language = "en"
  48. use_openssl = True
  49. if repr(1483108852.565) != "1483108852.565":
  50. fix_float_decimals = True
  51. else:
  52. fix_float_decimals = False
  53. this_file = os.path.abspath(__file__).replace("\\", "/").rstrip("cd")
  54. if this_file.endswith("/Contents/Resources/core/src/Config.py"):
  55. # Running as ZeroNet.app
  56. if this_file.startswith("/Application") or this_file.startswith("/private") or this_file.startswith(os.path.expanduser("~/Library")):
  57. # Runnig from non-writeable directory, put data to Application Support
  58. start_dir = os.path.expanduser("~/Library/Application Support/ZeroNet").decode(sys.getfilesystemencoding())
  59. else:
  60. # Running from writeable directory put data next to .app
  61. start_dir = re.sub("/[^/]+/Contents/Resources/core/src/Config.py", "", this_file).decode(sys.getfilesystemencoding())
  62. config_file = start_dir + "/zeronet.conf"
  63. data_dir = start_dir + "/data"
  64. log_dir = start_dir + "/log"
  65. elif this_file.endswith("/core/src/Config.py"):
  66. # Running as exe or source is at Application Support directory, put var files to outside of core dir
  67. start_dir = this_file.replace("/core/src/Config.py", "").decode(sys.getfilesystemencoding())
  68. config_file = start_dir + "/zeronet.conf"
  69. data_dir = start_dir + "/data"
  70. log_dir = start_dir + "/log"
  71. elif this_file.endswith("usr/share/zeronet/src/Config.py"):
  72. # Running from non-writeable location, e.g., AppImage
  73. start_dir = os.path.expanduser("~/ZeroNet").decode(sys.getfilesystemencoding())
  74. config_file = start_dir + "/zeronet.conf"
  75. data_dir = start_dir + "/data"
  76. log_dir = start_dir + "/log"
  77. else:
  78. config_file = "zeronet.conf"
  79. data_dir = "data"
  80. log_dir = "log"
  81. ip_local = ["127.0.0.1"]
  82. # Main
  83. action = self.subparsers.add_parser("main", help='Start UiServer and FileServer (default)')
  84. # SiteCreate
  85. action = self.subparsers.add_parser("siteCreate", help='Create a new site')
  86. # SiteNeedFile
  87. action = self.subparsers.add_parser("siteNeedFile", help='Get a file from site')
  88. action.add_argument('address', help='Site address')
  89. action.add_argument('inner_path', help='File inner path')
  90. # SiteDownload
  91. action = self.subparsers.add_parser("siteDownload", help='Download a new site')
  92. action.add_argument('address', help='Site address')
  93. # SiteSign
  94. action = self.subparsers.add_parser("siteSign", help='Update and sign content.json: address [privatekey]')
  95. action.add_argument('address', help='Site to sign')
  96. action.add_argument('privatekey', help='Private key (default: ask on execute)', nargs='?')
  97. action.add_argument('--inner_path', help='File you want to sign (default: content.json)',
  98. default="content.json", metavar="inner_path")
  99. action.add_argument('--remove_missing_optional', help='Remove optional files that is not present in the directory', action='store_true')
  100. action.add_argument('--publish', help='Publish site after the signing', action='store_true')
  101. # SitePublish
  102. action = self.subparsers.add_parser("sitePublish", help='Publish site to other peers: address')
  103. action.add_argument('address', help='Site to publish')
  104. action.add_argument('peer_ip', help='Peer ip to publish (default: random peers ip from tracker)',
  105. default=None, nargs='?')
  106. action.add_argument('peer_port', help='Peer port to publish (default: random peer port from tracker)',
  107. default=15441, nargs='?')
  108. action.add_argument('--inner_path', help='Content.json you want to publish (default: content.json)',
  109. default="content.json", metavar="inner_path")
  110. # SiteVerify
  111. action = self.subparsers.add_parser("siteVerify", help='Verify site files using sha512: address')
  112. action.add_argument('address', help='Site to verify')
  113. # SiteCmd
  114. action = self.subparsers.add_parser("siteCmd", help='Execute a ZeroFrame API command on a site')
  115. action.add_argument('address', help='Site address')
  116. action.add_argument('cmd', help='API command name')
  117. action.add_argument('parameters', help='Parameters of the command', nargs='?')
  118. # dbRebuild
  119. action = self.subparsers.add_parser("dbRebuild", help='Rebuild site database cache')
  120. action.add_argument('address', help='Site to rebuild')
  121. # dbQuery
  122. action = self.subparsers.add_parser("dbQuery", help='Query site sql cache')
  123. action.add_argument('address', help='Site to query')
  124. action.add_argument('query', help='Sql query')
  125. # PeerPing
  126. action = self.subparsers.add_parser("peerPing", help='Send Ping command to peer')
  127. action.add_argument('peer_ip', help='Peer ip')
  128. action.add_argument('peer_port', help='Peer port', nargs='?')
  129. # PeerGetFile
  130. action = self.subparsers.add_parser("peerGetFile", help='Request and print a file content from peer')
  131. action.add_argument('peer_ip', help='Peer ip')
  132. action.add_argument('peer_port', help='Peer port')
  133. action.add_argument('site', help='Site address')
  134. action.add_argument('filename', help='File name to request')
  135. action.add_argument('--benchmark', help='Request file 10x then displays the total time', action='store_true')
  136. # PeerCmd
  137. action = self.subparsers.add_parser("peerCmd", help='Request and print a file content from peer')
  138. action.add_argument('peer_ip', help='Peer ip')
  139. action.add_argument('peer_port', help='Peer port')
  140. action.add_argument('cmd', help='Command to execute')
  141. action.add_argument('parameters', help='Parameters to command', nargs='?')
  142. # CryptSign
  143. action = self.subparsers.add_parser("cryptSign", help='Sign message using Bitcoin private key')
  144. action.add_argument('message', help='Message to sign')
  145. action.add_argument('privatekey', help='Private key')
  146. # Crypt Verify
  147. action = self.subparsers.add_parser("cryptVerify", help='Verify message using Bitcoin public address')
  148. action.add_argument('message', help='Message to verify')
  149. action.add_argument('sign', help='Signiture for message')
  150. action.add_argument('address', help='Signer\'s address')
  151. # Crypt GetPrivatekey
  152. action = self.subparsers.add_parser("cryptGetPrivatekey", help='Generate a privatekey from master seed')
  153. action.add_argument('master_seed', help='Source master seed')
  154. action.add_argument('site_address_index', help='Site address index', type=int)
  155. action = self.subparsers.add_parser("getConfig", help='Return json-encoded info')
  156. action = self.subparsers.add_parser("testConnection", help='Testing')
  157. action = self.subparsers.add_parser("testAnnounce", help='Testing')
  158. # Config parameters
  159. self.parser.add_argument('--verbose', help='More detailed logging', action='store_true')
  160. self.parser.add_argument('--debug', help='Debug mode', action='store_true')
  161. self.parser.add_argument('--silent', help='Disable logging to terminal output', action='store_true')
  162. self.parser.add_argument('--debug_socket', help='Debug socket connections', action='store_true')
  163. self.parser.add_argument('--batch', help="Batch mode (No interactive input for commands)", action='store_true')
  164. self.parser.add_argument('--config_file', help='Path of config file', default=config_file, metavar="path")
  165. self.parser.add_argument('--data_dir', help='Path of data directory', default=data_dir, metavar="path")
  166. self.parser.add_argument('--log_dir', help='Path of logging directory', default=log_dir, metavar="path")
  167. self.parser.add_argument('--log_level', help='Level of logging to file', default="DEBUG", choices=["DEBUG", "INFO", "ERROR"])
  168. self.parser.add_argument('--language', help='Web interface language', default=language, metavar='language')
  169. self.parser.add_argument('--ui_ip', help='Web interface bind address', default="127.0.0.1", metavar='ip')
  170. self.parser.add_argument('--ui_port', help='Web interface bind port', default=43110, type=int, metavar='port')
  171. self.parser.add_argument('--ui_restrict', help='Restrict web access', default=False, metavar='ip', nargs='*')
  172. self.parser.add_argument('--ui_host', help='Allow access using this hosts', metavar='host', nargs='*')
  173. self.parser.add_argument('--ui_trans_proxy', help='Allow access using a transparent proxy', action='store_true')
  174. self.parser.add_argument('--open_browser', help='Open homepage in web browser automatically',
  175. nargs='?', const="default_browser", metavar='browser_name')
  176. self.parser.add_argument('--homepage', help='Web interface Homepage', default='1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D',
  177. metavar='address')
  178. self.parser.add_argument('--updatesite', help='Source code update site', default='1UPDatEDxnvHDo7TXvq6AEBARfNkyfxsp',
  179. metavar='address')
  180. self.parser.add_argument('--size_limit', help='Default site size limit in MB', default=10, type=int, metavar='limit')
  181. self.parser.add_argument('--file_size_limit', help='Maximum per file size limit in MB', default=10, type=int, metavar='limit')
  182. self.parser.add_argument('--connected_limit', help='Max connected peer per site', default=8, type=int, metavar='connected_limit')
  183. self.parser.add_argument('--global_connected_limit', help='Max connections', default=512, type=int, metavar='global_connected_limit')
  184. self.parser.add_argument('--workers', help='Download workers per site', default=5, type=int, metavar='workers')
  185. self.parser.add_argument('--fileserver_ip', help='FileServer bind address', default="*", metavar='ip')
  186. self.parser.add_argument('--fileserver_port', help='FileServer bind port (0: randomize)', default=0, type=int, metavar='port')
  187. self.parser.add_argument('--fileserver_port_range', help='FileServer randomization range', default="10000-40000", metavar='port')
  188. self.parser.add_argument('--ip_local', help='My local ips', default=ip_local, type=int, metavar='ip', nargs='*')
  189. self.parser.add_argument('--disable_udp', help='Disable UDP connections', action='store_true')
  190. self.parser.add_argument('--proxy', help='Socks proxy address', metavar='ip:port')
  191. self.parser.add_argument('--bind', help='Bind outgoing sockets to this address', metavar='ip')
  192. self.parser.add_argument('--ip_external', help='Set reported external ip (tested on start if None)', metavar='ip')
  193. self.parser.add_argument('--trackers', help='Bootstraping torrent trackers', default=trackers, metavar='protocol://address', nargs='*')
  194. self.parser.add_argument('--trackers_file', help='Load torrent trackers dynamically from a file', default=False, metavar='path')
  195. self.parser.add_argument('--trackers_proxy', help='Force use proxy to connect to trackers', default="disable", choices=["disable", "tor"])
  196. self.parser.add_argument('--use_openssl', help='Use OpenSSL liblary for speedup',
  197. type='bool', choices=[True, False], default=use_openssl)
  198. self.parser.add_argument('--disable_db', help='Disable database updating', action='store_true')
  199. self.parser.add_argument('--disable_encryption', help='Disable connection encryption', action='store_true')
  200. self.parser.add_argument('--force_encryption', help="Enforce encryption to all peer connections", action='store_true')
  201. self.parser.add_argument('--disable_sslcompression', help='Disable SSL compression to save memory',
  202. type='bool', choices=[True, False], default=True)
  203. self.parser.add_argument('--keep_ssl_cert', help='Disable new SSL cert generation on startup', action='store_true')
  204. self.parser.add_argument('--max_files_opened', help='Change maximum opened files allowed by OS to this value on startup',
  205. default=2048, type=int, metavar='limit')
  206. self.parser.add_argument('--stack_size', help='Change thread stack size', default=None, type=int, metavar='thread_stack_size')
  207. self.parser.add_argument('--use_tempfiles', help='Use temporary files when downloading (experimental)',
  208. type='bool', choices=[True, False], default=False)
  209. self.parser.add_argument('--stream_downloads', help='Stream download directly to files (experimental)',
  210. type='bool', choices=[True, False], default=False)
  211. self.parser.add_argument("--msgpack_purepython", help='Use less memory, but a bit more CPU power',
  212. type='bool', choices=[True, False], default=False)
  213. self.parser.add_argument("--fix_float_decimals", help='Fix content.json modification date float precision on verification',
  214. type='bool', choices=[True, False], default=fix_float_decimals)
  215. self.parser.add_argument("--db_mode", choices=["speed", "security"], default="speed")
  216. self.parser.add_argument("--download_optional", choices=["manual", "auto"], default="manual")
  217. self.parser.add_argument('--coffeescript_compiler', help='Coffeescript compiler for developing', default=coffeescript,
  218. metavar='executable_path')
  219. self.parser.add_argument('--tor', help='enable: Use only for Tor peers, always: Use Tor for every connection', choices=["disable", "enable", "always"], default='enable')
  220. self.parser.add_argument('--tor_controller', help='Tor controller address', metavar='ip:port', default='127.0.0.1:9051')
  221. self.parser.add_argument('--tor_proxy', help='Tor proxy address', metavar='ip:port', default='127.0.0.1:9050')
  222. self.parser.add_argument('--tor_password', help='Tor controller password', metavar='password')
  223. self.parser.add_argument('--tor_use_bridges', help='Use obfuscated bridge relays to avoid Tor block', action='store_true')
  224. self.parser.add_argument('--tor_hs_limit', help='Maximum number of hidden services in Tor always mode', metavar='limit', type=int, default=10)
  225. self.parser.add_argument('--tor_hs_port', help='Hidden service port in Tor always mode', metavar='limit', type=int, default=15441)
  226. self.parser.add_argument('--version', action='version', version='ZeroNet %s r%s' % (self.version, self.rev))
  227. self.parser.add_argument('--end', help='Stop multi value argument parsing', action='store_true')
  228. return self.parser
  229. def loadTrackersFile(self):
  230. self.trackers = []
  231. for tracker in open(self.trackers_file):
  232. if "://" in tracker:
  233. self.trackers.append(tracker.strip())
  234. # Find arguments specified for current action
  235. def getActionArguments(self):
  236. back = {}
  237. arguments = self.parser._subparsers._group_actions[0].choices[self.action]._actions[1:] # First is --version
  238. for argument in arguments:
  239. back[argument.dest] = getattr(self, argument.dest)
  240. return back
  241. # Try to find action from argv
  242. def getAction(self, argv):
  243. actions = [action.choices.keys() for action in self.parser._actions if action.dest == "action"][0] # Valid actions
  244. found_action = False
  245. for action in actions: # See if any in argv
  246. if action in argv:
  247. found_action = action
  248. break
  249. return found_action
  250. # Move plugin parameters to end of argument list
  251. def moveUnknownToEnd(self, argv, default_action):
  252. valid_actions = sum([action.option_strings for action in self.parser._actions], [])
  253. valid_parameters = []
  254. plugin_parameters = []
  255. plugin = False
  256. for arg in argv:
  257. if arg.startswith("--"):
  258. if arg not in valid_actions:
  259. plugin = True
  260. else:
  261. plugin = False
  262. elif arg == default_action:
  263. plugin = False
  264. if plugin:
  265. plugin_parameters.append(arg)
  266. else:
  267. valid_parameters.append(arg)
  268. return valid_parameters + plugin_parameters
  269. # Parse arguments from config file and command line
  270. def parse(self, silent=False, parse_config=True):
  271. if silent: # Don't display messages or quit on unknown parameter
  272. original_print_message = self.parser._print_message
  273. original_exit = self.parser.exit
  274. def silencer(parser, function_name):
  275. parser.exited = True
  276. return None
  277. self.parser.exited = False
  278. self.parser._print_message = lambda *args, **kwargs: silencer(self.parser, "_print_message")
  279. self.parser.exit = lambda *args, **kwargs: silencer(self.parser, "exit")
  280. argv = self.argv[:] # Copy command line arguments
  281. self.parseCommandline(argv, silent) # Parse argv
  282. self.setAttributes()
  283. if parse_config:
  284. argv = self.parseConfig(argv) # Add arguments from config file
  285. self.parseCommandline(argv, silent) # Parse argv
  286. self.setAttributes()
  287. if not silent:
  288. if self.fileserver_ip != "*" and self.fileserver_ip not in self.ip_local:
  289. self.ip_local.append(self.fileserver_ip)
  290. if silent: # Restore original functions
  291. if self.parser.exited and self.action == "main": # Argument parsing halted, don't start ZeroNet with main action
  292. self.action = None
  293. self.parser._print_message = original_print_message
  294. self.parser.exit = original_exit
  295. # Parse command line arguments
  296. def parseCommandline(self, argv, silent=False):
  297. # Find out if action is specificed on start
  298. action = self.getAction(argv)
  299. if not action:
  300. argv.append("--end")
  301. argv.append("main")
  302. action = "main"
  303. argv = self.moveUnknownToEnd(argv, action)
  304. if silent:
  305. res = self.parser.parse_known_args(argv[1:])
  306. if res:
  307. self.arguments = res[0]
  308. else:
  309. self.arguments = {}
  310. else:
  311. self.arguments = self.parser.parse_args(argv[1:])
  312. # Parse config file
  313. def parseConfig(self, argv):
  314. # Find config file path from parameters
  315. if "--config_file" in argv:
  316. self.config_file = argv[argv.index("--config_file") + 1]
  317. # Load config file
  318. if os.path.isfile(self.config_file):
  319. config = ConfigParser.ConfigParser(allow_no_value=True)
  320. config.read(self.config_file)
  321. for section in config.sections():
  322. for key, val in config.items(section):
  323. if section != "global": # If not global prefix key with section
  324. key = section + "_" + key
  325. if val:
  326. for line in val.strip().split("\n"): # Allow multi-line values
  327. argv.insert(1, line)
  328. argv.insert(1, "--%s" % key)
  329. return argv
  330. # Expose arguments as class attributes
  331. def setAttributes(self):
  332. # Set attributes from arguments
  333. if self.arguments:
  334. args = vars(self.arguments)
  335. for key, val in args.items():
  336. setattr(self, key, val)
  337. def loadPlugins(self):
  338. from Plugin import PluginManager
  339. @PluginManager.acceptPlugins
  340. class ConfigPlugin(object):
  341. def __init__(self, config):
  342. self.parser = config.parser
  343. self.createArguments()
  344. def createArguments(self):
  345. pass
  346. ConfigPlugin(self)
  347. def saveValue(self, key, value):
  348. if not os.path.isfile(self.config_file):
  349. content = ""
  350. else:
  351. content = open(self.config_file).read()
  352. lines = content.splitlines()
  353. global_line_i = None
  354. key_line_i = None
  355. i = 0
  356. for line in lines:
  357. if line.strip() == "[global]":
  358. global_line_i = i
  359. if line.startswith(key + " ="):
  360. key_line_i = i
  361. i += 1
  362. if value is None: # Delete line
  363. if key_line_i:
  364. del lines[key_line_i]
  365. else: # Add / update
  366. new_line = "%s = %s" % (key, str(value).replace("\n", "").replace("\r", ""))
  367. if key_line_i: # Already in the config, change the line
  368. lines[key_line_i] = new_line
  369. elif global_line_i is None: # No global section yet, append to end of file
  370. lines.append("[global]")
  371. lines.append(new_line)
  372. else: # Has global section, append the line after it
  373. lines.insert(global_line_i + 1, new_line)
  374. open(self.config_file, "w").write("\n".join(lines))
  375. def getServerInfo(self):
  376. from Plugin import PluginManager
  377. info = {
  378. "platform": sys.platform,
  379. "fileserver_ip": self.fileserver_ip,
  380. "fileserver_port": self.fileserver_port,
  381. "ui_ip": self.ui_ip,
  382. "ui_port": self.ui_port,
  383. "version": self.version,
  384. "rev": self.rev,
  385. "language": self.language,
  386. "debug": self.debug,
  387. "plugins": PluginManager.plugin_manager.plugin_names,
  388. "log_dir": os.path.abspath(self.log_dir),
  389. "data_dir": os.path.abspath(self.data_dir),
  390. "src_dir": os.path.dirname(os.path.abspath(__file__))
  391. }
  392. try:
  393. info["ip_external"] = sys.modules["main"].file_server.port_opened
  394. info["tor_enabled"] = sys.modules["main"].file_server.tor_manager.enabled
  395. info["tor_status"] = sys.modules["main"].file_server.tor_manager.status
  396. except:
  397. pass
  398. return info
  399. config = Config(sys.argv)