# Path: celerynode/api.py
# def api_vulPoc(task_id, task_name, netloc, pluginInfo):
#     # .json payload
#     # for v in netlocList
#     for item_netloc in netloc:
#         vulPocCheck.apply_async(args=[task_id, task_name, item_netloc, pluginInfo, item_netloc["queue"]], queue=item_netloc["queue"])
# 
# def api_vulScript(task_id, task_name, netloc, pfileName, passDic):
#     #.py scripts
#     # for v in netlocList
#     for item_netloc in netloc:
#         vulScriptCheck.apply_async(args=[task_id, task_name, item_netloc, pfileName, passDic, item_netloc["queue"]], queue=item_netloc["queue"])
# 
# Path: server/es.py
# class Es():
#     def __init__(self):
#         USERNAME = ElasticConfig.USERNAME
#         PASSWORD = ElasticConfig.PASSWORD
#         HOST = ElasticConfig.HOST
#         PORT = ElasticConfig.PORT
#         if USERNAME == "" and PASSWORD == "":
#             CONN = "http://"+HOST+":"+PORT
#         else:
#             CONN = "http://"+USERNAME+":"+PASSWORD+"@"+HOST+":"+PORT
#         self.es = Elasticsearch([CONN])
#         self.confIndex = ElasticConfig.INDEX_CONFIG["index"]
#         self.confType = ElasticConfig.INDEX_CONFIG["type"]
#         self.assetIndex = ElasticConfig.INDEX_ASSETS["index"]
#         self.assetType = ElasticConfig.INDEX_ASSETS["type"]
# 
#     def init_scan_config(self):
#         doc = SCANCONFIG
#         if self.es.exists(index=self.confIndex, doc_type=self.confType, id="1"):
#             self.es.delete(index=self.confIndex, doc_type=self.confType, id="1")
#         self.es.create(index=self.confIndex, doc_type=self.confType, id="1", body=doc)
# 
#     def get_scan_config(self):
#         config = self.es.search(index=self.confIndex, doc_type=self.confType, body={
#         "query":{
#             "term" : { "_id" : "1" }
#         }
#     }, request_timeout=5)["hits"]["hits"]
#         if len(config) == 0:
#             return False
#         else:
#             return config[0]["_source"]
# 
#     def update_scan_config(self, name, value):
#         self.es.update(index=self.confIndex, doc_type=self.confType, id="1", body={
#             "doc":{
#                 name: value.strip()
#             }    
#         })
#         return True
# 
#     #vulscan 
#     def init_plugins(self):
#         if self.es.indices.exists(ElasticConfig.INDEX_VULTASKS["index"]):
#             self.es.indices.delete(ElasticConfig.INDEX_VULTASKS["index"])
#         self.es.indices.create(ElasticConfig.INDEX_VULTASKS["index"])
# 
#     #vulscan
#     def get_plugins_doc(self, name, value):
#         v = self.es.search(index=ElasticConfig.INDEX_VULTASKS["index"], doc_type=ElasticConfig.INDEX_VULTASKS["type"], body={
#             "query":{"term" : {  name : value}}
#             }, request_timeout=5)["hits"]["hits"]
#         
#         if len(v) == 0:
#             return False
#         else:
#             return v[0]["_source"]
# 
#     #vulscan
#     def insert_plugins_doc(self, doc):
#         self.es.index(index=ElasticConfig.INDEX_VULTASKS["index"], doc_type=ElasticConfig.INDEX_VULTASKS["type"], body=doc)
#         return True
# 
#     #return data <type 'generator'>
#     def get_all_plugins(self):
#         index = ElasticConfig.INDEX_VULTASKS["index"]
#         doc_type = ElasticConfig.INDEX_VULTASKS["type"]
#         scroll = "2m"
#         size = 30
#         body = {"query" : {"match_all": {}}}
#         data = helpers.scan(client=self.es, query=body, index=index, doc_type=doc_type, scroll=scroll, size=size)
#         return data
# 
#     #search assets
#     def get_assets_doc(self, searchGroup):
#         index = self.assetIndex
#         doc_type = self.assetType
#         scroll = "5m"
#         size = 100
#         body = {"query":{"bool": {"must": searchGroup}}}
#         data = helpers.scan(client=self.es, query=body, index=index, doc_type=doc_type, scroll=scroll, size=size)
#         return data


import os
import sys
import urllib2
import time
import datetime
import hashlib
import json
import re
import uuid
from celerynode.api import api_vulPoc, api_vulScript
from server.es import Es
                    self.poc_check()
                except Exception as e:
                    return None
            else:
                try:
                    self.script_check()
                except Exception as e:
                    return None

    def get_plugin_info(self):
        info = es.get_plugins_doc("_id", self.task_plugin_id) 
        self.plugin_info = info
        
    def load_json_plugin(self):
        json_plugin = open(sys.path[0] + '/vulscan/vuldb/' + self.plugin_info['filename']).read()
        self.plugin_info['plugin'] = json.loads(json_plugin)['plugin']

    def poc_check(self):
        task_id = self.task_id
        task_name = self.task_name
        netloc = self.task_netloc
        pluginInfo = self.plugin_info['plugin']
        api_vulPoc(task_id=task_id, task_name=task_name, netloc=netloc, pluginInfo=pluginInfo)

    def script_check(self):
        task_id = self.task_id
        task_name = self.task_name
        netloc = self.task_netloc
        pfileName = self.plugin_info['filename']
        passDic = self.passDic

