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]

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # This is a simple whois daemon intended to
  5. # provide a basic whois for GNet.
  6. #
  7.  
  8. import SocketServer, re, math
  9.  
  10. ip_database = {}
  11. domain_database = {}
  12. config = {}
  13.  
  14. def ip_in_subnet(ip, network, subnet):
  15. ip = map(int, ip.split("."))
  16. network = map(int, network.split("."))
  17. if "." in subnet:
  18. subnet = map(int, subnet.split("."))
  19. subnet = map(lambda x: 255 - x, subnet)
  20. subnet = math.log(subnet[3] + subnet[2] * 256 + subnet[1] * pow(256,2) + subnet[0] * pow(256,3), 2)
  21. subnet = pow(2, 32 - int(subnet))
  22. ip = ip[3] + ip[2] * 256 + ip[1] * pow(256,2) + ip[0] * pow(256,3)
  23. network = network[3] + network[2] * 256 + network[1] * pow(256,2) + network[0]*pow(256,3)
  24. if ((ip - network) < subnet) and (ip-network >= 0):
  25. return True
  26. else:
  27. return False
  28.  
  29. def get_net_of_ip(ip):
  30. for (address, net) in ip_database.items():
  31. nip, netmask = address.split("/")
  32.  
  33. if ip_in_subnet(ip, nip, netmask):
  34. return address
  35.  
  36. def format_output(field, value):
  37. """
  38. Append a value to the output
  39. """
  40. output = (field + ":").ljust(24)
  41. output += value + "\n"
  42. return " "*4 + output
  43.  
  44. def format_message(message):
  45. output = "\n"
  46. for line in message.split("\n"):
  47. line = line.strip()
  48. output += """\n %% """ + line.ljust(85) + """ %%"""
  49. return output + "\n\n"
  50.  
  51. def resolve_ip(ip):
  52. output = ""
  53. net = get_net_of_ip(ip)
  54. if net is not None:
  55. output += format_message("GNet whoisd answering for ip %s" % ip)
  56. net = ip_database[net]
  57. if net.name is not None:
  58. output += format_output ("Name", net.name)
  59. if net.owner is not None:
  60. output += format_output ("Owner", net.owner)
  61. if net.ns is not None:
  62. output += format_output ("Nameserver", net.ns)
  63. if len(net.data.items()) > 0:
  64. output += format_message("Additional data")
  65. for k, v in data.items():
  66. output += format_output (k.capitalize(),v)
  67. else:
  68. output = format_message("IP %s not found\n" % ip)
  69. output += format_message("Bye")
  70. return output
  71.  
  72. def resolve_domain(domain):
  73. return format_message("Feature not yet implemented")
  74.  
  75. def hello():
  76. if config.has_key('welcome_banner'):
  77. return config['welcome_banner']
  78. else:
  79. return ""
  80.  
  81.  
  82. class WhoisRequestHandler(SocketServer.StreamRequestHandler):
  83. """
  84. This handler answers to the whois request
  85. """
  86.  
  87. def handle(self):
  88. # Come prima cosa salutiamo
  89. response = format_message(hello())
  90.  
  91. # Obtain the whois request
  92. request = self.rfile.readline().strip()
  93.  
  94. if re.search(r"\d+\.\d+\.\d+\.\d+", request.strip()):
  95. response += resolve_ip(request.split("/")[0])
  96. else:
  97. response += resolve_domain(request)
  98.  
  99. self.request.send (response)
  100. return
  101.  
  102. class Net():
  103.  
  104. def __init__(self, name, owner, ns, data):
  105. self.name = name
  106. self.owner = owner
  107. self.ns = ns
  108. self.data = data
  109.  
  110. def populate_databases(config_file):
  111. """
  112. Populate databases with the data of our zones
  113. """
  114. print "Populating database"
  115. try:
  116. f = open(config_file, 'r')
  117. data = f.read()
  118. except IOError:
  119. print >>stderr, "Error while loading config file. exiting"
  120.  
  121. nets = re.findall (r"net\s+(\d+\.\d+\.\d+\.\d+[/\d]*)[\s|\n]+\{([^\}]+)\};", data)
  122. for net in nets:
  123. print "> net %s loaded" % net[0]
  124. fields = re.findall(r"(\w+)\s*=\s*([^;]+);", net[1])
  125. name, owner, ns = None, None, None
  126. data = {}
  127. for field, value in fields:
  128. if field.lower() == 'name':
  129. name = value.strip()
  130. elif field.lower() == 'owner':
  131. owner = value.strip()
  132. elif field.lower() == 'ns':
  133. ns = value.strip()
  134. else:
  135. data[field] = value
  136. ip_database[net[0]] = Net(name = name, owner = owner, ns = ns,
  137. data = data)
  138.  
  139. def parse_config_file(config_file):
  140.  
  141. config = {}
  142. f = open(config_file, 'r')
  143. content = f.read()
  144. f.close()
  145. for (key, value) in re.findall(r"([^\s]+)\s*=\s*([^;]+)\s*;", content):
  146. config[key] = value
  147. return config
  148.  
  149.  
  150.  
  151.  
  152. if __name__ == "__main__":
  153.  
  154. config = parse_config_file ('pywhoisd.conf')
  155. populate_databases(config['config_file'])
  156. host, port = "localhost", 43
  157. server = SocketServer.TCPServer((host, port), WhoisRequestHandler)
  158. try:
  159. server.serve_forever(0.1)
  160. except KeyboardInterrupt:
  161. print "Exiting...",
  162. print "done"
  163.  
  164.  
  165.