import random
import socket
import threading
from TheSilent.clear import clear

CYAN = "\033[1;36m"

def dolphin_thread(host,port):
    global hosts
    tcp_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    tcp_socket.settimeout(10)
    success = False
    try:
        tcp_socket.connect((host,port))
        success = True
        banner = tcp_socket.recv(65536)
        tcp_socket.close()
        hosts.append(f"{port}/tcp- {banner}")
    except:
        if success:
            hosts.append(f"{port}/tcp")
        
def dolphin(host):
    global hosts
    clear()
    print(CYAN + "scanning the top 100 ports")
    hosts = []
    # top 100 ports according to nmap
    port_list = [80,23,443,21,22,25,3389,110,445,139,143,53,135,3306,8080,1723,111,995,993,5900,1025,587,8888,199,1720,465,548,113,81,6001,10000,514,5060,179,1026,2000,8443,8000,32768,554,26,1433,49152,2001,515,8008,49154,1027,5666,646,5000,5631,631,49153,8081,2049,88,79,5800,106,2121,1110,49155,6000,513,990,5357,427,49156,543,544,5101,144,7,389,8009,3128,444,9999,5009,7070,5190,3000,5432,1900,3986,13,1029,9,5051,6646,49157,1028,873,1755,2717,4899,9100,119,37]
    port_list = random.sample(port_list,len(port_list))
    thread_list = []
    for port in port_list:
        my_thread = threading.Thread(target=dolphin_thread,args=(host,port,))
        thread_list.append(my_thread)
        my_thread.start()
    for thread in thread_list:
        thread.join()
    hosts.sort()
    clear()
    return hosts
