def main():
    print('Put txt file in the same folder of exe!')
    table_name = 'lst_' + (input('Please name your list (lst_ will be add as prefix automatically):').lower())
    if table_name == 'lst_':
        sys.exit(0)
    exist_control = exist_list(table_name)
    if exist_control is None:
        create_table(table_name)
    elif exist_control == 0:
        drop_table(table_name)
        create_table(table_name)
    else:
        print('The named table is exist and contain ' + str(exist_control) + ' Records')
        ask = input('Do you want to drop ' + str(exist_control) + ' table? (yes/no)')
        if ask.lower() == 'yes':
            drop_table(table_name)
            create_table(table_name)

        else:
            print('Exit without any change!')
            sys.exit(0)

    extension = str(input('Please Enter Your Extension(ir,co.ir ,...):'))
    list_method=input('Choice your Method( 1:Auto Letter Generator 2:Import Text File ) :')
    if list_method == '1':
        letter_number = int(input('Number of Letters:'))
        keywords = [''.join(i) for i in product(ascii_lowercase, repeat=letter_number)]
        db_config = read_db_config()
        conn = MySQLConnection(**db_config)

        for line in range(0, len(keywords)):
            cursor = conn.cursor()
            Query = "insert into %s (site) values (concat('%s','.','%s'));" \
                    % (table_name, '{0}'.format(str(keywords[line])),extension)
            cursor.execute(Query)
            print(str(line+1),end='\r')
        print(str(line+1),'Records Imported!')
    elif list_method == '2':
        dic_filename_mask = str(input('Whats the Text Dictionary Filename {without extension}:'))
        filename = dic_filename_mask + '.txt'
        dic_list = open(filename)
        print('Total Count of Records: ' + str(sum(1 for _ in dic_list)))
        dic_list.close()
        db_config = read_db_config()
        conn = MySQLConnection(**db_config)
        cursor = conn.cursor()
        load_text_file_query = "LOAD DATA LOCAL INFILE '%s' INTO TABLE %s  LINES TERMINATED BY '\\r\\n' (SITE);" \
                               % (filename, table_name)
        print('Transferring List...')
        cursor.execute(load_text_file_query)
        print('Add Extension to list...')
        update_extension_query = "update %s set site=concat(site,'.','%s')" % (table_name, extension)
        cursor.execute(update_extension_query)
        conn.commit()
        cursor.close()
        conn.close()
    else:
        print('Wrong Choice,Bye!')

    print("Finish")
def db_update(query):
    db_reconnect_status = 0
    global conn
    while db_reconnect_status == 0:
        try:
            query_result_exec = conn.cursor()
            query_result_exec.execute(query)
            conn.commit()
            db_reconnect_status += 1
            query_rows = query_result_exec.rowcount
            query_id = conn.connection_id
            return query_rows, query_id
        except Exception:
            db_reconnect2_status = 0
            while 0 == db_reconnect2_status:
                print(Fore.RED + 'Connecting Database...' + '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(),
                                                                                          end='\r'))
                try:
                    db_config = read_db_config()
                    conn = MySQLConnection(**db_config)
                    db_reconnect2_status += 1
                    print(Fore.BLUE + 'Database Connection ID:' + str(conn.connection_id))
                except Exception:
                    sleep(5)
                    db_reconnect2_status = 0
def create_table(tbl_name_val):
    query = "CREATE TABLE IF NOT EXISTS %s ( " \
            "No int NOT NULL auto_increment," \
            "Site varchar(100)," \
            "Status varchar(30)," \
            "Email varchar(100)," \
            "Person varchar(100)," \
            "Phone varchar(25)," \
            "lockedby bigint,  " \
            "lockedat datetime," \
            "Trycount int default 0 , " \
            "Hostname varchar(50)," \
            "HFlag char(1) DEFAULT NULL," \
            "IP varchar(15) DEFAULT NULL," \
            "PRIMARY KEY (No)" \
            ") ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;" % tbl_name_val
    args = tbl_name_val

    try:
        db_config = read_db_config()
        conn = MySQLConnection(**db_config)

        cursor = conn.cursor()
        cursor.execute(query)
        print('New Table Created:' + str(tbl_name_val))

    except Error as error:
        print(error)

    finally:
        cursor.close()
        conn.close()
def db_select(query):
    db_reconnect_status = 0
    while db_reconnect_status == 0:
        try:
            global conn
            query_result_exec = conn.cursor()
            query_result_exec.execute(query)
            query_result = query_result_exec.fetchall()
            db_reconnect_status += 1
            return query_result
        except Exception:
            db_reconnect2_status = 0
            while 0 == db_reconnect2_status:
                print(
                    Fore.RED
                    + "Reconnecting Database..."
                    + "{:%Y-%m-%d %H:%M:%S}".format(datetime.datetime.now(), end="\r")
                )
                try:
                    db_config = read_db_config()
                    conn = MySQLConnection(**db_config)
                    db_reconnect2_status += 1
                    print(Fore.RED + "Database Connection ID:" + str(conn.connection_id))
                except Exception:
                    sleep(5)
                    db_reconnect2_status = 0
