luci-app-firewall: rework rule descriptions, deduplicate code
[project/luci.git] / applications / luci-app-firewall / htdocs / luci-static / resources / view / firewall / forwards.js
1 'use strict';
2 'require ui';
3 'require rpc';
4 'require uci';
5 'require form';
6 'require firewall as fwmodel';
7 'require tools.firewall as fwtool';
8 'require tools.widgets as widgets';
9
10 function rule_proto_txt(s, ctHelpers) {
11 var proto = L.toArray(uci.get('firewall', s, 'proto')).filter(function(p) {
12 return (p != '*' && p != 'any' && p != 'all');
13 }).map(function(p) {
14 var pr = fwtool.lookupProto(p);
15 return {
16 num: pr[0],
17 name: pr[1],
18 types: (pr[0] == 1 || pr[0] == 58) ? L.toArray(uci.get('firewall', s, 'icmp_type')) : null
19 };
20 });
21
22 m = String(uci.get('firewall', s, 'helper') || '').match(/^(!\s*)?(\S+)$/);
23 var h = m ? {
24 val: m[0].toUpperCase(),
25 inv: m[1],
26 name: (ctHelpers.filter(function(ctH) { return ctH.name.toLowerCase() == m[2].toLowerCase() })[0] || {}).description
27 } : null;
28
29 m = String(uci.get('firewall', s, 'mark')).match(/^(!\s*)?(0x[0-9a-f]{1,8}|[0-9]{1,10})(?:\/(0x[0-9a-f]{1,8}|[0-9]{1,10}))?$/i);
30 var f = m ? {
31 val: m[0].toUpperCase().replace(/X/g, 'x'),
32 inv: m[1],
33 num: '0x%02X'.format(+m[2]),
34 mask: m[3] ? '0x%02X'.format(+m[3]) : null
35 } : null;
36
37 return fwtool.fmt(_('Incoming IPv4%{proto?, protocol %{proto#%{next?, }%{item.types?<var class="cbi-tooltip-container">%{item.name}<span class="cbi-tooltip">ICMP with types %{item.types#%{next?, }<var>%{item}</var>}</span></var>:<var>%{item.name}</var>}}}%{mark?, mark <var%{mark.inv? data-tooltip="Match fwmarks except %{mark.num}%{mark.mask? with mask %{mark.mask}}.":%{mark.mask? data-tooltip="Mask fwmark value with %{mark.mask} before compare."}}>%{mark.val}</var>}%{helper?, helper %{helper.inv?<var data-tooltip="Match any helper except &quot;%{helper.name}&quot;">%{helper.val}</var>:<var data-tooltip="%{helper.name}">%{helper.val}</var>}}'), {
38 proto: proto,
39 helper: h,
40 mark: f
41 });
42 }
43
44 function rule_src_txt(s, hosts) {
45 var z = uci.get('firewall', s, 'src');
46
47 return fwtool.fmt(_('From %{src}%{src_ip?, IP %{src_ip#%{next?, }<var%{item.inv? data-tooltip="Match IP addresses except %{item.val}."}>%{item.ival}</var>}}%{src_port?, port %{src_port#%{next?, }<var%{item.inv? data-tooltip="Match ports except %{item.val}."}>%{item.ival}</var>}}%{src_mac?, MAC %{src_mac#%{next?, }<var%{item.inv? data-tooltip="Match MACs except %{item.val}%{item.hint.name? a.k.a. %{item.hint.name}}.":%{item.hint.name? data-tooltip="%{item.hint.name}"}}>%{item.ival}</var>}}'), {
48 src: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel.getColorForName((z && z != '*') ? z : null) }, [(z == '*') ? E('em', _('any zone')) : (z || E('em', _('this device')))]),
49 src_ip: fwtool.map_invert(uci.get('firewall', s, 'src_ip'), 'toLowerCase'),
50 src_mac: fwtool.map_invert(uci.get('firewall', s, 'src_mac'), 'toUpperCase').map(function(v) { return Object.assign(v, { hint: hosts[v.val] }) }),
51 src_port: fwtool.map_invert(uci.get('firewall', s, 'src_port'))
52 });
53 }
54
55 function rule_dest_txt(s) {
56 return fwtool.fmt(_('To %{dest}%{dest_ip?, IP %{dest_ip#%{next?, }<var%{item.inv? data-tooltip="Match IP addresses except %{item.val}."}>%{item.ival}</var>}}%{dest_port?, port %{dest_port#%{next?, }<var%{item.inv? data-tooltip="Match ports except %{item.val}."}>%{item.ival}</var>}}'), {
57 dest: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel.getColorForName(null) }, [E('em', _('this device'))]),
58 dest_ip: fwtool.map_invert(uci.get('firewall', s, 'src_dip'), 'toLowerCase'),
59 dest_port: fwtool.map_invert(uci.get('firewall', s, 'src_dport'))
60 });
61 }
62
63 function rule_limit_txt(s) {
64 var m = String(uci.get('firewall', s, 'limit')).match(/^(\d+)\/([smhd])\w*$/i),
65 l = m ? {
66 num: +m[1],
67 unit: ({ s: _('second'), m: _('minute'), h: _('hour'), d: _('day') })[m[2]],
68 burst: uci.get('firewall', s, 'limit_burst')
69 } : null;
70
71 if (!l)
72 return '';
73
74 return fwtool.fmt(_('Limit matching to <var>%{limit.num}</var> packets per <var>%{limit.unit}</var>%{limit.burst? burst <var>%{limit.burst}</var>}'), { limit: l });
75 }
76
77 function rule_target_txt(s) {
78 var z = uci.get('firewall', s, 'dest');
79
80 return fwtool.fmt(_('<var data-tooltip="DNAT">Forward</var> to %{dest}%{dest_ip? IP <var>%{dest_ip}</var>}%{dest_port? port <var>%{dest_port}</var>}'), {
81 dest: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel.getColorForName((z && z != '*') ? z : null) }, [(z == '*') ? E('em', _('any zone')) : (z || E('em', _('this device')))]),
82 dest_ip: (uci.get('firewall', s, 'dest_ip') || '').toLowerCase(),
83 dest_port: uci.get('firewall', s, 'dest_port')
84 });
85 }
86
87 return L.view.extend({
88 callHostHints: rpc.declare({
89 object: 'luci-rpc',
90 method: 'getHostHints',
91 expect: { '': {} }
92 }),
93
94 callConntrackHelpers: rpc.declare({
95 object: 'luci',
96 method: 'getConntrackHelpers',
97 expect: { result: [] }
98 }),
99
100 callNetworkDevices: rpc.declare({
101 object: 'luci-rpc',
102 method: 'getNetworkDevices',
103 expect: { '': {} }
104 }),
105
106 load: function() {
107 return Promise.all([
108 this.callHostHints(),
109 this.callConntrackHelpers(),
110 this.callNetworkDevices()
111 ]);
112 },
113
114 render: function(data) {
115 var hosts = data[0],
116 ctHelpers = data[1],
117 devs = data[2],
118 m, s, o;
119
120 m = new form.Map('firewall', _('Firewall - Port Forwards'),
121 _('Port forwarding allows remote computers on the Internet to connect to a specific computer or service within the private LAN.'));
122
123 s = m.section(form.GridSection, 'redirect', _('Port Forwards'));
124 s.addremove = true;
125 s.anonymous = true;
126 s.sortable = true;
127
128 s.tab('general', _('General Settings'));
129 s.tab('advanced', _('Advanced Settings'));
130
131 s.filter = function(section_id) {
132 return (uci.get('firewall', section_id, 'target') != 'SNAT');
133 };
134
135 s.sectiontitle = function(section_id) {
136 return uci.get('firewall', section_id, 'name') || _('Unnamed forward');
137 };
138
139 s.handleAdd = function(ev) {
140 var config_name = this.uciconfig || this.map.config,
141 section_id = uci.add(config_name, this.sectiontype);
142
143 uci.set(config_name, section_id, 'target', 'DNAT');
144
145 this.addedSection = section_id;
146 this.renderMoreOptionsModal(section_id);
147 };
148
149 o = s.taboption('general', form.Value, 'name', _('Name'));
150 o.placeholder = _('Unnamed forward');
151 o.modalonly = true;
152
153 o = s.option(form.DummyValue, '_match', _('Match'));
154 o.modalonly = false;
155 o.textvalue = function(s) {
156 return E('small', [
157 rule_proto_txt(s, ctHelpers), E('br'),
158 rule_src_txt(s, hosts), E('br'),
159 rule_dest_txt(s), E('br'),
160 rule_limit_txt(s)
161 ]);
162 };
163
164 o = s.option(form.ListValue, '_dest', _('Action'));
165 o.modalonly = false;
166 o.textvalue = function(s) {
167 return E('small', [
168 rule_target_txt(s)
169 ]);
170 };
171
172 o = s.option(form.Flag, 'enabled', _('Enable'));
173 o.modalonly = false;
174 o.default = o.enabled;
175 o.editable = true;
176
177 o = s.taboption('general', fwtool.CBIProtocolSelect, 'proto', _('Protocol'));
178 o.modalonly = true;
179 o.default = 'tcp udp';
180
181 o = s.taboption('general', widgets.ZoneSelect, 'src', _('Source zone'));
182 o.modalonly = true;
183 o.rmempty = false;
184 o.nocreate = true;
185 o.default = 'wan';
186
187 o = fwtool.addMACOption(s, 'advanced', 'src_mac', _('Source MAC address'),
188 _('Only match incoming traffic from these MACs.'), hosts);
189 o.rmempty = true;
190 o.datatype = 'list(neg(macaddr))';
191
192 o = fwtool.addIPOption(s, 'advanced', 'src_ip', _('Source IP address'),
193 _('Only match incoming traffic from this IP or range.'), 'ipv4', hosts);
194 o.rmempty = true;
195 o.datatype = 'neg(ipmask4)';
196
197 o = s.taboption('advanced', form.Value, 'src_port', _('Source port'),
198 _('Only match incoming traffic originating from the given source port or port range on the client host'));
199 o.modalonly = true;
200 o.rmempty = true;
201 o.datatype = 'neg(portrange)';
202 o.placeholder = _('any');
203 o.depends({ proto: 'tcp', '!contains': true });
204 o.depends({ proto: 'udp', '!contains': true });
205
206 o = fwtool.addLocalIPOption(s, 'advanced', 'src_dip', _('External IP address'),
207 _('Only match incoming traffic directed at the given IP address.'), devs);
208 o.datatype = 'neg(ipmask4)';
209 o.rmempty = true;
210
211 o = s.taboption('general', form.Value, 'src_dport', _('External port'),
212 _('Match incoming traffic directed at the given destination port or port range on this host'));
213 o.modalonly = true;
214 o.rmempty = false;
215 o.datatype = 'neg(portrange)';
216 o.depends({ proto: 'tcp', '!contains': true });
217 o.depends({ proto: 'udp', '!contains': true });
218
219 o = s.taboption('general', widgets.ZoneSelect, 'dest', _('Internal zone'));
220 o.modalonly = true;
221 o.rmempty = true;
222 o.nocreate = true;
223 o.default = 'lan';
224
225 o = fwtool.addIPOption(s, 'general', 'dest_ip', _('Internal IP address'),
226 _('Redirect matched incoming traffic to the specified internal host'), 'ipv4', hosts);
227 o.rmempty = true;
228 o.datatype = 'ipmask4';
229
230 o = s.taboption('general', form.Value, 'dest_port', _('Internal port'),
231 _('Redirect matched incoming traffic to the given port on the internal host'));
232 o.modalonly = true;
233 o.rmempty = true;
234 o.placeholder = _('any');
235 o.datatype = 'portrange';
236 o.depends({ proto: 'tcp', '!contains': true });
237 o.depends({ proto: 'udp', '!contains': true });
238
239 o = s.taboption('advanced', form.Flag, 'reflection', _('Enable NAT Loopback'));
240 o.modalonly = true;
241 o.rmempty = true;
242 o.default = o.enabled;
243
244 o = s.taboption('advanced', form.ListValue, 'reflection_src', _('Loopback source IP'), _('Specifies whether to use the external or the internal IP address for reflected traffic.'));
245 o.modalonly = true;
246 o.depends('reflection', '1');
247 o.value('internal', _('Use internal IP address'));
248 o.value('external', _('Use external IP address'));
249 o.write = function(section_id, value) {
250 uci.set('firewall', section_id, 'reflection_src', (value != 'internal') ? value : null);
251 };
252
253 o = s.taboption('advanced', form.Value, 'helper', _('Match helper'), _('Match traffic using the specified connection tracking helper.'));
254 o.modalonly = true;
255 o.placeholder = _('any');
256 for (var i = 0; i < ctHelpers.length; i++)
257 o.value(ctHelpers[i].name, '%s (%s)'.format(ctHelpers[i].description, ctHelpers[i].name.toUpperCase()));
258 o.validate = function(section_id, value) {
259 if (value == '' || value == null)
260 return true;
261
262 value = value.replace(/^!\s*/, '');
263
264 for (var i = 0; i < ctHelpers.length; i++)
265 if (value == ctHelpers[i].name)
266 return true;
267
268 return _('Unknown or not installed conntrack helper "%s"').format(value);
269 };
270
271 fwtool.addMarkOption(s, false);
272 fwtool.addLimitOption(s);
273 fwtool.addLimitBurstOption(s);
274
275 o = s.taboption('advanced', form.Value, 'extra', _('Extra arguments'),
276 _('Passes additional arguments to iptables. Use with care!'));
277 o.modalonly = true;
278 o.rmempty = true;
279
280 return m.render();
281 }
282 });