sessionStats 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 PublicToIp6 = require('./lib/publicToIp6');
  18. var Cjdns = require('./lib/cjdnsadmin/cjdnsadmin');
  19. var nThen = require('nthen');
  20. var printSession = function (session) {
  21. var state = session.state.replace(/CryptoAuth_/,'');
  22. while (state.length < ('ESTABLISHED').length) { state = state + ' ' }
  23. var addr = session.addr;
  24. if (process.argv.indexOf('--ip6') !== -1) {
  25. addr = addr.replace(/[a-z0-9]{52}.k/, PublicToIp6.convert);
  26. }
  27. var out = [ addr, state, session.handle, session.sendHandle, Number(session.metric).toString(16) ];
  28. if (Number(session.duplicates) !== 0) { out.push(' DUP ', session.duplicates); }
  29. if (Number(session.lostPackets) !== 0) { out.push(' LOS ', session.lostPackets); }
  30. if (Number(session.receivedOutOfRange) !== 0) { out.push(' OOR ', session.receivedOutOfRange); }
  31. console.log(out.join(' '));
  32. };
  33. var cjdns;
  34. var handles = [];
  35. var sessions = [];
  36. nThen(function (waitFor) {
  37. Cjdns.connectWithAdminInfo(waitFor(function (c) { cjdns = c; }));
  38. }).nThen(function (waitFor) {
  39. var more = function (i) {
  40. cjdns.SessionManager_getHandles(i, waitFor(function (err, ret) {
  41. if (err) { throw err; }
  42. handles.push.apply(handles, ret.handles);
  43. if (ret.more) { more(i+1); }
  44. }));
  45. };
  46. more(0);
  47. }).nThen(function (waitFor) {
  48. var next = function (i) {
  49. cjdns.SessionManager_sessionStats(Number(handles[i]), waitFor(function (err, ret) {
  50. if (err) { throw err; }
  51. sessions.push(ret);
  52. i++
  53. if (i < handles.length) { next(i); }
  54. }));
  55. };
  56. if (handles.length) {
  57. next(0);
  58. } else {
  59. console.log("No active sessions");
  60. }
  61. }).nThen(function (waitFor) {
  62. cjdns.disconnect();
  63. sessions.sort(function (a, b) {
  64. return (a.addr.substring(a.addr.indexOf('.')) < b.addr.substring(b.addr.indexOf('.')))
  65. ? -1 : 1;
  66. });
  67. for (var i = 0; i < sessions.length; i++) {
  68. printSession(sessions[i]);
  69. }
  70. });