-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
408 lines (330 loc) · 14.4 KB
/
Copy pathmain.py
File metadata and controls
408 lines (330 loc) · 14.4 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import subprocess
import platform
import socket
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.clock import Clock
from kivy.graphics import Color, Rectangle
import threading
# Set window constraints before app initialization
Window.minimum_width = 800
Window.minimum_height = 600
class TracertApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.is_tracing = False
def build(self):
Window.size = (1200, 800)
self.title = 'Trace Route Scanner'
main_layout = BoxLayout(orientation='vertical', padding=15, spacing=15)
main_layout.canvas.before.clear()
with main_layout.canvas.before:
Color(0.1, 0.1, 0.15, 1) # Dark blue-gray background
Rectangle(size=main_layout.size, pos=main_layout.pos)
main_layout.bind(size=self._update_bg, pos=self._update_bg)
header_layout = BoxLayout(orientation='horizontal', size_hint_y=None, height=50, spacing=15)
title_label = Label(
text='[b]Trace Route Scanner[/b]',
markup=True,
font_size='18sp',
color=(0.2, 0.8, 1, 1),
size_hint_x=0.3
)
header_layout.add_widget(title_label)
lookup_label = Label(text='Domain Lookup:', size_hint_x=0.15, bold=True, color=(1, 1, 1, 1))
self.target_input = TextInput(
multiline=False,
hint_text='Enter IP or hostname (e.g. google.com or 8.8.8.8)',
size_hint_x=0.65,
background_color=(0.15, 0.15, 0.2, 1),
foreground_color=(1, 1, 1, 1),
padding=(10, 16),
)
self.target_input.bind(on_text_validate=self.on_trace_pressed)
header_layout.add_widget(lookup_label)
header_layout.add_widget(self.target_input)
self.trace_button = Button(
text='Scan Network',
size_hint_x=0.2,
background_color=(0.1, 0.7, 1, 1),
background_normal=''
)
self.trace_button.bind(on_press=self.on_trace_pressed)
header_layout.add_widget(self.trace_button)
main_layout.add_widget(header_layout)
table_header = BoxLayout(size_hint_y=None, height=40, spacing=10, padding=(5, 0, 5, 0))
table_header.canvas.before.clear()
with table_header.canvas.before:
Color(0.15, 0.15, 0.25, 1)
self.table_header_rect = Rectangle(size=table_header.size, pos=table_header.pos)
table_header.bind(size=self._update_table_header_bg, pos=self._update_table_header_bg)
self._table_header_ref = table_header # Keep reference for updates
hop_num_header = Label(text='[b]#[/b]', markup=True, color=(0.5, 0.9, 1, 1), size_hint_x=0.15, font_size='18sp')
hop_addr_header = Label(text='[b]Hop[/b]', markup=True, color=(0.5, 0.9, 1, 1), size_hint_x=0.45, font_size='18sp')
rtt_header = Label(text='[b]Round-Trip Time[/b]', markup=True, color=(0.5, 0.9, 1, 1), size_hint_x=0.4, font_size='18sp')
table_header.add_widget(hop_num_header)
table_header.add_widget(hop_addr_header)
table_header.add_widget(rtt_header)
main_layout.add_widget(table_header)
scroll_view = ScrollView(size_hint=(1, 1))
self.results_container = GridLayout(
cols=1,
size_hint_y=None,
spacing=1,
padding=0
)
self.results_container.bind(minimum_height=self.results_container.setter('height'))
scroll_view.add_widget(self.results_container)
main_layout.add_widget(scroll_view)
status_layout = BoxLayout(size_hint_y=None, height=40, spacing=10)
self.status_label = Label(
text='Ready to scan',
size_hint_x=0.8,
color=(0.2, 1, 0.2, 1),
font_size='16sp'
)
status_layout.add_widget(self.status_label)
self.clear_button = Button(
text='Clear',
size_hint_x=0.2,
background_color=(1, 0.5, 0.2, 1),
background_normal=''
)
self.clear_button.bind(on_press=self.on_clear_pressed)
status_layout.add_widget(self.clear_button)
main_layout.add_widget(status_layout)
return main_layout
def _update_bg(self, instance, value):
"""Update background when widget resizes"""
instance.canvas.before.clear()
with instance.canvas.before:
Color(0.1, 0.1, 0.15, 1)
Rectangle(size=instance.size, pos=instance.pos)
def _update_table_header_bg(self, instance, value):
"""Update table header background when widget resizes or moves"""
if hasattr(self, 'table_header_rect'):
self.table_header_rect.pos = instance.pos
self.table_header_rect.size = instance.size
def _update_widget_bg(self, widget, color):
"""Update widget background"""
widget.canvas.before.clear()
with widget.canvas.before:
Color(*color)
Rectangle(size=widget.size, pos=widget.pos)
def get_local_ip(self):
"""Get the local IP address of the computer"""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except Exception:
return "127.0.0.1"
def on_trace_pressed(self, instance):
target = self.target_input.text.strip()
if not target:
self.status_label.text = '[color=ff3333]Please enter a target host[/color]'
self.status_label.markup = True
return
if self.is_tracing:
self.status_label.text = '[color=ffaa00]Trace already in progress...[/color]'
self.status_label.markup = True
return
thread = threading.Thread(target=self.run_tracert, args=(target,))
thread.daemon = True
thread.start()
def run_tracert(self, target):
self.is_tracing = True
Clock.schedule_once(lambda dt: self._update_trace_start_ui(), 0)
Clock.schedule_once(lambda dt: self.results_container.clear_widgets(), 0)
try:
system = platform.system()
if system == 'Windows':
cmd = ['tracert', target]
else:
cmd = ['traceroute', target]
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1, # Line buffered
universal_newlines=True
)
hop_count = 0
try:
for line in process.stdout:
line = line.strip()
print(line) # Checking the output of the subprocess
if not line:
continue
if 'Tracing route' in line or 'traceroute to' in line:
Clock.schedule_once(lambda dt, l=line: self._set_status_header(l), 0)
continue
if 'over a maximum' in line or 'Maximum hops' in line:
continue
if line and line[0].isdigit():
hop_count += 1
Clock.schedule_once(lambda dt, l=line: self._add_hop_line(l), 0)
else:
if line:
Clock.schedule_once(lambda dt, l=line: self._add_info_line(l), 0)
process.wait(timeout=3600)
Clock.schedule_once(
lambda dt, h=hop_count: self._show_completion(h),
0
)
except subprocess.TimeoutExpired:
process.kill()
Clock.schedule_once(lambda dt: self.show_error_timeout(), 0)
except Exception as e:
Clock.schedule_once(lambda dt, err=str(e): self.show_error(err), 0)
finally:
self.is_tracing = False
Clock.schedule_once(lambda dt: setattr(self.trace_button, 'disabled', False), 0)
def _set_status_header(self, line):
"""Set the status label to show the header info (on main thread)"""
self.status_label.text = f'[color=33ccff]{line}[/color]'
self.status_label.markup = True
def _add_hop_line(self, line):
"""Add a hop line to results (called on main thread)"""
hop_widget = self.create_hop_box(line)
if hop_widget:
self.results_container.add_widget(hop_widget)
def _add_info_line(self, line):
"""Add an info line to results (called on main thread)"""
info = Label(
text=line,
size_hint_y=None,
height=30,
markup=False,
color=(0.8, 0.8, 0.8, 1),
font_size='12sp'
)
self.results_container.add_widget(info)
def _show_completion(self, hop_count):
"""Show completion message (called on main thread)"""
self.status_label.text = f'[color=33ff33]Trace completed successfully ({hop_count} hops)[/color]'
self.status_label.markup = True
def _update_trace_start_ui(self):
"""Update UI when trace starts (on main thread)"""
self.status_label.text = '[color=ffaa00]Running trace... Please wait[/color]'
self.status_label.markup = True
self.trace_button.disabled = True
def show_error_timeout(self):
"""Show timeout error (called on main thread)"""
error_label = Label(
text='[color=ff3333]Error: Trace timed out after 60 seconds[/color]',
size_hint_y=None,
height=50,
markup=True
)
self.results_container.add_widget(error_label)
self.status_label.text = '[color=ff3333]Trace timeout[/color]'
self.status_label.markup = True
def show_error(self, error_msg):
"""Show general error (called on main thread)"""
error_label = Label(
text=f'[color=ff3333]Error: {error_msg}[/color]',
size_hint_y=None,
height=50,
markup=True
)
self.results_container.add_widget(error_label)
self.status_label.text = '[color=ff3333]Error occurred[/color]'
self.status_label.markup = True
def create_hop_box(self, line):
"""Create a table row for displaying hop information"""
try:
parts = line.split()
if not parts or not parts[0].isdigit():
return None
hop_num = parts[0]
address = 'Unknown' # Initialize with default
response_times = []
ip_start_idx = 1
for i in range(1, len(parts)):
if parts[i] == '*':
response_times.append('*')
elif i + 1 < len(parts) and parts[i + 1] == 'ms':
response_times.append(f"{parts[i]} ms")
ip_start_idx = i + 2
elif parts[i] == 'ms':
ip_start_idx = i + 1
elif parts[i] == 'Request':
address = 'Request timed out'
ip_start_idx = len(parts)
break
else:
break
if ip_start_idx < len(parts) and address != 'Request timed out':
address = ' '.join(parts[ip_start_idx:])
address = address.replace('[', '').replace(']', '')
elif address != 'Request timed out' and 'Request timed out' in line:
address = 'Request timed out'
if response_times and response_times[0] != '*':
response_time = ' '.join(response_times[:3]) # Show up to 3 response times
else:
response_time = 'N/A'
row_layout = BoxLayout(
orientation='horizontal',
size_hint_y=None,
height=35,
spacing=10,
padding=(5, 2, 5, 2)
)
hop_num_int = int(hop_num)
if hop_num_int % 2 == 0:
row_color = (0.12, 0.12, 0.18, 1)
else:
row_color = (0.14, 0.14, 0.2, 1)
row_layout.canvas.before.clear()
with row_layout.canvas.before:
Color(*row_color)
row_rect = Rectangle(size=row_layout.size, pos=row_layout.pos)
row_layout._row_color = row_color
row_layout._row_rect = row_rect
def update_row_bg(instance, value):
instance._row_rect.pos = instance.pos
instance._row_rect.size = instance.size
row_layout.bind(size=update_row_bg, pos=update_row_bg)
hop_label = Label(
text=hop_num,
size_hint_x=0.15,
color=(0.2, 0.8, 1, 1),
font_size='14sp',
bold=True
)
addr_label = Label(
text=address,
size_hint_x=0.45,
color=(1, 1, 1, 1),
font_size='14sp',
markup=False
)
rtt_label = Label(
text=response_time,
size_hint_x=0.4,
color=(0.8, 0.8, 0.8, 1),
font_size='14sp'
)
row_layout.add_widget(hop_label)
row_layout.add_widget(addr_label)
row_layout.add_widget(rtt_label)
return row_layout
except Exception:
return None
def on_clear_pressed(self, instance):
self.results_container.clear_widgets()
self.status_label.text = 'Ready'
self.status_label.markup = False
if __name__ == '__main__':
app = TracertApp()
app.run()