search 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 PublicToIP6 = require('./lib/publicToIp6');
  20. var Dns = require('dns');
  21. var WAIT_TIME = 5000;
  22. var INTERVAL = 1000;
  23. var now = function () { return (new Date()).getTime(); };
  24. var nowSeconds = function () { return Math.floor(now() / 1000); };
  25. var IP6_REGEX = new RegExp('^' + new Array(9).join(':[0-9a-f]{1,4}').substring(1) + '$');
  26. var equalsIp = function (ip, fullAddr) {
  27. var key = fullAddr.replace(/.*\.([^\.]*\.k)/, function (all, one) { return one; });
  28. return (PublicToIP6.convert(key) === ip);
  29. };
  30. var main = function (argv) {
  31. var count = -1;
  32. if (argv.indexOf('-c') !== -1) {
  33. count = Number(argv[argv.indexOf('-c')+1]);
  34. }
  35. var target = argv[argv.length-1];
  36. var cjdns;
  37. nThen(function (waitFor) {
  38. Cjdns.connectWithAdminInfo(waitFor(function (c) { cjdns = c; }));
  39. if (!IP6_REGEX.test(target)) {
  40. Dns.lookup(target, 6, waitFor(function (err, res) {
  41. if (err) { throw err; }
  42. console.log(target + ' has ip address ' + res);
  43. target = res;
  44. }));
  45. }
  46. }).nThen(function (waitFor) {
  47. // Cannonicalize the ipv6...
  48. var arr = target.split(':');
  49. for (var i = 0; i < arr.length; i++) {
  50. while (arr[i].length < 4) { arr[i] = '0' + arr[i]; }
  51. }
  52. target = arr.join(':');
  53. }).nThen(function (waitFor) {
  54. var startTime = (new Date()).getTime();
  55. var timeOfFirstFind;
  56. var handleMessage = function (err, msg) {
  57. if (err) { throw err; }
  58. if (msg.error && msg.error !== 'none') {
  59. cjdns.disconnect();
  60. console.log(msg);
  61. }
  62. if (msg.complete === "1") {
  63. cjdns.disconnect();
  64. console.log("Done -- first result in " + (timeOfFirstFind - startTime) + "ms." +
  65. " total: " + ((new Date()).getTime() - startTime) + "ms.");
  66. return;
  67. }
  68. console.log();
  69. console.log(msg.from + ' ' + msg.ms + 'ms ' + msg.nodes.length + ' results');
  70. var addresses = [msg.from];
  71. addresses.push.apply(addresses, msg.nodes);
  72. for (var i = 0; i < addresses.length; i++) {
  73. if (equalsIp(target, addresses[i])) {
  74. console.log(" found " + addresses[i]);
  75. if (!timeOfFirstFind) { timeOfFirstFind = (new Date()).getTime(); }
  76. }
  77. }
  78. };
  79. cjdns.setDefaultHandler(handleMessage);
  80. cjdns.SearchRunner_search(target, count, handleMessage);
  81. });
  82. };
  83. main(process.argv);