index.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #function to split ISP company names from ASN
  11. def ASN_Split(asn):
  12. name_parts = []
  13. for i in asn.split():
  14. if re.match(r'AS\d{1,6}$', i):
  15. asn = i
  16. elif not re.match(r'AS\d{1,6}$', i):
  17. name_parts.append(i)
  18. company = ' '.join(name_parts)
  19. return (asn, company)
  20. got_data = False
  21. msg=""
  22. url=""
  23. try:
  24. # extract data from a xml file
  25. f = open('data.xml', 'r')
  26. f2 = open('data.xml', 'r')
  27. xml = ET.parse(f)
  28. data = f2.read()
  29. dom = parseString(data.encode('utf-8'))
  30. f.close()
  31. f2.close()
  32. n_hops = dom.getElementsByTagName('hop')[-1].toxml().replace('<hop>', '').replace('</hop','')
  33. hop_list = []
  34. hop_ip_list =[]
  35. geoarray = []
  36. latlong= []
  37. asn_list =[]
  38. server_name_list = []
  39. timestamp_list = []
  40. telco_list = []
  41. last_hop = int(xml.findall('hop')[-1].text)
  42. country_code_list = []
  43. got_data = True
  44. url = xml.getroot().text
  45. for counter in range(0, last_hop+1):
  46. hop_element = parseString(dom.getElementsByTagName('hop')[counter].toxml().encode('utf-8'))
  47. hop = xml.findall('hop')[counter].text
  48. server_name = hop_element.getElementsByTagName('server_name')[0].toxml().replace('<server_name>','').replace('</server_name>','')
  49. asn = hop_element.getElementsByTagName('asn')[0].toxml().replace('<asn>','').replace('</asn>','')
  50. hop_ip = hop_element.getElementsByTagName('hop_ip')[0].toxml().replace('<hop_ip>','').replace('</hop_ip>','')
  51. longitude = hop_element.getElementsByTagName('longitude')[0].toxml().replace('<longitude>','').replace('</longitude>','')
  52. latitude = hop_element.getElementsByTagName('latitude')[0].toxml().replace('<latitude>','').replace('</latitude>','')
  53. timestamp = hop_element.getElementsByTagName('timestamp')[0].toxml().replace('<timestamp>','').replace('</timestamp>','')
  54. country_code = hop_element.getElementsByTagName('country_code')[0].toxml().replace('<country_code>','').replace('</country_code>','')
  55. if str(asn) == "<asn/>": #parse when no asn present/network owner present
  56. asn_list.append(str("Not Available"))
  57. telco_list.append(str("Unknown"))
  58. else:
  59. asn_list.append(ASN_Split(asn.encode('utf-8'))[0])
  60. telco_list.append(ASN_Split(asn.encode('utf-8'))[1])
  61. latlong = [float(latitude.encode('utf-8')), float(longitude.encode('utf-8'))]
  62. geoarray.append(latlong)
  63. # asn_list.append(ASN_Split(asn.encode('utf-8'))[0])
  64. # telco_list.append(ASN_Split(asn.encode('utf-8'))[1])
  65. hop = int(hop) +1
  66. hop_list.append(str(hop))
  67. hop_ip_list.append(hop_ip.encode('utf-8'))
  68. server_name_list.append(server_name.encode('utf-8'))
  69. timestamp_list.append(float(timestamp))
  70. country_code_list.append(country_code.encode('utf-8'))
  71. unique_country_code_list = set(country_code_list)
  72. except:
  73. args['error']="Data error."
  74. if 'error' in args:
  75. msg=args['error'];
  76. # HTML + JS container
  77. output = """
  78. <html>
  79. <head>
  80. <title>Border Check - Web Viewer</title>
  81. <link rel="stylesheet" href="js/leaflet/leaflet.css" />
  82. <link rel="stylesheet" href="style.css" />
  83. <link rel="stylesheet" href="js/cluster/MarkerCluster.Default.css"/>
  84. <link rel="stylesheet" href="js/cluster/MarkerCluster.css"/>
  85. <script src="js/leaflet/leaflet.js"></script>
  86. <script src="js/jquery-1.10.2.min.js"></script>
  87. <script src="js/rlayer-src.js"></script>
  88. <script src="js/raphael.js"></script>
  89. <script src="js/bc.js"></script>
  90. <script src="js/bc-history.js"></script>
  91. <script src="js/favicon.js"></script>
  92. <script src="js/cluster/leaflet.markercluster-src.js"></script>
  93. <script src="js/bc-control.js"></script>
  94. <script type="text/javascript">
  95. $(document).ready (function(){
  96. var h = $(window).innerHeight();
  97. var w = $(window).innerWidth();
  98. $("#wrapper").css({
  99. "width": w, "height": h
  100. })
  101. })
  102. </script>
  103. </head>
  104. <body>
  105. <div id="wrapper">
  106. <div class="header" id="status">Travelling to:</div>
  107. <div class ="header" id="url">"""+url+"""</div>
  108. <div id="map" style="width: 100%; height: 100%"></div>
  109. <div class ="bar">
  110. <div id="button" class='toggle'> > </div>
  111. <div class = info>
  112. <div> <img src='images/bclogo.png'></img></div>
  113. <div id='info-text'>
  114. <br /><div class='toggle' id='about'>About</div>
  115. <div id='about-content'>
  116. <p>As you surf the net, data packets are sent from your computer to the target server. These data packets go on a journey hopping from server to server, potentially crossing multiple countries and networks, until the packets reach the desired website.</p>
  117. <p> Border Check allows you to retrace the path your data takes across the internet's infrastructure. It will map out all the servers your data passes and shows you in which countries or cities these servers are located. Additionally Border Check will try to provide you with additional data on these servers, such as the companies they belong to.</p>
  118. <p> Visit the <a href="http://bordercheck.org"> project homepage</a> for more information.
  119. </div>
  120. <p class='divider'>------------------------------</p>
  121. </div>
  122. <div class='toggle' id='legend'> Map legend </div>
  123. <div id='legend-content'>
  124. <center><pre> <img id="home" class='toggle' src='images/markers/marker-icon-0.png'></img> <img class='toggle' id="hop" src='images/markers/marker-icon-11.png'></img> <img class='toggle'id="cluster" src='images/markers/cluster-marker.png'></img> <img class='toggle' id="destination" src='images/markers/marker-icon-last.png'></img> </pre></center>
  125. <div id=legend-text></div></div>
  126. <p class='divider'>------------------------------</p>
  127. <div class='toggle' id='form'>New Travel (enter URL)</div>
  128. <br />
  129. <div id='form-content'>
  130. <center>
  131. <form onsubmit="return false" id='host'><input id='form-host' name='hostname' placeholder='http://bordercheck.org'><input id='form-submit' type='button' value='GO'>
  132. </center>
  133. <div id='ajax-out'></div>
  134. <div id='form-target'></div>
  135. </div>
  136. <div class='toggle' id='history'>History</div>
  137. <div id='history-content'>
  138. </div>
  139. <p class='divider'>------------------------------</p>
  140. <div class='toggle' id='contact'>Get in touch</div>
  141. <div id='contact-content'>
  142. <br />
  143. <center>
  144. Roel Roscam Abbing (rscmbbng@riseup.net) <br />
  145. psy (epsylon@riseup.net)
  146. </center>
  147. <div>
  148. </div>
  149. </div>
  150. <div id='error' style='display:block;color:red'>"""+msg+""" </div>
  151. </div>
  152. </div>"""
  153. if got_data:
  154. output +="""
  155. <script type="text/javascript">
  156. cur_url = '"""+url+"""'
  157. counter_max = """+str(last_hop)+"""
  158. latlong = """+str(geoarray)+"""
  159. asn_list = """+str(asn_list)+"""
  160. hop_ip_list = """+str(hop_ip_list)+"""
  161. telco_list = """+str(telco_list)+"""
  162. server_name_list = """+str(server_name_list)+"""
  163. timestamp_list = """+str(timestamp_list)+"""
  164. country_code_list = """+str(country_code_list)+"""
  165. unique_country_code_list = """+str(list(unique_country_code_list))+"""
  166. window.onload = function(){
  167. initMap()
  168. bcHistory.add(cur_url,Array(counter_max,latlong,asn_list, hop_ip_list,telco_list,server_name_list,timestamp_list,country_code_list,unique_country_code_list))
  169. }
  170. </script>
  171. </html>
  172. """