getBrokenPeers 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Ping public peers from github and show broken peers
  5. */
  6. // clone or update peers repo
  7. if (file_exists('peers'))
  8. passthru('cd peers; git pull > /dev/null; cd ..');
  9. else
  10. passthru('git clone "https://github.com/hyperboria/peers.git" > /dev/null');
  11. // get peers and ping it
  12. require_once 'publicKey2ipv6.php';
  13. $data = json_decode(@file_get_contents(FNAME), true);
  14. if (!$data) $data = [];
  15. printf("%-15s | %4s | %5s | %4s | %s
  16. --------------- | ---- | ----- | ---- | -----\n", 'IPv4', 'ping', 'IPv6', 'ping', 'url');
  17. foreach(get_files('peers') as $fname)
  18. {
  19. $st = file_get_contents($fname);
  20. parse($st, $fname);
  21. }
  22. function get_files($dir)
  23. {
  24. ob_start();
  25. passthru("find $dir | grep '\.k'");
  26. $st = ob_get_clean();
  27. return explode("\n", trim($st));
  28. }
  29. function parse($st, $fname)
  30. {
  31. global $data;
  32. $a = json_decode($st, true);
  33. foreach ($a as $k => $a)
  34. if (strpos($k, '[') === false)
  35. {
  36. $st = '';
  37. $ip = explode(':', $k)[0];
  38. $st .= sprintf("%-15s", $ip);
  39. exec("ping -c3 -W5 $ip", $out, $res);
  40. $st .= sprintf(" | %4s", $res ? 'FAIL' : 'OK');
  41. $ip = publicKey2ipv6($a['publicKey']); $x = explode(':', $ip);
  42. $st .= sprintf(" | %5s", ':'.$x[count($x)-1]);
  43. exec("ping -c3 -6 -W5 $ip", $out, $res);
  44. $st .= sprintf(" | %4s", $res ? 'FAIL' : 'OK');
  45. $x = str_replace('peers/', '', $fname);
  46. $x = "[$x](https://github.com/hyperboria/peers/blob/master/$x)";
  47. $st .= ' | '.$x;
  48. $st .= "\n";
  49. if (strpos($st, 'FAIL')) echo $st;
  50. }
  51. }