All checks were successful
Build and Release / Build Windows Exe (push) Successful in 10s
69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
import re
|
|
from typing import List
|
|
|
|
def clean_header(header: str) -> str:
|
|
if not header:
|
|
return ""
|
|
# Remove HTML tags if any (from Ruby script logic)
|
|
header = re.sub(r'<[^>]+>', '', str(header))
|
|
return header.strip().lower()
|
|
|
|
def parse_ports(port_str: str) -> List[int]:
|
|
"""
|
|
Parses a string containing ports, ranges, or 'any'.
|
|
Returns a list of integer ports.
|
|
"""
|
|
if not port_str:
|
|
return []
|
|
|
|
s = str(port_str).lower()
|
|
|
|
# Remove 'udp' if present to focus on port numbers,
|
|
# but arguably we might want to capture protocol.
|
|
# The Ruby script removed it. We'll strip it for port extraction.
|
|
s = re.sub(r'udp', '', s)
|
|
|
|
ports = set()
|
|
|
|
# Handle 'any' or 'all' - defaulting to common ports as per Ruby script
|
|
if 'any' in s or 'all' in s:
|
|
return [22, 3389, 80, 443, 3306, 5432, 8443, 60000]
|
|
|
|
# Split by common delimiters
|
|
parts = re.split(r'[,\n\s]+', s)
|
|
|
|
for part in parts:
|
|
part = part.strip()
|
|
if not part:
|
|
continue
|
|
|
|
# Range handling: 8000-8010
|
|
# The ruby script had issues with ranges, let's do it right.
|
|
range_match = re.match(r'^(\d+)[-](\d+)$', part)
|
|
if range_match:
|
|
start, end = map(int, range_match.groups())
|
|
if start <= end:
|
|
# User Request: "only add the first, last, and middle port"
|
|
ports.add(start)
|
|
ports.add(end)
|
|
if end - start > 1:
|
|
middle = start + (end - start) // 2
|
|
ports.add(middle)
|
|
continue
|
|
|
|
# Single port
|
|
if part.isdigit():
|
|
ports.add(int(part))
|
|
|
|
return sorted(list(ports))
|
|
|
|
def parse_ip(ip_str: str) -> List[str]:
|
|
"""Finds all IPv4 addresses in a string."""
|
|
if not ip_str:
|
|
return []
|
|
|
|
s = str(ip_str)
|
|
# Simple regex for IPv4
|
|
ips = re.findall(r'\b(?:\d{1,3}\.){3}\d{1,3}\b', s)
|
|
return list(set(ips))
|