interfaces.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. 'use strict';
  2. 'require uci';
  3. 'require view';
  4. 'require rpc';
  5. 'require ui';
  6. 'require network';
  7. return view.extend({
  8. callGetJsonStatus: rpc.declare({
  9. object: 'olsrinfo',
  10. method: 'getjsondata',
  11. params: ['otable', 'v4_port', 'v6_port'],
  12. }),
  13. fetch_jsoninfo: function (otable) {
  14. var jsonreq4 = '';
  15. var jsonreq6 = '';
  16. var v4_port = parseInt(uci.get('olsrd', 'olsrd_jsoninfo', 'port') || '') || 9090;
  17. var v6_port = parseInt(uci.get('olsrd6', 'olsrd_jsoninfo', 'port') || '') || 9090;
  18. var json;
  19. var self = this;
  20. return new Promise(function (resolve, reject) {
  21. L.resolveDefault(self.callGetJsonStatus(otable, v4_port, v6_port), {})
  22. .then(function (res) {
  23. json = res;
  24. jsonreq4 = JSON.parse(json.jsonreq4);
  25. jsonreq6 = json.jsonreq6 !== '' ? JSON.parse(json.jsonreq6) : [];
  26. var jsondata4 = {};
  27. var jsondata6 = {};
  28. var data4 = [];
  29. var data6 = [];
  30. var has_v4 = false;
  31. var has_v6 = false;
  32. if (jsonreq4 === '' && jsonreq6 === '') {
  33. window.location.href = 'error_olsr';
  34. reject([null, 0, 0, true]);
  35. return;
  36. }
  37. if (jsonreq4 !== '') {
  38. has_v4 = true;
  39. jsondata4 = jsonreq4 || {};
  40. if (otable === 'status') {
  41. data4 = jsondata4;
  42. } else {
  43. data4 = jsondata4[otable] || [];
  44. }
  45. for (var i = 0; i < data4.length; i++) {
  46. data4[i]['proto'] = '4';
  47. }
  48. }
  49. if (jsonreq6 !== '') {
  50. has_v6 = true;
  51. jsondata6 = jsonreq6 || {};
  52. if (otable === 'status') {
  53. data6 = jsondata6;
  54. } else {
  55. data6 = jsondata6[otable] || [];
  56. }
  57. for (var j = 0; j < data6.length; j++) {
  58. data6[j]['proto'] = '6';
  59. }
  60. }
  61. for (var k = 0; k < data6.length; k++) {
  62. data4.push(data6[k]);
  63. }
  64. resolve([data4, has_v4, has_v6, false]);
  65. })
  66. .catch(function (err) {
  67. console.error(err);
  68. reject([null, 0, 0, true]);
  69. });
  70. });
  71. },
  72. action_interfaces: async function () {
  73. try {
  74. const [data, has_v4, has_v6, error] = await this.fetch_jsoninfo('interfaces');
  75. if (error) {
  76. throw error;
  77. }
  78. function compare(a, b) {
  79. return a.proto < b.proto;
  80. }
  81. const modifiedData = await Promise.all(
  82. data.map(async function (v) {
  83. const interfac = await network.getStatusByAddress(v.olsrInterface.ipAddress);
  84. if (interfac) {
  85. v.interface = interfac;
  86. }
  87. return v;
  88. })
  89. );
  90. modifiedData.sort(compare);
  91. const result = {
  92. iface: modifiedData,
  93. has_v4: has_v4,
  94. has_v6: has_v6,
  95. };
  96. return result;
  97. } catch (err) {
  98. throw err;
  99. }
  100. },
  101. load: function () {
  102. return Promise.all([uci.load('olsrd'), uci.load('luci_olsr')]);
  103. },
  104. render: function () {
  105. var iface_res;
  106. var has_v4;
  107. var has_v6;
  108. var self = this;
  109. return this.action_interfaces()
  110. .then(function (result) {
  111. iface_res = result.iface;
  112. has_v4 = result.has_v4;
  113. has_v6 = result.has_v6;
  114. var table = E('div', { 'class': 'table cbi-section-table' }, [
  115. E('div', { 'class': 'tr' }, [
  116. E('div', { 'class': 'th cbi-section-table-cell' }, _('Interface')),
  117. E('div', { 'class': 'th cbi-section-table-cell' }, _('Device')),
  118. E('div', { 'class': 'th cbi-section-table-cell' }, _('State')),
  119. E('div', { 'class': 'th cbi-section-table-cell' }, _('MTU')),
  120. E('div', { 'class': 'th cbi-section-table-cell' }, _('WLAN')),
  121. E('div', { 'class': 'th cbi-section-table-cell' }, _('Source address')),
  122. E('div', { 'class': 'th cbi-section-table-cell' }, _('Netmask')),
  123. E('div', { 'class': 'th cbi-section-table-cell' }, _('Broadcast address')),
  124. ]),
  125. ]);
  126. var i = 1;
  127. for (var k = 0; k < iface_res.length; k++) {
  128. var iface = iface_res[k];
  129. var tr = E('div', { 'class': 'tr cbi-section-table-row cbi-rowstyle-' + i + ' proto-' + iface.proto }, [
  130. E('div', { 'class': 'td cbi-section-table-cell left' }, iface?.interface?.interface ?? '?'),
  131. E('div', { 'class': 'td cbi-section-table-cell left' }, iface.name),
  132. E('div', { 'class': 'td cbi-section-table-cell left' }, iface.olsrInterface.up ? _('up') : _('down')),
  133. E('div', { 'class': 'td cbi-section-table-cell left' }, iface.olsrInterface.mtu),
  134. E('div', { 'class': 'td cbi-section-table-cell left' }, iface.olsrInterface.wireless ? _('yes') : _('no')),
  135. E('div', { 'class': 'td cbi-section-table-cell left' }, iface.olsrInterface.ipAddress),
  136. E('div', { 'class': 'td cbi-section-table-cell left' }, iface.olsrInterface.ipv4Address !== '0.0.0.0' ? iface.olsrInterface.ipv4Netmask : ''),
  137. E('div', { 'class': 'td cbi-section-table-cell left' }, iface.olsrInterface.ipv4Address !== '0.0.0.0' ? iface.olsrInterface.ipv4Broadcast : iface.olsrInterface.ipv6Multicast),
  138. ]);
  139. table.appendChild(tr);
  140. i = (i % 2) + 1;
  141. }
  142. var fieldset = E('fieldset', { 'class': 'cbi-section' }, [E('legend', {}, _('Overview of interfaces where OLSR is running')), table]);
  143. var h2 = E('h2', { 'name': 'content' }, _('Interfaces'));
  144. var divToggleButtons = E('div', { 'id': 'togglebuttons' });
  145. var statusOlsrCommonJs = null;
  146. if (has_v4 && has_v6) {
  147. statusOlsrCommonJs = E('script', { 'type': 'text/javascript', 'src': L.resource('common/common_js.js') });
  148. }
  149. var result = E([], {}, [h2, divToggleButtons, fieldset, statusOlsrCommonJs]);
  150. return result;
  151. })
  152. .catch(function (error) {
  153. console.error(error);
  154. });
  155. },
  156. handleSaveApply: null,
  157. handleSave: null,
  158. handleReset: null,
  159. });