def main():
    conn_id = None
    global conn

    while conn_id is None:
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            conn_id = conn.connection_id
        except Exception:
            conn_id = None
            print('Try connecting to database...')

    if config_section_map("ip_resolver")['default_list_name']:
        selected_list = str(config_section_map("ip_resolver")['default_list_name'])
        loop_counter = 1
        process_counter = 0
        while loop_counter > 0:
            # lock record for process
            lock_record_query = 'update %s set lockedby=connection_id(),lockedat=now(),HFlag=\'I\' ' \
                                'where lockedby is null and status=\'Site registered\' and HFlag<>\'R\' ' \
                                'and (ip is null or ip=\'Fail\') and trycount<%s order by trycount limit %s '\
                                % (selected_list,config_section_map("ip_resolver")['try_count'],
                                    int(config_section_map("ip_resolver")['buffer']))
            loop_counter, connection_d = db_update(lock_record_query)

            if loop_counter:
                todo_query = 'select no,site from %s where  lockedby=%s ' % (
                    selected_list, connection_d)
                todo_result = db_select(todo_query)
                if todo_result:

                    for to_do_line in range(0, len(todo_result)):
                        process_counter += 1
                        try:
                            look_for = todo_result[to_do_line][1]
                            looking_for = (str(look_for.strip()))
                            ip = host2ip('www.' + looking_for)
                            print(Fore.CYAN + looking_for+': '+ip)
                            if ip == 'Fail':
                                return_update_query = "update %s set HFlag=\'I\',lockedby=null,ip='%s' " \
                                                      ",trycount=trycount+1 where  No=%s " \
                                                      % (selected_list, ip, todo_result[to_do_line][0])
                            else:
                                return_update_query = "update %s set HFlag=\'R\',lockedby=null,ip='%s' " \
                                                      ",trycount=trycount+1 where  No=%s " \
                                                      % (selected_list, ip, todo_result[to_do_line][0])

                            db_update(return_update_query)

                        except Exception:
                            print('exception')
                            return_update_query = "update %s set HFlag=Null,lockedby=null " \
                                                  ",trycount=trycount+1 where  No=%s " \
                                                  % (selected_list, todo_result[to_do_line][0])
                            db_update(return_update_query)
                else:
                    print('All record are processed for ' + selected_list + ' list!')
def drop_table(tbl_name_val):
    drop_item = "drop table %s" % tbl_name_val
    args = tbl_name_val
    try:
        db_config = read_db_config()
        conn = MySQLConnection(**db_config)

        cursor = conn.cursor()
        cursor.execute(drop_item)
        print(str(tbl_name_val) + ' Table Droped!')

    except Error as error:
        print(error)

    finally:
        cursor.close()
        conn.close()
def exist_list(tbl_name_val):
    table_list = "select table_name,table_rows from information_schema.tables where table_name='%s' ;" % tbl_name_val
    args = tbl_name_val
    try:

        db_config = read_db_config()
        conn = MySQLConnection(**db_config)
        cursor = conn.cursor()
        cursor.execute(table_list)
        res1 = cursor.fetchall()
        for row_count in res1:
            return row_count[1]
    except Error as error:
        print(error)

    finally:
        cursor.close()
        conn.close()
def main():
    conn_id = None
    global conn

    while conn_id is None:
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            conn_id = conn.connection_id
        except Exception:
            conn_id = None
            print('Try connecting to database...')

    if config_section_map("dns_reverser")['default_list_name']:
        selected_list = str(config_section_map("dns_reverser")['default_list_name'])
        todo_result_rows = 1
        process_counter = 0
        while todo_result_rows > 0:
            todo_query = 'select distinct(ip) from %s where ip is not null and ip!=\'Fail\' and ip not in ' \
                         '(select refrence from harverster_hosts) limit %s' % \
                         (selected_list, int(config_section_map("dns_reverser")['buffer']))
            todo_result, todo_result_rows = db_select(todo_query)

            if todo_result:

                for to_do_line in range(0, len(todo_result)):
                    process_counter += 1

                    look_for = todo_result[to_do_line][0]
                    looking_for = (str(look_for.strip()))

                    reverse_result = reverse_dns(looking_for)
                    if reverse_result is None:
                        return_insert_query = "insert into  harverster_hosts (host,refrence) " \
                                              "values ('%s','%s')" \
                                              % ('None', todo_result[to_do_line][0])
                        db_update(return_insert_query)

                    else:
                        for line in reverse_result:
                            return_insert_query = "insert into  harverster_hosts (host,refrence) " \
                                                  "values ('%s','%s')" \
                                                  % ((reverse_result[line][0])[:100], todo_result[to_do_line][0])
                            db_update(return_insert_query)
