summaryrefslogtreecommitdiffstats
path: root/applications/luci-app-statistics/htdocs/luci-static/resources/view/statistics/plugins/iptables.js
blob: 81616bfb38c030ef5aef6cb849779110a0c5831e (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
'require baseclass';
'require fs';
'require uci';
'require form';

return baseclass.extend({
	title: _('Iptables Plugin Configuration'),
	description: _('The iptables plugin will monitor selected firewall rules and collect information about processed bytes and packets per rule.'),

	addFormOptions(s) {
		let o, ss;

		o = s.option(form.Flag, 'enable', _('Enable this plugin'));

		for (let family = 4; family <= 6; family += 2) {
			const suffix = (family == 4 ? '' : '6');

			o = s.option(form.SectionValue, '__match' + suffix, form.TableSection, 'collectd_iptables_match' + suffix,
				suffix ? _('Match IPv6 iptables rules') : _('Match IPv4 iptables rules'),
				_('Here you can define various criteria by which the monitored iptables rules are selected.'));

			o.depends('enable', '1');
			o.load = L.bind(function(suffix, section_id) {
				return L.resolveDefault(fs.exec_direct('/usr/sbin/ip' + suffix + 'tables-save', []), '').then(L.bind(function(res) {
					const lines = res.split(/\n/);
					let table, count;
					const iptables = {};

					for (let line of lines) {
						let m;

						if ((m = line.match(/^\*(\S+)$/)) != null) {
							table = m[1];
							count = {};
						}
						else if ((m = line.match(/^-A (.+?) ([!-].+)$/)) != null) {
							count[m[1]] = (count[m[1]] || 0) + 1;

							iptables[table] = iptables[table] || {};
							iptables[table][m[1]] = iptables[table][m[1]] || {};
							iptables[table][m[1]][count[m[1]]] = E('span', {
								'style': 'overflow:hidden; text-overflow:ellipsis; max-width:200px',
								'data-tooltip': m[2]
							}, [
								'#%d: '.format(count[m[1]]),
								m[2].replace(/-m comment --comment "(.+?)" /, '')
							]);

							/*
							 * collectd currently does not support comments with spaces:
							 * https://github.com/collectd/collectd/issues/2766
							 */
							const c = m[2].match(/-m comment --comment "(.+)" -/);
							if (c && c[1] != '!fw3' && !c[1].match(/[ \t\n]/))
								iptables[table][m[1]][c[1]] = E('span', {}, [ c[1] ]);
						}
					}

					this.subsection.iptables = iptables;

					return form.SectionValue.prototype.load.apply(this, [section_id]);
				}, this));
			}, o, suffix);

			ss = o.subsection;
			ss.anonymous = true;
			ss.addremove = true;
			ss.addbtntitle = suffix ? _('Add IPv6 rule selector') : _('Add IPv4 rule selector');

			o = ss.option(form.Value, 'name', _('Instance name'));
			o.datatype = 'maxlength(63)';
			o.validate = function(section_id, v) {
				const table_opt = this.section.children.filter(function(o) { return o.option == 'table' })[0];
				const table_elem = table_opt.getUIElement(section_id);

				table_elem.clearChoices();
				table_elem.addChoices(Object.keys(this.section.iptables).sort());

				if (v != '' && v.match(/[ \t\n]/))
					return _('The instance name must not contain spaces');

				return true;
			};

			o = ss.option(form.Value, 'table', _('Table'));
			o.default = 'filter';
			o.optional = true;
			o.transformChoices = function() { return this.super('transformChoices', []) || {} };
			o.validate = function(section_id, table) {
				const chain_opt = this.section.children.filter(function(o) { return o.option == 'chain' })[0];
				const chain_elem = chain_opt.getUIElement(section_id);

				chain_elem.clearChoices();
				chain_elem.addChoices(Object.keys(this.section.iptables[table]).sort());

				return true;
			};

			o = ss.option(form.Value, 'chain', _('Chain'));
			o.optional = true;
			o.transformChoices = function() { return this.super('transformChoices', []) || {} };
			o.validate = function(section_id, chain) {
				const table_opt = this.section.children.filter(function(o) { return o.option == 'table' })[0];
				const rule_opt = this.section.children.filter(function(o) { return o.option == 'rule' })[0];
				const rule_elem = rule_opt.getUIElement(section_id);
				const table = table_opt.formvalue(section_id);

				rule_elem.clearChoices();

				if (this.section.iptables[table][chain]) {
					const keys = Object.keys(this.section.iptables[table][chain]).sort(function(a, b) {
						const x = a.match(/^(\d+)/);
						const y = b.match(/^(\d+)/);

						if (x && y)
							return +x[1] > +y[1];
						else if (x || y)
							return +!!x > +!!y;
						else
							return a > b;
					});

					const labels = {};

					for (let key of keys)
						labels[key] = this.section.iptables[table][chain][key].cloneNode(true);

					rule_elem.addChoices(keys, labels);
				}

				if (chain != '' && chain.match(/[ \t\n]/))
					return _('The chain name must not contain spaces');

				return true;
			};

			o = ss.option(form.Value, 'rule', _('Comment / Rule Number'));
			o.optional = true;
			o.transformChoices = function() { return this.super('transformChoices', []) || {} };
			o.load = function(section_id) {
				const table = uci.get('luci_statistics', section_id, 'table');
				const chain = uci.get('luci_statistics', section_id, 'chain');
				const rule = uci.get('luci_statistics', section_id, 'rule');
				const ipt = this.section.iptables;

				if (ipt[table] && ipt[table][chain] && ipt[table][chain][rule])
					this.value(rule, ipt[table][chain][rule].cloneNode(true));

				return rule;
			};
			o.validate = function(section_id, rule) {
				if (rule != '' && rule.match(/[ \t\n]/))
					return _('The comment to match must not contain spaces');

				return true;
			};
		}
	},

	configSummary(section) {
		return _('Rule monitoring enabled');
	}
});