diff options
author | Daniel Dickinson <openwrt@daniel.thecshore.com> | 2015-12-14 07:38:31 -0500 |
---|---|---|
committer | Daniel Dickinson <openwrt@daniel.thecshore.com> | 2015-12-15 13:10:15 -0500 |
commit | 38880407aa6c0246bc9af52073a79ff4a2096a6e (patch) | |
tree | 340fa7fe332077526fdd8fdc487330394c3d9e24 | |
parent | d83642c20519692c5d3883e23d01bb84a48dd85a (diff) |
modules/luci-base: Fix ipaddrport validator to support ipv6
The previous versiono of ipaddrport validator only worked for ipv4
due to disallowing colons (:) in ip address which obvious fails for
ipv6. We now instead allow either ipv4 address or an ipv6 address of
the form [<ipv6address>]:port
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/cbi.js | 25 | ||||
-rw-r--r-- | modules/luci-base/luasrc/cbi/datatypes.lua | 18 |
2 files changed, 39 insertions, 4 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/cbi.js b/modules/luci-base/htdocs/luci-static/resources/cbi.js index 8a3cb6fca..6ce947def 100644 --- a/modules/luci-base/htdocs/luci-static/resources/cbi.js +++ b/modules/luci-base/htdocs/luci-static/resources/cbi.js @@ -172,17 +172,38 @@ var cbi_validators = { return false; }, - 'ipaddrport': function() + 'ip4addrport': function() { var hp = this.split(/:/); if (hp.length == 2) return (cbi_validators.ipaddr.apply(hp[0]) && cbi_validators.port.apply(hp[1])); - return false; }, + 'ipaddrport': function(bracket) + { + if (this.match(/^([^\[\]:]+):([^:]+)$/)) { + var addr = RegExp.$1 + var port = RegExp.$2 + return (cbi_validators.ip4addr.apply(addr) && + cbi_validators.port.apply(port)); + } else if ((bracket == 1) && (this.match(/^\[(.+)\]:([^:]+)$/))) { + var addr = RegExp.$1 + var port = RegExp.$2 + return (cbi_validators.ip6addr.apply(addr) && + cbi_validators.port.apply(port)); + } else if ((bracket != 1) && (this.match(/^([^\[\]]+):([^:]+)$/))) { + var addr = RegExp.$1 + var port = RegExp.$2 + return (cbi_validators.ip6addr.apply(addr) && + cbi_validators.port.apply(port)); + } else { + return false; + } + }, + 'wpakey': function() { var v = this; diff --git a/modules/luci-base/luasrc/cbi/datatypes.lua b/modules/luci-base/luasrc/cbi/datatypes.lua index 52f90afee..aafdc5c3c 100644 --- a/modules/luci-base/luasrc/cbi/datatypes.lua +++ b/modules/luci-base/luasrc/cbi/datatypes.lua @@ -189,9 +189,23 @@ function hostport(val) return not not (h and p and host(h) and port(p)) end -function ipaddrport(val) +function ip4addrport(val) local h, p = val:match("^([^:]+):([^:]+)$") - return not not (h and p and ipaddr(h) and port(p)) + return (h and p and ip4addr(h) and port(p)) +end + +function ipaddrport(val, bracket) + local h, p = val:match("^([^%[%]:]+):([^:]+)$") + if (h and p and ip4addr(h) and port(p)) then + return true + elseif (bracket == 1) then + h, p = val:match("^(%[.+%]):([^:]+)$") + if (h and p and ip6addr(h) and port(p)) then + return true + end + end + h, p = val:match("^([^%[%]]+):([^:]+)$") + return (h and p and ip6addr(h) and port(p)) end function wpakey(val) |