def main():
    from mysql.connector import MySQLConnection, Error
    from tabulate import tabulate
    from config import read_db_config
    import sys
    import os
    # List available table

    table_list = "select table_name,table_rows from information_schema.tables " \
                 "where table_name like 'lst_%' and table_rows>0;"
    db_config = read_db_config()
    conn = MySQLConnection(**db_config)
    cursor_table_list = conn.cursor()
    cursor_table_list.execute(table_list)
    table_list_exec = (cursor_table_list.fetchall())
    table_list_result = {}
    for line in range(0, len(table_list_exec)):
        cursor_table_detail = conn.cursor()
        table_records_status_query = "select count(site),(select case when count(site)=0 then 'Completed'" \
                                     " else 'Not Completed' end from %s where status='Passed' or status is null ) " \
                                     "as Status  from %s where email!='' " \
                                     % (table_list_exec[line][0],table_list_exec[line][0])
        cursor_table_detail.execute(table_records_status_query)
        table_records_status_result = cursor_table_detail.fetchall()
        table_list_result.setdefault(table_list_exec[line][0], []).append(table_records_status_result[0][0])
        table_list_result.setdefault(table_list_exec[line][0], []).append(table_list_exec[line][1])
        table_list_result.setdefault(table_list_exec[line][0], []).append(table_records_status_result[0][1])
    print('Please Choice your dictionary list from below list')
    print(tabulate(zip(table_list_result.keys(), table_list_result.values(), ),
                   headers=['List Name', 'Email Count/Record Count/Status'],
                   tablefmt="grid"))
    selected_list = input('List Name:').lower()
    if selected_list in table_list_result.keys():
        export_list_type = input('0 for all records,1 for records with email\n')
        start_record = int(input('Export start from record No:(0 for beginning)\n'))
        result_filename = selected_list + '_result.txt'
        if os.path.isfile(result_filename):
            overwrite_ask = input('The result file is exist! do you want to overwrite it?(y/n):')
            if overwrite_ask.lower() == 'y':
                try:
                    os.remove(result_filename)
                except OSError:
                    print('Error:' + OSError.winerror)
            else:
                sys.exit(0)
            result_file = open(result_filename,"w")
            result_file.write('No|Site|Status|Email|Person|Phone\n')
            result_file.close()
        if export_list_type == '1': #Just Data with Email
            export_list_query = "select No,Site,ifnull(Status,'') as Status,ifnull(Email,'') as Email," \
                                "ifnull(Person,'') as Person,ifnull(Phone,'') as Phone from %s " \
                                "where email!='' and email is not null and status='Site registered' " \
                                "and no>%s order by No " % (selected_list,start_record)
        else:
            export_list_query = "select No,Site,ifnull(Status,'') as Status,ifnull(Email,'') as Email," \
                                "ifnull(Person,'') as Person,ifnull(Phone,'') as Phone from %s " \
                                "where no>%s order by No;" % (selected_list,start_record)
        export_list_curssor = conn.cursor()
        export_list_curssor.execute(export_list_query)
        export_result=export_list_curssor.fetchall()
        for line in range(0, len(export_result)):
            with open(result_filename, "a") as fw:
                fw.write('{0}|{1}|{2}|{3}|{4}|{5}\n'.format(str(export_result[line][0]), str(export_result[line][1]),
                                                        str(export_result[line][2]), str(export_result[line][3]),
                                                        str(export_result[line][4]),str(export_result[line][5])))
                print(str(line+1),'/',str(export_list_curssor.rowcount),'Processed',end='\r')
        conn.close

    else:
        print('Wrong List Name')
