# Path: lib/settings.py
# def get_top_domains(dbname):


import time
import threading
import os
import lib.utils as utils
import glob
import requests
import base64
import json
import sys
import hashlib
import GeoIP
from lib.objects import *
from lib.logger import *
from user_agents import parse
from lib.settings import homenet, lock, top_domains
class ReadDHCPLeases(threading.Thread):
    def __init__(self, threadID):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.ctime = None

    def run(self):

        while 1:
            self.ctime = int(time.time())
            try:
                f = open('/var/log/dnsmasq.leases', 'r')
                lines = f.readlines()
                if len(lines) > 0:
                    with lock:
                        for line in lines:
                            line = line.strip()
                            fields = line.split()
                            ts = int(fields[0]) - 604800
                            mac = fields[1].upper()
                            ip = str(fields[2])
                            hostname = str(fields[3])
                            if ip not in homenet.hosts:
                                device = Host()
                                device.ts = ts
                                device.lseen = ts
                                device.mac = mac
                                device.ip = ip
                                device.hostname = hostname
                                device.vendor = utils.get_vendor(mac)

# Path: lib/settings.py
# def get_top_domains(dbname):


# Path: lib/logparser.py
import time
import threading
import os
import lib.utils as utils
import glob
import requests
import base64
import json
import sys
import hashlib
import GeoIP
from lib.objects import *
from lib.logger import *
from user_agents import parse
from lib.settings import homenet, lock, top_domains

class ReadDHCPLeases(threading.Thread):
    def __init__(self, threadID):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.ctime = None

    def run(self):

        while 1:
            self.ctime = int(time.time())
            try:
                f = open('/var/log/dnsmasq.leases', 'r')
                lines = f.readlines()
                if len(lines) > 0:
                    with lock:
                        for line in lines:
                            line = line.strip()
                            fields = line.split()
                            ts = int(fields[0]) - 604800
                            mac = fields[1].upper()
                            ip = str(fields[2])
                            hostname = str(fields[3])
                            if ip not in homenet.hosts:
                                device = Host()
                                device.ts = ts
                                device.lseen = ts
                                device.mac = mac
                                device.ip = ip
                                device.hostname = hostname
                                device.vendor = utils.get_vendor(mac)

                                homenet.hosts[ip] = device