Merge pull request #3526 from urbalazs/improve-splash
[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 uci.load('firewall')
112 ]);
113 },
114
115 render: function(data) {
116 if (fwtool.checkLegacySNAT())
117 return fwtool.renderMigration();
118 else
119 return this.renderForwards(data);
120 },
121
122 renderForwards: function(data) {
123 var hosts = data[0],
124 ctHelpers = data[1],
125 devs = data[2],
126 m, s, o;
127
128 m = new form.Map('firewall', _('Firewall - Port Forwards'),
129 _('Port forwarding allows remote computers on the Internet to connect to a specific computer or service within the private LAN.'));
130
131 s = m.section(form.GridSection, 'redirect', _('Port Forwards'));
132 s.addremove = true;
133 s.anonymous = true;
134 s.sortable = true;
135
136 s.tab('general', _('General Settings'));
137 s.tab('advanced', _('Advanced Settings'));
138
139 s.filter = function(section_id) {
140 return (uci.get('firewall', section_id, 'target') != 'SNAT');
141 };
142
143 s.sectiontitle = function(section_id) {
144 return uci.get('firewall', section_id, 'name') || _('Unnamed forward');
145 };
146
147 s.handleAdd = function(ev) {
148 var config_name = this.uciconfig || this.map.config,
149 section_id = uci.add(config_name, this.sectiontype);
150
151 uci.set(config_name, section_id, 'target', 'DNAT');
152
153 this.addedSection = section_id;
154 this.renderMoreOptionsModal(section_id);
155 };
156
157 o = s.taboption('general', form.Value, 'name', _('Name'));
158 o.placeholder = _('Unnamed forward');
159 o.modalonly = true;
160
161 o = s.option(form.DummyValue, '_match', _('Match'));
162 o.modalonly = false;
163 o.textvalue = function(s) {
164 return E('small', [
165 rule_proto_txt(s, ctHelpers), E('br'),
166 rule_src_txt(s, hosts), E('br'),
167 rule_dest_txt(s), E('br'),
168 rule_limit_txt(s)
169 ]);
170 };
171
172 o = s.option(form.ListValue, '_dest', _('Action'));
173 o.modalonly = false;
174 o.textvalue = function(s) {
175 return E('small', [
176 rule_target_txt(s)
177 ]);
178 };
179
180 o = s.option(form.Flag, 'enabled', _('Enable'));
181 o.modalonly = false;
182 o.default = o.enabled;
183 o.editable = true;
184
185 o = s.taboption('general', fwtool.CBIProtocolSelect, 'proto', _('Protocol'));
186 o.modalonly = true;
187 o.default = 'tcp udp';
188
189 o = s.taboption('general', widgets.ZoneSelect, 'src', _('Source zone'));
190 o.modalonly = true;
191 o.rmempty = false;
192 o.nocreate = true;
193 o.default = 'wan';
194
195 o = fwtool.addMACOption(s, 'advanced', 'src_mac', _('Source MAC address'),
196 _('Only match incoming traffic from these MACs.'), hosts);
197 o.rmempty = true;
198 o.datatype = 'list(neg(macaddr))';
199
200 o = fwtool.addIPOption(s, 'advanced', 'src_ip', _('Source IP address'),
201 _('Only match incoming traffic from this IP or range.'), 'ipv4', hosts);
202 o.rmempty = true;
203 o.datatype = 'neg(ipmask4)';
204
205 o = s.taboption('advanced', form.Value, 'src_port', _('Source port'),
206 _('Only match incoming traffic originating from the given source port or port range on the client host'));
207 o.modalonly = true;
208 o.rmempty = true;
209 o.datatype = 'neg(portrange)';
210 o.placeholder = _('any');
211 o.depends({ proto: 'tcp', '!contains': true });
212 o.depends({ proto: 'udp', '!contains': true });
213
214 o = fwtool.addLocalIPOption(s, 'advanced', 'src_dip', _('External IP address'),
215 _('Only match incoming traffic directed at the given IP address.'), devs);
216 o.datatype = 'neg(ipmask4)';
217 o.rmempty = true;
218
219 o = s.taboption('general', form.Value, 'src_dport', _('External port'),
220 _('Match incoming traffic directed at the given destination port or port range on this host'));
221 o.modalonly = true;
222 o.rmempty = false;
223 o.datatype = 'neg(portrange)';
224 o.depends({ proto: 'tcp', '!contains': true });
225 o.depends({ proto: 'udp', '!contains': true });
226
227 o = s.taboption('general', widgets.ZoneSelect, 'dest', _('Internal zone'));
228 o.modalonly = true;
229 o.rmempty = true;
230 o.nocreate = true;
231 o.default = 'lan';
232
233 o = fwtool.addIPOption(s, 'general', 'dest_ip', _('Internal IP address'),
234 _('Redirect matched incoming traffic to the specified internal host'), 'ipv4', hosts);
235 o.rmempty = true;
236 o.datatype = 'ipmask4';
237
238 o = s.taboption('general', form.Value, 'dest_port', _('Internal port'),
239 _('Redirect matched incoming traffic to the given port on the internal host'));
240 o.modalonly = true;
241 o.rmempty = true;
242 o.placeholder = _('any');
243 o.datatype = 'portrange';
244 o.depends({ proto: 'tcp', '!contains': true });
245 o.depends({ proto: 'udp', '!contains': true });
246
247 o = s.taboption('advanced', form.Flag, 'reflection', _('Enable NAT Loopback'));
248 o.modalonly = true;
249 o.rmempty = true;
250 o.default = o.enabled;
251
252 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.'));
253 o.modalonly = true;
254 o.depends('reflection', '1');
255 o.value('internal', _('Use internal IP address'));
256 o.value('external', _('Use external IP address'));
257 o.write = function(section_id, value) {
258 uci.set('firewall', section_id, 'reflection_src', (value != 'internal') ? value : null);
259 };
260
261 o = s.taboption('advanced', form.Value, 'helper', _('Match helper'), _('Match traffic using the specified connection tracking helper.'));
262 o.modalonly = true;
263 o.placeholder = _('any');
264 for (var i = 0; i < ctHelpers.length; i++)
265 o.value(ctHelpers[i].name, '%s (%s)'.format(ctHelpers[i].description, ctHelpers[i].name.toUpperCase()));
266 o.validate = function(section_id, value) {
267 if (value == '' || value == null)
268 return true;
269
270 value = value.replace(/^!\s*/, '');
271
272 for (var i = 0; i < ctHelpers.length; i++)
273 if (value == ctHelpers[i].name)
274 return true;
275
276 return _('Unknown or not installed conntrack helper "%s"').format(value);
277 };
278
279 fwtool.addMarkOption(s, false);
280 fwtool.addLimitOption(s);
281 fwtool.addLimitBurstOption(s);
282
283 o = s.taboption('advanced', form.Value, 'extra', _('Extra arguments'),
284 _('Passes additional arguments to iptables. Use with care!'));
285 o.modalonly = true;
286 o.rmempty = true;
287
288 return m.render();
289 }
290 });