ajax.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. # -*- coding: iso-8859-15 -*-
  3. """
  4. BC (Border-Check) is a tool to retrieve info of traceroute tests over website navigation routes.
  5. GPLv3 - 2013-2014-2015 by psy (epsylon@riseup.net)
  6. """
  7. from xml.dom.minidom import parseString
  8. import xml.etree.ElementTree as ET
  9. import re
  10. import traceback
  11. reload=False
  12. try:
  13. bc_status_file = open('bc.status')
  14. s = str(bc_status_file.read())
  15. if s =='fresh':
  16. with open('bc.status', 'w') as file:
  17. file.write("old")
  18. reload=True
  19. except:
  20. pass
  21. # todo :
  22. # * call ajax.py from here
  23. # * save last url timestamp as get parameter
  24. # * do parsing only if timestamp changes
  25. # * ui integration
  26. #function to split ISP company names from ASN
  27. def ASN_Split(asn):
  28. name_parts = []
  29. for i in asn.split():
  30. if re.match(r'AS\d{1,6}$', i):
  31. asn = i
  32. elif not re.match(r'AS\d{1,6}$', i):
  33. name_parts.append(i)
  34. company = ' '.join(name_parts)
  35. return (asn, company)
  36. def extract_data():
  37. url=""
  38. hop_list = []
  39. hop_ip_list =[]
  40. geoarray = []
  41. latlong= []
  42. asn_list =[]
  43. server_name_list = []
  44. timestamp_list = []
  45. telco_list = []
  46. country_code_list = []
  47. last_hop = 0
  48. n_hops=0
  49. unique_country_code_list = []
  50. # extract data from a xml file
  51. f = open('data.xml', 'r')
  52. f2 = open('data.xml', 'r')
  53. xml = ET.parse(f)
  54. data = f2.read()
  55. dom = parseString(data.encode('utf-8'))
  56. f.close()
  57. f2.close()
  58. last_hop = int(xml.findall('hop')[-1].text)
  59. n_hops = dom.getElementsByTagName('hop')[-1].toxml().replace('<hop>', '').replace('</hop','')
  60. for counter in range(0, last_hop+1):
  61. url = xml.getroot().text
  62. hop_element = parseString(dom.getElementsByTagName('hop')[counter].toxml().encode('utf-8'))
  63. hop = xml.findall('hop')[counter].text
  64. server_name = hop_element.getElementsByTagName('server_name')[0].toxml().replace('<server_name>','').replace('</server_name>','')
  65. asn = hop_element.getElementsByTagName('asn')[0].toxml().replace('<asn>','').replace('</asn>','')
  66. hop_ip = hop_element.getElementsByTagName('hop_ip')[0].toxml().replace('<hop_ip>','').replace('</hop_ip>','')
  67. longitude = hop_element.getElementsByTagName('longitude')[0].toxml().replace('<longitude>','').replace('</longitude>','')
  68. latitude = hop_element.getElementsByTagName('latitude')[0].toxml().replace('<latitude>','').replace('</latitude>','')
  69. timestamp = hop_element.getElementsByTagName('timestamp')[0].toxml().replace('<timestamp>','').replace('</timestamp>','')
  70. country_code = hop_element.getElementsByTagName('country_code')[0].toxml().replace('<country_code>','').replace('</country_code>','')
  71. latlong = [float(latitude.encode('utf-8')), float(longitude.encode('utf-8'))]
  72. geoarray.append(latlong)
  73. asn_list.append(ASN_Split(asn.encode('utf-8'))[0])
  74. telco_list.append(ASN_Split(asn.encode('utf-8'))[1])
  75. hop = int(hop) +1
  76. hop_list.append(str(hop))
  77. hop_ip_list.append(hop_ip.encode('utf-8'))
  78. server_name_list.append(server_name.encode('utf-8'))
  79. timestamp_list.append(float(timestamp))
  80. country_code_list.append(country_code.encode('utf-8'))
  81. unique_country_code_list = set(country_code_list)
  82. return """
  83. <script type="text/javascript">
  84. counter_max = """+str(last_hop)+"""
  85. latlong = """+str(geoarray)+"""
  86. asn_list = """+str(asn_list)+"""
  87. hop_ip_list = """+str(hop_ip_list)+"""
  88. telco_list = """+str(telco_list)+"""
  89. server_name_list = """+str(server_name_list)+"""
  90. timestamp_list = """+str(timestamp_list)+"""
  91. country_code_list = """+str(country_code_list)+"""
  92. unique_country_code_list = """+str(list(unique_country_code_list))+"""
  93. bcHistory.add('"""+url+"""',Array(counter_max,latlong,asn_list, hop_ip_list,telco_list,server_name_list,timestamp_list,country_code_list,unique_country_code_list))
  94. </script>
  95. """
  96. try:
  97. if reload:
  98. output=extract_data()
  99. except:
  100. args['error']="No data available"
  101. traceback.print_exc()
  102. reload=False
  103. if 'error' in args:
  104. output=output+'<script>$("#error").html("'+args['error']+'")</script>'