gtk3-listdomains.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import gi
  2. gi.require_version('Gtk', '3.0')
  3. from gi.repository import GLib, Gtk, GObject
  4. import json
  5. from nginxparser import loads
  6. import re
  7. import spur
  8. import threading
  9. import time
  10. fp = open('creds.json', 'r')
  11. creds = json.load(fp)
  12. def ssh_command(command, sudo=False):
  13. shell = spur.SshShell(
  14. hostname=creds['host'],
  15. username=creds['user'],
  16. password=creds['passphrase'],
  17. private_key_file=creds['ssh_key_path'])
  18. with shell:
  19. command_bits = command.split(" ")
  20. if sudo:
  21. command_bits.insert(0, "sudo")
  22. process = shell.spawn(command_bits)
  23. if sudo:
  24. process.stdin_write(creds['password'])
  25. result = process.wait_for_result()
  26. return result.output.decode()
  27. #
  28. # def get_check_domain_command(domain):
  29. # return "sudo openssl x509 -text -in /etc/letsencrypt/live/" + domain + "/fullchain.pem"
  30. #
  31. # def join_commands(commands):
  32. # return " && ".join(commands)
  33. class EntryWindow(Gtk.Window):
  34. def __init__(self):
  35. Gtk.Window.__init__(self, title="Entry Demo")
  36. self.set_size_request(200, 100)
  37. self.timeout_id = None
  38. vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
  39. self.add(vbox)
  40. self.progressbar = Gtk.ProgressBar(show_text=True)
  41. # self.progressbar.set_fraction(0.0)
  42. vbox.pack_start(self.progressbar, True, True, 0)
  43. hbox = Gtk.Box(spacing=6)
  44. vbox.add(hbox)
  45. self.entry_passphrase = Gtk.Entry()
  46. # https://developer.gnome.org/gtk3/stable/GtkEntry.html#gtk-entry-set-invisible-char
  47. self.entry_passphrase.set_visibility(False)
  48. # self.entry_passphrase.set_text("Enter SSH key passphrase")
  49. hbox.pack_start(self.entry_passphrase, True, True, 0)
  50. self.entry_password = Gtk.Entry()
  51. self.entry_password.set_visibility(False)
  52. # self.entry_password.set_text("Enter sudo user password")
  53. hbox.pack_start(self.entry_password, True, True, 0)
  54. self.button = Gtk.Button(label="Click Here")
  55. self.button.connect("clicked", self.on_button_clicked)
  56. hbox.pack_start(self.button, True, True, 0)
  57. def update_progess(self, domain):
  58. self.progressbar.pulse()
  59. self.progressbar.set_text('Done: ' + domain)
  60. return False
  61. def get_https_domains(self):
  62. for d in self.domains:
  63. GLib.idle_add(self.get_https_subdomains_for_domain, d)
  64. time.sleep(0.4)
  65. def get_nginx_vhosts(self):
  66. for v in self.vhosts:
  67. GLib.idle_add(self.get_nginx_vhost, v)
  68. time.sleep(0.4)
  69. def start_thread(self, func):
  70. thread = threading.Thread(target=func)
  71. thread.daemon = True
  72. thread.start()
  73. def on_button_clicked(self, widget):
  74. decoded = ssh_command("ls /etc/nginx/sites-enabled")
  75. vhosts = decoded.split("\n")
  76. vhosts.pop()
  77. self.vhosts = vhosts
  78. self.num_vhosts = len(vhosts)
  79. self.vhosts_done = 0
  80. decoded = ssh_command("ls /etc/letsencrypt/live", True)
  81. domains = decoded.split("\n")
  82. domains.pop()
  83. self.domains = domains
  84. self.num_domains = len(domains)
  85. self.domains_done = 0
  86. self.start_thread(self.get_nginx_vhosts)
  87. # self.start_thread(self.get_https_domains)
  88. # subdomains = [self.get_https_subdomains_for_domain(d) for d in domains]
  89. # subdomains_dict = dict(zip(domains, subdomains))
  90. # print(subdomains_dict)
  91. def get_nginx_vhost(self, vhost):
  92. print(vhost)
  93. vhost_file = ssh_command("cat /etc/nginx/sites-enabled/" + vhost)
  94. parsed = loads(vhost_file)
  95. port_subdmomains = {}
  96. for server in parsed:
  97. server_inner = server[1]
  98. port = 0
  99. subdomains = []
  100. for directive in server_inner:
  101. if not port and "listen" in directive:
  102. p = re.compile('(\d+)')
  103. print('listen')
  104. ports = p.findall(directive[1])
  105. port = int(ports[0])
  106. if "server_name" in directive:
  107. print('server_name')
  108. print(directive)
  109. subd_trimmed = directive[1].strip()
  110. subdomains = subd_trimmed.split(' ')
  111. port_subdmomains[port] = subdomains
  112. print(port_subdmomains)
  113. self.vhosts_done += 1
  114. percent_done = self.vhosts_done * 1.0 / self.num_vhosts
  115. self.progressbar.set_fraction(percent_done)
  116. return False
  117. def get_https_subdomains_for_domain(self, domain):
  118. print(domain)
  119. p = re.compile('DNS:([0-9a-z-.]+)')
  120. cert_data = ssh_command("sudo openssl x509 -text -in /etc/letsencrypt/live/" + domain + "/fullchain.pem", True)
  121. self.domains_done += 1
  122. percent_done = self.domains_done * 1.0 / self.num_domains
  123. # print(percent_done)
  124. self.progressbar.set_fraction(percent_done)
  125. # self.progressbar.set_text('Done: ' + domain)
  126. # print(cert_data)
  127. # return p.findall (cert_data)
  128. return False
  129. def app_main():
  130. win = EntryWindow()
  131. win.connect("delete-event", Gtk.main_quit)
  132. win.show_all()
  133. if __name__ == '__main__':
  134. import signal
  135. signal.signal(signal.SIGINT, signal.SIG_DFL)
  136. # Calling GObject.threads_init() is not needed for PyGObject 3.10.2+
  137. GObject.threads_init()
  138. app_main()
  139. Gtk.main()