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