1 var table_names = [ 'Filter', 'NAT', 'Mangle', 'Raw' ],
2 current_mode = document.querySelector('.cbi-tab[data-mode="6"]') ? 6 : 4;
4 function create_table_section(table)
6 var idiv = document.getElementById('iptables'),
7 tdiv = idiv.querySelector('[data-table="%s"]'.format(table)),
8 title = '%s: %s'.format(_('Table'), table);
11 tdiv = E('div', { 'data-table': table }, [
16 if (idiv.firstElementChild.nodeName.toLowerCase() === 'p')
17 idiv.removeChild(idiv.firstElementChild);
19 var added = false, thisIdx = table_names.indexOf(table);
21 idiv.querySelectorAll('[data-table]').forEach(function(child) {
22 var childIdx = table_names.indexOf(child.getAttribute('data-table'));
24 if (added === false && childIdx > thisIdx) {
25 idiv.insertBefore(tdiv, child);
31 idiv.appendChild(tdiv);
34 return tdiv.lastElementChild;
37 function create_chain_section(table, chain, policy, packets, bytes, references)
39 var tdiv = create_table_section(table),
40 cdiv = tdiv.querySelector('[data-chain="%s"]'.format(chain)),
44 title = '%s <em>%s</em> <span>(%s: <em>%s</em>, %d %s, %.2mB %s)</span>'
45 .format(_('Chain'), chain, _('Policy'), policy, packets, _('Packets'), bytes, _('Traffic'));
47 title = '%s <em>%s</em> <span class="references">(%d %s)</span>'
48 .format(_('Chain'), chain, references, _('References'));
51 cdiv = E('div', { 'data-chain': chain }, [
52 E('h4', { 'id': 'rule_%s_%s'.format(table.toLowerCase(), chain) }, title),
53 E('div', { 'class': 'table' }, [
54 E('div', { 'class': 'tr table-titles' }, [
55 E('div', { 'class': 'th center' }, _('Pkts.')),
56 E('div', { 'class': 'th center' }, _('Traffic')),
57 E('div', { 'class': 'th' }, _('Target')),
58 E('div', { 'class': 'th' }, _('Prot.')),
59 E('div', { 'class': 'th' }, _('In')),
60 E('div', { 'class': 'th' }, _('Out')),
61 E('div', { 'class': 'th' }, _('Source')),
62 E('div', { 'class': 'th' }, _('Destination')),
63 E('div', { 'class': 'th' }, _('Options')),
64 E('div', { 'class': 'th' }, _('Comment'))
69 tdiv.appendChild(cdiv);
72 cdiv.firstElementChild.innerHTML = title;
75 return cdiv.lastElementChild;
78 function update_chain_section(chaintable, rows)
83 cbi_update_table(chaintable, rows, _('No rules in this chain.'));
85 if (rows.length === 0 &&
86 document.querySelector('form > [data-hide-empty="true"]'))
87 chaintable.parentNode.style.display = 'none';
89 chaintable.parentNode.style.display = '';
91 chaintable.parentNode.setAttribute('data-empty', rows.length === 0);
94 function hide_empty(btn)
96 var hide = (btn.getAttribute('data-hide-empty') === 'false');
98 btn.setAttribute('data-hide-empty', hide);
99 btn.value = hide ? _('Show empty chains') : _('Hide empty chains');
102 document.querySelectorAll('[data-chain][data-empty="true"]')
103 .forEach(function(chaintable) {
104 chaintable.style.display = hide ? 'none' : '';
108 function jump_target(ev)
110 var link = ev.target,
111 table = findParent(link, '[data-table]').getAttribute('data-table'),
112 chain = link.textContent,
113 num = +link.getAttribute('data-num'),
114 elem = document.getElementById('rule_%s_%s'.format(table.toLowerCase(), chain));
117 (document.documentElement || document.body.parentNode || document.body).scrollTop = elem.offsetTop - 40;
118 elem.classList.remove('flash');
119 void elem.offsetWidth;
120 elem.classList.add('flash');
123 var rule = elem.nextElementSibling.childNodes[num];
125 rule.classList.remove('flash');
126 void rule.offsetWidth;
127 rule.classList.add('flash');
133 function parse_output(table, s)
135 var current_chain = null;
136 var current_rules = [];
137 var seen_chains = {};
139 var re = /([^\n]*)\n/g;
142 while ((m = re.exec(s)) != null) {
143 if (m[1].match(/^Chain (.+) \(policy (\w+) (\d+) packets, (\d+) bytes\)$/)) {
144 var chain = RegExp.$1,
146 packets = +RegExp.$3,
149 update_chain_section(current_chain, current_rules);
151 seen_chains[chain] = true;
152 current_chain = create_chain_section(table, chain, policy, packets, bytes);
155 else if (m[1].match(/^Chain (.+) \((\d+) references\)$/)) {
156 var chain = RegExp.$1,
157 references = +RegExp.$2;
159 update_chain_section(current_chain, current_rules);
161 seen_chains[chain] = true;
162 current_chain = create_chain_section(table, chain, null, null, null, references);
165 else if (m[1].match(/^num /)) {
168 else if ((m2 = m[1].match(/^(\d+) +(\d+) +(\d+) +(.*?) +(\S+) +(\S*) +(\S+) +(\S+) +([a-f0-9:.]+(?:\/[a-f0-9:.]+)?) +([a-f0-9:.]+(?:\/[a-f0-9:.]+)?) +(.+)$/)) !== null) {
178 options = m2[11] || '-',
181 options = options.trim().replace(/(?:^| )\/\* (.+) \*\//,
183 comment = m2.replace(/^!fw3(: |$)/, '').trim() || '-';
188 '%.2m'.format(pkts).nobr(),
189 '%.2mB'.format(bytes).nobr(),
190 target ? '<span class="target">%s</span>'.format(target) : '-',
192 (indev !== '*') ? '<span class="ifacebadge">%s</span>'.format(indev) : '*',
193 (outdev !== '*') ? '<span class="ifacebadge">%s</span>'.format(outdev) : '*',
201 chain_refs[target] = chain_refs[target] || [];
202 chain_refs[target].push([ current_chain, num ]);
207 update_chain_section(current_chain, current_rules);
209 document.querySelectorAll('[data-table="%s"] [data-chain]'.format(table))
210 .forEach(function(cdiv) {
211 if (!seen_chains[cdiv.getAttribute('data-chain')]) {
212 cdiv.parentNode.removeChild(cdiv);
216 cdiv.querySelectorAll('.target').forEach(function(tspan) {
217 if (seen_chains[tspan.textContent]) {
218 tspan.classList.add('jump');
219 tspan.addEventListener('click', jump_target);
223 cdiv.querySelectorAll('.references').forEach(function(rspan) {
224 var refs = chain_refs[cdiv.getAttribute('data-chain')];
225 if (refs && refs.length) {
226 rspan.classList.add('cbi-tooltip-container');
227 rspan.appendChild(E('small', { 'class': 'cbi-tooltip ifacebadge', 'style': 'top:1em; left:auto' }, [ E('ul') ]));
229 refs.forEach(function(ref) {
230 var chain = ref[0].parentNode.getAttribute('data-chain'),
233 rspan.lastElementChild.lastElementChild.appendChild(E('li', {}, [
238 'onclick': 'jump_target(event)'
240 ', %s #%d'.format(_('Rule'), num)
248 table_names.forEach(function(table) {
249 L.Poll.add(function() {
250 var cmd = (current_mode == 6) ? '/usr/sbin/ip6tables' : '/usr/sbin/iptables';
251 return L.fs.exec_direct(cmd, [ '--line-numbers', '-w', '-nvxL', '-t', table.toLowerCase() ]).then(function(output) {
252 parse_output(table, output);