viewgit/index.php:465 Only variables should be passed by reference [2048]
viewgit/index.php:466 Non-static method GeSHi::get_language_name_from_extension() should not be called statically [2048]
#!/usr/bin/env python # -*- coding: utf-8 -*- # # This is a simple whois daemon intended to # provide a basic whois for GNet. # import SocketServer, re, math, syslog from ipcalc import IP, Network # Dictionaries that will contain runtime data # for the server ip_database = {} domain_database = {} config = {} def Log(message): syslog.syslog ("[pywhoisd] " + message) def get_net_of_ip(ip): """ Get network in which the given ip is """ interesting_networks = [] for (address, net) in ip_database.items(): if IP(ip) in Network(address): interesting_networks.append (address) return interesting_networks def format_output(field, value): """ Append a value to the output """ output = (field + ":").ljust(24) output += value + "\n" return " "*4 + output def format_message(message): """ Normal message of the server """ output = "\n" for line in message.split("\n"): line = line.strip() output += """\n %% """ + line.ljust(65) + """ %%""" return output + "\n\n" def resolve_ip(ip): """ Resolve an ip: it search all the data that match it """ output = "" nets = get_net_of_ip(ip) for net in nets: output += format_message("GNet whoisd answering for ip %s" % ip) output += format_output ("Range", net) net = ip_database[net] if net.name is not None: output += format_output ("Name", net.name) if net.owner is not None: output += format_output ("Owner", net.owner) if net.ns is not None: output += format_output ("Nameserver", net.ns) if len(net.data.items()) > 0: output += format_message("Additional data") for k, v in net.data.items(): output += format_output (k.capitalize(),v) if len(nets) == 0: output = format_message("IP %s not found\n" % ip) return output def get_domains_of_name(domain): interesting_domains = [] for domain_name, domain_data in domain_database.items(): if domain.endswith(domain_name): interesting_domains.append (domain_name) return interesting_domains def resolve_domain(domain): """ Resolve a domain. It is not yet implemented """ domain = domain.strip(".") output = "" domains = get_domains_of_name(domain) for d in domains: output += format_message("GNet whoisd answering for domain %s" % d) output += format_output ("Domain", d) domain = domain_database[d] if domain.name is not None: output += format_output ("Name", domain.name) if domain.owner is not None: output += format_output ("Owner", domain.owner) if domain.ns is not None: output += format_output ("Nameserver", domain.ns) if len(domain.data.items()) > 0: output += format_message("Additional data") for k, v in domain.data.items(): output += format_output (k.capitalize(),v) if len(domains) == 0: return format_message("No domain found for %s" % domain) return output def hello(): """ Say hello, server! """ if config.has_key('welcome_banner'): return config['welcome_banner'] else: return "" class WhoisRequestHandler(SocketServer.StreamRequestHandler): """ This handler answers to the whois request """ def handle(self): # Come prima cosa salutiamo response = format_message(hello()) # Obtain the whois request request = self.rfile.readline().strip() if re.search(r"\d+\.\d+\.\d+\.\d+", request.strip()): response += resolve_ip(request.split("/")[0]) else: response += resolve_domain(request) response += format_message("Bye") self.request.send (response) return class Net(): # # Net is a class representing the network # contained in the local database and the # net obtained from other whois server. # # It act as a C struct containing only the # data and no method. def __init__(self, name, owner, ns, data): self.name = name self.owner = owner self.ns = ns self.data = data class Domain(): # # # def __init__(self, name, owner, ns, data): self.name, self.owner, self.ns = name, owner, ns self.data = data def populate_databases(config_file): """ Populate databases with the data of our zones """ Log ("Populating database with entry in %s" % config_file) try: f = open(config_file, 'r') config_data = f.read() except IOError: Log ("Error while loading config file. exiting") nets = re.findall (r"net\s+(\d+\.\d+\.\d+\.\d+[/\d]*)[\s|\n]+\{([^\}]+)\};", config_data) for net in nets: Log ("Loading net %s" % net[0]) fields = re.findall(r"(\w+)\s*=\s*([^;]+);", net[1]) name, owner, ns = None, None, None data = {} for field, value in fields: if field.lower() == 'name': name = value.strip() elif field.lower() == 'owner': owner = value.strip() elif field.lower() == 'ns': ns = value.strip() else: data[field] = value ip_database[net[0]] = Net(name = name, owner = owner, ns = ns, data = data) domains = re.findall(r"domain\s+([^\s]+)[\s|\n]+\{([^\}]+)\};", config_data) for domain in domains: Log ("Loading domain %s" % domain[0]) fields = re.findall(r"(\w+)\s*=\s*([^;]+);", domain[1]) name, owner, ns = None, None, None data = {} for field, value in fields: if field.lower() == 'name': name = value.strip() elif field.lower() == 'owner': owner = value.strip() elif field.lower() == 'ns': ns = value.strip() else: data[field] = value domain_database[domain[0]] = Domain(name = name, owner = owner, ns = ns, data = data) def parse_config_file(config_file): """ Parse config file and load_data into memory """ config = {} f = open(config_file, 'r') content = f.read() f.close() for (key, value) in re.findall(r"([^\s]+)\s*=\s*([^;]+)\s*;", content): # Gestiamo in maniera particolare # i campi che mi forniscono un array. if key == "master_domains": config[key] = [] for domain in eval(value): config[key].append(domain) else: config[key] = value return config if __name__ == "__main__": config = parse_config_file ('pywhoisd.conf') populate_databases(config['config_file']) host, port = "0.0.0.0", 43 server = SocketServer.ThreadingTCPServer((host, port), WhoisRequestHandler) server.allow_reuse_address = True try: server.serve_forever(0.1) except KeyboardInterrupt: Log ("Exiting...")