#!/bin/python3

import subprocess
import re
import sys
import urllib.request
import urllib.parse
from json2html import *
from os import listdir
from os.path import isfile, join



def export_to_html(cve_id, response):
    file=cve_id+".html"
    print("file ",file)
    print("input ",response)
    output=json2html.convert(json=response)
    print()
    outputFile=open(file,'w')
    outputFile.write(output)
    outputFile.close()
    return file


def search_by_CVE(cve_id):
    url = 'http://cve.circl.lu/api/cve/'+cve_id
    f = urllib.request.urlopen(url)
    response = f.read().decode('utf-8')
    #print(response)
    html=input("Export-html? Y/N: ")
    #print(html)
    if html == "Y" or html =="y":
        print("Export to: ",cve_id)
        file= export_to_html(cve_id, response)
        print("Success exported: ",file)
    input(" press any key ")

def search_lastes_CVE():
    print("last CVE")
    url = 'http://cve.circl.lu/api/last'
    f = urllib.request.urlopen(url)
    response = f.read().decode('utf-8')
    print(response)
    html=input("Export-html? Y/N: ")
    #print(html)
    cve_id="lastest_CVE"
    if html == "Y" or html =="y":
        print("Export to: ",cve_id)
        file= export_to_html(cve_id, response)
        print("Success exported: ",file)
    input(" press any key ")
        
def search_cve_year(year):
    print("year:",year)
    for id in range(0,9999):
    	cve_id="CVE-"+year+"-"+str(id).zfill(4)
    	print(cve_id)
    	url = 'http://cve.circl.lu/api/cve/'+cve_id
    	f = urllib.request.urlopen(url)
    	response = f.read().decode('utf-8')
    	if response != None and response !="" and response != "null":
    		file=export_to_html(cve_id, response)
    		print("wirte on : ",file)
    input(" press any key ")
    

def banner():
    print("\t  ____                      _        ______     _______ ")
    print("\t / ___|  ___  __ _ _ __ ___| |__    / ___\ \   / / ____|")
    print("\t \___ \ / _ \/ _` | '__/ __| '_ \  | |    \ \ / /|  _|  ")
    print("\t  ___) |  __/ (_| | | | (__| | | | | |___  \ V / | |___ ")
    print("\t |____/ \___|\__,_|_|  \___|_| |_|  \____|  \_/  |_____|")
    print("\t                                                               ")
    print("\t    __  __    _    ____ _____ _____ ____     ___  ____   ____ ____  ")
    print("\t   |  \/  |  / \  / ___|_   _| ____|  _ \   / _ \/ ___| / ___|  _ \  ")
    print("\t   | |\/| | / _ \ \___ \ | | |  _| | |_) | | | | \___ \| |   | |_) |")
    print("\t   | |  | |/ ___ \ ___) || | | |___|  _ <  | |_| |___) | |___|  __/ ")
    print("\t   |_|  |_/_/   \_\____/ |_| |_____|_| \_\  \___/|____/ \____|_|    Powered by F0ns1! ")




def menu_options():
    option= False
    while not option:
        banner()
        print("\n\t Welcome to CVE Search tool ");
        print("\tplease wait application started ..............\n\n")
        print("\t\t#####################################################################")
        print("\t\t## Menu Options ..")
        print("\t\t## \t 1) Search CVE by id")
        print("\t\t## \t 2) Search last CVE ")
        print("\t\t## \t 3) Search CVE by year ")
        print("\t\t## \t 4) launch webserver ")
        print("\t\t## \t 5) Exit Program ")
        print("\t\t#######################################################################")
        choose = str(input("\n\tselect an allowed option : "))
        if choose == "5" :
            option= True
            print("\tExiting........")
        if choose == "1":
            cve_id= input(" Enter CVE identifier ")
            search_by_CVE(cve_id)
        elif choose == "2":
            search_lastes_CVE()
        elif choose == "3":
            year= input("Enter Year : ")
            search_cve_year(year)
        elif choose == "4":
            port = input("Enter webserver port number: ")
            subprocess.call("python3 -m http.server "+port, shell =True)
        subprocess.call(["clear"])



#######
### Main 
##########

menu_options()
