# Path: lib/api/zoomeye/base.py
# class ZoomEye(object):
#     def __init__(self, username=None, password=None):
#         self.username = username
#         self.password = password
# 
#         self.token = ''
#         self.zoomeye_login_api = "https://api.zoomeye.org/user/login"
#         self.zoomeye_dork_api = "https://api.zoomeye.org/{}/search"
# 
#     def auto_login(self):
#         msg = 'Trying to login with credentials in config file: %s' % paths.CONFIG_PATH
#         logger.info(msg)
#         try:
#             self.username = ConfigFileParser().ZoomEyeEmail()
#             self.password = ConfigFileParser().ZoomEyePassword()
#         except:
#             pass
# 
#         if bool(self.username and self.password):
#             if self.get_token():
#                 return
# 
#         msg = 'Automatic authorization failed.Please add your Account and Password to toolkit.conf'
#         logger.warning(msg)
#         self.manual_login()
# 
#     def manual_login(self):
#         msg = 'Please input your ZoomEye Email and Password below'
#         logger.info(msg)
#         self.username = raw_input('ZoomEye Account: ').strip()
#         self.password = getpass.getpass(prompt='ZoomEye Password: ').strip()
#         if not self.get_token():
#             msg = 'Invalid ZoomEye username or password'
#             sys.exit(logger.error(msg))
# 
#     def get_token(self):
#         # Please access https://www.zoomeye.org/api/doc#overview
#         data = '{{"username": "{}", "password": "{}"}}'.format(self.username,self.password)
#         resp = requests.post(self.zoomeye_login_api, data=data)
#         if resp and resp.status_code == 200 and 'access_token' in resp.json():
#             self.token = resp.json().get('access_token')
#             return self.token
#         return False
# 
#     def setToken(self, token):
#         """set Token from exist token string"""
#         self.token = token.strip()
# 
#     def dork_search(self, dork, page=0, resource='web', facet=['ip']):
#         """Search records with ZoomEye dorks.
# 
#         param: dork
#                ex: country:cn
#                access https://www.zoomeye.org/search/dorks for more details.
#         param: page
#                total page(s) number
#         param: resource
#                set a search resource type, ex: [web, host]
#         param: facet
#                ex: [app, device]
#                A comma-separated list of properties to get summary information
#         """
#         result = []
#         if isinstance(facet, (tuple, list)):
#             facet = ','.join(facet)
# 
#         zoomeye_api = self.zoomeye_dork_api.format(resource)
#         headers = {'Authorization': 'JWT %s' % self.token}
#         params = {'query': dork, 'page': page + 1, 'facet': facet}
#         resp = requests.get(zoomeye_api, params=params, headers=headers)
#         if resp and resp.status_code == 200 and 'matches' in resp.json():
#             matches = resp.json().get('matches')
#             # total = resp.json().get('total')  # all matches items num
#             result = matches
# 
#             # Every match item incudes the following information:
#             # geoinfo
#             # description
#             # check_time
#             # title
#             # ip
#             # site
#             # system
#             # headers
#             # keywords
#             # server
#             # domains
# 
#         return result
# 
#     def resources_info(self):
#         """Resource info shows us available search times.
# 
#         host-search: total number of available host records to search
#         web-search: total number of available web records to search
#         """
#         data = None
#         zoomeye_api = "https://api.zoomeye.org/resources-info"
#         headers = {'Authorization': 'JWT %s' % self.token}
#         resp = requests.get(zoomeye_api, headers=headers)
#         if resp and resp.status_code == 200 and 'plan' in resp.json():
#             data = resp.json()
# 
#         return data
# 
# Path: lib/core/data.py



import sys
from lib.api.zoomeye.base import ZoomEye
from lib.core.data import logger
#!/usr/bin/env python
#-*- coding:utf-8 -*-



def _initial():
    z = ZoomEye()
    z.auto_login()
    info = z.resources_info()['resources']
    if info:
        msg = 'Available ZoomEye search: (web:%s,host:%s)' % (info['web-search'], info['host-search'])
        logger.info(msg)
    else:
        msg = 'ZoomEye API authorization failed, Please re-run it and enter a new token.'

