Changeset 8117

Show
Ignore:
Timestamp:
12/20/11 04:14:54 (17 months ago)
Author:
jow
Message:

applications/luci-firewall: use option "name" instead of deprecated "_name", expose "extra" option, add enable/disable toggles for portforwards, snats and rules

Location:
luci/trunk/applications/luci-firewall/luasrc
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/applications/luci-firewall/luasrc/model/cbi/firewall/forward-details.lua

    r8108 r8117  
    1515local sys = require "luci.sys" 
    1616local dsp = require "luci.dispatcher" 
     17local ft  = require "luci.tools.firewall" 
     18 
     19local m, s, o 
    1720 
    1821arg[1] = arg[1] or "" 
     
    3033    return 
    3134else 
    32     local name = m:get(arg[1], "_name") 
     35    local name = m:get(arg[1], "name") or m:get(arg[1], "_name") 
    3336    if not name or #name == 0 then 
    3437        name = translate("(Unnamed Entry)") 
     
    5760s.addremove = false 
    5861 
    59 s:tab("general", translate("General Settings")) 
    60 s:tab("advanced", translate("Advanced Settings")) 
     62ft.opt_enabled(s, Button) 
     63ft.opt_name(s, Value, translate("Name")) 
    6164 
    62 name = s:taboption("general", Value, "_name", translate("Name")) 
    63 name.rmempty = true 
    64 name.size = 10 
    6565 
    66 src = s:taboption("advanced", Value, "src", translate("Source zone")) 
    67 src.nocreate = true 
    68 src.default = "wan" 
    69 src.template = "cbi/firewall_zonelist" 
     66o = s:option(Value, "proto", translate("Protocol")) 
     67o:value("tcp udp", "TCP+UDP") 
     68o:value("tcp", "TCP") 
     69o:value("udp", "UDP") 
     70o:value("icmp", "ICMP") 
    7071 
    71 proto = s:taboption("general", Value, "proto", translate("Protocol")) 
    72 proto.optional = true 
    73 proto:value("tcp udp", "TCP+UDP") 
    74 proto:value("tcp", "TCP") 
    75 proto:value("udp", "UDP") 
    76 proto:value("icmp", "ICMP") 
    77  
    78 function proto.cfgvalue(...) 
     72function o.cfgvalue(...) 
    7973    local v = Value.cfgvalue(...) 
    8074    if not v or v == "tcpudp" then 
     
    8478end 
    8579 
    86 dport = s:taboption("general", Value, "src_dport", translate("External port"), 
     80 
     81o = s:option(Value, "src", translate("Source zone")) 
     82o.nocreate = true 
     83o.default = "wan" 
     84o.template = "cbi/firewall_zonelist" 
     85 
     86 
     87o = s:option(DynamicList, "src_mac", 
     88    translate("Source MAC address"), 
     89    translate("Only match incoming traffic from these MACs.")) 
     90o.rmempty = true 
     91o.datatype = "macaddr" 
     92o.placeholder = translate("any") 
     93 
     94 
     95o = s:option(Value, "src_ip", 
     96    translate("Source IP address"), 
     97    translate("Only match incoming traffic from this IP or range.")) 
     98o.rmempty = true 
     99o.datatype = "neg(ip4addr)" 
     100o.placeholder = translate("any") 
     101 
     102 
     103o = s:option(Value, "src_port", 
     104    translate("Source port"), 
     105    translate("Only match incoming traffic originating from the given source port or port range on the client host")) 
     106o.rmempty = true 
     107o.datatype = "portrange" 
     108o.placeholder = translate("any") 
     109 
     110 
     111o = s:option(Value, "src_dip", 
     112    translate("External IP address"), 
     113    translate("Only match incoming traffic directed at the given IP address.")) 
     114 
     115o.rmempty = true 
     116o.datatype = "ip4addr" 
     117o.placeholder = translate("any") 
     118 
     119 
     120o = s:option(Value, "src_dport", translate("External port"), 
    87121    translate("Match incoming traffic directed at the given " .. 
    88122        "destination port or port range on this host")) 
    89 dport.datatype = "portrange" 
     123o.datatype = "portrange" 
    90124 
    91 to = s:taboption("general", Value, "dest_ip", translate("Internal IP address"), 
    92     translate("Redirect matched incoming traffic to the specified " .. 
    93         "internal host")) 
    94 to.datatype = "ip4addr" 
     125 
     126 
     127o = s:option(Value, "dest", translate("Internal zone")) 
     128o.nocreate = true 
     129o.default = "lan" 
     130o.template = "cbi/firewall_zonelist" 
     131 
     132 
     133o = s:option(Value, "dest_ip", translate("Internal IP address"), 
     134    translate("Redirect matched incoming traffic to the specified \ 
     135        internal host")) 
     136o.datatype = "ip4addr" 
    95137for i, dataset in ipairs(sys.net.arptable()) do 
    96     to:value(dataset["IP address"]) 
     138    o:value(dataset["IP address"]) 
    97139end 
    98140 
    99 toport = s:taboption("general", Value, "dest_port", translate("Internal port (optional)"), 
    100     translate("Redirect matched incoming traffic to the given port on " .. 
    101         "the internal host")) 
    102 toport.optional = true 
    103 toport.placeholder = "0-65535" 
    104 toport.datatype = "portrange" 
    105141 
    106 dest = s:taboption("advanced", Value, "dest", translate("Destination zone")) 
    107 dest.nocreate = true 
    108 dest.default = "lan" 
    109 dest.template = "cbi/firewall_zonelist" 
     142o = s:option(Value, "dest_port", 
     143    translate("Internal port"), 
     144    translate("Redirect matched incoming traffic to the given port on \ 
     145        the internal host")) 
     146o.placeholder = translate("any") 
     147o.datatype = "portrange" 
    110148 
    111 src_dip = s:taboption("advanced", Value, "src_dip", 
    112     translate("Intended destination address"), 
    113     translate("Only match incoming traffic directed at the given IP address.")) 
    114149 
    115 src_dip.optional = true 
    116 src_dip.datatype = "ip4addr" 
    117 src_dip.placeholder = translate("any") 
    118  
    119 src_mac = s:taboption("advanced", DynamicList, "src_mac", 
    120     translate("Source MAC address"), 
    121     translate("Only match incoming traffic from these MACs.")) 
    122 src_mac.optional = true 
    123 src_mac.datatype = "macaddr" 
    124 src_mac.placeholder = translate("any") 
    125  
    126 src_ip = s:taboption("advanced", Value, "src_ip", 
    127     translate("Source IP address"), 
    128     translate("Only match incoming traffic from this IP or range.")) 
    129 src_ip.optional = true 
    130 src_ip.datatype = "neg(ip4addr)" 
    131 src_ip.placeholder = translate("any") 
    132  
    133 sport = s:taboption("advanced", Value, "src_port", 
    134     translate("Source port"), 
    135     translate("Only match incoming traffic originating from the given source port or port range on the client host")) 
    136 sport.optional = true 
    137 sport.datatype = "portrange" 
    138 sport.placeholder = translate("any") 
    139  
    140 reflection = s:taboption("advanced", Flag, "reflection", translate("Enable NAT Loopback")) 
    141 reflection.rmempty = true 
    142 reflection.default = reflection.enabled 
    143 reflection:depends({ target = "DNAT", src = wan_zone }) 
    144 reflection.cfgvalue = function(...) 
     150o = s:option(Flag, "reflection", translate("Enable NAT Loopback")) 
     151o.rmempty = true 
     152o.default = o.enabled 
     153o:depends("src", wan_zone) 
     154o.cfgvalue = function(...) 
    145155    return Flag.cfgvalue(...) or "1" 
    146156end 
    147157 
     158 
     159s:option(Value, "extra", 
     160    translate("Extra arguments"), 
     161    translate("Passes additional arguments to iptables. Use with care!")) 
     162 
     163 
    148164return m 
  • luci/trunk/applications/luci-firewall/luasrc/model/cbi/firewall/forwards.lua

    r8112 r8117  
    5050        self.map:set(created, "dest_ip",   a) 
    5151        self.map:set(created, "dest_port", i) 
    52         self.map:set(created, "_name",     n) 
     52        self.map:set(created, "name",      n) 
    5353    end 
    5454 
     
    7272end 
    7373 
    74 name = s:option(DummyValue, "_name", translate("Name")) 
    75 function name.cfgvalue(self, s) 
    76     return self.map:get(s, "_name") or "-" 
    77 end 
     74 
     75ft.opt_name(s, DummyValue, translate("Name")) 
     76 
    7877 
    7978proto = s:option(DummyValue, "proto", translate("Protocol")) 
     
    132131end 
    133132 
     133ft.opt_enabled(s, Flag, translate("Enable")).width = "1%" 
     134 
    134135return m 
  • luci/trunk/applications/luci-firewall/luasrc/model/cbi/firewall/rule-details.lua

    r8108 r8117  
    1818local nxo = require "nixio" 
    1919 
     20local ft = require "luci.tools.firewall" 
    2021local nw = require "luci.model.network" 
    2122local m, s, o, k, v 
     
    4748elseif rule_type == "redirect" then 
    4849 
    49     local name = m:get(arg[1], "_name") 
     50    local name = m:get(arg[1], "name") or m:get(arg[1], "_name") 
    5051    if not name or #name == 0 then 
    5152        name = translate("(Unnamed SNAT)") 
     
    7778 
    7879 
    79     o = s:option(Value, "_name", translate("Name")) 
    80     o.rmempty = true 
    81     o.size = 10 
     80    ft.opt_enabled(s, Button) 
     81    ft.opt_name(s, Value, translate("Name")) 
    8282 
    8383 
     
    116116    o = s:option(Value, "src_ip", translate("Source IP address")) 
    117117    o.rmempty = true 
    118     o.datatype = "neg(ip4addr)" 
     118    o.datatype = "neg(ipaddr)" 
    119119    o.placeholder = translate("any") 
    120120 
     
    177177 
    178178 
     179    s:option(Value, "extra", 
     180        translate("Extra arguments"), 
     181        translate("Passes additional arguments to iptables. Use with care!")) 
     182 
     183 
    179184-- 
    180185-- Rule 
    181186-- 
    182187else 
     188    local name = m:get(arg[1], "name") or m:get(arg[1], "_name") 
     189    if not name or #name == 0 then 
     190        name = translate("(Unnamed Rule)") 
     191    end 
     192 
     193    m.title = "%s - %s" %{ translate("Firewall - Traffic Rules"), name } 
     194 
     195 
    183196    s = m:section(NamedSection, arg[1], "rule", "") 
    184197    s.anonymous = true 
    185198    s.addremove = false 
    186199 
    187     s:option(Value, "_name", translate("Name").." "..translate("(optional)")) 
     200    ft.opt_enabled(s, Button) 
     201    ft.opt_name(s, Value, translate("Name")) 
    188202 
    189203 
     
    296310    o:value("REJECT", translate("reject")) 
    297311    o:value("NOTRACK", translate("don't track")) 
     312 
     313 
     314    s:option(Value, "extra", 
     315        translate("Extra arguments"), 
     316        translate("Passes additional arguments to iptables. Use with care!")) 
    298317end 
    299318 
  • luci/trunk/applications/luci-firewall/luasrc/model/cbi/firewall/rules.lua

    r8112 r8117  
    6060        self.map:set(created, "proto",     (i_p ~= "other") and i_p or "all") 
    6161        self.map:set(created, "dest_port", i_e) 
    62         self.map:set(created, "_name",     i_n) 
     62        self.map:set(created, "name",      i_n) 
    6363 
    6464        if i_p ~= "other" and i_e and #i_e > 0 then 
     
    7272        self.map:set(created, "src",    f_s) 
    7373        self.map:set(created, "dest",   f_d) 
    74         self.map:set(created, "_name",  f_n) 
     74        self.map:set(created, "name",   f_n) 
    7575    end 
    7676 
     
    8383end 
    8484 
    85 name = s:option(DummyValue, "_name", translate("Name")) 
    86 function name.cfgvalue(self, s) 
    87     return self.map:get(s, "_name") or "-" 
    88 end 
     85ft.opt_name(s, DummyValue, translate("Name")) 
    8986 
    9087family = s:option(DummyValue, "family", translate("Family")) 
     
    170167    end 
    171168end 
     169 
     170ft.opt_enabled(s, Flag, translate("Enable")).width = "1%" 
    172171 
    173172 
     
    211210        self.map:set(created, "src_dip",   a) 
    212211        self.map:set(created, "src_dport", p) 
    213         self.map:set(created, "_name",     n) 
     212        self.map:set(created, "name",      n) 
    214213    end 
    215214 
     
    226225end 
    227226 
    228 name = s:option(DummyValue, "_name", translate("Name")) 
    229 function name.cfgvalue(self, s) 
    230     return self.map:get(s, "_name") or "-" 
    231 end 
     227ft.opt_name(s, DummyValue, translate("Name")) 
    232228 
    233229proto = s:option(DummyValue, "proto", translate("Protocol")) 
     
    286282end 
    287283 
     284ft.opt_enabled(s, Flag, translate("Enable")).width = "1%" 
     285 
    288286 
    289287return m 
  • luci/trunk/applications/luci-firewall/luasrc/tools/firewall.lua

    r8112 r8117  
    228228    end 
    229229end 
     230 
     231 
     232function opt_enabled(s, t, ...) 
     233    if t == luci.cbi.Button then 
     234        local o = s:option(t, "__enabled") 
     235        function o.render(self, section) 
     236            if self.map:get(section, "enabled") ~= "0" then 
     237                self.title      = translate("Rule is enabled") 
     238                self.inputtitle = translate("Disable") 
     239                self.inputstyle = "reset" 
     240            else 
     241                self.title      = translate("Rule is disabled") 
     242                self.inputtitle = translate("Enable") 
     243                self.inputstyle = "apply" 
     244            end 
     245            t.render(self, section) 
     246        end 
     247        function o.write(self, section, value) 
     248            if self.map:get(section, "enabled") ~= "0" then 
     249                self.map:set(section, "enabled", "0") 
     250            else 
     251                self.map:del(section, "enabled") 
     252            end 
     253        end 
     254        return o 
     255    else 
     256        local o = s:option(t, "enabled", ...) 
     257              o.enabled = "" 
     258              o.disabled = "0" 
     259              o.default = o.enabled 
     260        return o 
     261    end 
     262end 
     263 
     264function opt_name(s, t, ...) 
     265    local o = s:option(t, "name", ...) 
     266 
     267    function o.cfgvalue(self, section) 
     268        return self.map:get(section, "name") or 
     269            self.map:get(section, "_name") or "-" 
     270    end 
     271 
     272    function o.write(self, section, value) 
     273        if value ~= "-" then 
     274            self.map:set(section, "name", value) 
     275            self.map:del(section, "_name") 
     276        else 
     277            self:remove(section) 
     278        end 
     279    end 
     280 
     281    function o.remove(self, section) 
     282        self.map:del(section, "name") 
     283        self.map:del(section, "_name") 
     284    end 
     285 
     286    return o 
     287end