-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.py
366 lines (325 loc) · 15.5 KB
/
gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""Window class where we visualize what we do"""
import time
import os
import socket
import webbrowser
import dearpygui.dearpygui as dpg
import paramiko
import requests
import config
import logger
import threading
class Window():
"""Manage visual interface"""
def __init__(self) -> None:
"""
Window initialization
"""
print('Window initialization started.')
self.cfg = config.Config()
self.logger = logger.Logger()
self.__version__ = '1.0.2.2'
self.functions = {
'Test Connect': self.connect,
'Mass Execute': self.execute_cmd,
}
def callback(self, sender, data):
"""
Returns callback info.
"""
print(f'{sender} ==> {data}')
return sender, data
def create(self) -> None:
"""
Creates window context.
"""
dpg.create_context()
with dpg.window(
label='Window',
width=400,
height=500,
no_title_bar=True,
no_resize=True,
no_move=True,
tag='w_main'):
with dpg.tab_bar(label="Menu"):
with dpg.tab(label="Main"):
dpg.add_button(label='Refresh List',
width=100,
height=20,
tag='b_refresh',
callback=lambda: dpg.configure_item(item='servers_list',
items=self.cfg.get_servers()))
with dpg.group(horizontal=True):
dpg.add_listbox(items=self.cfg.get_servers(),
num_items=8,
width=200,
tag='servers_list')
with dpg.group(horizontal=False):
dpg.add_text(label='IP',
show=False,
color=(0, 255, 0),
tag='ip')
dpg.add_text(label='Username',
show=False,
color=(220, 0, 40),
tag='username')
dpg.add_text(label='Password',
show=False,
color=(220, 0, 40),
tag='password')
dpg.add_separator()
dpg.add_text(default_value='Commands File Name')
dpg.add_input_text(default_value='commands',
width=200,
tag='i_commands')
with dpg.group(horizontal=True):
dpg.add_radio_button(label='File Extension',
items=['.txt', '.json'],
default_value='.txt',
horizontal=True,
tag='rb_extension')
dpg.add_separator()
dpg.add_text(default_value='Chose Operation:')
dpg.add_combo(items=tuple(self.functions.keys()), tag='i_function')
dpg.add_button(label='Start',
width=80,
height=20,
tag='b_start',
callback=lambda: threading.Thread(target=self.functions.get(f"{dpg.get_value('i_function')}"),
daemon=True).start())
with dpg.tab(label="Logs"):
dpg.add_input_text(label='',
readonly=True,
multiline=True,
enabled=False,
width=350,
height=150,
tag='i_logs_area')
with dpg.group(horizontal=True):
dpg.add_button(label='Clear Logs',
callback=self.logger.flush)
with dpg.tab(label="Settings"):
dpg.add_checkbox(label='Show Server Info', tag='c_show_server_info')
dpg.add_input_float(label='Server Timeout',
default_value=3.0,
min_value=1.0,
max_value=15.0,
min_clamped=True,
max_clamped=True,
format='%.1f',
width=200,
tag='i_server_timeout')
dpg.add_button(label='Check For Updates',
width=140,
height=20,
tag='b_update',
callback=lambda: webbrowser.open(
'https://github.com/OpsecGuy/Awesome-Server-Manager/releases'
))
# Tooltips area
with dpg.tooltip(parent='c_show_server_info'):
dpg.add_text('Shows/Hides server info in Main tab.\nip\nlogin\npassword\n')
with dpg.tooltip(parent='i_server_timeout'):
dpg.add_text('Sets connection timeout for all functions.')
with dpg.tooltip(parent='i_commands'):
dpg.add_text('Sets name of file storing commands.\nProvide file name only!')
def update(self) -> None:
"""
Keeps GUI updated when changes are done.
"""
last_changed = ''
while True:
cmd_file_win = f"{os.getcwd()}\\{dpg.get_value('i_commands') + dpg.get_value('rb_extension')}"
cmd_file_lin = f"{os.getcwd()}/{dpg.get_value('i_commands') + dpg.get_value('rb_extension')}"
# Check if config file exists
if os.path.exists(self.cfg.config_path) is False:
self.logger.log(f'Could not find {self.cfg.config_file}.\n\
New config has been created.')
self.cfg.create_example()
# Check if command file exists
if config.platform.system() == 'Windows':
if os.path.exists(cmd_file_win) is False:
dpg.configure_item(item='text_color', value=(255, 0, 0))
else:
dpg.configure_item(item='text_color', value=(0, 255, 0))
elif config.platform.system() == 'Linux':
if os.path.exists(cmd_file_lin) is False:
dpg.configure_item(item='text_color', value=(255, 0, 0))
else:
dpg.configure_item(item='text_color', value=(0, 255, 0))
# TO:DO - Call it once if server has changed
if dpg.get_value('c_show_server_info') == True:
self.show_server_info()
dpg.set_value('ip', self.cfg.get_value(dpg.get_value('servers_list'), 'IP'))
dpg.set_value('username', self.cfg.get_value(dpg.get_value('servers_list'), 'username'))
dpg.set_value('password', self.cfg.get_value(dpg.get_value('servers_list'), 'password'))
elif dpg.get_value('c_show_server_info') == False:
self.hide_server_info()
# Static updates
dpg.set_value('i_logs_area', '\n'.join(self.logger.logs_buffer))
# Resizable
vp_width = dpg.get_viewport_width()
vp_height = dpg.get_viewport_height()
dpg.configure_item(item='w_main', width=vp_width - 5)
dpg.configure_item(item='w_main', height=vp_height - 5)
dpg.configure_item(item='i_logs_area', width=vp_width - 30)
time.sleep(0.001)
def run(self) -> None:
"""
Execute/Start window thread.
"""
self.custom_themes()
dpg.create_viewport(
title=f'Awesome Server Manager {self.__version__}',
height=500,
width=400,
resizable=True,
vsync=True)
dpg.bind_theme('base_theme')
dpg.bind_item_theme('i_commands', 'i_commands_file')
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
def custom_themes(self) -> None:
dpg.add_theme(tag='base_theme')
dpg.add_theme(tag='i_commands_file')
with dpg.theme_component(parent='base_theme'):
dpg.add_theme_style(dpg.mvStyleVar_WindowTitleAlign, 0.50, 0.50)
dpg.add_theme_color(dpg.mvThemeCol_CheckMark, (0, 255, 0))
dpg.add_theme_color(dpg.mvThemeCol_ButtonActive,(0, 142, 100, 130))
with dpg.theme_component(parent='i_commands_file'):
dpg.add_theme_color(dpg.mvThemeCol_Text, (255, 0, 0, 255), tag='text_color')
def destroy(self) -> None:
"""
Destroys window context.
"""
dpg.destroy_context()
def hide_server_info(self) -> None:
"""
Hides server info.
"""
dpg.hide_item('ip')
dpg.hide_item('username')
dpg.hide_item('password')
def show_server_info(self) -> None:
"""
Shows server info.
"""
dpg.show_item('ip')
dpg.show_item('username')
dpg.show_item('password')
def get_input_file(self) -> str:
"""
Retrieves file which stores commands.
Returns:
str: full file name with extension.
"""
command_file_name = dpg.get_value('i_commands')
file_extension = dpg.get_value('rb_extension')
abs_path = f"{os.getcwd()}\\{command_file_name + file_extension}"
if command_file_name != '' and os.path.exists(abs_path) is True:
return command_file_name + file_extension
self.logger.log(f'Could not find {command_file_name + file_extension} in\n{os.getcwd()}!')
return 'None'
def is_valid(self, protection: str) -> None:
"""Most of exceptions should be handled here.
Args:
protection (int):\n
light - Only checks if server name is not None.\n
full - Checks if server name is not None and checks if file with commands is valid.
Returns:
bool: True/False/None
"""
if dpg.get_value('servers_list') == 'None':
self.logger.log('[OTHER] Chose correct server!')
return False
if protection == 'full':
if self.get_input_file() == 'None':
return False
self.logger.log('[OTHER] Grabbing data from commands file.')
elif protection == 'light':
pass
return True
def parse_command(self) -> str:
"""
Command builder which is used later to execute on server.
Returns:
str: command
"""
buffer = ''
self.logger.log('[OTHER] Parsing commands...')
with open(self.get_input_file(), 'r', encoding='utf-8') as file:
for line in file.readlines():
if line != '\n':
escaped = ''.join(line.replace('\n', ' && '))
buffer = buffer + escaped
return buffer
def get_current_version(self):
"""Grabs version data from GitHub page.\n
Visit Github for the newest versions.
Returns:
str: current version
"""
try:
return requests.get(url='https://raw.githubusercontent.com/OpsecGuy/Awesome-Server-Manager/main/version',
headers={'Cache-Control': 'no-cache', 'Pragma': 'no-cache'},
timeout=5.0).text.replace('\n', '')
except requests.ReadTimeout as err:
pass
def connect(self) -> bool:
"""
Check if it's possible to connect to the server.\n
Error log is printed out in GUI.
"""
server_ip = self.cfg.get_value(dpg.get_value("servers_list"), "IP")
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if self.is_valid(protection='light'):
self.logger.log(f'[CONNECT] Connecting to {server_ip}')
try:
client.connect(hostname=server_ip,
port=int(self.cfg.get_value(dpg.get_value('servers_list'), 'port')),
username=self.cfg.get_value(dpg.get_value("servers_list"), "username"),
password=self.cfg.get_value(dpg.get_value("servers_list"), "password"),
timeout=dpg.get_value('i_server_timeout'))
client.close()
self.logger.log(f'[{server_ip}] Task Completed!')
except socket.timeout:
self.logger.log(f'[{server_ip}] Fail: Server Timed out!')
return False
def execute_cmd(self) -> bool:
"""
Executes prepared commands to be used on the server.\n
Creates log file in format: *ip*.txt.\n
If the same server has already log file, previous logs will be flushed.
"""
server_ip = self.cfg.get_value(dpg.get_value("servers_list"), "IP")
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if self.is_valid(protection='full'):
self.logger.log(f'[EXECUTE] Connecting to {server_ip}')
try:
client.connect(hostname=server_ip,
port=int(self.cfg.get_value(dpg.get_value('servers_list'), 'port')),
username=self.cfg.get_value(dpg.get_value("servers_list"), "username"),
password=self.cfg.get_value(dpg.get_value("servers_list"), "password"),
timeout=dpg.get_value('i_server_timeout'))
except socket.timeout:
self.logger.log(f'[{server_ip}] Fail: Server Timed out!')
return
try:
with open(f'log_{server_ip}.txt', 'w+', encoding='utf-8') as log_file:
self.logger.log(f'[{server_ip}] Writing server logs to log_{self.cfg.get_value(dpg.get_value("servers_list"), "IP")}.txt')
try:
stdin, stdout, stderr = client.exec_command(self.parse_command())
self.logger.log(f'[{server_ip}] Executing commands. Please wait...')
except paramiko.SSHException:
self.logger.log(f'[{server_ip}] Failed to execute commands!')
return
for output in iter(stdout.readline, ''):
log_file.writelines(output)
except OSError:
self.logger.log(f'[{server_ip}] Failed open/write to the log file!')
client.close()
self.logger.log(f'[{server_ip}] Task Completed!')