sessionStats 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. if (Number(session.timeOfLastUsage) !== 0) { out.push('USE'); }
  32. console.log(out.join(' '));
  33. };
  34. var cjdns;
  35. var handles = [];
  36. var sessions = [];
  37. nThen(function (waitFor) {
  38. Cjdns.connectWithAdminInfo(waitFor(function (c) { cjdns = c; }));
  39. }).nThen(function (waitFor) {
  40. var more = function (i) {
  41. cjdns.SessionManager_getHandles(i, waitFor(function (err, ret) {
  42. if (err) { throw err; }
  43. handles.push.apply(handles, ret.handles);
  44. if (ret.more) { more(i+1); }
  45. }));
  46. };
  47. more(0);
  48. }).nThen(function (waitFor) {
  49. var next = function (i) {
  50. cjdns.SessionManager_sessionStats(Number(handles[i]), waitFor(function (err, ret) {
  51. if (err) { throw err; }
  52. sessions.push(ret);
  53. i++
  54. if (i < handles.length) { next(i); }
  55. }));
  56. };
  57. if (handles.length) {
  58. next(0);
  59. } else {
  60. console.log("No active sessions");
  61. }
  62. }).nThen(function (waitFor) {
  63. cjdns.disconnect();
  64. sessions.sort(function (a, b) {
  65. return (a.addr.substring(a.addr.indexOf('.')) < b.addr.substring(b.addr.indexOf('.')))
  66. ? -1 : 1;
  67. });
  68. for (var i = 0; i < sessions.length; i++) {
  69. printSession(sessions[i]);
  70. }
  71. });