def main():
    # List available table
    conn_id = None
    global conn

    internet_try_counter = 1
    while internet_status() is not True:
        print(
            "Connection is lost!,Check your internet connection! Refresh interval:",
            int(config_section_map("general")["ip_refresh_second"]),
            "second,Try:",
            internet_try_counter,
        )
        sleep(int(config_section_map("general")["ip_refresh_second"]))
        internet_try_counter += 1
    ip_address = public_ip()

    while conn_id is None:
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            conn_id = conn.connection_id
        except Exception:
            conn_id = None
            print("Try connecting to database...")

    table_list = (
        "select table_name,table_rows from information_schema.tables " "where table_name like 'lst_%' and table_rows>0;"
    )
    table_list_exec = db_select(table_list)
    table_list_result = {}
    for line in range(0, len(table_list_exec)):
        table_records_status_query = (
            "select count(site) from %s where status='Passed' or status is null "
            "or status='Registered No Email'" % (table_list_exec[line][0])
        )
        table_records_status_result = db_select(table_records_status_query)
        table_list_result.setdefault(table_list_exec[line][0], []).append(table_records_status_result[0][0])
        table_list_result.setdefault(table_list_exec[line][0], []).append(table_list_exec[line][1])
    if config_section_map("general")["default_list_name"]:
        selected_list = str(config_section_map("general")["default_list_name"])
        print("Searching " + selected_list + " list...")
        if selected_list in table_list_result.keys():
            print("Processable,Total Record:" + str(table_list_result[selected_list]))
    else:
        print("Please Choice your dictionary list from below list")
        print(
            tabulate(
                zip(table_list_result.keys(), table_list_result.values()),
                headers=["List Name", "Processable/Record Count"],
                tablefmt="grid",
            )
        )
        selected_list = input("List Name:").lower()

    if selected_list in table_list_result.keys():
        delay = float(config_section_map("search")["delay_second"])
        whois = "whois.nic.ir"
        port = 43
        try_count = config_section_map("search")["try_count"]
        loop_counter = 1
        process_counter = 0
        passed_counter = 0
        monitoring(socket.gethostname(), "Searching", str(ip_address))
        while loop_counter > 0:
            # lock record for process
            lock_record_query = (
                "update %s set lockedby=connection_id(),lockedat=now(),"
                "trycount=trycount+1,Hostname='%s' "
                "where lockedby is null and (status='Passed' or status is null "
                "or status='Registered No Email' ) and trycount<%s order by trycount limit %s"
                % (selected_list, socket.gethostname(), try_count, int(config_section_map("search")["buffer"]))
            )
            loop_counter, connection_d = db_update(lock_record_query)

            if loop_counter:
                todo_query = "select no,site from %s where  lockedby=%s and trycount<=%s " % (
                    selected_list,
                    connection_d,
                    try_count,
                )
                todo_result = db_select(todo_query)
                if todo_result:

                    for to_do_line in range(0, len(todo_result)):

                        if (
                            process_counter > 0
                            and (process_counter / int(config_section_map("search")["mid_sleep_record"]))
                            - (math.floor(process_counter / int(config_section_map("search")["mid_sleep_record"])))
                            == 0
                        ):
                            print(
                                Fore.BLUE
                                + "Sleep mode:"
                                + str(config_section_map("search")["mid_sleep_time"])
                                + " second {:%Y-%m-%d %H:%M:%S}".format(datetime.datetime.now()),
                                "Session Processed Count:",
                                process_counter,
                                end="\r",
                            )
                            monitoring(socket.gethostname(), "Sleep Mode", str(ip_address))
                            sleep(int(config_section_map("search")["mid_sleep_time"]))
                            monitoring(socket.gethostname(), "Searching", str(ip_address))
                        process_counter += 1

                        try:
                            sleep(delay)
                            look_for = todo_result[to_do_line][1]
                            looking_for = str(look_for.strip())
                            telnet_connect = Telnet(
                                whois, port, timeout=int(config_section_map("telnet")["telnet_timeout"])
                            )
                            telnet_connect.write(looking_for.encode("ascii") + b"\r\n")
                            telnet_result = str(telnet_connect.read_all())

                            if telnet_result == "b''":
                                print(str(todo_result[to_do_line][0]) + " : Passed --> " + looking_for, end="\r")
                                return_update_query = (
                                    "update %s set Status='Passed',lockedby=null ,Hostname='%s' "
                                    "where  No=%s " % (selected_list, socket.gethostname(), todo_result[to_do_line][0])
                                )
                                db_update(return_update_query)

                            else:
                                telnet_parser(
                                    telnet_result, todo_result, to_do_line, look_for, selected_list, looking_for
                                )
                                passed_counter = 0

                        except Exception:
                            passed_counter += 1
                            print(
                                Fore.RED + "Telnet exception Passed -->",
                                str(look_for),
                                "Count",
                                passed_counter,
                                end="\r",
                            )
                            winsound.Beep(9000, 300)
                            return_update_query = (
                                "update %s set Status='Passed',lockedby=null ,Hostname='%s' "
                                "where  No=%s " % (selected_list, socket.gethostname(), todo_result[to_do_line][0])
                            )
                            db_update(return_update_query)
                            if passed_counter >= int(config_section_map("search")["block_counter_out"]):
                                ip_address = public_ip()
                                monitoring(socket.gethostname(), "Blocked", str(ip_address))
                                mail_sender(socket.gethostname(), "Blocked")
                                block_out(ip_address)
        else:
            print("All record are processed for " + selected_list + " list!")
    else:
        monitoring(socket.gethostname(), "Finished", str(ip_address))
        print("List does not exist!Bye!")
        sys.exit(0)