# Path: mhn/ui/constants.py
# DEFAULT_FLAG_URL = 'img/unknown.png'
# DEFAULT_COUNTRY_NAME = 'Unknown'
# 
# Path: mhn/api/models.py
# class Sensor(db.Model, APIModel):
# 
#     # Defines some properties on the fields:
#     # required: Is required for creating object via
#     #           a POST request.
#     # editable: Can be edited via a PUT request.
#     all_fields = {
#         'uuid': {'required': False, 'editable': False},
#         'name': {'required': True, 'editable': True},
#         'created_date': {'required': False, 'editable': False},
#         'ip': {'required': False, 'editable': True},
#         'hostname': {'required': True, 'editable': True},
#         'honeypot': {'required': True, 'editable': False}
#     }
# 
#     __tablename__ = 'sensors'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     uuid = db.Column(db.String(36), unique=True)
#     name = db.Column(db.String(50))
#     created_date = db.Column(
#             db.DateTime(), default=datetime.utcnow)
#     ip = db.Column(db.String(15))
#     hostname = db.Column(db.String(50))
#     identifier = db.Column(db.String(50), unique=True)
#     honeypot = db.Column(db.String(50))
# 
#     def __init__(
#           self, uuid=None, name=None, created_date=None, honeypot=None,
#           ip=None, hostname=None, identifier=None, **args):
#         self.uuid = uuid
#         self.name = name
#         self.created_date = created_date
#         self.ip = ip
#         self.hostname = hostname
#         self.identifier = identifier
#         self.honeypot = honeypot
# 
#     def __repr__(self):
#         return '<Sensor>{}'.format(self.to_dict())
# 
#     def to_dict(self):
#         return dict(
#             uuid=self.uuid, name=self.name, honeypot=self.honeypot,
#             created_date=str(self.created_date), ip=self.ip,
#             hostname=self.hostname, identifier=self.uuid,
#             # Extending with info from Mnemosyne.
#             secret=self.authkey.secret, publish=self.authkey.publish)
# 
#     def new_auth_dict(self):
#         el = string.ascii_letters + string.digits
#         rand_str = lambda n: ''.join(choice(el) for _ in range(n))
#         return dict(secret=rand_str(16), owner="chn",
#                     identifier=self.uuid, honeypot=self.honeypot,
#                     subscribe=[], publish=Sensor.get_channels(self.honeypot))
# 
#     @property
#     def attacks_count(self):
#         return Clio().counts.get_count(identifier=self.uuid)
# 
#     @property
#     def authkey(self):
#         return Clio().authkey.get(identifier=self.uuid)
# 
#     @staticmethod
#     def get_channels(honeypot):
#         from mhn import mhn
#         return mhn.config.get('HONEYPOT_CHANNELS', {}).get(honeypot, [])


import requests
import os
import struct
from flask import current_app as app, url_for
from mhn.ui import constants
from config import MHN_SERVER_HOME
from werkzeug.contrib.cache import SimpleCache
from ipaddress import ip_address
from mhn.api.models import Sensor

flag_cache = SimpleCache(threshold=1000, default_timeout=300)
country_cache = SimpleCache(threshold=1000, default_timeout=300)
sensor_cache = SimpleCache(threshold=1000, default_timeout=300)


def is_private_addr(ip):
    # 10.0.0.0/8
    # 127.0.0.0/8
    # 172.16.0.0/12
    # 192.168.0.0/16
    # fc00::/7 (unique local addresses)
    # ::1/128 (localhost)

    try:
        ip_obj = ip_address(ip)
        # Make exception for ::ffff/96 (ipv4-mapped)
        if ip_obj.version == 6 and ip_obj.ipv4_mapped:
            return False
        if ip_obj.is_private:
            return True
    except Exception as e:
        print('Error (%s) on is_private_addr: %s' % (e, ip))

    return False


def get_flag_ip(ipaddr):
    if is_private_addr(ipaddr):

