import os
import pyfiglet
import time

def terminal(client,jobs):
    print(pyfiglet.figlet_format(text = "MANAGE MAIN",font = "slant"))
    while True:
        try:
            print('press [1] to session')
            print('press [2] to job')
            print('press [0] to go back')
            command = input('Input Command Here: ')
            if(command == '1'):
                os.system('cls' if os.name == 'nt' else 'clear')
                __session_manage(client)
                print(pyfiglet.figlet_format(text = "MANAGE MAIN",font = "slant"))
            elif(command == '2'):
                os.system('cls' if os.name == 'nt' else 'clear')
                __jobs_manage(client,jobs)
                print(pyfiglet.figlet_format(text = "MANAGE MAIN",font = "slant"))
            elif command == '0':
                os.system('cls' if os.name == 'nt' else 'clear')
                break
            else:
                os.system('cls' if os.name == 'nt' else 'clear')
                print(pyfiglet.figlet_format(text = "MANAGE MAIN",font = "slant"))
                print('Error: Command Not Found..')
        except KeyboardInterrupt:
            os.system('cls' if os.name == 'nt' else 'clear')
            break
        # # print(pyfiglet.figlet_format(text = "MANAGE MAIN",font = "slant"))
        # print('press [1] to session')
        # print('press [2] to job')
        # print('press [0] to go back')
        # command = input('Input Command Here: ')
        # if(command == '1'):
        #     os.system('cls' if os.name == 'nt' else 'clear')
        #     __session_manage(client)
        #     print(pyfiglet.figlet_format(text = "MANAGE MAIN",font = "slant"))
        # elif(command == '2'):
        #     os.system('cls' if os.name == 'nt' else 'clear')
        #     __jobs_manage(client,jobs)
        #     print(pyfiglet.figlet_format(text = "MANAGE MAIN",font = "slant"))
        # elif command == '0':
        #     os.system('cls' if os.name == 'nt' else 'clear')
        #     break
        # else:
        #     os.system('cls' if os.name == 'nt' else 'clear')
        #     print(pyfiglet.figlet_format(text = "MANAGE MAIN",font = "slant"))
        #     print('Error: Command Not Found..')

def __session_manage(client):
    print(pyfiglet.figlet_format(text = "SESSION",font = "slant"))
    while True:
        # print(pyfiglet.figlet_format(text = "SESSION",font = "slant"))
        print(f'{len(client.sessions.list)} session(s) running' )
        session_list = client.sessions.list
        session_keys = session_list.keys()
        print('ID\tTYPE\tEXPLOIT\t\t\t\t\t\tPAYLOAD\t\t\t\tTUNNEL')
        for key in session_keys:
            # print(session_list[key])
            session = session_list[key]
            print(key,end='\t')
            print(session['type'],end='\t')
            print("{:48.48}".format(session['via_exploit']),end='')
            print(session['via_payload'],end='\t')
            print(session['tunnel_peer'],end='')
            print()
        print('press [1] to Stop session')
        print('press [2] to Stop all sessions')
        print('press [99] for help')
        print('press [0] to go back')
        command = input('Input Command Here: ')
        if command == '1':
            #individual stop function
            try:
                session_id = input('Input session Id to Stop: ')
                shell = client.sessions.session(session_id)
                shell.stop()
                os.system('cls' if os.name == 'nt' else 'clear')
                print(pyfiglet.figlet_format(text = "SESSION",font = "slant"))
            except:
                os.system('cls' if os.name == 'nt' else 'clear')
                print(pyfiglet.figlet_format(text = "SESSION",font = "slant"))
            
        elif command == '2':
            #stop all sessions
            # print('stop all')
            for s in client.sessions.list.keys():
                # print(s)
                shell = client.sessions.session(s)
                shell.stop()
            time.sleep(2)
            os.system('cls' if os.name == 'nt' else 'clear')
            print(pyfiglet.figlet_format(text = "SESSION",font = "slant"))
        elif command == '99':
            os.system('cls' if os.name == 'nt' else 'clear')
            print(pyfiglet.figlet_format(text = "SESSION",font = "slant"))
            print('Funciton [1]\tinput selected session ID to stop')
            print('Funciton [2]\tStop all Session')
            print('Go back with Ctrl+c')
            print()
        elif command == '0':
            os.system('cls' if os.name == 'nt' else 'clear')
            break
        else:
            os.system('cls' if os.name == 'nt' else 'clear')
            print(pyfiglet.figlet_format(text = "SESSION",font = "slant"))
            print('Error: Command Not Found..')


def __jobs_manage(client,jobs):
    print(pyfiglet.figlet_format(text = "JOB",font = "slant"))
    while True:
        
        print(f'{len(jobs.list)} job(s) running' )
        jobs_list = jobs.list
        jobs_keys = jobs_list.keys()
        print('ID\tEXPLOIT')
        for key in jobs_keys:
            print(key,end='\t')
            print(jobs_list[key].split('Exploit: ',1)[1])
            # print(jobs_list[key]['exploit'])
        print('press [1] to Stop Job')
        print('press [2] to Stop all Jobs')
        print('press [99] for help')
        print('press [0] to go back')
        command = input('Input Command Here: ')
        if command == '1':
            #individual stop function
            try:
                job_id = input('Input Job Id to Stop: ')
                # shell = client.sessions.session(session_id)
                jobs.stop(job_id)
                # shell.stop()
                os.system('cls' if os.name == 'nt' else 'clear')
                print(pyfiglet.figlet_format(text = "JOB",font = "slant"))
            except:
                os.system('cls' if os.name == 'nt' else 'clear')
                print(pyfiglet.figlet_format(text = "JOB",font = "slant"))
            
        elif command == '2':
            #stop all sessions
            # print('stop all')
            for j in jobs.list:
                print('stopping job ' + j+ ' ' + jobs.list[j])
                jobs.stop(jobid=j)
            time.sleep(2)
            os.system('cls' if os.name == 'nt' else 'clear')
            print(pyfiglet.figlet_format(text = "JOB",font = "slant"))
        elif command == '99':
            os.system('cls' if os.name == 'nt' else 'clear')
            print(pyfiglet.figlet_format(text = "SESSION",font = "slant"))
            print('Funciton [1]\tinput selected job ID to stop')
            print('Funciton [2]\tStop all Jobs')
            print('Go back with Ctrl+c')
            print()
        elif command == '0':
            os.system('cls' if os.name == 'nt' else 'clear')
            break
        else:
            os.system('cls' if os.name == 'nt' else 'clear')
            print(pyfiglet.figlet_format(text = "JOB",font = "slant"))
            print('Error: Command Not Found..')

def main():
    print('nmap module')
# This line runs the main function
if __name__ == "__main__":
    main()