dumpLinks 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 <https://www.gnu.org/licenses/>.
  16. */
  17. var Cjdns = require('./lib/cjdnsadmin/cjdnsadmin');
  18. var nThen = require('nthen');
  19. var getLinks = function (cjdns, callback) {
  20. var links = [];
  21. var again = function (i) {
  22. cjdns.NodeStore_getLink(i, function (err, link) {
  23. if (err) { throw err; }
  24. if (link.error === 'none') {
  25. links.push(link.result);
  26. again(i+1);
  27. } else if (link.error === 'not_found') {
  28. callback(links);
  29. } else {
  30. throw new Error(link.error);
  31. }
  32. });
  33. };
  34. again(0);
  35. };
  36. var keyOnly = function (addr) {
  37. return addr.replace(/v[0-9]+[a-f0-9\.]{21}/, '');
  38. };
  39. Cjdns.connectWithAdminInfo(function (cjdns) {
  40. var links;
  41. nThen(function (waitFor) {
  42. getLinks(cjdns, waitFor(function (l) { links = l; }));
  43. }).nThen(function (waitFor) {
  44. links.sort(function (l1, l2) {
  45. return (l1.cannonicalLabel > l2.cannonicalLabel) ? 1 : -1;
  46. });
  47. links.forEach(function (l) {
  48. console.log(l.cannonicalLabel + ' ' + keyOnly(l.parent) + ' -> ' + keyOnly(l.child) +
  49. ((Number(l.bestParent)) ? ' bp' : ' ') + ((Number(l.isOneHop)) ? ' 1hop' : ''));
  50. });
  51. }).nThen(function (waitFor) {
  52. cjdns.disconnect();
  53. });
  54. });