softether-status.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. 'require view';
  3. 'require fs';
  4. return view.extend({
  5. setElementShown: function(selector, shown) {
  6. var loaderE = document.querySelector(selector);
  7. if (loaderE) {
  8. if (loaderE.classList.contains('hidden') === shown)
  9. {
  10. if (shown) {
  11. loaderE.classList.remove('hidden');
  12. } else {
  13. loaderE.classList.add('hidden');
  14. }
  15. }
  16. }
  17. },
  18. fixupResponse: function(response) {
  19. return response.toString().replace(/(\n$)/, '');
  20. },
  21. rpcDownloadAccountLists: function() {
  22. return new Promise(L.bind(function (resolve, fail) {
  23. fs.exec_direct('/usr/libexec/vpncmd-call', [ 'account-list' ])
  24. .then(L.bind(function(resp) {
  25. var accounts = [];
  26. var responseList = this.fixupResponse(resp).split('\n');
  27. responseList.forEach(function(d) {
  28. var s = d.split(',');
  29. var tmp = {};
  30. tmp.name = s[0];
  31. tmp.properties = {};
  32. tmp.properties['Status'] = s[1];
  33. tmp.properties['Remote'] = s[2];
  34. tmp.properties['Hub'] = s[3];
  35. accounts.push(tmp);
  36. })
  37. resolve(accounts);
  38. },this));
  39. },this));
  40. },
  41. rpcDownloadAccountStatus: function(account) {
  42. return new Promise(L.bind(function (resolve, fail) {
  43. fs.exec_direct('/usr/libexec/vpncmd-call', [ 'account-status-get', account.name ])
  44. .then(L.bind(function(resp) {
  45. var detailList = this.fixupResponse(resp).split('\n');
  46. detailList.forEach(function(d) {
  47. var s = d.split(',');
  48. if (s.length === 2)
  49. account.properties[s[0]] = s[1];
  50. });
  51. resolve(account);
  52. },this));
  53. },this));
  54. },
  55. downloadAllStatus: function(accountList) {
  56. var promises = [];
  57. accountList.forEach(L.bind(function(account) { promises.push(this.rpcDownloadAccountStatus(account)); },this));
  58. return Promise.all(promises);
  59. },
  60. downloadAllAccounts: function() {
  61. return new Promise(L.bind(function(resolve) {
  62. this.rpcDownloadAccountLists().then(L.bind(function(accountList) {
  63. this.downloadAllStatus(accountList).then(function(accountListWDetail) {
  64. resolve(accountListWDetail);
  65. });
  66. },this));
  67. },this));
  68. },
  69. updateAccountTable: function(listData) {
  70. var tableSelector = '#accountTable';
  71. var table = isElem(tableSelector) ? tableSelector : document.querySelector(tableSelector);
  72. if (listData.length > 0 ) {
  73. listData.forEach(L.bind(function(account) {
  74. table.appendChild(this.renderAccountRow(account));
  75. },this));
  76. } else {
  77. this.setElementShown('#emptyLabel', true);
  78. }
  79. this.setElementShown('#loader', false);
  80. },
  81. renderAccountRow: function(account) {
  82. var properties = [];
  83. for(var key in account.properties) {
  84. if (account.properties.hasOwnProperty(key)) {
  85. properties.push(E('strong', {}, [key + ':']));
  86. properties.push(account.properties[key]);
  87. properties.push(E('br', {}, []));
  88. }
  89. }
  90. var row = E('div', {'class':'tr cbi-section-table-row'}, [
  91. E('div', {'class':'td', 'style': 'width: 20%;vertical-align:top;'}, [
  92. E('strong', {}, ['Account:']),
  93. account.name
  94. ]),
  95. E('div', {'class':'td'}, properties)
  96. ]);
  97. return row;
  98. },
  99. render: function() {
  100. var view = E([], [
  101. E('h2', {}, _('SoftEther Status')),
  102. E('div', { 'class': 'cbi-section'}, [
  103. E('div', { 'class': 'cbi-section-node'}, [
  104. E('div', { 'id': 'accountTable', 'class': 'table cbi-section-table' }, [ ])
  105. ])
  106. ]),
  107. E('div', { 'id': 'loader', 'class': 'spinning' }, _('Loading account information…')),
  108. E('div', { 'id': 'emptyLabel', 'class': 'hidden'}, _('No VPN account configured.'))
  109. ]);
  110. this.downloadAllAccounts().then(L.bind(function(v) { this.updateAccountTable(v); },this));
  111. return view;
  112. },
  113. handleSave: null,
  114. handleSaveApply: null,
  115. handleReset: null,
  116. });