hna.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. 'use strict';
  2. 'require uci';
  3. 'require view';
  4. 'require poll';
  5. 'require network';
  6. 'require rpc';
  7. 'require ui';
  8. return view.extend({
  9. callGetJsonStatus: rpc.declare({
  10. object: 'olsrinfo',
  11. method: 'getjsondata',
  12. params: ['otable', 'v4_port', 'v6_port'],
  13. }),
  14. callGetHosts: rpc.declare({
  15. object: 'olsrinfo',
  16. method: 'hosts',
  17. }),
  18. fetch_jsoninfo: function (otable) {
  19. var jsonreq4 = '';
  20. var jsonreq6 = '';
  21. var v4_port = parseInt(uci.get('olsrd', 'olsrd_jsoninfo', 'port') || '') || 9090;
  22. var v6_port = parseInt(uci.get('olsrd6', 'olsrd_jsoninfo', 'port') || '') || 9090;
  23. var json;
  24. var self = this;
  25. return new Promise(function (resolve, reject) {
  26. L.resolveDefault(self.callGetJsonStatus(otable, v4_port, v6_port), {})
  27. .then(function (res) {
  28. json = res;
  29. jsonreq4 = JSON.parse(json.jsonreq4);
  30. jsonreq6 = json.jsonreq6 !== '' ? JSON.parse(json.jsonreq6) : [];
  31. var jsondata4 = {};
  32. var jsondata6 = {};
  33. var data4 = [];
  34. var data6 = [];
  35. var has_v4 = false;
  36. var has_v6 = false;
  37. if (jsonreq4 === '' && jsonreq6 === '') {
  38. window.location.href = 'error_olsr';
  39. reject([null, 0, 0, true]);
  40. return;
  41. }
  42. if (jsonreq4 !== '') {
  43. has_v4 = true;
  44. jsondata4 = jsonreq4 || {};
  45. if (otable === 'status') {
  46. data4 = jsondata4;
  47. } else {
  48. data4 = jsondata4[otable] || [];
  49. }
  50. for (var i = 0; i < data4.length; i++) {
  51. data4[i]['proto'] = '4';
  52. }
  53. }
  54. if (jsonreq6 !== '') {
  55. has_v6 = true;
  56. jsondata6 = jsonreq6 || {};
  57. if (otable === 'status') {
  58. data6 = jsondata6;
  59. } else {
  60. data6 = jsondata6[otable] || [];
  61. }
  62. for (var j = 0; j < data6.length; j++) {
  63. data6[j]['proto'] = '6';
  64. }
  65. }
  66. for (var k = 0; k < data6.length; k++) {
  67. data4.push(data6[k]);
  68. }
  69. resolve([data4, has_v4, has_v6, false]);
  70. })
  71. .catch(function (err) {
  72. console.error(err);
  73. reject([null, 0, 0, true]);
  74. });
  75. });
  76. },
  77. action_hna: function () {
  78. var self = this;
  79. return new Promise(function (resolve, reject) {
  80. self
  81. .fetch_jsoninfo('hna')
  82. .then(function ([data, has_v4, has_v6, error]) {
  83. if (error) {
  84. reject(error);
  85. }
  86. var resolveVal = uci.get('luci_olsr', 'general', 'resolve');
  87. function compare(a, b) {
  88. if (a.proto === b.proto) {
  89. return a.genmask < b.genmask;
  90. } else {
  91. return a.proto < b.proto;
  92. }
  93. }
  94. var modifiedData;
  95. self
  96. .callGetHosts()
  97. .then(function (res) {
  98. function matchHostnames(ip) {
  99. var lines = res.hosts.split('\n');
  100. for (var i = 0; i < lines.length; i++) {
  101. var ipandhostname = lines[i].trim().split(/\s+/);
  102. if (ipandhostname[0] === ip) {
  103. return ipandhostname[1];
  104. }
  105. }
  106. return null;
  107. }
  108. modifiedData = data.map(function (v) {
  109. if (resolveVal === '1') {
  110. var hostname = matchHostnames(v.gateway);
  111. if (hostname) {
  112. v.hostname = hostname;
  113. }
  114. }
  115. if (v.validityTime) {
  116. v.validityTime = parseInt((v.validityTime / 1000).toFixed(0));
  117. }
  118. return v;
  119. });
  120. modifiedData.sort(compare);
  121. var result = { hna: modifiedData, has_v4: has_v4, has_v6: has_v6 };
  122. resolve(result);
  123. })
  124. .catch(function (err) {
  125. modifiedData = data;
  126. console.error(err);
  127. });
  128. })
  129. .catch(function (err) {
  130. reject(err);
  131. });
  132. });
  133. },
  134. load: function () {
  135. var self = this;
  136. poll.add(function () {
  137. self.render();
  138. }, 5);
  139. return Promise.all([uci.load('olsrd'), uci.load('luci_olsr')]);
  140. },
  141. render: function () {
  142. var hna_res;
  143. var has_v4;
  144. var has_v6;
  145. var self = this;
  146. return this.action_hna()
  147. .then(function (result) {
  148. hna_res = result.hna;
  149. has_v4 = result.has_v4;
  150. has_v6 = result.has_v6;
  151. var table = E('div', { 'class': 'table cbi-section-table', 'id': 'olsrd_hna' }, [
  152. E('div', { 'class': 'tr cbi-section-table-titles' }, [
  153. E('div', { 'class': 'th cbi-section-table-cell' }, _('Announced network')),
  154. E('div', { 'class': 'th cbi-section-table-cell' }, _('OLSR gateway')),
  155. E('div', { 'class': 'th cbi-section-table-cell' }, _('Validity Time')),
  156. ]),
  157. ]);
  158. var i = 1;
  159. var rv = [];
  160. for (var k = 0; k < hna_res.length; k++) {
  161. var entry = hna_res[k];
  162. rv.push({
  163. proto: entry.proto,
  164. destination: entry.destination,
  165. genmask: entry.genmask,
  166. gateway: entry.gateway,
  167. hostname: entry.hostname,
  168. validityTime: entry.validityTime,
  169. });
  170. }
  171. var info = rv;
  172. var hnadiv = document.getElementById('olsrd_hna');
  173. if (hnadiv) {
  174. var s =
  175. '<div class="tr cbi-section-table-titles">' +
  176. '<div class="th cbi-section-table-cell">Announced network</div>' +
  177. '<div class="th cbi-section-table-cell">OLSR gateway</div>' +
  178. '<div class="th cbi-section-table-cell">Validity Time</div>' +
  179. '</div>';
  180. for (var idx = 0; idx < info.length; idx++) {
  181. var hna = info[idx];
  182. var linkgw = '';
  183. s += '<div class="tr cbi-section-table-row cbi-rowstyle-' + (1 + (idx % 2)) + ' proto-' + hna.proto + '">';
  184. if (hna.proto === '6') {
  185. linkgw = '<a href="http://[' + hna.gateway + ']/cgi-bin-status.html">' + hna.gateway + '</a>';
  186. } else {
  187. linkgw = '<a href="http://' + hna.gateway + '/cgi-bin-status.html">' + hna.gateway + '</a>';
  188. }
  189. var validity = hna.validityTime !== undefined ? hna.validityTime + 's' : '-';
  190. var hostname = hna.hostname !== null ? ' / <a href="http://%q/cgi-bin-status.html">%h</a>'.format(hna.hostname, hna.hostname) : '';
  191. s +=
  192. '<div class="td cbi-section-table-cell left">' +
  193. (hna.destination + '/' + hna.genmask) +
  194. '</div>' +
  195. '<div class="td cbi-section-table-cell left">' +
  196. (linkgw + hostname) +
  197. '</div>' +
  198. '<div class="td cbi-section-table-cell left">' +
  199. validity +
  200. '</div>' +
  201. '</div>';
  202. }
  203. hnadiv.innerHTML = s;
  204. }
  205. var i = 1;
  206. for (var k = 0; k < hna_res.length; k++) {
  207. var route = hna_res[k];
  208. var tr = E('div', { 'class': 'tr cbi-section-table-row cbi-rowstyle-' + i + ' proto-' + route.proto }, [
  209. E('div', { 'class': 'td cbi-section-table-cell left' }, route.destination + '/' + route.genmask),
  210. E('div', { 'class': 'td cbi-section-table-cell left' }, [
  211. route.proto === '6' ? E('a', { 'href': 'http://[' + route.gateway + ']/cgi-bin-status.html' }, route.gateway) : E('a', { 'href': 'http://' + route.gateway + '/cgi-bin-status.html' }, route.gateway),
  212. route.hostname ? E('span', {}, [' / ', E('a', { 'href': 'http://%q/cgi-bin-status.html'.format(route.hostname) }, '%h'.format(route.hostname))]) : '',
  213. ]),
  214. E('div', { 'class': 'td cbi-section-table-cell left' }, route.validityTime ? route.validityTime + 's' : '-'),
  215. ]);
  216. table.appendChild(tr);
  217. i = (i % 2) + 1;
  218. }
  219. var fieldset = E('fieldset', { 'class': 'cbi-section' }, [E('legend', {}, _('Overview of currently active OLSR host net announcements')), table]);
  220. var h2 = E('h2', { 'name': 'content' }, _('Active host net announcements'));
  221. var divToggleButtons = E('div', { 'id': 'togglebuttons' });
  222. var statusOlsrCommonJs = null;
  223. if (has_v4 && has_v6) {
  224. statusOlsrCommonJs = E('script', { 'type': 'text/javascript', 'src': L.resource('common/common_js.js') });
  225. }
  226. var result = E([], {}, [h2, divToggleButtons, fieldset, statusOlsrCommonJs]);
  227. return result;
  228. })
  229. .catch(function (error) {
  230. console.error(error);
  231. });
  232. },
  233. handleSaveApply: null,
  234. handleSave: null,
  235. handleReset: null,
  236. });