<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@title           :MiniServerDNS
@author          :Engelx (<NAME>)
@email           :<EMAIL> 
@date            :2017-08-31
@version         :0.7
@python_version  :3.5
"""
import socket #socket related
import re, os, time
from sys import exit
from threading import Thread
import handler_dns_query as hdnsq
from handler_db import dbman


def forwardQueryToDNS(data, conf, ps, dns="primary_dns"):
    forward = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    forward.settimeout(3) # 3 second timout
    forward.connect((conf[dns], 53)) #call to the dns server
    forward.send(hdnsq.IntToByte(data)) # send data in bytes
    try: #if no timeout
        return hdnsq.processResponse(forward.recv(512)) #get answer
    except socket.timeout: #if timeout
        ps.writeLog("dnsServer", "El servidor {} {} no respondió".format(dns, conf[dns]))
        if dns == "primary_dns":
            return forwardQueryToDNS(data, conf, ps, dns="secondary_dns")
        else:
            return False

def dnsServer(data, ip, conf, udps, ps, server):
        sender = udps
        data, host = hdnsq.processQuery(data)
        
        if not host:
            if conf["debug"]: 
                ps.writeLog("dnsServer","No existe host a resolver: \n data.\n {}".format(data))

            data = hdnsq.generateResponse(data) # we cannot resolve this
            sender.sendto(data, ip)
            
            return False
        
        ps(ip,host)# load into pool
        
        filt = re.compile("^([\w\.\-])+$") # filter to aviod risks, 
        if not filt.match(host):
            if conf["debug"]:
                ps.writeLog("dnsServer","Host invalido: {}".format(host))

            data = hdnsq.generateResponse(data) # we cannot resolve this
            sender.sendto(data, ip)
            ps.ended(ip)
            return False
          
        db = dbman(server)
        
        if db.blocked(host, ip[0]) and conf["use_block"]:  # if is blacked and we are blocking
            if conf["debug"]:
                    ps.writeLog("dnsServer","Host bloqueado ip: {:<16}, host:{}".format(ip[0], host))
            data = hdnsq.generateResponse(data, conf["redirect_blocked_ip"]) # blocker ip
            if conf["data_display"] == 2: print(ip[0],ip[1],host, conf["redirect_blocked_ip"])
            sender.sendto(data, ip)
            if conf["use_log"]:
                db.log(host, ip[0], conf["redirect_blocked_ip"]) 
            ps.ended(ip)
            return True
        
        answer = db.cache(host)
        
        if not answer: # if no in cache
            answer = forwardQueryToDNS(data, conf, ps)
            if not answer: #if not in dns server or conection problem
                if conf["debug"]:
                    ps.writeLog("dnsServer","Servidor remoto no respondió, host: {}".format(host))
                data = hdnsq.generateResponse(data) # we cannot resolve this
                sender.sendto(data, ip)
                ps.ended(ip)
                return False            
            db.cache(host, answer)
            
        if conf["use_log"]:
            db.log(host, ip[0], answer) 
        data = hdnsq.generateResponse(data, answer) # blocker ip     
        
        if conf["data_display"] == 2: print(ip[0],ip[1],host, answer)
        
        
        sender.sendto(data, ip)
        ps.ended(ip)
        return True
    
def getServers():
    path = "/".join(os.path.realpath(__file__).split("/")[:-1])+"/"
    file = open(path + "servers.conf", "r")
    server = file.read()
    servers = {}
    for serv in re.findall("(\w+)\:\{([\w\:\s\.]*)\}",server):
        temp = {}
        for val in re.findall("(\w+)\:\s*([\w\.]*)",serv[1]):
            temp[val[0]] = val[1]
        if len(temp)  != 5:
            return False
        servers[serv[0]] = temp
    file.close()
    return servers


class printSystem:
    def __init__(self, conf):
        self.querys = {}
        self.count = 0
        self.log = conf["log_file"]
        file = open(self.log, "a")
        if not file:
            msg = "No se pudo abrir el archivo de logs {}, saliendo del programa".format(self.log)
            self.printToScreen("ps",msg, "error", True)
            exit()
        
        if not self.writeLog("main","Servidor iniciado"):
            msg = "No se pudo imprimir en el archivo de logs {}, saliendo del programa".format(self.log)
            self.printToScreen("ps",msg, "error", True)
            exit()            
        file.close()
        
            
    def __call__(self, ipPort, host):
        ip = ipPort[0]
        port = ipPort[1]
        self.count += 1
        if ip in self.querys: # if the ip is in the query dict
            if port in self.querys[ip]: # if the port is already in user from that ip, is an error?
                self.writeLog("Query control", "ip: {}:{} is already in pool, duplicated query".format(ip,port))
                return False 
            
            self.querys[ip][port] = [host, time.time()] #show port in ip
            
            return True
        
        self.querys[ip] = {port: [host, time.time()]} #load ip with port -> host
        return True
        <commit_msg>corección al servidor, error de carga o desdecarga de elementos en el ps<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@title           :MiniServerDNS
@author          :Engelx (<NAME>)
@email           :<EMAIL> 
@date            :2017-08-31
@version         :0.7
@python_version  :3.5
"""
import socket #socket related
import re, os, time
from sys import exit
from threading import Thread
import handler_dns_query as hdnsq
from handler_db import dbman