# Path: lib/api/zoomeye/base.py
# class ZoomEye(object):
#     def __init__(self, username=None, password=None):
#         self.username = username
#         self.password = password
# 
#         self.token = ''
#         self.zoomeye_login_api = "https://api.zoomeye.org/user/login"
#         self.zoomeye_dork_api = "https://api.zoomeye.org/{}/search"
# 
#     def auto_login(self):
#         msg = 'Trying to login with credentials in config file: %s' % paths.CONFIG_PATH
#         logger.info(msg)
#         try:
#             self.username = ConfigFileParser().ZoomEyeEmail()
#             self.password = ConfigFileParser().ZoomEyePassword()
#         except:
#             pass
# 
#         if bool(self.username and self.password):
#             if self.get_token():
#                 return
# 
#         msg = 'Automatic authorization failed.Please add your Account and Password to toolkit.conf'
#         logger.warning(msg)
#         self.manual_login()
# 
#     def manual_login(self):
#         msg = 'Please input your ZoomEye Email and Password below'
#         logger.info(msg)
#         self.username = raw_input('ZoomEye Account: ').strip()
#         self.password = getpass.getpass(prompt='ZoomEye Password: ').strip()
#         if not self.get_token():
#             msg = 'Invalid ZoomEye username or password'
#             sys.exit(logger.error(msg))
# 
#     def get_token(self):
#         # Please access https://www.zoomeye.org/api/doc#overview
#         data = '{{"username": "{}", "password": "{}"}}'.format(self.username,self.password)
#         resp = requests.post(self.zoomeye_login_api, data=data)
#         if resp and resp.status_code == 200 and 'access_token' in resp.json():
#             self.token = resp.json().get('access_token')
#             return self.token
#         return False
# 
#     def setToken(self, token):
#         """set Token from exist token string"""
#         self.token = token.strip()
# 
#     def dork_search(self, dork, page=0, resource='web', facet=['ip']):
#         """Search records with ZoomEye dorks.
# 
#         param: dork
#                ex: country:cn
#                access https://www.zoomeye.org/search/dorks for more details.
#         param: page
#                total page(s) number
#         param: resource
#                set a search resource type, ex: [web, host]
#         param: facet
#                ex: [app, device]
#                A comma-separated list of properties to get summary information
#         """
#         result = []
#         if isinstance(facet, (tuple, list)):
#             facet = ','.join(facet)
# 
#         zoomeye_api = self.zoomeye_dork_api.format(resource)
#         headers = {'Authorization': 'JWT %s' % self.token}
#         params = {'query': dork, 'page': page + 1, 'facet': facet}
#         resp = requests.get(zoomeye_api, params=params, headers=headers)
#         if resp and resp.status_code == 200 and 'matches' in resp.json():
#             matches = resp.json().get('matches')
#             # total = resp.json().get('total')  # all matches items num
#             result = matches
# 
#             # Every match item incudes the following information:
#             # geoinfo
#             # description
#             # check_time
#             # title
#             # ip
#             # site
#             # system
#             # headers
#             # keywords
#             # server
#             # domains
# 
#         return result
# 
#     def resources_info(self):
#         """Resource info shows us available search times.
# 
#         host-search: total number of available host records to search
#         web-search: total number of available web records to search
#         """
#         data = None
#         zoomeye_api = "https://api.zoomeye.org/resources-info"
#         headers = {'Authorization': 'JWT %s' % self.token}
#         resp = requests.get(zoomeye_api, headers=headers)
#         if resp and resp.status_code == 200 and 'plan' in resp.json():
#             data = resp.json()
# 
#         return data
# 
# Path: lib/core/data.py



# Path: lib/api/zoomeye/pack.py
import sys
from lib.api.zoomeye.base import ZoomEye
from lib.core.data import logger

#!/usr/bin/env python
#-*- coding:utf-8 -*-



def _initial():
    z = ZoomEye()
    z.auto_login()
    info = z.resources_info()['resources']
    if info:
        msg = 'Available ZoomEye search: (web:%s,host:%s)' % (info['web-search'], info['host-search'])
        logger.info(msg)
    else:
        msg = 'ZoomEye API authorization failed, Please re-run it and enter a new token.'

        sys.exit(logger.error(msg))