# ============== #
# NMAP FUNCTIONS #
# ============== #
import os
import re
from typing import List


def ip_from_gnmap(filepath: str) -> List[str]:
    data = open(filepath).readlines()
    data = [e for e in data if "Status: Up" in e]
    ip = [e.split()[1] for e in data]
    return ip


def get_ports_from_gnmap(filepath: str, udp=False) -> str:
    if udp:
        protocol = "udp"
    else:
        protocol = "tcp"
    data = open(filepath).read()
    # FIXME: We can filter by IP?
    ports = re.findall(rf"(\d+)/open/{protocol}", data)
    ports = ",".join(ports)
    return ports


def get_nmap_host_path(target):
    return os.path.join(FILE_NMAP_HOSTS, f"{target}")


def get_nmap_host_path_name(target, name):
    host_path = get_nmap_host_path(target)
    return os.path.join(host_path, f"{target}_{name}")
