8a1d41e719748ea49141d142005cf1cf920e888e
6 'require firewall as fwmodel';
7 'require tools.firewall as fwtool';
8 'require tools.widgets as widgets';
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');
14 var pr
= fwtool
.lookupProto(p
);
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);
23 val
: m
[0].toUpperCase().replace(/X
/g
, 'x'),
25 num
: '0x%02X'.format(+m
[2]),
26 mask
: m
[3] ? '0x%02X'.format(+m
[3]) : null
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>}'), {
35 function rule_src_txt(s
, hosts
) {
36 var z
= uci
.get('firewall', s
, 'src');
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'))
45 function rule_dest_txt(s
) {
46 var z
= uci
.get('firewall', s
, 'src');
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')
56 function rule_limit_txt(s
) {
57 var m
= String(uci
.get('firewall', s
, 'limit')).match(/^(\d+)\/([smhd])\w*$/i),
60 unit
: ({ s
: _('second'), m
: _('minute'), h
: _('hour'), d
: _('day') })[m
[2]],
61 burst
: uci
.get('firewall', s
, 'limit_burst')
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
});
70 function rule_target_txt(s
) {
71 var t
= uci
.get('firewall', s
, 'target'),
74 snat_ip
: uci
.get('firewall', s
, 'snat_ip'),
75 snat_port
: uci
.get('firewall', s
, 'snat_port')
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
);
83 return fwtool
.fmt(_('<var data-tooltip="MASQUERADE">Automatically rewrite</var> source IP'));
86 return fwtool
.fmt(_('<var data-tooltip="ACCEPT">Prevent source rewrite</var>'));
93 return L
.view
.extend({
94 callHostHints
: rpc
.declare({
96 method
: 'getHostHints',
100 callNetworkDevices
: rpc
.declare({
102 method
: 'getNetworkDevices',
108 this.callHostHints(),
109 this.callNetworkDevices(),
114 render: function(data
) {
115 if (fwtool
.checkLegacySNAT())
116 return fwtool
.renderMigration();
118 return this.renderNats(data
);
121 renderNats: function(data
) {
126 m
= new form
.Map('firewall', _('Firewall - NAT Rules'),
127 _('NAT rules allow fine grained control over the source IP to use for outbound or forwarded traffic.'));
129 s
= m
.section(form
.GridSection
, 'nat', _('NAT Rules'));
134 s
.tab('general', _('General Settings'));
135 s
.tab('advanced', _('Advanced Settings'));
136 s
.tab('timed', _('Time Restrictions'));
138 s
.sectiontitle = function(section_id
) {
139 return uci
.get('firewall', section_id
, 'name') || _('Unnamed NAT');
142 o
= s
.taboption('general', form
.Value
, 'name', _('Name'));
143 o
.placeholder
= _('Unnamed NAT');
146 o
= s
.option(form
.DummyValue
, '_match', _('Match'));
148 o
.textvalue = function(s
) {
150 rule_proto_txt(s
), E('br'),
151 rule_src_txt(s
, hosts
), E('br'),
152 rule_dest_txt(s
), E('br'),
157 o
= s
.option(form
.ListValue
, '_target', _('Action'));
159 o
.textvalue = function(s
) {
160 return rule_target_txt(s
);
163 o
= s
.option(form
.Flag
, 'enabled', _('Enable'));
165 o
.default = o
.enabled
;
168 o
= s
.taboption('general', fwtool
.CBIProtocolSelect
, 'proto', _('Protocol'));
172 o
= s
.taboption('general', widgets
.ZoneSelect
, 'src', _('Outbound zone'));
179 o
= fwtool
.addIPOption(s
, 'general', 'src_ip', _('Source address'),
180 _('Match forwarded traffic from this IP or range.'), 'ipv4', hosts
);
182 o
.datatype
= 'neg(ipmask4)';
184 o
= s
.taboption('general', form
.Value
, 'src_port', _('Source port'),
185 _('Match forwarded traffic originating from the given source port or port range.'));
188 o
.datatype
= 'neg(portrange)';
189 o
.placeholder
= _('any');
190 o
.depends({ proto
: 'tcp', '!contains': true });
191 o
.depends({ proto
: 'udp', '!contains': true });
193 o
= fwtool
.addIPOption(s
, 'general', 'dest_ip', _('Destination address'),
194 _('Match forwarded traffic directed at the given IP address.'), 'ipv4', hosts
);
196 o
.datatype
= 'neg(ipmask4)';
198 o
= s
.taboption('general', form
.Value
, 'dest_port', _('Destination port'),
199 _('Match forwarded traffic directed at the given destination port or port range.'));
202 o
.placeholder
= _('any');
203 o
.datatype
= 'neg(portrange)';
204 o
.depends({ proto
: 'tcp', '!contains': true });
205 o
.depends({ proto
: 'udp', '!contains': true });
207 o
= s
.taboption('general', form
.ListValue
, 'target', _('Action'));
210 o
.value('SNAT', _('SNAT - Rewrite to specific source IP or port'));
211 o
.value('MASQUERADE', _('MASQUERADE - Automatically rewrite to outbound interface IP'));
212 o
.value('ACCEPT', _('ACCEPT - Disable address rewriting'));
214 o
= fwtool
.addLocalIPOption(s
, 'general', 'snat_ip', _('Rewrite IP address'),
215 _('Rewrite matched traffic to the specified source IP address.'), devs
);
216 o
.placeholder
= null;
217 o
.depends('target', 'SNAT');
218 o
.validate = function(section_id
, value
) {
219 var port
= this.map
.lookupOption('snat_port', section_id
),
220 a
= this.formvalue(section_id
),
221 p
= port
? port
[0].formvalue(section_id
) : null;
223 if ((a
== null || a
== '') && (p
== null || p
== '') && value
== '')
224 return _('A rewrite IP must be specified!');
229 o
= s
.taboption('general', form
.Value
, 'snat_port', _('Rewrite port'),
230 _('Rewrite matched traffic to the specified source port or port range.'));
233 o
.placeholder
= _('do not rewrite');
234 o
.datatype
= 'portrange';
235 o
.depends({ proto
: 'tcp', '!contains': true });
236 o
.depends({ proto
: 'udp', '!contains': true });
238 o
= s
.taboption('advanced', widgets
.DeviceSelect
, 'device', _('Outbound device'),
239 _('Matches forwarded traffic using the specified outbound network device.'));
244 fwtool
.addMarkOption(s
, false);
245 fwtool
.addLimitOption(s
);
246 fwtool
.addLimitBurstOption(s
);
248 o
= s
.taboption('advanced', form
.Value
, 'extra', _('Extra arguments'),
249 _('Passes additional arguments to iptables. Use with care!'));
253 o
= s
.taboption('timed', form
.MultiValue
, 'weekdays', _('Week Days'));
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(' ') ]);
269 o
= s
.taboption('timed', form
.MultiValue
, 'monthdays', _('Month Days'));
273 o
.placeholder
= _('Any day');
274 o
.write = function(section_id
, value
) {
275 return this.super('write', [ section_id
, L
.toArray(value
).join(' ') ]);
277 for (var i
= 1; i
<= 31; i
++)
280 o
= s
.taboption('timed', form
.Value
, 'start_time', _('Start Time (hh.mm.ss)'));
282 o
.datatype
= 'timehhmmss';
284 o
= s
.taboption('timed', form
.Value
, 'stop_time', _('Stop Time (hh.mm.ss)'));
286 o
.datatype
= 'timehhmmss';
288 o
= s
.taboption('timed', form
.Value
, 'start_date', _('Start Date (yyyy-mm-dd)'));
290 o
.datatype
= 'dateyyyymmdd';
292 o
= s
.taboption('timed', form
.Value
, 'stop_date', _('Stop Date (yyyy-mm-dd)'));
294 o
.datatype
= 'dateyyyymmdd';
296 o
= s
.taboption('timed', form
.Flag
, 'utc_time', _('Time in UTC'));
298 o
.default = o
.disabled
;