olsrdplugins6.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. 'use strict';
  2. 'require view';
  3. 'require form';
  4. 'require uci';
  5. 'require ui';
  6. 'require fs';
  7. return view.extend({
  8. load: function () {
  9. return Promise.all([
  10. uci.load('olsrd6').then(() => {
  11. return fs.list('/usr/lib').then((files) => {
  12. const sections = uci.sections('olsrd6', 'LoadPlugin');
  13. const libsArr = [];
  14. sections.forEach((section) => {
  15. const lib = section.library;
  16. libsArr.push(lib);
  17. });
  18. files.forEach((v) => {
  19. if (v.name.substr(0, 6) === 'olsrd_') {
  20. var pluginname = v.name.match(/^(olsrd_.*)\.so\..*/)[1];
  21. if (!libsArr.includes(pluginname)) {
  22. var sid = uci.add('olsrd6', 'LoadPlugin');
  23. uci.set('olsrd6', sid, 'ignore', '1');
  24. uci.set('olsrd6', sid, 'library', pluginname);
  25. }
  26. }
  27. });
  28. });
  29. }),
  30. ]);
  31. },
  32. render: function () {
  33. var pathname = window.location.pathname;
  34. var segments = pathname.split('/');
  35. var sidIndex = segments.lastIndexOf('plugins') + 1;
  36. var sid = null;
  37. if (sidIndex !== -1 && sidIndex < segments.length) {
  38. sid = segments[sidIndex];
  39. }
  40. if (sid) {
  41. var mp = new form.Map('olsrd6', _('OLSR - Plugins'));
  42. var p = mp.section(form.NamedSection, sid, 'LoadPlugin', _('Plugin configuration'));
  43. p.anonymous = true;
  44. var plname = uci.get('olsrd6', sid, 'library');
  45. var ign = p.option(form.Flag, 'ignore', _('Enable'));
  46. ign.enabled = '0';
  47. ign.disabled = '1';
  48. ign.rmempty = false;
  49. ign.cfgvalue = function (section_id) {
  50. return uci.get('olsrd6', section_id, 'ignore') || '0';
  51. };
  52. var lib = p.option(form.DummyValue, 'library', _('Library'));
  53. lib.default = plname;
  54. function Range(x, y) {
  55. var t = [];
  56. for (var i = x; i <= y; i++) {
  57. t.push(i);
  58. }
  59. return t;
  60. }
  61. function Cidr2IpMask(val) {
  62. function prefixToMask(prefix, isIPv6) {
  63. return isIPv6 ? network.prefixToMask(prefix, true) : network.prefixToMask(prefix, false);
  64. }
  65. if (val) {
  66. var newVal = val.map(cidr => {
  67. var [ip, prefix] = cidr.split('/');
  68. var networkip, mask;
  69. if (validation.parseIPv6(ip)) {
  70. networkip = ip;
  71. mask = prefixToMask(parseInt(prefix), true);
  72. } else if (validation.parseIPv4(ip)) {
  73. networkip = ip;
  74. mask = prefixToMask(parseInt(prefix), false);
  75. }
  76. return networkip && mask ? networkip + ' ' + mask : cidr;
  77. });
  78. }
  79. return newVal;
  80. }
  81. function IpMask2Cidr(val) {
  82. if (val) {
  83. for (let i = 0; i < val.length; i++) {
  84. var [ip, mask] = val[i].match(/([^ ]+)%s+([^ ]+)/) || [];
  85. var cidr;
  86. if (ip && mask) {
  87. if (validation.parseIPv6(ip)) {
  88. cidr = ip + '/' + mask;
  89. } else if (validation.parseIPv4(ip)) {
  90. var ipParts = ip.split('.');
  91. var maskParts = mask.split('.');
  92. var cidrParts = [];
  93. for (let j = 0; j < 4; j++) {
  94. var ipPart = parseInt(ipParts[j]);
  95. var maskPart = parseInt(maskParts[j]);
  96. var cidrPart = ipPart & maskPart;
  97. cidrParts.push(cidrPart);
  98. }
  99. var cidrPrefix = network.maskToPrefix(maskParts.join('.'));
  100. cidr = cidrParts.join('.') + '/' + cidrPrefix;
  101. }
  102. }
  103. if (cidr) {
  104. val[i] = cidr;
  105. }
  106. }
  107. }
  108. return val;
  109. }
  110. const knownPlParams = {
  111. olsrd_bmf: [
  112. [form.Value, 'BmfInterface', 'bmf0'],
  113. [form.Value, 'BmfInterfaceIp', '10.10.10.234/24'],
  114. [form.Flag, 'DoLocalBroadcast', 'no'],
  115. [form.Flag, 'CapturePacketsOnOlsrInterfaces', 'yes'],
  116. [form.ListValue, 'BmfMechanism', ['UnicastPromiscuous', 'Broadcast']],
  117. [form.Value, 'BroadcastRetransmitCount', '2'],
  118. [form.Value, 'FanOutLimit', '4'],
  119. [form.DynamicList, 'NonOlsrIf', 'br-lan'],
  120. ],
  121. olsrd_dyn_gw: [
  122. [form.Value, 'Interval', '40'],
  123. [form.DynamicList, 'Ping', '141.1.1.1'],
  124. [form.DynamicList, 'HNA', '192.168.80.0/24', IpMask2Cidr, Cidr2IpMask],
  125. ],
  126. olsrd_httpinfo: [
  127. [form.Value, 'port', '80'],
  128. [form.DynamicList, 'Host', '163.24.87.3'],
  129. [form.DynamicList, 'Net', '0.0.0.0/0', Cidr2IpMask],
  130. ],
  131. olsrd_nameservice: [
  132. [form.DynamicList, 'name', 'my-name.mesh'],
  133. [form.DynamicList, 'hosts', '1.2.3.4 name-for-other-interface.mesh'],
  134. [form.Value, 'suffix', '.olsr'],
  135. [form.Value, 'hosts_file', '/path/to/hosts_file'],
  136. [form.Value, 'add_hosts', '/path/to/file'],
  137. [form.Value, 'dns_server', '141.1.1.1'],
  138. [form.Value, 'resolv_file', '/path/to/resolv.conf'],
  139. [form.Value, 'interval', '120'],
  140. [form.Value, 'timeout', '240'],
  141. [form.Value, 'lat', '12.123'],
  142. [form.Value, 'lon', '12.123'],
  143. [form.Value, 'latlon_file', '/var/run/latlon.js.ipv6'],
  144. [form.Value, 'latlon_infile', '/var/run/gps.txt'],
  145. [form.Value, 'sighup_pid_file', '/var/run/dnsmasq.pid'],
  146. [form.Value, 'name_change_script', '/usr/local/bin/announce_new_hosts.sh'],
  147. [form.DynamicList, 'service', 'http://me.olsr:80|tcp|my little homepage'],
  148. [form.Value, 'services_file', '/var/run/services_olsr'],
  149. [form.Value, 'services_change_script', '/usr/local/bin/announce_new_services.sh'],
  150. [form.DynamicList, 'mac', 'xx:xx:xx:xx:xx:xx[,0-255]'],
  151. [form.Value, 'macs_file', '/path/to/macs_file'],
  152. [form.Value, 'macs_change_script', '/path/to/script'],
  153. ],
  154. olsrd_quagga: [
  155. [form.DynamicList, 'redistribute', ['system', 'kernel', 'connect', 'static', 'rip', 'ripng', 'ospf', 'ospf6', 'isis', 'bgp', 'hsls']],
  156. [form.ListValue, 'ExportRoutes', ['only', 'both']],
  157. [form.Flag, 'LocalPref', 'true'],
  158. [form.Value, 'Distance', Range(0, 255)],
  159. ],
  160. olsrd_secure: [[form.Value, 'Keyfile', '/etc/private-olsr.key']],
  161. olsrd_txtinfo: [[form.Value, 'accept', '::1/128']],
  162. olsrd_jsoninfo: [
  163. [form.Value, 'accept', '::1/128'],
  164. [form.Value, 'port', '9090'],
  165. [form.Value, 'UUIDFile', '/etc/olsrd/olsrd.uuid.ipv6'],
  166. ],
  167. olsrd_watchdog: [
  168. [form.Value, 'file', '/var/run/olsrd.watchdog.ipv6'],
  169. [form.Value, 'interval', '30'],
  170. ],
  171. olsrd_mdns: [[form.DynamicList, 'NonOlsrIf', 'lan']],
  172. olsrd_p2pd: [
  173. [form.DynamicList, 'NonOlsrIf', 'lan'],
  174. [form.Value, 'P2pdTtl', '10'],
  175. ],
  176. olsrd_arprefresh: [],
  177. olsrd_dot_draw: [],
  178. olsrd_dyn_gw_plain: [],
  179. olsrd_pgraph: [],
  180. olsrd_tas: [],
  181. };
  182. if (knownPlParams[plname]) {
  183. for (const option of knownPlParams[plname]) {
  184. const [otype, name, defaultVal, uci2cbi, cbi2uci] = option;
  185. let values;
  186. if (Array.isArray(defaultVal)) {
  187. values = defaultVal;
  188. defaultVal = defaultVal[0];
  189. }
  190. if (otype === form.Flag) {
  191. const bool = p.option(form.Flag, name, name);
  192. if (defaultVal === 'yes' || defaultVal === 'no') {
  193. bool.enabled = 'yes';
  194. bool.disabled = 'no';
  195. } else if (defaultVal === 'on' || defaultVal === 'off') {
  196. bool.enabled = 'on';
  197. bool.disabled = 'off';
  198. } else if (defaultVal === '1' || defaultVal === '0') {
  199. bool.enabled = '1';
  200. bool.disabled = '0';
  201. } else {
  202. bool.enabled = 'true';
  203. bool.disabled = 'false';
  204. }
  205. bool.optional = true;
  206. bool.placeholder = defaultVal;
  207. bool.cfgvalue = function (section_id) {
  208. return uci.get('olsrd6', section_id, name);
  209. };
  210. } else {
  211. const field = p.option(otype, name, name);
  212. if (values) {
  213. for (const value of values) {
  214. field.value(value);
  215. }
  216. }
  217. field.cfgvalue = function (section_id) {
  218. return uci.get('olsrd6', section_id, name);
  219. };
  220. if (typeof uci2cbi === 'function') {
  221. field.cfgvalue = function (section_id) {
  222. return uci2cbi(uci.get('olsrd6', section_id, name));
  223. };
  224. }
  225. if (typeof cbi2uci === 'function') {
  226. field.write = function (section_id, formvalue) {
  227. var saveval=cbi2uci(formvalue);
  228. uci.set('olsrd6', section_id, name, saveval);
  229. };
  230. }
  231. field.optional = true;
  232. field.placeholder = defaultVal;
  233. }
  234. }
  235. }
  236. return mp.render();
  237. } else {
  238. var mpi = new form.Map('olsrd6', _('OLSR - Plugins'));
  239. var t = mpi.section(form.TableSection, 'LoadPlugin', _('Plugins'));
  240. t.anonymous = true;
  241. t.extedit = function (eve) {
  242. var editButton = eve.target;
  243. var sid;
  244. var row = editButton.closest('.cbi-section-table-row');
  245. if (row) {
  246. sid = row.getAttribute('data-sid');
  247. console.log(sid);
  248. }
  249. window.location.href = `plugins/${sid}`;
  250. };
  251. var ign = t.option(form.Flag, 'ignore', _('Enabled'));
  252. ign.enabled = '0';
  253. ign.disabled = '1';
  254. ign.rmempty = false;
  255. function ign_cfgvalue(section_id) {
  256. return uci.get(section_id, 'ignore') || '0';
  257. }
  258. t.option(form.DummyValue, 'library', _('Library'));
  259. return mpi.render();
  260. }
  261. },
  262. });