luci-app-firewall: rework rule descriptions, deduplicate code
[project/luci.git] / applications / luci-app-firewall / htdocs / luci-static / resources / view / firewall / snats.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) {
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 };
19 });
20
21 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);
22 var f = m ? {
23 val: m[0].toUpperCase().replace(/X/g, 'x'),
24 inv: m[1],
25 num: '0x%02X'.format(+m[2]),
26 mask: m[3] ? '0x%02X'.format(+m[3]) : null
27 } : null;
28
29 return fwtool.fmt(_('Forwarded IPv4%{proto?, protocol %{proto#%{next?, }<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>}'), {
30 proto: proto,
31 mark: f
32 });
33 }
34
35 function rule_src_txt(s, hosts) {
36 var z = uci.get('firewall', s, 'src');
37
38 return fwtool.fmt(_('From %{src}%{src_device?, interface <var>%{src_device}</var>}%{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>}}'), {
39 src: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel.getColorForName(null) }, [E('em', _('any zone'))]),
40 src_ip: fwtool.map_invert(uci.get('firewall', s, 'src_ip'), 'toLowerCase'),
41 src_port: fwtool.map_invert(uci.get('firewall', s, 'src_port'))
42 });
43 }
44
45 function rule_dest_txt(s) {
46 var z = uci.get('firewall', s, 'src');
47
48 return fwtool.fmt(_('To %{dest}%{dest_device?, via interface <var>%{dest_device}</var>}%{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>}}'), {
49 dest: E('span', { 'class': 'zonebadge', 'style': 'background-color:' + fwmodel.getColorForName((z && z != '*') ? z : null) }, [(z == '*') ? E('em', _('any zone')) : (z || E('em', _('this device')))]),
50 dest_ip: fwtool.map_invert(uci.get('firewall', s, 'dest_ip'), 'toLowerCase'),
51 dest_port: fwtool.map_invert(uci.get('firewall', s, 'dest_port')),
52 dest_device: uci.get('firewall', s, 'device')
53 });
54 }
55
56 function rule_limit_txt(s) {
57 var m = String(uci.get('firewall', s, 'limit')).match(/^(\d+)\/([smhd])\w*$/i),
58 l = m ? {
59 num: +m[1],
60 unit: ({ s: _('second'), m: _('minute'), h: _('hour'), d: _('day') })[m[2]],
61 burst: uci.get('firewall', s, 'limit_burst')
62 } : null;
63
64 if (!l)
65 return '';
66
67 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 });
68 }
69
70 function rule_target_txt(s) {
71 var t = uci.get('firewall', s, 'target'),
72 s = {
73 target: t,
74 snat_ip: uci.get('firewall', s, 'snat_ip'),
75 snat_port: uci.get('firewall', s, 'snat_port')
76 };
77
78 switch (t) {
79 case 'SNAT':
80 return fwtool.fmt(_('<var data-tooltip="SNAT">Statically rewrite</var> to source %{snat_ip?IP <var>%{snat_ip}</var>} %{snat_port?port <var>%{snat_port}</var>}'), s);
81
82 case 'MASQUERADE':
83 return fwtool.fmt(_('<var data-tooltip="MASQUERADE">Automatically rewrite</var> source IP'));
84
85 case 'ACCEPT':
86 return fwtool.fmt(_('<var data-tooltip="ACCEPT">Prevent source rewrite</var>'));
87
88 default:
89 return t;
90 }
91 }
92
93 return L.view.extend({
94 callHostHints: rpc.declare({
95 object: 'luci-rpc',
96 method: 'getHostHints',
97 expect: { '': {} }
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.callNetworkDevices()
110 ]);
111 },
112
113 render: function(data) {
114 var hosts = data[0],
115 devs = data[1],
116 m, s, o;
117
118 m = new form.Map('firewall', _('Firewall - NAT Rules'),
119 _('NAT rules allow fine grained control over the source IP to use for outbound or forwarded traffic.'));
120
121 s = m.section(form.GridSection, 'nat', _('NAT Rules'));
122 s.addremove = true;
123 s.anonymous = true;
124 s.sortable = true;
125
126 s.tab('general', _('General Settings'));
127 s.tab('advanced', _('Advanced Settings'));
128 s.tab('timed', _('Time Restrictions'));
129
130 s.sectiontitle = function(section_id) {
131 return uci.get('firewall', section_id, 'name') || _('Unnamed NAT');
132 };
133
134 o = s.taboption('general', form.Value, 'name', _('Name'));
135 o.placeholder = _('Unnamed NAT');
136 o.modalonly = true;
137
138 o = s.option(form.DummyValue, '_match', _('Match'));
139 o.modalonly = false;
140 o.textvalue = function(s) {
141 return E('small', [
142 rule_proto_txt(s), E('br'),
143 rule_src_txt(s, hosts), E('br'),
144 rule_dest_txt(s), E('br'),
145 rule_limit_txt(s)
146 ]);
147 };
148
149 o = s.option(form.ListValue, '_target', _('Action'));
150 o.modalonly = false;
151 o.textvalue = function(s) {
152 return rule_target_txt(s);
153 };
154
155 o = s.option(form.Flag, 'enabled', _('Enable'));
156 o.modalonly = false;
157 o.default = o.enabled;
158 o.editable = true;
159
160 o = s.taboption('general', fwtool.CBIProtocolSelect, 'proto', _('Protocol'));
161 o.modalonly = true;
162 o.default = 'all';
163
164 o = s.taboption('general', widgets.ZoneSelect, 'src', _('Outbound zone'));
165 o.modalonly = true;
166 o.rmempty = false;
167 o.nocreate = true;
168 o.allowany = true;
169 o.default = 'lan';
170
171 o = fwtool.addIPOption(s, 'general', 'src_ip', _('Source address'),
172 _('Match forwarded traffic from this IP or range.'), 'ipv4', hosts);
173 o.rmempty = true;
174 o.datatype = 'neg(ipmask4)';
175
176 o = s.taboption('general', form.Value, 'src_port', _('Source port'),
177 _('Match forwarded traffic originating from the given source port or port range.'));
178 o.modalonly = true;
179 o.rmempty = true;
180 o.datatype = 'neg(portrange)';
181 o.placeholder = _('any');
182 o.depends({ proto: 'tcp', '!contains': true });
183 o.depends({ proto: 'udp', '!contains': true });
184
185 o = fwtool.addIPOption(s, 'general', 'dest_ip', _('Destination address'),
186 _('Match forwarded traffic directed at the given IP address.'), 'ipv4', hosts);
187 o.rmempty = true;
188 o.datatype = 'neg(ipmask4)';
189
190 o = s.taboption('general', form.Value, 'dest_port', _('Destination port'),
191 _('Match forwarded traffic directed at the given destination port or port range.'));
192 o.modalonly = true;
193 o.rmempty = true;
194 o.placeholder = _('any');
195 o.datatype = 'neg(portrange)';
196 o.depends({ proto: 'tcp', '!contains': true });
197 o.depends({ proto: 'udp', '!contains': true });
198
199 o = s.taboption('general', form.ListValue, 'target', _('Action'));
200 o.modalonly = true;
201 o.default = 'SNAT';
202 o.value('SNAT', _('SNAT - Rewrite to specific source IP or port'));
203 o.value('MASQUERADE', _('MASQUERADE - Automatically rewrite to outbound interface IP'));
204 o.value('ACCEPT', _('ACCEPT - Disable address rewriting'));
205
206 o = fwtool.addLocalIPOption(s, 'general', 'snat_ip', _('Rewrite IP address'),
207 _('Rewrite matched traffic to the specified source IP address.'), devs);
208 o.placeholder = null;
209 o.depends('target', 'SNAT');
210 o.validate = function(section_id, value) {
211 var port = this.map.lookupOption('snat_port', section_id),
212 a = this.formvalue(section_id),
213 p = port ? port[0].formvalue(section_id) : null;
214
215 if ((a == null || a == '') && (p == null || p == ''))
216 return _('A rewrite IP must be specified!');
217
218 return true;
219 };
220
221 o = s.taboption('general', form.Value, 'snat_port', _('Rewrite port'),
222 _('Rewrite matched traffic to the specified source port or port range.'));
223 o.modalonly = true;
224 o.rmempty = true;
225 o.placeholder = _('do not rewrite');
226 o.datatype = 'portrange';
227 o.depends({ proto: 'tcp', '!contains': true });
228 o.depends({ proto: 'udp', '!contains': true });
229
230 o = s.taboption('advanced', widgets.DeviceSelect, 'device', _('Outbound device'),
231 _('Matches forwarded traffic using the specified outbound network device.'));
232 o.noaliases = true;
233 o.modalonly = true;
234 o.rmempty = true;
235
236 fwtool.addMarkOption(s, false);
237 fwtool.addLimitOption(s);
238 fwtool.addLimitBurstOption(s);
239
240 o = s.taboption('advanced', form.Value, 'extra', _('Extra arguments'),
241 _('Passes additional arguments to iptables. Use with care!'));
242 o.modalonly = true;
243 o.rmempty = true;
244
245 o = s.taboption('timed', form.MultiValue, 'weekdays', _('Week Days'));
246 o.modalonly = true;
247 o.multiple = true;
248 o.display = 5;
249 o.placeholder = _('Any day');
250 o.value('Sun', _('Sunday'));
251 o.value('Mon', _('Monday'));
252 o.value('Tue', _('Tuesday'));
253 o.value('Wed', _('Wednesday'));
254 o.value('Thu', _('Thursday'));
255 o.value('Fri', _('Friday'));
256 o.value('Sat', _('Saturday'));
257 o.write = function(section_id, value) {
258 return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
259 };
260
261 o = s.taboption('timed', form.MultiValue, 'monthdays', _('Month Days'));
262 o.modalonly = true;
263 o.multiple = true;
264 o.display_size = 15;
265 o.placeholder = _('Any day');
266 o.write = function(section_id, value) {
267 return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
268 };
269 for (var i = 1; i <= 31; i++)
270 o.value(i);
271
272 o = s.taboption('timed', form.Value, 'start_time', _('Start Time (hh.mm.ss)'));
273 o.modalonly = true;
274 o.datatype = 'timehhmmss';
275
276 o = s.taboption('timed', form.Value, 'stop_time', _('Stop Time (hh.mm.ss)'));
277 o.modalonly = true;
278 o.datatype = 'timehhmmss';
279
280 o = s.taboption('timed', form.Value, 'start_date', _('Start Date (yyyy-mm-dd)'));
281 o.modalonly = true;
282 o.datatype = 'dateyyyymmdd';
283
284 o = s.taboption('timed', form.Value, 'stop_date', _('Stop Date (yyyy-mm-dd)'));
285 o.modalonly = true;
286 o.datatype = 'dateyyyymmdd';
287
288 o = s.taboption('timed', form.Flag, 'utc_time', _('Time in UTC'));
289 o.modalonly = true;
290 o.default = o.disabled;
291
292 return m.render();
293 }
294 });