summaryrefslogtreecommitdiffstats
path: root/applications/luci-app-banip/htdocs/luci-static/resources/view/banip/blocklist.js
blob: 40d41ba08e195a0a7e3cb96321454f67889211b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict';
'require view';
'require fs';
'require ui';

const localFile = '/etc/banip/banip.blocklist';
const maxSize = 100000;
let notMsg = false;

const resetScroll = () => {
	document.body.scrollTop = document.documentElement.scrollTop = 0;
};

return view.extend({
	load: function () {
		return L.resolveDefault(fs.stat(localFile), null)
			.then(function (stat) {
				if (!stat) {
					return fs.write(localFile, "").then(() => [{ size: 0 }, ""]);
				}
				return Promise.all([
					Promise.resolve(stat),
					L.resolveDefault(fs.read_direct(localFile), "")
				]);
			});
	},

	render: function (blocklist) {
		const size = blocklist[0] ? blocklist[0].size : 0;
		const content = blocklist[1] != null ? blocklist[1] : '';
		const tooBig = size >= maxSize;

		if (tooBig) {
			resetScroll();
			ui.addNotification(null, E('p', _('The blocklist is too big, unable to save modifications.')), 'error');
		}
		return E('div', { 'class': 'cbi-section cbi-section-descr' }, [
			E('p', _('This is the local banIP blocklist that will prevent certain MAC-, IP-addresses or domain names.<br /> \
				<em><b>Please note:</b></em> add only exactly one MAC/IPv4/IPv6 address or domain name per line. Ranges in CIDR notation and MAC/IP-bindings are allowed.')),
			E('textarea', {
				'style': 'width: 100% !important; padding: 5px; font-family: monospace; margin-top: .4em',
				'spellcheck': 'false',
				'wrap': 'off',
				'rows': 25,
				'readonly': tooBig ? 'readonly' : null,
				'input': function () { notMsg = false; }
			}, [content])
		]);
	},

	handleSave: function (_ev) {
		const value = ((document.querySelector('textarea').value || '').trim().toLowerCase().replace(/\r\n?/g, '\n'));
		return fs.write(localFile, value + "\n")
			.then(function () {
				document.querySelector('textarea').value = value + "\n";
				resetScroll();
				if (!notMsg) {
					notMsg = true;
					ui.addNotification(null, E('p', _('Blocklist modifications have been saved, reload banIP that changes take effect.')), 'info');
				}
			}).catch(function (e) {
				resetScroll();
				ui.addNotification(null, E('p', _('Unable to save modifications: %s').format(e.message)), 'error');
			});
	},

	handleSaveApply: null,
	handleReset: null
});