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