Merge pull request #1735 from sumpfralle/olsr-jsoninfo-parser-handle-empty-result
[oweals/luci.git] / applications / luci-app-firewall / htdocs / luci-static / resources / view / firewall / rules.js
index 9fa1aa252dcc61b5ac8d94942c12615132a4a24e..6df3bc7f8550bf425674ee44f1bb8b82d168504e 100644 (file)
@@ -6,34 +6,6 @@
 'require tools.firewall as fwtool';
 'require tools.widgets as widgets';
 
-function skeys(obj, key, mode) {
-       if (obj == null || typeof(obj) != 'object')
-               return [];
-
-       return Object.keys(obj).map(function(e) {
-               var v = (key != null) ? obj[e][key] : e;
-
-               switch (mode) {
-               case 'addr':
-                       v = (v != null) ? v.replace(/(?:^|[.:])([0-9a-fA-F]{1,4})/g,
-                               function(m0, m1) { return ('000' + m1.toLowerCase()).substr(-4) }) : null;
-                       break;
-
-               case 'num':
-                       v = (v != null) ? +v : null;
-                       break;
-               }
-
-               return [ e, v ];
-       }).filter(function(e) {
-               return (e[1] != null);
-       }).sort(function(a, b) {
-               return (a[1] > b[1]);
-       }).map(function(e) {
-               return e[0];
-       });
-}
-
 function fmt(fmt /*, ...*/) {
        var repl = [], wrap = false;
 
@@ -70,7 +42,8 @@ function fmt(fmt /*, ...*/) {
 }
 
 function forward_proto_txt(s) {
-       return fmt('%s-%s', _('IPv4'),
+       return fmt('%s-%s',
+               fwtool.fmt_family(uci.get('firewall', s, 'family')),
                fwtool.fmt_proto(uci.get('firewall', s, 'proto'),
                                 uci.get('firewall', s, 'icmp_type')) || 'TCP+UDP');
 }
@@ -139,7 +112,8 @@ function rule_target_txt(s) {
 return L.view.extend({
        callHostHints: rpc.declare({
                object: 'luci',
-               method: 'host_hints'
+               method: 'getHostHints',
+               expect: { '': {} }
        }),
 
        load: function() {
@@ -171,6 +145,27 @@ return L.view.extend({
                        return uci.get('firewall', section_id, 'name') || _('Unnamed rule');
                };
 
+               s.handleAdd = function(ev) {
+                       var config_name = this.uciconfig || this.map.config,
+                           section_id = uci.add(config_name, this.sectiontype),
+                           opt1, opt2;
+
+                       for (var i = 0; i < this.children.length; i++)
+                               if (this.children[i].option == 'src')
+                                       opt1 = this.children[i];
+                               else if (this.children[i].option == 'dest')
+                                       opt2 = this.children[i];
+
+                       opt1.default = 'wan';
+                       opt2.default = 'lan';
+
+                       this.addedSection = section_id;
+                       this.renderMoreOptionsModal(section_id);
+
+                       delete opt1.default;
+                       delete opt2.default;
+               };
+
                o = s.taboption('general', form.Value, 'name', _('Name'));
                o.placeholder = _('Unnamed rule');
                o.modalonly = true;
@@ -227,42 +222,44 @@ return L.view.extend({
                o.cast = 'table';
                o.placeholder = _('any');
                o.value('', 'any');
-               o.value('echo-reply');
+               o.value('address-mask-reply');
+               o.value('address-mask-request');
+               o.value('communication-prohibited');
                o.value('destination-unreachable');
-               o.value('network-unreachable');
-               o.value('host-unreachable');
-               o.value('protocol-unreachable');
-               o.value('port-unreachable');
+               o.value('echo-reply');
+               o.value('echo-request');
                o.value('fragmentation-needed');
-               o.value('source-route-failed');
-               o.value('network-unknown');
+               o.value('host-precedence-violation');
+               o.value('host-prohibited');
+               o.value('host-redirect');
                o.value('host-unknown');
+               o.value('host-unreachable');
+               o.value('ip-header-bad');
+               o.value('neighbour-advertisement');
+               o.value('neighbour-solicitation');
                o.value('network-prohibited');
-               o.value('host-prohibited');
-               o.value('TOS-network-unreachable');
-               o.value('TOS-host-unreachable');
-               o.value('communication-prohibited');
-               o.value('host-precedence-violation');
+               o.value('network-redirect');
+               o.value('network-unknown');
+               o.value('network-unreachable');
+               o.value('parameter-problem');
+               o.value('port-unreachable');
                o.value('precedence-cutoff');
-               o.value('source-quench');
+               o.value('protocol-unreachable');
                o.value('redirect');
-               o.value('network-redirect');
-               o.value('host-redirect');
-               o.value('TOS-network-redirect');
-               o.value('TOS-host-redirect');
-               o.value('echo-request');
+               o.value('required-option-missing');
                o.value('router-advertisement');
                o.value('router-solicitation');
+               o.value('source-quench');
+               o.value('source-route-failed');
                o.value('time-exceeded');
-               o.value('ttl-zero-during-transit');
-               o.value('ttl-zero-during-reassembly');
-               o.value('parameter-problem');
-               o.value('ip-header-bad');
-               o.value('required-option-missing');
-               o.value('timestamp-request');
                o.value('timestamp-reply');
-               o.value('address-mask-request');
-               o.value('address-mask-reply');
+               o.value('timestamp-request');
+               o.value('TOS-host-redirect');
+               o.value('TOS-host-unreachable');
+               o.value('TOS-network-redirect');
+               o.value('TOS-network-unreachable');
+               o.value('ttl-zero-during-reassembly');
+               o.value('ttl-zero-during-transit');
                o.depends('proto', 'icmp');
 
                o = s.taboption('general', widgets.ZoneSelect, 'src', _('Source zone'));
@@ -270,13 +267,12 @@ return L.view.extend({
                o.nocreate = true;
                o.allowany = true;
                o.allowlocal = 'src';
-               o.default = 'wan';
 
                o = s.taboption('advanced', form.Value, 'src_mac', _('Source MAC address'));
                o.modalonly = true;
                o.datatype = 'list(macaddr)';
                o.placeholder = _('any');
-               skeys(hosts).forEach(function(mac) {
+               L.sortedKeys(hosts).forEach(function(mac) {
                        o.value(mac, '%s (%s)'.format(
                                mac,
                                hosts[mac].name || hosts[mac].ipv4 || hosts[mac].ipv6 || '?'
@@ -287,7 +283,7 @@ return L.view.extend({
                o.modalonly = true;
                o.datatype = 'list(neg(ipmask))';
                o.placeholder = _('any');
-               skeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
+               L.sortedKeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
                        o.value(hosts[mac].ipv4, '%s (%s)'.format(
                                hosts[mac].ipv4,
                                hosts[mac].name || mac
@@ -303,28 +299,17 @@ return L.view.extend({
                o.depends('proto', 'tcp udp');
                o.depends('proto', 'tcpudp');
 
-               o = s.taboption('general', widgets.ZoneSelect, 'dest_local', _('Output zone'));
-               o.modalonly = true;
-               o.nocreate = true;
-               o.allowany = true;
-               o.alias = 'dest';
-               o.default = 'wan';
-               o.depends('src', '');
-
-               o = s.taboption('general', widgets.ZoneSelect, 'dest_remote', _('Destination zone'));
+               o = s.taboption('general', widgets.ZoneSelect, 'dest', _('Destination zone'));
                o.modalonly = true;
                o.nocreate = true;
                o.allowany = true;
                o.allowlocal = true;
-               o.alias = 'dest';
-               o.default = 'lan';
-               o.depends({'src': '', '!reverse': true});
 
                o = s.taboption('general', form.Value, 'dest_ip', _('Destination address'));
                o.modalonly = true;
                o.datatype = 'list(neg(ipmask))';
                o.placeholder = _('any');
-               skeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
+               L.sortedKeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
                        o.value(hosts[mac].ipv4, '%s (%s)'.format(
                                hosts[mac].ipv4,
                                hosts[mac].name || mac
@@ -393,9 +378,6 @@ return L.view.extend({
                o.modalonly = true;
                o.default = o.disabled;
 
-               return m.render().catch(function(e) {
-                       console.debug('render fail')
-               });
-
+               return m.render();
        }
 });