Application to use your DigitalOcean account/service as a dynamic DNS service. https://gitlab.no/rune/DO_DynDNS
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.

229 lines
9.4 KiB

  1. # !/usr/bin/env python3
  2. #################################
  3. # @author Rune Olsen #
  4. # @license GPL v3 #
  5. # #
  6. #################################
  7. from os import path
  8. import PySimpleGUI as sg
  9. import utils as dyndns
  10. import sys
  11. bundle_dir = getattr(sys, '_MEIPASS', path.abspath(path.dirname(__file__)))
  12. def add_new_domain():
  13. add_new = True
  14. layout4 = [[sg.Text('This will add a new subdomain to you DO networkin resouces '
  15. '\nand update your config.ini file.\n', font=(any,11))
  16. ],
  17. [sg.InputText('', font=(any, 12), key='domainname',size=(20,1)),
  18. sg.Text('.'+dyndns.domain_name, font=(any, 12))],
  19. [sg.Text('\n\n')],
  20. [sg.Button('Exit', key='Exit'), sg.Button('Add', key='Add')]
  21. ]
  22. window4 = sg.Window('Add new domain to '+dyndns.domain_name, layout4)
  23. while add_new:
  24. event4, values4 = window4.Read()
  25. if event4 == 'Add':
  26. domain = values4['domainname']
  27. result = dyndns.addnewdomin(domain)
  28. if result:
  29. sg.popup_ok('The domain ' + domain + ' added to config.ini', title='SUCCESS added domain ' + domain,
  30. font=(any, 12), )
  31. else:
  32. sg.popup_ok('The domain ' + domain + ' was NOT added to config.ini', title='FAILURE in adding ' +
  33. domain, font=(any, 12), )
  34. if event4 is None or event4 == 'Exit':
  35. window4.close()
  36. add_new = False
  37. def remove_domain_from_dyndns():
  38. remove_from_ini = True
  39. domains = dyndns.domainnames()
  40. layout5 = [
  41. [sg.Text('Select domain ', font=(any, 12)),
  42. sg.Combo(default_value=domains[0], values=domains, size=(20, 1), enable_events=True, key='-domain5-',
  43. font=(any, 12)),
  44. ],
  45. [sg.Button('Exit', key='Exit'), sg.Button('Remove', key='Remove')]
  46. ]
  47. window5 = sg.Window('Remove domain from DO DynDns', layout5)
  48. while remove_from_ini:
  49. event5, values5 = window5.Read()
  50. if event5 == 'Remove':
  51. domain = values5['-domain5-']
  52. result = dyndns.remove_from_config(domain)
  53. if result:
  54. sg.popup_ok('The domain ' + domain + ' removed from config.ini', title='SUCCESS removed domain ' +
  55. domain, font=(any, 12), )
  56. else:
  57. sg.popup_ok('The domain ' + domain + ' was NOT removed to config.ini', title='FAILURE in removing ' +
  58. domain, font=(any, 12), )
  59. if event5 is None or event5 == 'Exit':
  60. window5.close()
  61. remove_from_ini = False
  62. def remove_domain_from_do():
  63. None
  64. def add_existing_domain():
  65. add_existing_active = True
  66. domains = dyndns.list_domains_to_array()
  67. layout3 = [
  68. [sg.Text('Select domain ', font=(any, 12)),
  69. sg.Combo(default_value=domains[0][0], values=domains[0], size=(20, 1), enable_events=True, key='-domain3-',
  70. font=(any, 12)),
  71. ],
  72. [sg.Button('Exit', key='Exit'), sg.Button('Add', key='Add')]
  73. ]
  74. window3 = sg.Window('Add existing domain to ' + dyndns.domain_name, layout3)
  75. while add_existing_active:
  76. event3, values3 = window3.Read()
  77. if event3 == 'Add':
  78. domain = values3['-domain3-']
  79. number = domains[0].index(domain)
  80. result = dyndns.add_existing_domain(domains[0][number], domains[1][number])
  81. if result:
  82. sg.popup_ok ('The domain ' + domain + ' added to config.ini', title='SUCCESS added domain ' + domain,
  83. font=(any, 12),)
  84. else:
  85. sg.popup_ok('The domain ' + domain + ' was NOT added to config.ini', title='FAILURE in adding ' +
  86. domain, font=(any, 12), )
  87. if event3 is None or event3 == 'Exit':
  88. window3.close()
  89. add_existing_active = False
  90. def list_domains():
  91. domains_active = True
  92. # window 2 layout - note - must be "new" every time a window is created
  93. layout2 = [
  94. [sg.Text(''.join(dyndns.list_all_domains_to_array()), font=(any, 12))],
  95. [sg.Button('Exit')]
  96. ]
  97. window2 = sg.Window('All domains on ' + dyndns.domain_name, layout2)
  98. # Read window 2's events. Must use timeout of 0
  99. while domains_active:
  100. event2, values2 = window2.Read()
  101. if event2 is None or event2 == 'Exit':
  102. window2.close()
  103. domains_active = False
  104. def main_window():
  105. subdomains = dyndns.domainnames()
  106. localip = dyndns.local_ip4
  107. # sg.change_look_and_feel('LightGreen')
  108. sg.change_look_and_feel('DarkGrey')
  109. sg.set_options(element_padding=(0, 0))
  110. local_ip4_frame = [
  111. [sg.T(localip, font=('Helvetica', 15, 'normal'), auto_size_text=True,key='localip')]
  112. ]
  113. remote_ip4_frame = [
  114. [sg.T(dyndns.getcurrentremoteip(subdomains[0]), font=('Helvetica', 15, 'normal'), auto_size_text=True,key='remoteip')]
  115. ]
  116. # ------ Menu Definition ------ #
  117. menu_def = [['&File', ['&Quit']],
  118. ['&Tools', ['Add &new domain',
  119. 'Add &existing domain',
  120. '&Remove domain',
  121. '&List domains',
  122. '---',
  123. '&Refresh']],
  124. ['&Help', ['&Help',
  125. '&About']]
  126. ]
  127. # ------ GUI Defintion ------ #
  128. layout = [
  129. [sg.Menu(menu_def, tearoff=False, pad=(20, 1))],
  130. [sg.Text('Select domain ',font=(any,12)),
  131. sg.Combo(default_value=subdomains[0], values=subdomains, size=(20, 1), enable_events=True, key='-domain-',
  132. font=(any,12)),
  133. sg.Frame('Local IP', local_ip4_frame, font='Any 14', title_color='black', element_justification='center',
  134. pad=((103, 3),3))],
  135. [sg.Frame('Remote IP', remote_ip4_frame, font='Any 14', title_color='black',
  136. element_justification='center', pad=((450, 3), 3))],
  137. # [sg.Text(dyndns.local_ip4)]
  138. ]
  139. window = sg.Window("Digital Ocean DynDNS",
  140. layout,
  141. default_element_size=(12, 1),
  142. auto_size_text=False,
  143. auto_size_buttons=False,
  144. default_button_element_size=(12, 1),
  145. size=(700, 200),
  146. icon=dyndns.filepath+'dyndns.png')
  147. domains_active = False
  148. add_existing_active = False
  149. add_new = False
  150. remove_from_ini = False
  151. # ------ Loop & Process button menu choices ------ #
  152. while True:
  153. event, values = window.read()
  154. if event in (None, 'Quit'):
  155. return
  156. if event == '-domain-':
  157. currentdomain = values['-domain-']
  158. remoteip = dyndns.getcurrentremoteip(values['-domain-'])
  159. window.element('remoteip').Update(remoteip)
  160. if remoteip != localip:
  161. update = sg.popup_ok_cancel("The IP address (" + remoteip + ") for " + currentdomain +
  162. ' is different than your local IP address (' + localip + ').'
  163. '\n\nDo you want to update the remote IP?\n\n',
  164. title='Update remote IP',font=(any, 12),)
  165. if update == 'OK':
  166. result = dyndns.updateip(localip, currentdomain)
  167. if result[0]:
  168. window.element('remoteip').Update(localip)
  169. else:
  170. sg.popup_ok(result[1], title='Error updating IP!', font=(any, 12))
  171. else:
  172. continue
  173. # print('Event = ', event)
  174. # ------ Process menu choices ------ #
  175. if event == 'About':
  176. window.disappear()
  177. sg.popup('DO DynDNS is created with Python and PySimpleGUI.\n\nAuthor: Rune Olsen\n\n\n\n',
  178. title='About Digital Ocean DynDNS - v0.1', grab_anywhere=True,font=(any, 11))
  179. window.reappear()
  180. if event == 'Refresh':
  181. remoteip = dyndns.getcurrentremoteip(values['-domain-'])
  182. localip = dyndns.refreshlocalip()
  183. window.element('remoteip').Update(remoteip)
  184. window.element('localip').Update(localip)
  185. window.element('-domain-').Update(values=dyndns.refresh_domainnames_from_file())
  186. if event == 'List domains' and not domains_active: # only run if not already showing List domains
  187. list_domains()
  188. if event == 'Add existing domain' and not add_existing_active: # only run if not already showing Add existing
  189. add_existing_domain()
  190. window.element('-domain-').Update(values=dyndns.refresh_domainnames_from_file())
  191. if event == 'Add new domain' and not add_new: # only run if not already showing Add new
  192. add_new_domain()
  193. window.element('-domain-').Update(values=dyndns.refresh_domainnames_from_file())
  194. if event == 'Remove domain' and not remove_from_ini: # only run if not already showing remove domain
  195. remove_domain_from_dyndns()
  196. window.element('-domain-').Update(values=dyndns.refresh_domainnames_from_file())
  197. window.close()
  198. main_window()