ping 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env node
  2. /* -*- Mode:js */
  3. /* vim: set expandtab ts=4 sw=4: */
  4. /*
  5. * You may redistribute this program and/or modify it under the terms of
  6. * the GNU General Public License as published by the Free Software Foundation,
  7. * either version 3 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. var Cjdns = require('./lib/cjdnsadmin/cjdnsadmin');
  18. var nThen = require('nthen');
  19. var Dns = require('dns');
  20. var WAIT_TIME = 5000;
  21. var INTERVAL = 1000;
  22. var now = function () { return (new Date()).getTime(); };
  23. var nowSeconds = function () { return Math.floor(now() / 1000); };
  24. var IP6_REGEX = new RegExp('^' + new Array(9).join(':[0-9a-f]{1,4}').substring(1) + '$');
  25. var LABEL_REGEX = new RegExp('^' + new Array(5).join('.[0-9a-f]{4}').substring(1) + '$');
  26. var ADDR_REGEX = new RegExp('^v[0-9]+' + new Array(5).join('\\.[0-9a-f]{4}') + '\\.[a-z0-9]{52}\\.k$');
  27. var validTarget = function (target) {
  28. if (IP6_REGEX.test(target)) { return true; }
  29. if (LABEL_REGEX.test(target)) { return true; }
  30. if (ADDR_REGEX.test(target)) { return true; }
  31. return false;
  32. };
  33. var main = function (argv) {
  34. var maxCount = Math.Infinity;
  35. var keyPing = (argv.indexOf('-k') !== -1);
  36. var switchMode = (argv.indexOf('-s') !== -1) || keyPing;
  37. var data = '';
  38. if (argv.indexOf('-d') !== -1) {
  39. data = argv[argv.indexOf('-d') + 1];
  40. if (!switchMode) {
  41. console.log('-d data flag not possible without -s');
  42. return;
  43. }
  44. }
  45. if (argv.indexOf('-c') !== -1) {
  46. maxCount = argv[argv.indexOf('-c') + 1];
  47. }
  48. var target = argv[argv.length-1];
  49. var cjdns;
  50. nThen(function (waitFor) {
  51. Cjdns.connectWithAdminInfo(waitFor(function (c) { cjdns = c; }));
  52. if (!validTarget(target)) {
  53. Dns.lookup(target, 6, waitFor(function (err, res) {
  54. if (err) { throw err; }
  55. console.log(target + ' has ip address ' + res);
  56. target = res;
  57. }));
  58. }
  59. }).nThen(function (waitFor) {
  60. var count = 0;
  61. var routerPing = function () {
  62. if (count++ >= maxCount) { return; }
  63. var startTime = now();
  64. cjdns.RouterModule_pingNode(target, WAIT_TIME, waitFor(function (err, ret) {
  65. if (err) { throw err; }
  66. if (ret.result === 'timeout') {
  67. console.log(
  68. nowSeconds() + ' timeout p' + ret.version + ' ' + ret.ms + 'ms');
  69. } else if (ret.result === 'pong') {
  70. console.log(nowSeconds() + ' ' + ret.addr + ' ' + ret.ms + 'ms');
  71. } else if (ret.error === 'not_found') {
  72. console.log(nowSeconds() + " No route to host");
  73. } else {
  74. console.log(ret);
  75. }
  76. var pingIn = INTERVAL - (now() - startTime);
  77. if (pingIn < 0) { pingIn = 0; }
  78. setTimeout(waitFor(routerPing), pingIn);
  79. }));
  80. };
  81. var switchPing = function (keyPing) {
  82. if (count++ >= maxCount) { return; }
  83. var startTime = now();
  84. cjdns.SwitchPinger_ping(target, WAIT_TIME, keyPing^0, data, waitFor(function (err, ret) {
  85. if (err) { throw err; }
  86. var out = nowSeconds() + ' switchPing ';
  87. if (ret.result === 'timeout') {
  88. out += 'timeout ' + ret.ms + 'ms';
  89. } else if (ret.result === 'pong') {
  90. out += ret.path + ' p' + ret.version + ' ' + ret.ms + 'ms';
  91. if (ret.key) {
  92. out += ' ' + ret.key;
  93. }
  94. } else {
  95. out += JSON.stringify(ret);
  96. }
  97. console.log(out);
  98. var pingIn = INTERVAL - (now() - startTime);
  99. if (pingIn < 0) { pingIn = 0; }
  100. setTimeout(waitFor(switchPing), pingIn);
  101. }));
  102. };
  103. if (switchMode) {
  104. switchPing(keyPing);
  105. } else {
  106. routerPing();
  107. }
  108. }).nThen(function (waitFor) {
  109. cjdns.disconnect();
  110. });
  111. };
  112. main(process.argv);