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.  
  101. class Net():
  102.  
  103. def __init__(self, name, owner, ns, data):
  104. self.name = name
  105. self.owner = owner
  106. self.ns = ns
  107. self.data = data
  108.  
  109. def populate_databases(config_file):
  110. """
  111. Populate databases with the data of our zones
  112. """
  113. print "Populating database"
  114. try:
  115. f = open(config_file, 'r')
  116. data = f.read()
  117. except IOError:
  118. print >>stderr, "Error while loading config file. exiting"
  119.  
  120. nets = re.findall (r"net\s+(\d+\.\d+\.\d+\.\d+[/\d]*)[\s|\n]+\{([^\}]+)\};", data)
  121. for net in nets:
  122. print "> net %s loaded" % net[0]
  123. fields = re.findall(r"(\w+)\s*=\s*([^;]+);", net[1])
  124. name, owner, ns = None, None, None
  125. data = {}
  126. for field, value in fields:
  127. if field.lower() == 'name':
  128. name = value.strip()
  129. elif field.lower() == 'owner':
  130. owner = value.strip()
  131. elif field.lower() == 'ns':
  132. ns = value.strip()
  133. else:
  134. data[field] = value
  135. ip_database[net[0]] = Net(name = name, owner = owner, ns = ns,
  136. data = data)
  137.  
  138. def parse_config_file(config_file):
  139.  
  140. config = {}
  141. f = open(config_file, 'r')
  142. content = f.read()
  143. f.close()
  144. for (key, value) in re.findall(r"([^\s]+)\s*=\s*([^;]+)\s*;", content):
  145. config[key] = value
  146. return config
  147.  
  148.  
  149.  
  150.  
  151. if __name__ == "__main__":
  152.  
  153. config = parse_config_file ('pywhoisd.conf')
  154. populate_databases(config['config_file'])
  155. host, port = "localhost", 43
  156. server = SocketServer.TCPServer((host, port), WhoisRequestHandler)
  157. try:
  158. server.serve_forever(0.1)
  159. except KeyboardInterrupt:
  160. print "Exiting...",
  161. print "done"
  162.  
  163.  
  164.