Merge pull request #5400 from systemcrash/wg_pubkey
[project/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': fwmodel.getZoneColorStyle(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': fwmodel.getZoneColorStyle(z) }, [(z == '*') ? E('em', _('any zone')) : (z ? E('strong', 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("true"))';
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("true"))';
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 a = this.formvalue(section_id),
221 p = this.section.formvalue(section_id, 'snat_port');
222
223 if ((a == null || a == '') && (p == null || p == '') && value == '')
224 return _('A rewrite IP must be specified!');
225
226 return true;
227 };
228
229 o = s.taboption('general', form.Value, 'snat_port', _('Rewrite port'),
230 _('Rewrite matched traffic to the specified source port or port range.'));
231 o.modalonly = true;
232 o.rmempty = true;
233 o.placeholder = _('do not rewrite');
234 o.datatype = 'portrange';
235 o.depends({ proto: 'tcp', '!contains': true });
236 o.depends({ proto: 'udp', '!contains': true });
237
238 o = s.taboption('advanced', widgets.DeviceSelect, 'device', _('Outbound device'),
239 _('Matches forwarded traffic using the specified outbound network device.'));
240 o.noaliases = true;
241 o.modalonly = true;
242 o.rmempty = true;
243
244 fwtool.addMarkOption(s, false);
245 fwtool.addLimitOption(s);
246 fwtool.addLimitBurstOption(s);
247
248 o = s.taboption('advanced', form.Value, 'extra', _('Extra arguments'),
249 _('Passes additional arguments to iptables. Use with care!'));
250 o.modalonly = true;
251 o.rmempty = true;
252
253 o = s.taboption('timed', form.MultiValue, 'weekdays', _('Week Days'));
254 o.modalonly = true;
255 o.multiple = true;
256 o.display = 5;
257 o.placeholder = _('Any day');
258 o.value('Sun', _('Sunday'));
259 o.value('Mon', _('Monday'));
260 o.value('Tue', _('Tuesday'));
261 o.value('Wed', _('Wednesday'));
262 o.value('Thu', _('Thursday'));
263 o.value('Fri', _('Friday'));
264 o.value('Sat', _('Saturday'));
265 o.write = function(section_id, value) {
266 return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
267 };
268
269 o = s.taboption('timed', form.MultiValue, 'monthdays', _('Month Days'));
270 o.modalonly = true;
271 o.multiple = true;
272 o.display_size = 15;
273 o.placeholder = _('Any day');
274 o.write = function(section_id, value) {
275 return this.super('write', [ section_id, L.toArray(value).join(' ') ]);
276 };
277 for (var i = 1; i <= 31; i++)
278 o.value(i);
279
280 o = s.taboption('timed', form.Value, 'start_time', _('Start Time (hh:mm:ss)'));
281 o.modalonly = true;
282 o.datatype = 'timehhmmss';
283
284 o = s.taboption('timed', form.Value, 'stop_time', _('Stop Time (hh:mm:ss)'));
285 o.modalonly = true;
286 o.datatype = 'timehhmmss';
287
288 o = s.taboption('timed', form.Value, 'start_date', _('Start Date (yyyy-mm-dd)'));
289 o.modalonly = true;
290 o.datatype = 'dateyyyymmdd';
291
292 o = s.taboption('timed', form.Value, 'stop_date', _('Stop Date (yyyy-mm-dd)'));
293 o.modalonly = true;
294 o.datatype = 'dateyyyymmdd';
295
296 o = s.taboption('timed', form.Flag, 'utc_time', _('Time in UTC'));
297 o.modalonly = true;
298 o.default = o.disabled;
299
300 return m.render();
301 }
302 });