sendGraph 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env php
  2. <?php
  3. require 'Bencode.php';
  4. require 'Cjdns.php';
  5. require 'publicKey2ipv6.php';
  6. define('PASSWORD', 'NONE');
  7. define('URL', 'http://www.fc00.org/sendGraph');
  8. define('EMAIL', 'your@email.here'); // update your email address, so I can contact you in case something goes wrong
  9. //define('DEBUG', 1);
  10. $cjdns = new Cjdns(PASSWORD, "127.0.0.1", 11234);
  11. //$_nds = dump_node_store($cjdns);
  12. $_nds = dump_near_nodes($cjdns);
  13. if (defined('DEBUG'))
  14. echo "Nodes: ".count($_nds)."\n";
  15. $nodes = []; $links = []; $edges = [];
  16. foreach ($_nds as $node)
  17. {
  18. if (defined('DEBUG')) echo 'Node '.(@++$i).' / '.count($_nds)."\n";
  19. $peers = get_peers($cjdns, $node['path']);
  20. foreach ($peers as $peer)
  21. if ($peer['ip'] != $node['ip'])
  22. {
  23. $x = parse_addr($peer['addr']);
  24. if (defined('DEBUG')) echo $node['ip'] . ' => '. $peer['ip']."\n";
  25. if (empty($links[$peer['ip'].$node['ip']]))
  26. $edges[] = ['a'=>$node['ip'], 'b'=>$peer['ip']];
  27. $nodes[$node['ip']] = ['ip'=>$x['ip'], 'version'=>@$x['version']];
  28. $nodes[$peer['ip']] = ['ip'=>$x['ip'], 'version'=>@$x['version']];
  29. $links[$peer['ip'].$node['ip']] = 1;
  30. $links[$node['ip'].$peer['ip']] = 1;
  31. }
  32. }
  33. $nodes = array_values($nodes);
  34. if (defined('DEBUG'))
  35. echo "Edges: ".count($edges)."\n";
  36. // send to server
  37. $json_graph = ['nodes' => $nodes, 'edges' => $edges];
  38. $payload = ['data'=>json_encode($json_graph), 'mail'=>EMAIL, 'version'=>2, 'name'=>'php'];
  39. $data = http_build_query($payload);
  40. $context = stream_context_create(['http'=>[
  41. 'method' => 'POST',
  42. 'header' => "Content-Type: application/x-www-form-urlencoded\r\nContent-Length: ".strlen($data)."\r\n",
  43. 'content' => $data,
  44. ]]);
  45. $st = file_get_contents(URL, false, $context);
  46. if (defined('DEBUG'))
  47. echo "Result: $st\n";
  48. function get_peers($cjdns, $path, $nearbyPath='')
  49. {
  50. if (!cache_get($path, $res))
  51. {
  52. if (defined('DEBUG')) echo "Call $path\n";
  53. $res = $cjdns->call('RouterModule_getPeers', ['path'=>$path] + ($nearbyPath?['nearbyPath'=>$nearbyPath]:[]));
  54. cache_set($path, $res);
  55. }
  56. if (empty($res['peers']) || $res['error'] != 'none') return [];
  57. $res = $res['peers'];
  58. foreach ($res as $k => $v) $res[$k] = ['addr'=>$v, 'ip'=>publicKey2ipv6(explode('.', $v)[5])];
  59. return $res;
  60. }
  61. function dump_near_nodes($cjdns)
  62. {
  63. $nodes = []; $page = 0;
  64. if (cache_get('near_nodes', $nodes)) return $nodes;
  65. while (1)
  66. {
  67. $res = $cjdns->call('InterfaceController_peerStats', ['page'=>$page]);
  68. if (!empty($res['peers']))
  69. foreach ($res['peers'] as $a)
  70. {
  71. $x = parse_addr($a['addr']);
  72. $nodes[] = $x;
  73. }
  74. if (empty($res['more']) || $res['more'] != 1) break;
  75. $page++;
  76. }
  77. return cache_set('near_nodes', $nodes);
  78. }
  79. function dump_node_store($cjdns)
  80. {
  81. $nodes = []; $page = 0;
  82. if (cache_get('nodes', $nodes)) return $nodes;
  83. while (1)
  84. {
  85. $res = $cjdns->call('NodeStore_dumpTable', ['page'=>$page]);
  86. if (!empty($res['routingTable']))
  87. foreach ($res['routingTable'] as $a)
  88. {
  89. $x = parse_addr($a['addr']);
  90. $nodes[] = $x;
  91. }
  92. if (empty($res['more']) || $res['more'] != 1) break;
  93. $page++;
  94. }
  95. return cache_set('nodes', $nodes);
  96. }
  97. function parse_addr($x)
  98. {
  99. $x = explode('.', $x);
  100. return [
  101. 'version' => str_replace('v', '', $x[0]),
  102. 'path' => implode('.', array_slice($x, 1, 4)),
  103. 'publicKey' => $pK = implode('.', array_slice($x, 5, 2)),
  104. 'ip' => publicKey2ipv6($pK),
  105. ];
  106. }
  107. // DEBUG: cache functions
  108. function cache_get($name, &$data)
  109. {
  110. if (!defined('DEBUG')) return false;
  111. $fname = cache_fname($name);
  112. if (file_exists($fname) && (time() - filemtime($fname) < 60*60))
  113. {
  114. $data = json_decode(file_get_contents($fname), true);
  115. return true;
  116. }
  117. return false;
  118. }
  119. function cache_set($name, $data)
  120. {
  121. if (defined('DEBUG')) file_put_contents(cache_fname($name), json_encode($data));
  122. return $data;
  123. }
  124. function cache_fname($name)
  125. {
  126. $dir = './cache/';
  127. if (!file_exists($dir)) mkdir($dir);
  128. return $dir.$name.'.json';
  129. }