def forwardQueryToDNS(data, conf, ps, dns="primary_dns"):
    forward = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    forward.settimeout(3) # 3 second timout
    forward.connect((conf[dns], 53)) #call to the dns server
    forward.send(hdnsq.IntToByte(data)) # send data in bytes
    try: #if no timeout
        return hdnsq.processResponse(forward.recv(512)) #get answer
    except socket.timeout: #if timeout
        ps.writeLog("dnsServer", "El servidor {} {} no respondió".format(dns, conf[dns]))
        if dns == "primary_dns":
            return forwardQueryToDNS(data, conf, ps, dns="secondary_dns")
        else:
            return False

def dnsServer(data, ip, conf, udps, ps, server):
        sender = udps
        data, host = hdnsq.processQuery(data)
        
        if not host:
            if conf["debug"]: 
                ps.writeLog("dnsServer","No existe host a resolver: \n data.\n {}".format(data))

            data = hdnsq.generateResponse(data) # we cannot resolve this
            sender.sendto(data, ip)
            
            return False
        
        if not ps(ip,host):# load into pool
            return False
        
        filt = re.compile("^([\w\.\-])+$") # filter to aviod risks, 
        if not filt.match(host):
            if conf["debug"]:
                ps.writeLog("dnsServer","Host invalido: {}".format(host))

            data = hdnsq.generateResponse(data) # we cannot resolve this
            sender.sendto(data, ip)
            ps.ended(ip)
            return False
          
        db = dbman(server)
        
        if db.blocked(host, ip[0]) and conf["use_block"]:  # if is blacked and we are blocking
            if conf["debug"]:
                    ps.writeLog("dnsServer","Host bloqueado ip: {:<16}, host:{}".format(ip[0], host))
            data = hdnsq.generateResponse(data, conf["redirect_blocked_ip"]) # blocker ip
            if conf["data_display"] == 2: print(ip[0],ip[1],host, conf["redirect_blocked_ip"])
            sender.sendto(data, ip)
            if conf["debug"]:
                db.log(host, ip[0], conf["redirect_blocked_ip"]) 
            ps.ended(ip)
            return True
        
        answer = db.cache(host)
        
        if not answer: # if no in cache
            answer = forwardQueryToDNS(data, conf, ps)
            if not answer: #if not in dns server or conection problem
                if conf["debug"]:
                    ps.writeLog("dnsServer","Servidor remoto no respondió, host: {}".format(host))
                data = hdnsq.generateResponse(data) # we cannot resolve this
                sender.sendto(data, ip)
                ps.ended(ip)
                return False            
            db.cache(host, answer)
            
        if conf["debug"]:
            db.log(host, ip[0], answer) 
        data = hdnsq.generateResponse(data, answer) # blocker ip     
        
        if conf["data_display"] == 2: print(ip[0],ip[1],host, answer)
        
        
        sender.sendto(data, ip)
        ps.ended(ip)
        return True
    
def getServers():
    path = "/".join(os.path.realpath(__file__).split("/")[:-1])+"/"
    file = open(path + "servers.conf", "r")
    server = file.read()
    servers = {}
    for serv in re.findall("(\w+)\:\{([\w\:\s\.]*)\}",server):
        temp = {}
        for val in re.findall("(\w+)\:\s*([\w\.]*)",serv[1]):
            temp[val[0]] = val[1]
        if len(temp)  != 5:
            return False
        servers[serv[0]] = temp
    file.close()
    return servers


class printSystem:
    def __init__(self, conf):
        self.querys = {}
        self.count = 0
        self.log = conf["log_file"]
        file = open(self.log, "a")
        if not file:
            msg = "No se pudo abrir el archivo de logs {}, saliendo del programa".format(self.log)
            self.printToScreen("ps",msg, "error", True)
            exit()
        
        if not self.writeLog("main","Servidor iniciado"):
            msg = "No se pudo imprimir en el archivo de logs {}, saliendo del programa".format(self.log)
            self.printToScreen("ps",msg, "error", True)
            exit()            
        file.close()
        
            
    def __call__(self, ipPort, host):
        ip = ipPort[0]
        port = ipPort[1]
        self.count += 1
        if ip in self.querys: # if the ip is in the query dict
            if port in self.querys[ip]: # if the port is already in user from that ip, is an error?
                self.writeLog("Query control", "ip: {}:{} is already in pool, duplicated query".format(ip,port))
                return False 
            
            self.querys[ip][port] = [host, time.time()] #show port in ip
            return True
        
        self.querys[ip] = {port: [host, time.time()]} #load ip with port -> host
        return True
        
{'fraction_non_alphanumeric': 0.13140206380763042, 'fraction_numerical': 0.008141626431884881, 'mean_word_length': 1.8644251626898047, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5229097', 'n_tokens_mistral': 3305, 'n_tokens_neox': 3216, 'n_words': 864}