-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reverted back to original Updated build script and .spec file for aut…
…o packaging to rpm
- Loading branch information
Showing
8 changed files
with
301 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
ipaddress | ||
asyncio | ||
numpy | ||
ttkbootstrap | ||
pillow | ||
logging | ||
tkthread |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,102 @@ | ||
import asyncio | ||
import ipaddress | ||
import socket | ||
import logging | ||
import asyncio | ||
import numpy as np | ||
import socket | ||
|
||
logger = logging.getLogger(__name__) | ||
Version = "1.0.1.5" | ||
|
||
|
||
async def perform_port_scan(ip, port): | ||
"""Check if a TCP connection can be established to the host on the given port.""" | ||
try: | ||
reader, writer = await asyncio.open_connection(ip, port) | ||
writer.close() | ||
await writer.wait_closed() | ||
return True | ||
except Exception: | ||
return False | ||
async def scan_host(self, subnet, port_list): | ||
self.logger.info(f"Scanning IP addresses in {subnet}...") | ||
self.logger.info("----------------------------------------------------------") | ||
ip_net = ipaddress.ip_network(subnet) | ||
all_hosts = list(ip_net.hosts()) | ||
tasks = [scan_ports(self, str(ip), port_list) for ip in all_hosts] | ||
await asyncio.gather(*tasks) | ||
|
||
|
||
async def TCP_connect(self, ip, port_number): | ||
|
||
async def resolve_hostname(ip): | ||
"""Resolve the hostname for an IP address.""" | ||
conn = asyncio.open_connection(ip, port_number) | ||
try: | ||
return socket.gethostbyaddr(ip)[0] | ||
except socket.herror: | ||
return "Unknown" | ||
reader, writer = await asyncio.wait_for(conn, timeout=5) | ||
writer.close() | ||
await writer.wait_closed() | ||
return port_number, "Listening" | ||
except (asyncio.TimeoutError, ConnectionRefusedError): | ||
return port_number, "" | ||
except OSError: | ||
return port_number, "" | ||
|
||
|
||
async def scan_host(window, host, port_list, update_progress): | ||
"""Scan a single host for open ports.""" | ||
window.logger.info(f"Scanning host: {host}") | ||
async def get_hostname(ip): | ||
try: | ||
# Convert port list to a numpy array for efficient handling | ||
port_list = np.array(port_list) | ||
open_ports = await perform_port_scan(host, port_list) | ||
hostname = await resolve_hostname(host) | ||
|
||
# Ensure Treeview update is called in the main thread | ||
window.loop.call_soon_threadsafe( | ||
lambda: window.update_treeview( | ||
host, open_ports.tolist(), hostname | ||
) # Convert numpy array back to list | ||
hostname, _, _ = await asyncio.wait_for( | ||
asyncio.to_thread(socket.gethostbyaddr, ip), timeout=3 | ||
) | ||
return hostname.split()[0] | ||
except (socket.herror, asyncio.TimeoutError): | ||
return "Unknown" | ||
|
||
# Await the update_progress coroutine to ensure it runs properly | ||
await update_progress() # Update the progress bar | ||
except Exception as e: | ||
window.logger.error(f"Error scanning {host}: {e}") | ||
finally: | ||
window.logger.info(f"Finished scanning host: {host}") | ||
|
||
|
||
async def scan_subnet(window, subnet, port_list, update_progress): | ||
"""Scan all hosts in the given subnet.""" | ||
network = ipaddress.IPv4Network(subnet, strict=False) | ||
total_hosts = len(list(network.hosts())) | ||
scanned_hosts = 0 | ||
|
||
# Convert port list to numpy array for better performance | ||
port_list = np.array(port_list) | ||
|
||
async def update_progress(): | ||
"""Update the progress bar and meter.""" | ||
nonlocal scanned_hosts | ||
scanned_hosts += 1 | ||
progress = (scanned_hosts / total_hosts) * 100 | ||
window.loop.call_soon_threadsafe( | ||
lambda: [ | ||
window.progress.config(value=progress), | ||
window.status.config(text=f"Scanning progress: {progress:.2f}%"), | ||
window.meter.configure(amountused=scanned_hosts), | ||
] | ||
) | ||
async def scan_ports(self, host_ip, port_list): | ||
hostname = await get_hostname(host_ip) | ||
max_concurrent_connections = self.meter.amountusedvar.get() | ||
|
||
tasks = [ | ||
scan_host(window, str(ip), port_list, update_progress) for ip in network.hosts() | ||
] | ||
try: | ||
await asyncio.gather(*tasks) | ||
except asyncio.CancelledError: | ||
window.logger.info("Scan was cancelled.") | ||
finally: | ||
window.loop.call_soon_threadsafe( | ||
lambda: window.status.config(text="Scan complete!") | ||
self.logger.info( | ||
"Running {} concurrent connections on host {}".format( | ||
max_concurrent_connections, host_ip | ||
) | ||
|
||
|
||
def Version(): | ||
return "1.0.15" | ||
) | ||
semaphore = asyncio.Semaphore(max_concurrent_connections) | ||
|
||
async def limited_tcp_connect(ip, port): | ||
async with semaphore: | ||
return await TCP_connect(self, ip, port) | ||
|
||
port_array = np.array(port_list, dtype=int) | ||
# tasks = [TCP_connect(self, host_ip, port) for port in port_array] | ||
tasks = [limited_tcp_connect(host_ip, port) for port in port_array] | ||
results = await asyncio.gather(*tasks) | ||
res = np.array([], dtype=int) | ||
for port, status in results: | ||
if status == "Listening": | ||
res = np.append(res, port) | ||
self.results[host_ip] = { | ||
"ports": res.tolist(), | ||
"hostname": hostname, | ||
} | ||
existing_item = None | ||
for item in self.tree.get_children(): | ||
if self.tree.item(item, "values")[0] == host_ip: | ||
existing_item = item | ||
break | ||
|
||
if existing_item: | ||
self.tree.item( | ||
existing_item, | ||
values=( | ||
host_ip, | ||
np.array2string(np.array(res.tolist()), separator=", ") | ||
.replace("[", "") | ||
.replace("]", "") | ||
.replace(" ", "") | ||
.strip(), | ||
hostname, | ||
), | ||
) | ||
else: | ||
self.tree.insert( | ||
"", | ||
"end", | ||
values=( | ||
host_ip, | ||
np.array2string(np.array(res.tolist()), separator=", ") | ||
.replace("[", "") | ||
.replace("]", "") | ||
.replace(" ", "") | ||
.strip(), | ||
hostname, | ||
), | ||
) | ||
print(f"{host_ip}:{port} - {status}") |
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.