Private Decentralized Communication Network
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

126 lines
3.6 KiB

  1. #!/usr/bin/env python3
  2. import argparse
  3. import os
  4. from threading import Thread
  5. from time import sleep
  6. from subprocess import DEVNULL
  7. import ujson
  8. from psutil import Popen
  9. from psutil import Process
  10. import psutil
  11. import sys
  12. import curses
  13. script_dir = os.path.dirname(os.path.realpath(__file__))
  14. sys.path.append(script_dir + '/src/')
  15. from etc import onionrvalues
  16. sub_script = script_dir + '/' + onionrvalues.SCRIPT_NAME
  17. def show_info(p: Process):
  18. def pbar(window):
  19. window.addstr(8, 10, "Onionr statistics")
  20. window.addstr(9, 10, "-" * 17)
  21. curses.curs_set(0)
  22. while True:
  23. threads = p.num_threads()
  24. open_files = len(p.open_files())
  25. cpu_percent = p.cpu_percent()
  26. block_count = len(blockmetadb.get_block_list())
  27. for proc in p.children(recursive=True):
  28. threads += proc.num_threads()
  29. cpu_percent += proc.cpu_percent()
  30. try:
  31. open_files += len(proc.open_files())
  32. except psutil.AccessDenied:
  33. pass
  34. cpu_percent = cpu_percent * 100
  35. window.addstr(11, 10, f"Threads: {threads}")
  36. window.addstr(10, 10, f"Open files: {open_files}")
  37. window.addstr(12, 10, f"CPU: {cpu_percent}%")
  38. window.addstr(13, 10, f"Blocks: {block_count}")
  39. window.refresh()
  40. sleep(0.5)
  41. sleep(15)
  42. curses.wrapper(pbar)
  43. while True:
  44. sleep(1)
  45. parser = argparse.ArgumentParser()
  46. parser.add_argument(
  47. "--show-stats", help="Display curses output of Onionr stats",
  48. type=int, default=0)
  49. parser.add_argument(
  50. "--onboarding", help="Use Onionr onboarding (if first load)",
  51. type=int, default=1)
  52. parser.add_argument(
  53. "--security-level", help="Set Onionr security level",
  54. type=int, default=0)
  55. parser.add_argument(
  56. '--open-ui', help='Open onionr web ui after started',
  57. type=int, default=1)
  58. parser.add_argument(
  59. '--random-localhost-ip', help='bind to random localhost IP for extra security',
  60. type=int, default=1)
  61. parser.add_argument(
  62. '--use-tor', help='Use Tor transport',
  63. type=int, default=1)
  64. parser.add_argument(
  65. '--private-key', help='Use existing private key',
  66. type=int, default=1)
  67. parser.add_argument(
  68. '--animated-background', help='Animated background on webui index. Just for looks.',
  69. type=int, default=0)
  70. parser.add_argument(
  71. '--keep-log-on-exit', help='Onionr can keep or delete its log file on exit',
  72. type=int, default=0)
  73. args = parser.parse_args()
  74. p = Popen([sub_script, 'version'], stdout=DEVNULL)
  75. p.wait()
  76. from filepaths import config_file
  77. from coredb import blockmetadb
  78. with open(config_file, 'r') as cf:
  79. config = ujson.loads(cf.read())
  80. if not args.onboarding:
  81. config['onboarding']['done'] = True
  82. if not args.random_localhost_ip:
  83. config['general']['random_bind_ip'] = False
  84. if not args.use_tor:
  85. config['transports']['tor'] = False
  86. if not args.animated_background:
  87. config['ui']['animated_background'] = False
  88. if args.keep_log_on_exit:
  89. config['log']['file']['remove_on_exit'] = True
  90. else:
  91. config['log']['file']['remove_on_exit'] = False
  92. config['general']['display_header'] = False
  93. config['general']['security_level'] = args.security_level
  94. with open(config_file, 'w') as cf:
  95. cf.write(ujson.dumps(config))
  96. if args.open_ui:
  97. p = Popen([sub_script, 'start'], stdout=DEVNULL)
  98. sleep(2)
  99. Popen([sub_script, 'openhome'], stdout=DEVNULL)
  100. else:
  101. p = Popen([sub_script, 'start'], stdout=DEVNULL)
  102. p = p.children()[0]
  103. if args.show_stats:
  104. Thread(target=show_info, args=[p], daemon=True).start()
  105. p.wait()