# Path: mobsf/MobSF/utils.py
# def find_java_binary():
#     """Find Java."""
#     # Respect user settings
#     if platform.system() == 'Windows':
#         jbin = 'java.exe'
#     else:
#         jbin = 'java'
#     if is_dir_exists(settings.JAVA_DIRECTORY):
#         if settings.JAVA_DIRECTORY.endswith('/'):
#             return settings.JAVA_DIRECTORY + jbin
#         elif settings.JAVA_DIRECTORY.endswith('\\'):
#             return settings.JAVA_DIRECTORY + jbin
#         else:
#             return settings.JAVA_DIRECTORY + '/' + jbin
#     if os.getenv('JAVA_HOME'):
#         java = os.path.join(
#             os.getenv('JAVA_HOME'),
#             'bin',
#             jbin)
#         if is_file_exists(java):
#             return java
#     return 'java'
# 
# def is_file_exists(file_path):
#     if os.path.isfile(file_path):
#         return True
#     # This fix situation where a user just typed "adb" or another executable
#     # inside settings.py/config.py
#     if shutil.which(file_path):
#         return True
#     else:
#         return False
# 
# def is_internet_available():
#     try:
#         proxies, verify = upstream_proxy('https')
#     except Exception:
#         logger.exception('Setting upstream proxy')
#     try:
#         requests.get(settings.GOOGLE,
#                      timeout=5,
#                      proxies=proxies,
#                      verify=verify)
#         return True
#     except Exception:
#         try:
#             requests.get(settings.BAIDU,
#                          timeout=5,
#                          proxies=proxies,
#                          verify=verify)
#             return True
#         except Exception:
#             return False
# 
# def update_local_db(db_name, url, local_file):
#     """Update Local DBs."""
#     update = None
#     inmemoryfile = None
#     try:
#         proxies, verify = upstream_proxy('https')
#     except Exception:
#         logger.exception('[ERROR] Setting upstream proxy')
#     try:
#         response = requests.get(url,
#                                 timeout=3,
#                                 proxies=proxies,
#                                 verify=verify)
#         resp = response.content
#         inmemoryfile = io.BytesIO(resp)
#         # Create on first run
#         if not is_file_exists(local_file):
#             return resp
#         # Check1: SHA256 Change
#         if sha256_object(inmemoryfile) != sha256(local_file):
#             # Hash Changed
#             logger.info('%s Database is outdated!', db_name)
#             update = resp
#         else:
#             logger.info('%s Database is up-to-date', db_name)
#         return update
#     except Exception:
#         logger.exception('[ERROR] %s DB Update', db_name)
#         return update
#     finally:
#         if inmemoryfile:
#             inmemoryfile.truncate(0)


import glob
import io
import itertools
import json
import logging
import os
import re
import subprocess
from collections import namedtuple
from django.conf import settings
from tldextract import extract
from mobsf.MobSF.utils import (
    find_java_binary,
    is_file_exists,
    is_internet_available,
    update_local_db,
)




logger = logging.getLogger(__name__)


class Trackers:
    def __init__(self, apk_dir, tools_dir):
        self.apk = None
        self.apk_dir = apk_dir
        self.tracker_db = os.path.join(
            settings.SIGNATURE_DIR,
            'exodus_trackers')
        self.signatures = None
        self.nb_trackers_signature = 0
        self.compiled_tracker_signature = None
        self.compiled_network_tracker_sig = None
        self.classes = None
        self.tools_dir = tools_dir
        self._update_tracker_db()


# Path: mobsf/MobSF/utils.py
# def find_java_binary():
#     """Find Java."""
#     # Respect user settings
#     if platform.system() == 'Windows':
#         jbin = 'java.exe'
#     else:
#         jbin = 'java'
#     if is_dir_exists(settings.JAVA_DIRECTORY):
#         if settings.JAVA_DIRECTORY.endswith('/'):
#             return settings.JAVA_DIRECTORY + jbin
#         elif settings.JAVA_DIRECTORY.endswith('\\'):
#             return settings.JAVA_DIRECTORY + jbin
#         else:
#             return settings.JAVA_DIRECTORY + '/' + jbin
#     if os.getenv('JAVA_HOME'):
#         java = os.path.join(
#             os.getenv('JAVA_HOME'),
#             'bin',
#             jbin)
#         if is_file_exists(java):
#             return java
#     return 'java'
# 
# def is_file_exists(file_path):
#     if os.path.isfile(file_path):
#         return True
#     # This fix situation where a user just typed "adb" or another executable
#     # inside settings.py/config.py
#     if shutil.which(file_path):
#         return True
#     else:
#         return False
# 
# def is_internet_available():
#     try:
#         proxies, verify = upstream_proxy('https')
#     except Exception:
#         logger.exception('Setting upstream proxy')
#     try:
#         requests.get(settings.GOOGLE,
#                      timeout=5,
#                      proxies=proxies,
#                      verify=verify)
#         return True
#     except Exception:
#         try:
#             requests.get(settings.BAIDU,
#                          timeout=5,
#                          proxies=proxies,
#                          verify=verify)
#             return True
#         except Exception:
#             return False
# 
# def update_local_db(db_name, url, local_file):
#     """Update Local DBs."""
#     update = None
#     inmemoryfile = None
#     try:
#         proxies, verify = upstream_proxy('https')
#     except Exception:
#         logger.exception('[ERROR] Setting upstream proxy')
#     try:
#         response = requests.get(url,
#                                 timeout=3,
#                                 proxies=proxies,
#                                 verify=verify)
#         resp = response.content
#         inmemoryfile = io.BytesIO(resp)
#         # Create on first run
#         if not is_file_exists(local_file):
#             return resp
#         # Check1: SHA256 Change
#         if sha256_object(inmemoryfile) != sha256(local_file):
#             # Hash Changed
#             logger.info('%s Database is outdated!', db_name)
#             update = resp
#         else:
#             logger.info('%s Database is up-to-date', db_name)
#         return update
#     except Exception:
#         logger.exception('[ERROR] %s DB Update', db_name)
#         return update
#     finally:
#         if inmemoryfile:
#             inmemoryfile.truncate(0)


# Path: mobsf/MalwareAnalyzer/views/Trackers.py
import glob
import io
import itertools
import json
import logging
import os
import re
import subprocess
from collections import namedtuple
from django.conf import settings
from tldextract import extract
from mobsf.MobSF.utils import (
    find_java_binary,
    is_file_exists,
    is_internet_available,
    update_local_db,
)





logger = logging.getLogger(__name__)


class Trackers:
    def __init__(self, apk_dir, tools_dir):
        self.apk = None
        self.apk_dir = apk_dir
        self.tracker_db = os.path.join(
            settings.SIGNATURE_DIR,
            'exodus_trackers')
        self.signatures = None
        self.nb_trackers_signature = 0
        self.compiled_tracker_signature = None
        self.compiled_network_tracker_sig = None
        self.classes = None
        self.tools_dir = tools_dir
        self._update_tracker_db()


    def _update_tracker_db(self):