You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. function get_substring($string, $start, $end){
  3. $string = ' ' . $string;
  4. $ini = strpos($string, $start);
  5. if ($ini == 0) return '';
  6. $ini += strlen($start);
  7. $len = strpos($string, $end, $ini) - $ini;
  8. return substr($string, $ini, $len);
  9. }
  10. function ip2geo ($host) {
  11. @$data = file_get_contents('http://ipinfo.io/'.$host);
  12. if ($data) {
  13. $data = json_decode($data);
  14. return $data->loc;
  15. } else {
  16. return "";
  17. }
  18. }
  19. function get_client_ip() {
  20. $ipaddress = '';
  21. if (getenv('HTTP_CLIENT_IP'))
  22. $ipaddress = getenv('HTTP_CLIENT_IP');
  23. else if(getenv('HTTP_X_FORWARDED_FOR'))
  24. $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
  25. else if(getenv('HTTP_X_FORWARDED'))
  26. $ipaddress = getenv('HTTP_X_FORWARDED');
  27. else if(getenv('HTTP_FORWARDED_FOR'))
  28. $ipaddress = getenv('HTTP_FORWARDED_FOR');
  29. else if(getenv('HTTP_FORWARDED'))
  30. $ipaddress = getenv('HTTP_FORWARDED');
  31. else if(getenv('REMOTE_ADDR'))
  32. $ipaddress = getenv('REMOTE_ADDR');
  33. else
  34. $ipaddress = 'UNKNOWN';
  35. return $ipaddress;
  36. }
  37. $data = array("points" => array());
  38. $traceroute = "";
  39. function traceroute() {
  40. global $data, $traceroute;
  41. $traceroute = shell_exec("traceroute " . get_client_ip());
  42. $first = true;
  43. foreach(preg_split("/((\r?\n)|(\r\n?))/", $traceroute) as $line){
  44. if ($first) {
  45. $first = false;
  46. continue;
  47. }
  48. $ipgeo = explode(",", ip2geo(get_substring($line, "(", ")")));
  49. $coordinates = array('x' => $ipgeo[0], 'y' => $ipgeo[1]);
  50. array_push($data['points'], $coordinates);
  51. }
  52. }
  53. function raw_output() {
  54. global $traceroute;
  55. echo json_encode($traceroute);
  56. }
  57. function json_output() {
  58. global $data;
  59. echo json_encode($data);
  60. }
  61. ?>