olsrdplugins.js 8.6 KB

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