ping 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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('../contrib/nodejs/cjdnsadmin/cjdnsadmin');
  18. var nThen = require('../contrib/nodejs/cjdnsadmin/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 keyPing = (argv.indexOf('-k') !== -1);
  35. var switchMode = (argv.indexOf('-s') !== -1) || keyPing;
  36. var data = '';
  37. if (argv.indexOf('-d') !== -1) {
  38. data = argv[argv.indexOf('-d') + 1];
  39. if (!switchMode) {
  40. console.log('-d data flag not possible without -s');
  41. return;
  42. }
  43. }
  44. var target = argv[argv.length-1];
  45. var cjdns;
  46. nThen(function (waitFor) {
  47. Cjdns.connectWithAdminInfo(waitFor(function (c) { cjdns = c; }));
  48. if (!validTarget(target)) {
  49. Dns.lookup(target, 6, waitFor(function (err, res) {
  50. if (err) { throw err; }
  51. console.log(target + ' has ip address ' + res);
  52. target = res;
  53. }));
  54. }
  55. }).nThen(function (waitFor) {
  56. var routerPing = function () {
  57. var startTime = now();
  58. cjdns.RouterModule_pingNode(target, WAIT_TIME, waitFor(function (err, ret) {
  59. if (err) { throw err; }
  60. if (ret.result === 'timeout') {
  61. console.log(
  62. nowSeconds() + ' timeout p' + ret.version + ' ' + ret.ms + 'ms');
  63. } else if (ret.result === 'pong') {
  64. console.log(nowSeconds() + ' ' + ret.addr + ' ' + ret.ms + 'ms');
  65. } else if (ret.error === 'not_found') {
  66. console.log(nowSeconds() + " No route to host");
  67. } else {
  68. console.log(ret);
  69. }
  70. var pingIn = INTERVAL - (now() - startTime);
  71. if (pingIn < 0) { pingIn = 0; }
  72. setTimeout(routerPing, pingIn);
  73. }));
  74. };
  75. var switchPing = function (keyPing) {
  76. var startTime = now();
  77. cjdns.SwitchPinger_ping(target, WAIT_TIME, keyPing^0, data, waitFor(function (err, ret) {
  78. if (err) { throw err; }
  79. var out = nowSeconds() + ' switchPing ';
  80. if (ret.result === 'timeout') {
  81. out += 'timeout ' + ret.ms + 'ms';
  82. } else if (ret.result === 'pong') {
  83. out += ret.path + ' p' + ret.version + ' ' + ret.ms + 'ms';
  84. if (ret.key) {
  85. out += ' ' + ret.key;
  86. }
  87. } else {
  88. out += JSON.stringify(ret);
  89. }
  90. console.log(out);
  91. var pingIn = INTERVAL - (now() - startTime);
  92. if (pingIn < 0) { pingIn = 0; }
  93. setTimeout(switchPing, pingIn);
  94. }));
  95. };
  96. if (switchMode) {
  97. switchPing(keyPing);
  98. } else {
  99. routerPing();
  100. }
  101. });
  102. };
  103. main(process.argv);