# Path: celerynode/api.py
# def api_vulPoc(task_id, task_name, netloc, pluginInfo):
#     # .json payload
#     # for v in netlocList
#     for item_netloc in netloc:
#         vulPocCheck.apply_async(args=[task_id, task_name, item_netloc, pluginInfo, item_netloc["queue"]], queue=item_netloc["queue"])
# 
# def api_vulScript(task_id, task_name, netloc, pfileName, passDic):
#     #.py scripts
#     # for v in netlocList
#     for item_netloc in netloc:
#         vulScriptCheck.apply_async(args=[task_id, task_name, item_netloc, pfileName, passDic, item_netloc["queue"]], queue=item_netloc["queue"])
# 
# Path: server/es.py
# class Es():
#     def __init__(self):
#         USERNAME = ElasticConfig.USERNAME
#         PASSWORD = ElasticConfig.PASSWORD
#         HOST = ElasticConfig.HOST
#         PORT = ElasticConfig.PORT
#         if USERNAME == "" and PASSWORD == "":
#             CONN = "http://"+HOST+":"+PORT
#         else:
#             CONN = "http://"+USERNAME+":"+PASSWORD+"@"+HOST+":"+PORT
#         self.es = Elasticsearch([CONN])
#         self.confIndex = ElasticConfig.INDEX_CONFIG["index"]
#         self.confType = ElasticConfig.INDEX_CONFIG["type"]
#         self.assetIndex = ElasticConfig.INDEX_ASSETS["index"]
#         self.assetType = ElasticConfig.INDEX_ASSETS["type"]
# 
#     def init_scan_config(self):
#         doc = SCANCONFIG
#         if self.es.exists(index=self.confIndex, doc_type=self.confType, id="1"):
#             self.es.delete(index=self.confIndex, doc_type=self.confType, id="1")
#         self.es.create(index=self.confIndex, doc_type=self.confType, id="1", body=doc)
# 
#     def get_scan_config(self):
#         config = self.es.search(index=self.confIndex, doc_type=self.confType, body={
#         "query":{
#             "term" : { "_id" : "1" }
#         }
#     }, request_timeout=5)["hits"]["hits"]
#         if len(config) == 0:
#             return False
#         else:
#             return config[0]["_source"]
# 
#     def update_scan_config(self, name, value):
#         self.es.update(index=self.confIndex, doc_type=self.confType, id="1", body={
#             "doc":{
#                 name: value.strip()
#             }    
#         })
#         return True
# 
#     #vulscan 
#     def init_plugins(self):
#         if self.es.indices.exists(ElasticConfig.INDEX_VULTASKS["index"]):
#             self.es.indices.delete(ElasticConfig.INDEX_VULTASKS["index"])
#         self.es.indices.create(ElasticConfig.INDEX_VULTASKS["index"])
# 
#     #vulscan
#     def get_plugins_doc(self, name, value):
#         v = self.es.search(index=ElasticConfig.INDEX_VULTASKS["index"], doc_type=ElasticConfig.INDEX_VULTASKS["type"], body={
#             "query":{"term" : {  name : value}}
#             }, request_timeout=5)["hits"]["hits"]
#         
#         if len(v) == 0:
#             return False
#         else:
#             return v[0]["_source"]
# 
#     #vulscan
#     def insert_plugins_doc(self, doc):
#         self.es.index(index=ElasticConfig.INDEX_VULTASKS["index"], doc_type=ElasticConfig.INDEX_VULTASKS["type"], body=doc)
#         return True
# 
#     #return data <type 'generator'>
#     def get_all_plugins(self):
#         index = ElasticConfig.INDEX_VULTASKS["index"]
#         doc_type = ElasticConfig.INDEX_VULTASKS["type"]
#         scroll = "2m"
#         size = 30
#         body = {"query" : {"match_all": {}}}
#         data = helpers.scan(client=self.es, query=body, index=index, doc_type=doc_type, scroll=scroll, size=size)
#         return data
# 
#     #search assets
#     def get_assets_doc(self, searchGroup):
#         index = self.assetIndex
#         doc_type = self.assetType
#         scroll = "5m"
#         size = 100
#         body = {"query":{"bool": {"must": searchGroup}}}
#         data = helpers.scan(client=self.es, query=body, index=index, doc_type=doc_type, scroll=scroll, size=size)
#         return data


# Path: server/vulscan/vulScan.py
import os
import sys
import urllib2
import time
import datetime
import hashlib
import json
import re
import uuid
from celerynode.api import api_vulPoc, api_vulScript
from server.es import Es

                    self.poc_check()
                except Exception as e:
                    return None
            else:
                try:
                    self.script_check()
                except Exception as e:
                    return None

    def get_plugin_info(self):
        info = es.get_plugins_doc("_id", self.task_plugin_id) 
        self.plugin_info = info
        
    def load_json_plugin(self):
        json_plugin = open(sys.path[0] + '/vulscan/vuldb/' + self.plugin_info['filename']).read()
        self.plugin_info['plugin'] = json.loads(json_plugin)['plugin']

    def poc_check(self):
        task_id = self.task_id
        task_name = self.task_name
        netloc = self.task_netloc
        pluginInfo = self.plugin_info['plugin']
        api_vulPoc(task_id=task_id, task_name=task_name, netloc=netloc, pluginInfo=pluginInfo)

    def script_check(self):
        task_id = self.task_id
        task_name = self.task_name
        netloc = self.task_netloc
        pfileName = self.plugin_info['filename']
        passDic = self.passDic

        api_vulScript(task_id=task_id, task_name=task_name, netloc=netloc, pfileName=pfileName, passDic=passDic)