dumped that AI-Shitt and rebuild from scratch . works now
This commit is contained in:
55
src/utils.py
55
src/utils.py
@@ -1,40 +1,33 @@
|
||||
def get_available_interfaces():
|
||||
import netifaces
|
||||
return netifaces.interfaces()
|
||||
|
||||
def format_ip_address(ip):
|
||||
try:
|
||||
parts = ip.split('.')
|
||||
if len(parts) != 4:
|
||||
raise ValueError("Invalid IP address format")
|
||||
return '.'.join(str(int(part)) for part in parts)
|
||||
except ValueError as e:
|
||||
raise ValueError(f"Error formatting IP address: {e}")
|
||||
|
||||
def validate_subnet_mask(mask):
|
||||
valid_masks = [
|
||||
'255.255.255.255', '255.255.255.254', '255.255.255.252',
|
||||
'255.255.255.248', '255.255.255.240', '255.255.255.224',
|
||||
'255.255.255.192', '255.255.255.128', '255.255.255.0',
|
||||
'255.255.254.0', '255.255.252.0', '255.255.248.0',
|
||||
'255.255.240.0', '255.255.224.0', '255.255.192.0',
|
||||
'255.255.128.0', '255.255.0.0', '255.254.0.0',
|
||||
'255.252.0.0', '255.248.0.0', '255.240.0.0',
|
||||
'255.224.0.0', '255.192.0.0', '255.128.0.0',
|
||||
'255.0.0.0', '254.0.0.0', '252.0.0.0',
|
||||
'248.0.0.0', '240.0.0.0', '224.0.0.0',
|
||||
'192.0.0.0', '128.0.0.0', '0.0.0.0'
|
||||
]
|
||||
if mask not in valid_masks:
|
||||
raise ValueError("Invalid subnet mask")
|
||||
|
||||
import socket
|
||||
import ipaddress
|
||||
import psutil
|
||||
from typing import Optional, Tuple
|
||||
|
||||
def get_network_interfaces():
|
||||
"""Return only interfaces that have an IPv4 address to keep choices sane."""
|
||||
interfaces = []
|
||||
for iface, addrs in psutil.net_if_addrs().items():
|
||||
for addr in addrs:
|
||||
if addr.family == socket.AF_INET:
|
||||
interfaces.append(iface)
|
||||
return interfaces
|
||||
break
|
||||
return sorted(interfaces)
|
||||
|
||||
def get_iface_ipv4_config(iface: str) -> Tuple[Optional[str], Optional[str]]:
|
||||
"""Return (ipv4_addr, netmask) for interface, or (None, None) if not present."""
|
||||
addrs = psutil.net_if_addrs().get(iface, [])
|
||||
for a in addrs:
|
||||
if a.family == socket.AF_INET:
|
||||
return a.address, a.netmask
|
||||
return None, None
|
||||
|
||||
def format_ip_address(ip: str) -> str:
|
||||
"""Normalize dotted quad; raises on invalid input."""
|
||||
ip_obj = ipaddress.ip_address(ip)
|
||||
if ip_obj.version != 4:
|
||||
raise ValueError("Only IPv4 is supported.")
|
||||
return str(ip_obj)
|
||||
|
||||
def validate_subnet_mask(mask: str) -> None:
|
||||
"""Accept masks like 255.255.255.0; raise ValueError if invalid."""
|
||||
ipaddress.IPv4Network(f"0.0.0.0/{mask}")
|
||||
|
||||
Reference in New Issue
Block a user