# Path: mhn/ui/constants.py
# DEFAULT_FLAG_URL = 'img/unknown.png'
# DEFAULT_COUNTRY_NAME = 'Unknown'
# 
# Path: mhn/api/models.py
# class Sensor(db.Model, APIModel):
# 
#     # Defines some properties on the fields:
#     # required: Is required for creating object via
#     #           a POST request.
#     # editable: Can be edited via a PUT request.
#     all_fields = {
#         'uuid': {'required': False, 'editable': False},
#         'name': {'required': True, 'editable': True},
#         'created_date': {'required': False, 'editable': False},
#         'ip': {'required': False, 'editable': True},
#         'hostname': {'required': True, 'editable': True},
#         'honeypot': {'required': True, 'editable': False}
#     }
# 
#     __tablename__ = 'sensors'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     uuid = db.Column(db.String(36), unique=True)
#     name = db.Column(db.String(50))
#     created_date = db.Column(
#             db.DateTime(), default=datetime.utcnow)
#     ip = db.Column(db.String(15))
#     hostname = db.Column(db.String(50))
#     identifier = db.Column(db.String(50), unique=True)
#     honeypot = db.Column(db.String(50))
# 
#     def __init__(
#           self, uuid=None, name=None, created_date=None, honeypot=None,
#           ip=None, hostname=None, identifier=None, **args):
#         self.uuid = uuid
#         self.name = name
#         self.created_date = created_date
#         self.ip = ip
#         self.hostname = hostname
#         self.identifier = identifier
#         self.honeypot = honeypot
# 
#     def __repr__(self):
#         return '<Sensor>{}'.format(self.to_dict())
# 
#     def to_dict(self):
#         return dict(
#             uuid=self.uuid, name=self.name, honeypot=self.honeypot,
#             created_date=str(self.created_date), ip=self.ip,
#             hostname=self.hostname, identifier=self.uuid,
#             # Extending with info from Mnemosyne.
#             secret=self.authkey.secret, publish=self.authkey.publish)
# 
#     def new_auth_dict(self):
#         el = string.ascii_letters + string.digits
#         rand_str = lambda n: ''.join(choice(el) for _ in range(n))
#         return dict(secret=rand_str(16), owner="chn",
#                     identifier=self.uuid, honeypot=self.honeypot,
#                     subscribe=[], publish=Sensor.get_channels(self.honeypot))
# 
#     @property
#     def attacks_count(self):
#         return Clio().counts.get_count(identifier=self.uuid)
# 
#     @property
#     def authkey(self):
#         return Clio().authkey.get(identifier=self.uuid)
# 
#     @staticmethod
#     def get_channels(honeypot):
#         from mhn import mhn
#         return mhn.config.get('HONEYPOT_CHANNELS', {}).get(honeypot, [])


# Path: mhn/ui/utils.py
import requests
import os
import struct
from flask import current_app as app, url_for
from mhn.ui import constants
from config import MHN_SERVER_HOME
from werkzeug.contrib.cache import SimpleCache
from ipaddress import ip_address
from mhn.api.models import Sensor


flag_cache = SimpleCache(threshold=1000, default_timeout=300)
country_cache = SimpleCache(threshold=1000, default_timeout=300)
sensor_cache = SimpleCache(threshold=1000, default_timeout=300)


def is_private_addr(ip):
    # 10.0.0.0/8
    # 127.0.0.0/8
    # 172.16.0.0/12
    # 192.168.0.0/16
    # fc00::/7 (unique local addresses)
    # ::1/128 (localhost)

    try:
        ip_obj = ip_address(ip)
        # Make exception for ::ffff/96 (ipv4-mapped)
        if ip_obj.version == 6 and ip_obj.ipv4_mapped:
            return False
        if ip_obj.is_private:
            return True
    except Exception as e:
        print('Error (%s) on is_private_addr: %s' % (e, ip))

    return False


def get_flag_ip(ipaddr):
    if is_private_addr(ipaddr):

        return url_for('static', filename=constants.DEFAULT_FLAG_URL)