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