luci-app-banip: fix "Apply & Restart" button call chain
[project/luci.git] / applications / luci-app-banip / htdocs / luci-static / resources / view / banip / overview.js
1 'use strict';
2 'require dom';
3 'require view';
4 'require poll';
5 'require fs';
6 'require ui';
7 'require uci';
8 'require form';
9 'require tools.widgets as widgets';
10
11 /*
12 button handling
13 */
14 function handleAction(ev) {
15 if (ev === 'restart') {
16 let map = document.querySelector('.cbi-map');
17 return dom.callClassMethod(map, 'save')
18 .then(L.bind(ui.changes.apply, ui.changes))
19 .then(function() {
20 return fs.exec_direct('/etc/init.d/banip', [ev]);
21 });
22 } else {
23 return fs.exec_direct('/etc/init.d/banip', [ev]);
24 }
25 }
26
27 return view.extend({
28 load: function () {
29 return Promise.all([
30 L.resolveDefault(fs.read_direct('/etc/banip/banip.custom.feeds'), ''),
31 L.resolveDefault(fs.read_direct('/etc/banip/banip.feeds'), ''),
32 L.resolveDefault(fs.read_direct('/etc/banip/banip.countries'), ''),
33 uci.load('banip')
34 ]);
35 },
36
37 render: function (result) {
38 let m, s, o;
39
40 m = new form.Map('banip', 'banIP', _('Configuration of the banIP package to ban incoming and outgoing IPs via named nftables Sets. \
41 For further information please check the <a style="color:#37c;font-weight:bold;" href="https://github.com/openwrt/packages/blob/master/net/banip/files/README.md" target="_blank" rel="noreferrer noopener" >online documentation</a>'));
42
43 /*
44 poll runtime information
45 */
46 let buttons, rtRes, infStat, infVer, infElements, infFeeds, infDevices, infUplink, infSystem, nftInfos, runInfos, infFlags, last_run
47
48 pollData: poll.add(function () {
49 return L.resolveDefault(fs.stat('/var/run/banip.lock')).then(function (stat) {
50 buttons = document.querySelectorAll('.cbi-button');
51 infStat = document.getElementById('status');
52 if (stat) {
53 for (let i = 0; i < buttons.length; i++) {
54 buttons[i].setAttribute('disabled', 'true');
55 }
56 if (infStat && !infStat.classList.contains('spinning')) {
57 infStat.classList.add('spinning');
58 }
59 } else {
60 for (let i = 0; i < buttons.length; i++) {
61 buttons[i].removeAttribute('disabled');
62 }
63 if (infStat && infStat.classList.contains('spinning')) {
64 infStat.classList.remove('spinning');
65 }
66 }
67 L.resolveDefault(fs.exec_direct('/etc/init.d/banip', ['status'])).then(function (result) {
68 if (result) {
69 rtRes = result.trim().split('\n');
70 if (rtRes) {
71 for (let i = 0; i < rtRes.length; i++) {
72 if (rtRes[i].match(/^\s+\+\sstatus\s+\:\s+(.*)$/)) {
73 rtRes.status = rtRes[i].match(/^\s+\+\sstatus\s+\:\s+(.*)$/)[1];
74 } else if (rtRes[i].match(/^\s+\+\sversion\s+\:\s+(.*)$/)) {
75 rtRes.version = rtRes[i].match(/^\s+\+\sversion\s+\:\s+(.*)$/)[1];
76 } else if (rtRes[i].match(/^\s+\+\selement_count\s+\:\s+(.*)$/)) {
77 rtRes.elementCount = rtRes[i].match(/^\s+\+\selement_count\s+\:\s+(.*)$/)[1];
78 } else if (rtRes[i].match(/^\s+\+\sactive_feeds\s+\:\s+(.*)$/)) {
79 rtRes.activeFeeds = rtRes[i].match(/^\s+\+\sactive_feeds\s+\:\s+(.*)$/)[1];
80 } else if (rtRes[i].match(/^\s+\+\sactive_devices\s+\:\s+(.*)$/)) {
81 rtRes.activeDevices = rtRes[i].match(/^\s+\+\sactive_devices\s+\:\s+(.*)$/)[1];
82 } else if (rtRes[i].match(/^\s+\+\sactive_uplink\s+\:\s+(.*)$/)) {
83 rtRes.activeUplink = rtRes[i].match(/^\s+\+\sactive_uplink\s+\:\s+(.*)$/)[1];
84 } else if (rtRes[i].match(/^\s+\+\snft_info\s+\:\s+(.*)$/)) {
85 rtRes.nftInfo = rtRes[i].match(/^\s+\+\snft_info\s+\:\s+(.*)$/)[1];
86 } else if (rtRes[i].match(/^\s+\+\srun_info\s+\:\s+(.*)$/)) {
87 rtRes.runInfo = rtRes[i].match(/^\s+\+\srun_info\s+\:\s+(.*)$/)[1];
88 } else if (rtRes[i].match(/^\s+\+\srun_flags\s+\:\s+(.*)$/)) {
89 rtRes.runFlags = rtRes[i].match(/^\s+\+\srun_flags\s+\:\s+(.*)$/)[1];
90 } else if (rtRes[i].match(/^\s+\+\slast_run\s+\:\s+(.*)$/)) {
91 rtRes.lastRun = rtRes[i].match(/^\s+\+\slast_run\s+\:\s+(.*)$/)[1];
92 } else if (rtRes[i].match(/^\s+\+\ssystem_info\s+\:\s+(.*)$/)) {
93 rtRes.systemInfo = rtRes[i].match(/^\s+\+\ssystem_info\s+\:\s+(.*)$/)[1];
94 }
95 }
96 }
97 if (rtRes) {
98 infStat = document.getElementById('status');
99 if (infStat) {
100 infStat.textContent = rtRes.status || '-';
101 }
102 infVer = document.getElementById('version');
103 if (infVer) {
104 infVer.textContent = rtRes.version || '-';
105 }
106 infElements = document.getElementById('elements');
107 if (infElements) {
108 infElements.textContent = rtRes.elementCount || '-';
109 }
110 infFeeds = document.getElementById('feeds');
111 if (infFeeds) {
112 infFeeds.textContent = rtRes.activeFeeds || '-';
113 }
114 infDevices = document.getElementById('devices');
115 if (infDevices) {
116 infDevices.textContent = rtRes.activeDevices || '-';
117 }
118 infUplink = document.getElementById('uplink');
119 if (infUplink) {
120 infUplink.textContent = rtRes.activeUplink || '-';
121 }
122 nftInfos = document.getElementById('nft');
123 if (nftInfos) {
124 nftInfos.textContent = rtRes.nftInfo || '-';
125 }
126 runInfos = document.getElementById('run');
127 if (runInfos) {
128 runInfos.textContent = rtRes.runInfo || '-';
129 }
130 infFlags = document.getElementById('flags');
131 if (infFlags) {
132 infFlags.textContent = rtRes.runFlags || '-';
133 }
134 last_run = document.getElementById('last');
135 if (last_run) {
136 last_run.textContent = rtRes.lastRun || '-';
137 }
138 infSystem = document.getElementById('system');
139 if (infSystem) {
140 infSystem.textContent = rtRes.systemInfo || '-';
141 }
142 }
143 } else {
144 infStat = document.getElementById('status');
145 if (infStat) {
146 infStat.textContent = '-';
147 poll.stop();
148 if (infStat.classList.contains('spinning')) {
149 infStat.classList.remove('spinning');
150 }
151 }
152 }
153 });
154 });
155 }, 2);
156
157 /*
158 runtime information and buttons
159 */
160 s = m.section(form.NamedSection, 'global');
161 s.render = L.bind(function (view, section_id) {
162 return E('div', { 'class': 'cbi-section' }, [
163 E('h3', _('Information')),
164 E('div', { 'class': 'cbi-value' }, [
165 E('label', { 'class': 'cbi-value-title', 'style': 'margin-bottom:-5px;float:left;font-weight:bold;padding-top:0rem;' }, _('Status')),
166 E('div', { 'class': 'cbi-value-field spinning', 'id': 'status', 'style': 'margin-bottom:-5px;float:left;color:#37c;font-weight:bold;' }, '\xa0')
167 ]),
168 E('div', { 'class': 'cbi-value' }, [
169 E('label', { 'class': 'cbi-value-title', 'style': 'margin-bottom:-5px;float:left;font-weight:bold;padding-top:0rem;' }, _('Version')),
170 E('div', { 'class': 'cbi-value-field', 'id': 'version', 'style': 'margin-bottom:-5px;float:left;color:#37c;font-weight:bold;' }, '-')
171 ]),
172 E('div', { 'class': 'cbi-value' }, [
173 E('label', { 'class': 'cbi-value-title', 'style': 'margin-bottom:-5px;float:left;font-weight:bold;padding-top:0rem;' }, _('Element Count')),
174 E('div', { 'class': 'cbi-value-field', 'id': 'elements', 'style': 'margin-bottom:-5px;float:left;color:#37c;font-weight:bold;' }, '-')
175 ]),
176 E('div', { 'class': 'cbi-value' }, [
177 E('label', { 'class': 'cbi-value-title', 'style': 'margin-bottom:-5px;float:left;font-weight:bold;padding-top:0rem;' }, _('Active Feeds')),
178 E('div', { 'class': 'cbi-value-field', 'id': 'feeds', 'style': 'margin-bottom:-5px;float:left;color:#37c;font-weight:bold;' }, '-')
179 ]),
180 E('div', { 'class': 'cbi-value' }, [
181 E('label', { 'class': 'cbi-value-title', 'style': 'margin-bottom:-5px;float:left;font-weight:bold;padding-top:0rem;' }, _('Active Devices')),
182 E('div', { 'class': 'cbi-value-field', 'id': 'devices', 'style': 'margin-bottom:-5px;float:left;color:#37c;font-weight:bold;' }, '-')
183 ]),
184 E('div', { 'class': 'cbi-value' }, [
185 E('label', { 'class': 'cbi-value-title', 'style': 'margin-bottom:-5px;float:left;font-weight:bold;padding-top:0rem;' }, _('Active Uplink')),
186 E('div', { 'class': 'cbi-value-field', 'id': 'uplink', 'style': 'margin-bottom:-5px;float:left;color:#37c;font-weight:bold;' }, '-')
187 ]),
188 E('div', { 'class': 'cbi-value' }, [
189 E('label', { 'class': 'cbi-value-title', 'style': 'margin-bottom:-5px;float:left;font-weight:bold;padding-top:0rem;' }, _('NFT Information')),
190 E('div', { 'class': 'cbi-value-field', 'id': 'nft', 'style': 'margin-bottom:-5px;float:left;color:#37c;font-weight:bold;' }, '-')
191 ]),
192 E('div', { 'class': 'cbi-value' }, [
193 E('label', { 'class': 'cbi-value-title', 'style': 'margin-bottom:-5px;float:left;font-weight:bold;padding-top:0rem;' }, _('Run Information')),
194 E('div', { 'class': 'cbi-value-field', 'id': 'run', 'style': 'margin-bottom:-5px;float:left;color:#37c;font-weight:bold;' }, '-')
195 ]),
196 E('div', { 'class': 'cbi-value' }, [
197 E('label', { 'class': 'cbi-value-title', 'style': 'margin-bottom:-5px;float:left;font-weight:bold;padding-top:0rem;' }, _('Run Flags')),
198 E('div', { 'class': 'cbi-value-field', 'id': 'flags', 'style': 'margin-bottom:-5px;float:left;color:#37c;font-weight:bold;' }, '-')
199 ]),
200 E('div', { 'class': 'cbi-value' }, [
201 E('label', { 'class': 'cbi-value-title', 'style': 'margin-bottom:-5px;float:left;font-weight:bold;padding-top:0rem;' }, _('Last Run')),
202 E('div', { 'class': 'cbi-value-field', 'id': 'last', 'style': 'margin-bottom:-5px;float:left;color:#37c;font-weight:bold;' }, '-')
203 ]),
204 E('div', { 'class': 'cbi-value' }, [
205 E('label', { 'class': 'cbi-value-title', 'style': 'margin-bottom:-5px;float:left;font-weight:bold;padding-top:0rem;' }, _('System Information')),
206 E('div', { 'class': 'cbi-value-field', 'id': 'system', 'style': 'margin-bottom:-5px;float:left;color:#37c;font-weight:bold;' }, '-')
207 ]),
208 E('div', { class: 'right' }, [
209 E('button', {
210 'class': 'btn cbi-button cbi-button-action',
211 'click': ui.createHandlerFn(this, function () {
212 return handleAction('lookup');
213 })
214 }, [_('Domain Lookup')]),
215 '\xa0\xa0\xa0',
216 E('button', {
217 'class': 'btn cbi-button cbi-button-negative',
218 'click': ui.createHandlerFn(this, function () {
219 return handleAction('stop');
220 })
221 }, [_('Stop')]),
222 '\xa0\xa0\xa0',
223 E('button', {
224 'class': 'btn cbi-button cbi-button-positive',
225 'click': ui.createHandlerFn(this, function () {
226 return handleAction('reload');
227 })
228 }, [_('Reload')]),
229 '\xa0\xa0\xa0',
230 E('button', {
231 'class': 'btn cbi-button cbi-button-positive',
232 'click': ui.createHandlerFn(this, function () {
233 return handleAction('restart');
234 })
235 }, [_('Apply & Restart')])
236 ])
237 ]);
238 }, o, this);
239 this.pollData;
240
241 /*
242 tabbed config section
243 */
244 s = m.section(form.NamedSection, 'global', 'banip', _('Settings'));
245 s.addremove = false;
246 s.tab('general', _('General Settings'));
247 s.tab('advanced', _('Advanced Settings'));
248 s.tab('adv_chain', _('Table/Chain Settings'));
249 s.tab('adv_set', _('Feed/Set Settings'));
250 s.tab('adv_log', _('Log Settings'));
251 s.tab('adv_email', _('E-Mail Settings'));
252 s.tab('feeds', _('Feed Selection'));
253
254 /*
255 general settings tab
256 */
257 o = s.taboption('general', form.Flag, 'ban_enabled', _('Enabled'), _('Enable the banIP service.'));
258 o.rmempty = false;
259
260 o = s.taboption('general', form.Flag, 'ban_debug', _('Verbose Debug Logging'), _('Enable verbose debug logging in case of processing errors.'));
261 o.rmempty = false;
262
263 o = s.taboption('general', form.Flag, 'ban_autodetect', _('Auto Detection'), _('Detect relevant network devices, interfaces, subnets, protocols and utilities automatically.'));
264 o.rmempty = false;
265
266 o = s.taboption('general', form.Flag, 'ban_protov4', _('IPv4 Support'), _('Enables IPv4 support.'));
267 o.depends('ban_autodetect', '0');
268 o.optional = true;
269 o.retain = true;
270
271 o = s.taboption('general', form.Flag, 'ban_protov6', _('IPv6 Support'), _('Enables IPv6 support.'));
272 o.depends('ban_autodetect', '0');
273 o.optional = true;
274 o.retain = true;
275
276 o = s.taboption('general', widgets.DeviceSelect, 'ban_dev', _('Network Devices'), _('Select the WAN network device(s).'));
277 o.depends('ban_autodetect', '0');
278 o.multiple = true;
279 o.nocreate = true;
280 o.optional = true;
281 o.retain = true;
282
283 o = s.taboption('general', widgets.NetworkSelect, 'ban_ifv4', _('IPv4 Network Interfaces'), _('Select the logical WAN IPv4 network interface(s).'));
284 o.depends('ban_autodetect', '0');
285 o.multiple = true;
286 o.nocreate = true;
287 o.optional = true;
288 o.retain = true;
289
290 o = s.taboption('general', widgets.NetworkSelect, 'ban_ifv6', _('IPv6 Network Interfaces'), _('Select the logical WAN IPv6 network interface(s).'));
291 o.depends('ban_autodetect', '0');
292 o.multiple = true;
293 o.nocreate = true;
294 o.optional = true;
295 o.retain = true;
296
297 o = s.taboption('general', form.ListValue, 'ban_fetchcmd', _('Download Utility'), _('Select one of the pre-configured download utilities.'));
298 o.depends('ban_autodetect', '0');
299 o.value('uclient-fetch');
300 o.value('wget');
301 o.value('curl');
302 o.value('aria2c');
303 o.optional = true;
304 o.retain = true;
305
306 o = s.taboption('general', form.Value, 'ban_fetchparm', _('Download Parameters'), _('Override the pre-configured download options for the selected download utility.'))
307 o.depends('ban_autodetect', '0');
308 o.optional = true;
309 o.retain = true;
310
311 o = s.taboption('general', widgets.NetworkSelect, 'ban_trigger', _('Reload Trigger Interface'), _('List of available reload trigger interface(s).'));
312 o.multiple = true;
313 o.nocreate = true;
314 o.rmempty = true;
315
316 o = s.taboption('general', form.Value, 'ban_triggerdelay', _('Trigger Delay'), _('Additional trigger delay in seconds during interface reload and boot.'));
317 o.placeholder = '10';
318 o.datatype = 'range(1,300)';
319 o.rmempty = true;
320
321 o = s.taboption('general', form.ListValue, 'ban_fetchretry', _('Download Retries'), _('Number of download attempts in case of an error (not supported by uclient-fetch).'));
322 o.value('1', _('1'));
323 o.value('3', _('3'));
324 o.value('5', _('5 (default)'));
325 o.value('10', _('10'));
326 o.value('20', _('20'));
327 o.optional = true;
328 o.rmempty = true;
329
330 o = s.taboption('general', form.Flag, 'ban_fetchinsecure', _('Download Insecure'), _('Don\'t check SSL server certificates during download.'));
331 o.rmempty = true;
332
333 /*
334 additional settings tab
335 */
336 o = s.taboption('advanced', form.DummyValue, '_sub');
337 o.rawhtml = true;
338 o.default = '<em style="color:#37c;font-weight:bold;">' + _('Changes on this tab needs a banIP service restart to take effect.') + '</em>';
339
340 o = s.taboption('advanced', form.ListValue, 'ban_nicelimit', _('Nice Level'), _('The selected priority will be used for banIP background processing.'));
341 o.value('-20', _('Highest Priority'));
342 o.value('-10', _('High Priority'));
343 o.value('0', _('Normal Priority (default)'));
344 o.value('10', _('Less Priority'));
345 o.value('19', _('Least Priority'));
346 o.optional = true;
347 o.rmempty = true;
348
349 o = s.taboption('advanced', form.ListValue, 'ban_filelimit', _('Max Open Files'), _('Increase the maximal number of open files, e.g. to handle the amount of temporary split files while loading the Sets.'));
350 o.value('512', _('512'));
351 o.value('1024', _('1024 (default)'));
352 o.value('2048', _('2048'));
353 o.value('4096', _('4096'));
354 o.optional = true;
355 o.rmempty = true;
356
357 o = s.taboption('advanced', form.ListValue, 'ban_cores', _('CPU Cores'), _('Limit the cpu cores used by banIP to save RAM.'));
358 o.value('1');
359 o.value('2');
360 o.value('4');
361 o.value('8');
362 o.value('16');
363 o.optional = true;
364 o.rmempty = true;
365
366 o = s.taboption('advanced', form.ListValue, 'ban_splitsize', _('Set Split Size'), _('Split external Set loading after every n members to save RAM.'));
367 o.value('512');
368 o.value('1024');
369 o.value('2048');
370 o.value('4096');
371 o.value('8192');
372 o.value('16384');
373 o.optional = true;
374 o.rmempty = true;
375
376 o = s.taboption('advanced', form.Value, 'ban_basedir', _('Base Directory'), _('Base working directory while banIP processing.'));
377 o.placeholder = '/tmp';
378 o.rmempty = true;
379
380 o = s.taboption('advanced', form.Value, 'ban_backupdir', _('Backup Directory'), _('Target directory for compressed feed backups.'));
381 o.placeholder = '/tmp/banIP-backup';
382 o.rmempty = true;
383
384 o = s.taboption('advanced', form.Value, 'ban_reportdir', _('Report Directory'), _('Target directory for banIP-related report files.'));
385 o.placeholder = '/tmp/banIP-report';
386 o.rmempty = true;
387
388 o = s.taboption('advanced', form.Flag, 'ban_deduplicate', _('Deduplicate IPs'), _('Deduplicate IP addresses across all active Sets and tidy up the local blocklist.'));
389 o.default = 1
390 o.rmempty = false;
391
392 o = s.taboption('advanced', form.Flag, 'ban_reportelements', _('Report Elements'), _('List Set elements in the status and report, disable this to reduce the CPU load.'));
393 o.default = 1
394 o.optional = true;
395
396 /*
397 advanced chain settings tab
398 */
399 o = s.taboption('adv_chain', form.DummyValue, '_sub');
400 o.rawhtml = true;
401 o.default = '<em style="color:#37c;font-weight:bold;">' + _('Changes on this tab needs a banIP service restart to take effect.') + '</em>';
402
403 o = s.taboption('adv_chain', form.ListValue, 'ban_nftpriority', _('Chain Priority'), _('Set the nft chain priority within the banIP table, lower values means higher priority.'));
404 o.value('0', _('0'));
405 o.value('-100', _('-100 (default)'));
406 o.value('-150', _('-150'));
407 o.value('-200', _('-200'));
408 o.optional = true;
409 o.rmempty = true;
410
411 o = s.taboption('adv_chain', form.Value, 'ban_allowflag', _('Allow Protocol/Ports'), _('Always allow a protocol \(tcp/udp\) with certain ports or port ranges in WAN-Input and WAN-Forward chain.'));
412 o.placeholder = 'tcp 80 443-445';
413 o.rmempty = true;
414
415 o = s.taboption('adv_chain', widgets.DeviceSelect, 'ban_vlanallow', _('Allow VLAN Forwards'), _('Always allow certain VLAN forwards.'));
416 o.multiple = true;
417 o.nocreate = true;
418 o.optional = true;
419 o.rmempty = true;
420
421 o = s.taboption('adv_chain', widgets.DeviceSelect, 'ban_vlanblock', _('Block VLAN Forwards'), _('Always block certain VLAN forwards.'));
422 o.multiple = true;
423 o.nocreate = true;
424 o.optional = true;
425 o.rmempty = true;
426
427 o = s.taboption('adv_chain', form.ListValue, 'ban_icmplimit', _('ICMP-Treshold'), _('ICMP-Treshold in packets per second to prevent WAN-DDoS attacks.'));
428 o.value('1', _('1'));
429 o.value('10', _('10 (default)'));
430 o.value('50', _('50'));
431 o.value('100', _('100'));
432 o.value('250', _('250'));
433 o.value('500', _('500'));
434 o.optional = true;
435 o.rmempty = true;
436
437 o = s.taboption('adv_chain', form.ListValue, 'ban_synlimit', _('SYN-Treshold'), _('SYN-Treshold in packets per second to prevent WAN-DDoS attacks.'));
438 o.value('1', _('1'));
439 o.value('10', _('10 (default)'));
440 o.value('50', _('50'));
441 o.value('100', _('100'));
442 o.value('250', _('250'));
443 o.value('500', _('500'));
444 o.optional = true;
445 o.rmempty = true;
446
447 o = s.taboption('adv_chain', form.ListValue, 'ban_udplimit', _('UDP-Treshold'), _('UDP-Treshold in packets per second to prevent WAN-DDoS attacks.'));
448 o.value('1', _('1'));
449 o.value('10', _('10'));
450 o.value('50', _('50'));
451 o.value('100', _('100 (default)'));
452 o.value('250', _('250'));
453 o.value('500', _('500'));
454 o.optional = true;
455 o.rmempty = true;
456
457 /*
458 advanced Set settings tab
459 */
460 o = s.taboption('adv_set', form.DummyValue, '_sub');
461 o.rawhtml = true;
462 o.default = '<em style="color:#37c;font-weight:bold;">' + _('Changes on this tab needs a banIP service restart to take effect.') + '</em>';
463
464 o = s.taboption('adv_set', form.ListValue, 'ban_nftpolicy', _('Set Policy'), _('Set the nft policy for banIP-related Sets.'));
465 o.value('memory', _('memory (default)'));
466 o.value('performance', _('performance'));
467 o.optional = true;
468 o.rmempty = true;
469
470 o = s.taboption('adv_set', form.ListValue, 'ban_blocktype', _('Block Type'), _('Drop packets silently or actively reject the traffic on WAN-Input and WAN-Forward chains.'));
471 o.value('drop', _('drop (default)'));
472 o.value('reject', _('reject'));
473 o.optional = true;
474 o.rmempty = true;
475
476 o = s.taboption('adv_set', form.ListValue, 'ban_blockpolicy', _('Default Block Policy'), _('By default each feed is active in all supported chains. Limit the default block policy to a certain chain.'));
477 o.value('input', _('WAN-Input Chain'));
478 o.value('forwardwan', _('WAN-Forward Chain'));
479 o.value('forwardlan', _('LAN-Forward Chain'));
480 o.optional = true;
481 o.rmempty = true;
482
483 let feed, feeds, descr;
484 if (result[0]) {
485 try {
486 feeds = JSON.parse(result[0]);
487 } catch (e) {
488 feeds = "";
489 ui.addNotification(null, E('p', _('Unable to parse the custom feed file: %s').format(e.message)), 'error');
490 }
491 } else if (result[1]) {
492 try {
493 feeds = JSON.parse(result[1]);
494 } catch (e) {
495 feeds = "";
496 ui.addNotification(null, E('p', _('Unable to parse the default feed file: %s').format(e.message)), 'error');
497 }
498 }
499 if (feeds) {
500 o = s.taboption('adv_set', form.MultiValue, 'ban_blockinput', _('WAN-Input Chain'), _('Limit certain feeds to the WAN-Input chain.'));
501 o.value('allowlist', _('local allowlist'));
502 o.value('blocklist', _('local blocklist'));
503 for (let i = 0; i < Object.keys(feeds).length; i++) {
504 feed = Object.keys(feeds)[i].trim();
505 o.value(feed);
506 }
507 o.optional = true;
508 o.rmempty = true;
509
510 o = s.taboption('adv_set', form.MultiValue, 'ban_blockforwardwan', _('WAN-Forward Chain'), _('Limit certain feeds to the WAN-Forward chain.'));
511 o.value('allowlist', _('local allowlist'));
512 o.value('blocklist', _('local blocklist'));
513 for (let i = 0; i < Object.keys(feeds).length; i++) {
514 feed = Object.keys(feeds)[i].trim();
515 o.value(feed);
516 }
517 o.optional = true;
518 o.rmempty = true;
519
520 o = s.taboption('adv_set', form.MultiValue, 'ban_blockforwardlan', _('LAN-Forward Chain'), _('Limit certain feeds to the LAN-Forward chain.'));
521 o.value('allowlist', _('local allowlist'));
522 o.value('blocklist', _('local blocklist'));
523 for (let i = 0; i < Object.keys(feeds).length; i++) {
524 feed = Object.keys(feeds)[i].trim();
525 o.value(feed);
526 }
527 o.optional = true;
528 o.rmempty = true;
529 }
530
531 /*
532 advanced log settings tab
533 */
534 o = s.taboption('adv_log', form.DummyValue, '_sub');
535 o.rawhtml = true;
536 o.default = '<em style="color:#37c;font-weight:bold;">' + _('Changes on this tab needs a banIP service restart to take effect.') + '</em>';
537
538 o = s.taboption('adv_log', form.ListValue, 'ban_nftloglevel', _('NFT Log Level'), _('Set the syslog level for NFT logging.'));
539 o.value('emerg', _('emerg'));
540 o.value('alert', _('alert'));
541 o.value('crit', _('crit'));
542 o.value('err', _('err'));
543 o.value('warn', _('warn (default)'));
544 o.value('notice', _('notice'));
545 o.value('info', _('info'));
546 o.value('debug', _('debug'));
547 o.optional = true;
548 o.rmempty = true;
549
550 o = s.taboption('adv_log', form.Flag, 'ban_logprerouting', _('Log Prerouting'), _('Log suspicious Prerouting packets.'));
551 o.rmempty = false;
552
553 o = s.taboption('adv_log', form.Flag, 'ban_loginput', _('Log WAN-Input'), _('Log suspicious incoming WAN packets.'));
554 o.rmempty = false;
555
556 o = s.taboption('adv_log', form.Flag, 'ban_logforwardwan', _('Log WAN-Forward'), _('Log suspicious forwarded WAN packets.'));
557 o.rmempty = false;
558
559 o = s.taboption('adv_log', form.Flag, 'ban_logforwardlan', _('Log LAN-Forward'), _('Log suspicious forwarded LAN packets.'));
560 o.rmempty = false;
561
562 o = s.taboption('adv_log', form.Value, 'ban_logreadfile', _('Logfile Location'), _('Location for parsing the log file, e.g. via syslog-ng, to deactivate the standard parsing via logread.'));
563 o.placeholder = '/var/log/messages';
564 o.rmempty = true;
565
566 o = s.taboption('adv_log', form.ListValue, 'ban_loglimit', _('Log Limit'), _('Parse only the last stated number of log entries for suspicious events. To disable the log monitor at all set it to \'0\'.'));
567 o.value('0', _('0 (disable)'));
568 o.value('50', _('50'));
569 o.value('100', _('100 (default)'));
570 o.value('250', _('250'));
571 o.value('500', _('500'));
572 o.value('1000', _('1000'));
573 o.optional = true;
574 o.rmempty = true;
575
576 o = s.taboption('adv_log', form.Value, 'ban_logcount', _('Log Count'), _('Number of failed login attempts of the same IP in the log before blocking.'));
577 o.placeholder = '1';
578 o.datatype = 'range(1,10)';
579 o.rmempty = true;
580
581 o = s.taboption('adv_log', form.DynamicList, 'ban_logterm', _('Log Terms'), _('The default regular expressions are filtering suspicious ssh, LuCI, nginx and asterisk traffic.'));
582 o.optional = true;
583 o.rmempty = true;
584
585 o = s.taboption('adv_log', form.Flag, 'ban_remotelog', _('Enable Remote Logging'), _('Enable the cgi interface to receive remote logging events.'));
586 o.default = 0
587 o.optional = true;
588 o.rmempty = true;
589
590 o = s.taboption('adv_log', form.Value, 'ban_remotetoken', _('Remote Token'), _('Token to communicate with the cgi interface.'));
591 o.depends('ban_remotelog', '1');
592 o.datatype = 'and(minlength(3),maxlength(20))';
593 o.validate = function (section_id, value) {
594 if (!value) {
595 return _('Empty field not allowed');
596 }
597 if (!value.match(/^[A-Za-z0-9\.\:]+$/)) {
598 return _('Invalid characters');
599 }
600 return true;
601 }
602 o.optional = true;
603 o.rmempty = true;
604
605 /*
606 advanced email settings tab
607 */
608 o = s.taboption('adv_email', form.DummyValue, '_sub');
609 o.rawhtml = true;
610 o.default = '<em style="color:#37c;font-weight:bold;">' + _('To enable email notifications, set up the \'msmtp\' package and specify a vaild E-Mail receiver address.') + '</em>';
611
612 o = s.taboption('adv_email', form.Flag, 'ban_mailnotification', _('E-Mail Notification'), _('Receive E-Mail notifications with every banIP run.'));
613 o.rmempty = true;
614
615 o = s.taboption('adv_email', form.Value, 'ban_mailreceiver', _('E-Mail Receiver Address'), _('Receiver address for banIP notification E-Mails, this information is required to enable E-Mail functionality.'));
616 o.placeholder = 'name@example.com';
617 o.rmempty = true;
618
619 o = s.taboption('adv_email', form.Value, 'ban_mailsender', _('E-Mail Sender Address'), _('Sender address for banIP notification E-Mails.'));
620 o.placeholder = 'no-reply@banIP';
621 o.rmempty = true;
622
623 o = s.taboption('adv_email', form.Value, 'ban_mailtopic', _('E-Mail Topic'), _('Topic for banIP notification E-Mails.'));
624 o.placeholder = 'banIP notification';
625 o.rmempty = true;
626
627 o = s.taboption('adv_email', form.Value, 'ban_mailprofile', _('E-Mail Profile'), _('Profile used by \'msmtp\' for banIP notification E-Mails.'));
628 o.placeholder = 'ban_notify';
629 o.datatype = 'uciname';
630 o.rmempty = true;
631
632 /*
633 feeds tab
634 */
635 o = s.taboption('feeds', form.DummyValue, '_sub');
636 o.rawhtml = true;
637 o.default = '<em style="color:#37c;font-weight:bold;">' + _('External Blocklist Feeds') + '</em>';
638
639 if (feeds) {
640 o = s.taboption('feeds', form.MultiValue, 'ban_feed', _('Blocklist Feed'));
641 for (let i = 0; i < Object.keys(feeds).length; i++) {
642 feed = Object.keys(feeds)[i].trim();
643 descr = feeds[feed].descr.trim() || '-';
644 o.value(feed, feed + ' (' + descr + ')');
645 }
646 o.optional = true;
647 o.rmempty = true;
648 }
649
650 let ccode, rir, country, countries = [];
651 if (result[2]) {
652 countries = result[2].trim().split('\n');
653
654 o = s.taboption('feeds', form.MultiValue, 'ban_country', _('Countries (RIR)'));
655 for (let i = 0; i < countries.length; i++) {
656 try {
657 ccode = countries[i].match(/^(\w+)\t/)[1].trim();
658 rir = countries[i].match(/^\w+\t(\w+)\t/)[1].trim();
659 country = countries[i].match(/^\w+\t\w+\t(.*$)/)[1].trim();
660 o.value(ccode, country + ' (' + rir + ')');
661 } catch (e) {
662 countries[i] = "";
663 ui.addNotification(null, E('p', _('Unable to parse the countries file: %s').format(e.message)), 'error');
664 }
665 }
666 o.optional = true;
667 o.rmempty = true;
668 }
669
670 o = s.taboption('feeds', form.MultiValue, 'ban_region', _('Regional Internet Registry'));
671 o.value('AFRINIC', _('AFRINIC - serving Africa and the Indian Ocean region'));
672 o.value('APNIC', _('APNIC - serving the Asia Pacific region'));
673 o.value('ARIN', _('ARIN - serving Canada and the United States'));
674 o.value('LACNIC', _('LACNIC - serving the Latin American and Caribbean region'));
675 o.value('RIPE', _('RIPE - serving Europe, Middle East and Central Asia'));
676 o.optional = true;
677 o.rmempty = true;
678
679 o = s.taboption('feeds', form.DynamicList, 'ban_asn', _('ASNs'));
680 o.datatype = 'uinteger';
681 o.optional = true;
682 o.rmempty = true;
683
684 o = s.taboption('feeds', form.DummyValue, '_feeds');
685 o.rawhtml = true;
686 o.default = '<hr style="width: 200px; height: 1px;" /><em style="color:#37c;font-weight:bold;">' + _('External Allowlist Feeds') + '</em>';
687
688 o = s.taboption('feeds', form.DynamicList, 'ban_allowurl', _('Allowlist Feed URLs'));
689 if (countries) {
690 for (let i = 0; i < countries.length; i++) {
691 try {
692 ccode = countries[i].match(/^(\w+)\t/)[1].trim();
693 rir = countries[i].match(/^\w+\t(\w+)\t/)[1].trim();
694 country = countries[i].match(/^\w+\t\w+\t(.*$)/)[1].trim();
695 o.value('https://www.ipdeny.com/ipblocks/data/aggregated/' + ccode + '-aggregated.zone', country + ' IPv4 (' + rir + ')');
696 o.value('https://www.ipdeny.com/ipv6/ipaddresses/aggregated/' + ccode + '-aggregated.zone', country + ' IPv6 (' + rir + ')');
697 } catch (e) {
698 countries[i] = "";
699 }
700 }
701 }
702 o.optional = true;
703 o.rmempty = true;
704 o.validate = function (section_id, value) {
705 if (!value) {
706 return true;
707 }
708 if (!value.match(/^(http:\/\/|https:\/\/)[A-Za-z0-9\/\.\-_\?\&\+=:~#]+$/)) {
709 return _('Protocol/URL format not supported');
710 }
711 return true;
712 }
713
714 o = s.taboption('feeds', form.DummyValue, '_feeds');
715 o.rawhtml = true;
716 o.default = '<hr style="width: 200px; height: 1px;" /><em style="color:#37c;font-weight:bold;">' + _('Local Feed Settings') + '</em>';
717
718 o = s.taboption('feeds', form.Flag, 'ban_autoallowlist', _('Auto Allowlist'), _('Automatically add resolved domains and uplink IPs to the local banIP allowlist.'));
719 o.default = 1
720 o.rmempty = false;
721
722 o = s.taboption('feeds', form.ListValue, 'ban_autoallowuplink', _('Auto Allow Uplink'), _('Limit the uplink autoallow function.'));
723 o.depends('ban_autoallowlist', '1');
724 o.value('disable', _('Disable'));
725 o.value('subnet', _('Subnet (default)'));
726 o.value('ip', _('IP'));
727 o.optional = true;
728 o.rmempty = true;
729
730 o = s.taboption('feeds', form.Flag, 'ban_autoblocklist', _('Auto Blocklist'), _('Automatically add resolved domains and suspicious IPs to the local banIP blocklist.'));
731 o.default = 1
732 o.rmempty = false;
733
734 o = s.taboption('feeds', form.Flag, 'ban_autoblocksubnet', _('Auto Block Subnet'), _('Automatically add entire subnets to the blocklist Set based on an additional RDAP request with the suspicious IP.'));
735 o.default = 0
736 o.optional = true;
737 o.rmempty = true;
738
739 o = s.taboption('feeds', form.ListValue, 'ban_nftexpiry', _('Blocklist Set Expiry'), _('Expiry time for auto added blocklist Set members.'));
740 o.value('10s');
741 o.value('1m');
742 o.value('5m');
743 o.value('1h');
744 o.value('2h');
745 o.value('1d');
746 o.optional = true;
747 o.rmempty = true;
748
749 o = s.taboption('feeds', form.Flag, 'ban_allowlistonly', _('Allowlist Only'), _('Restrict the internet access from/to a small number of secure IPs.'));
750 o.rmempty = false;
751
752 return m.render();
753 },
754 handleReset: null
755 });