# /var/bin/env python3
"""
Script para descubrir puertos abiertos en un servidor web 
a través de un proxy Squid.
"""


import requests
import signal
import sys


def signal_handler(sig, frame):
    print('You pressed Ctrl+C!')
    sys.exit(0)


# Ctrl+C handler
signal.signal(signal.SIGINT, signal_handler)

MAIN_URL = "http://127.0.0.1"
squid_proxy = {'http': 'http://192.168.1.150:3128'}


def port_discovery():

    # Puertos TCP más comunes
    puertos_comunes = {21, 22, 23, 25, 53, 80, 110, 111, 135, 139, 143, 443,
                       445, 993, 995, 1723, 3306, 3389, 5900, 8080, 8443,
                       8888, 9100, 1433, 1521, 2049, 3128, 389, 636, 873,
                       1080, 1434, 1521, 1604, 1720, 1863, 2048, 4000,
                       5800, 5900, 6000, 6660, 6661, 6662, 6663, 6664,
                       6665, 6666, 6667, 6668, 6669}

    for tcp_port in puertos_comunes:

        try:

            r = requests.get(
                main_url + ':' + str(tcp_port),
                proxies=squid_proxy, timeout=1
            )

            print(r.status_code)

        except requests.exceptions.RequestException as e:

            print("Error: " + str(e))
            continue

        if r.status_code != 503:

            print("\n[+] Puerto abierto: " + str(tcp_port))


if __name__ == "__main__":

    port_discovery()
