From 8dcad1697e2780f373800ef07f3681809b7a293a Mon Sep 17 00:00:00 2001 From: FelisCatus Date: Sun, 23 Oct 2016 01:06:29 -0400 Subject: [PATCH] Support IpCondition in omega-web. Fix #705. --- omega-locales/ach/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/cs/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/de/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/en_US/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/es_AR/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/fa/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/fr/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/is/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/ja/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/nb_NO/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/pt_BR/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/ru/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/sk/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/sl/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/tr/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/zh_CN/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-locales/zh_TW/LC_MESSAGES/omega-web.po | 14 ++++++++++++++ omega-pac/package.json | 3 ++- omega-pac/src/conditions.coffee | 13 +++++++------ omega-pac/test/conditions.coffee | 14 ++++++++++++++ .../src/omega/controllers/switch_profile.coffee | 6 ++++++ omega-web/src/omega/directives.coffee | 15 +++++++++++++++ omega-web/src/partials/profile_switch.jade | 4 ++++ 23 files changed, 286 insertions(+), 7 deletions(-) diff --git a/omega-locales/ach/LC_MESSAGES/omega-web.po b/omega-locales/ach/LC_MESSAGES/omega-web.po index acb5433..b7688ae 100644 --- a/omega-locales/ach/LC_MESSAGES/omega-web.po +++ b/omega-locales/ach/LC_MESSAGES/omega-web.po @@ -68,6 +68,20 @@ msgstr "" "the host (domain name).
Example: www.example.com is with a " "host level of 3, while internal is of host level 1." +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "URL wildcard" diff --git a/omega-locales/cs/LC_MESSAGES/omega-web.po b/omega-locales/cs/LC_MESSAGES/omega-web.po index 7bee6b4..f9fa429 100644 --- a/omega-locales/cs/LC_MESSAGES/omega-web.po +++ b/omega-locales/cs/LC_MESSAGES/omega-web.po @@ -69,6 +69,20 @@ msgstr "" "název domény).
Příklad: www.example.com je na úrovni " "hostitele 3, zatímco vnitřní hostitele úrovně 1." +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "URL wildcard" diff --git a/omega-locales/de/LC_MESSAGES/omega-web.po b/omega-locales/de/LC_MESSAGES/omega-web.po index 7c30b6e..7e4c27f 100644 --- a/omega-locales/de/LC_MESSAGES/omega-web.po +++ b/omega-locales/de/LC_MESSAGES/omega-web.po @@ -73,6 +73,20 @@ msgstr "" "the host (domain name).
Example: www.example.com is with a " "host level of 3, while internal is of host level 1." +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "URL wildcard" diff --git a/omega-locales/en_US/LC_MESSAGES/omega-web.po b/omega-locales/en_US/LC_MESSAGES/omega-web.po index c54d8b1..0964540 100644 --- a/omega-locales/en_US/LC_MESSAGES/omega-web.po +++ b/omega-locales/en_US/LC_MESSAGES/omega-web.po @@ -69,6 +69,20 @@ msgstr "" "the host (domain name).
Example: www.example.com is with a " "host level of 3, while internal is of host level 1." +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "URL wildcard" diff --git a/omega-locales/es_AR/LC_MESSAGES/omega-web.po b/omega-locales/es_AR/LC_MESSAGES/omega-web.po index a8c4914..111d5ee 100644 --- a/omega-locales/es_AR/LC_MESSAGES/omega-web.po +++ b/omega-locales/es_AR/LC_MESSAGES/omega-web.po @@ -72,6 +72,20 @@ msgstr "" "www.ejemplo.com tiene un nivel 3 de host, mientras que " "interno es un host de nivel 1." +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "Comodín de URL" diff --git a/omega-locales/fa/LC_MESSAGES/omega-web.po b/omega-locales/fa/LC_MESSAGES/omega-web.po index 4636197..ba0a9cd 100644 --- a/omega-locales/fa/LC_MESSAGES/omega-web.po +++ b/omega-locales/fa/LC_MESSAGES/omega-web.po @@ -72,6 +72,20 @@ msgstr "" "دارای سطح میزابن سه میباشد ، در حالی که داخل سطح میزبان یک را " "دارد." +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "فرانویسه ادرس (URL)" diff --git a/omega-locales/fr/LC_MESSAGES/omega-web.po b/omega-locales/fr/LC_MESSAGES/omega-web.po index b3985a9..06c2f61 100644 --- a/omega-locales/fr/LC_MESSAGES/omega-web.po +++ b/omega-locales/fr/LC_MESSAGES/omega-web.po @@ -70,6 +70,20 @@ msgstr "" "the host (domain name).
Example: www.example.com is with a " "host level of 3, while internal is of host level 1." +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "URL wildcard" diff --git a/omega-locales/is/LC_MESSAGES/omega-web.po b/omega-locales/is/LC_MESSAGES/omega-web.po index bc6453d..ac5fa3d 100644 --- a/omega-locales/is/LC_MESSAGES/omega-web.po +++ b/omega-locales/is/LC_MESSAGES/omega-web.po @@ -53,6 +53,20 @@ msgstr "" msgid "condition_help_HostLevelsCondition" msgstr "" +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "" diff --git a/omega-locales/ja/LC_MESSAGES/omega-web.po b/omega-locales/ja/LC_MESSAGES/omega-web.po index f557acc..53b38a0 100644 --- a/omega-locales/ja/LC_MESSAGES/omega-web.po +++ b/omega-locales/ja/LC_MESSAGES/omega-web.po @@ -66,6 +66,20 @@ msgstr "" "the host (domain name).
Example: www.example.com is with a " "host level of 3, while internal is of host level 1." +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "URLワイルドカード" diff --git a/omega-locales/nb_NO/LC_MESSAGES/omega-web.po b/omega-locales/nb_NO/LC_MESSAGES/omega-web.po index ad5936f..64cad87 100644 --- a/omega-locales/nb_NO/LC_MESSAGES/omega-web.po +++ b/omega-locales/nb_NO/LC_MESSAGES/omega-web.po @@ -54,6 +54,20 @@ msgstr "" msgid "condition_help_HostLevelsCondition" msgstr "" +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "" diff --git a/omega-locales/pt_BR/LC_MESSAGES/omega-web.po b/omega-locales/pt_BR/LC_MESSAGES/omega-web.po index 79536aa..2d84cfa 100644 --- a/omega-locales/pt_BR/LC_MESSAGES/omega-web.po +++ b/omega-locales/pt_BR/LC_MESSAGES/omega-web.po @@ -69,6 +69,20 @@ msgstr "" "the host (domain name).
Example: www.example.com is with a " "host level of 3, while internal is of host level 1." +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "URL wildcard" diff --git a/omega-locales/ru/LC_MESSAGES/omega-web.po b/omega-locales/ru/LC_MESSAGES/omega-web.po index d5d9e02..28b7fca 100644 --- a/omega-locales/ru/LC_MESSAGES/omega-web.po +++ b/omega-locales/ru/LC_MESSAGES/omega-web.po @@ -73,6 +73,20 @@ msgstr "" "the host (domain name).
Example: www.example.com is with a " "host level of 3, while internal is of host level 1." +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "URL wildcard" diff --git a/omega-locales/sk/LC_MESSAGES/omega-web.po b/omega-locales/sk/LC_MESSAGES/omega-web.po index 81f3c10..bf4e39a 100644 --- a/omega-locales/sk/LC_MESSAGES/omega-web.po +++ b/omega-locales/sk/LC_MESSAGES/omega-web.po @@ -52,6 +52,20 @@ msgstr "" msgid "condition_help_HostLevelsCondition" msgstr "" +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "" diff --git a/omega-locales/sl/LC_MESSAGES/omega-web.po b/omega-locales/sl/LC_MESSAGES/omega-web.po index 5edf593..9c9986a 100644 --- a/omega-locales/sl/LC_MESSAGES/omega-web.po +++ b/omega-locales/sl/LC_MESSAGES/omega-web.po @@ -54,6 +54,20 @@ msgstr "" msgid "condition_help_HostLevelsCondition" msgstr "" +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "" diff --git a/omega-locales/tr/LC_MESSAGES/omega-web.po b/omega-locales/tr/LC_MESSAGES/omega-web.po index 07e9668..681557f 100644 --- a/omega-locales/tr/LC_MESSAGES/omega-web.po +++ b/omega-locales/tr/LC_MESSAGES/omega-web.po @@ -72,6 +72,20 @@ msgstr "" "the host (domain name).
Example: www.example.com is with a " "host level of 3, while internal is of host level 1." +msgid "condition_IpCondition" +msgstr "IP Literals" + +msgid "condition_help_IpCondition" +msgstr "" +"Matches the request if and only if the host is a literal IP address and" +" in the subnet as specified by " +"CIDR notation.
For example, given the rule 127.0.0.1/16, " +"it matches all IP addresses like 127.0.*.*.
" +"So 127.0.0.1 matches while 127.1.0.0 does not. " +"Host names like localhost will never match because they are " +"not IP literals." + msgid "condition_UrlWildcardCondition" msgstr "URL wildcard" diff --git a/omega-locales/zh_CN/LC_MESSAGES/omega-web.po b/omega-locales/zh_CN/LC_MESSAGES/omega-web.po index 52f47fb..a33dff4 100644 --- a/omega-locales/zh_CN/LC_MESSAGES/omega-web.po +++ b/omega-locales/zh_CN/LC_MESSAGES/omega-web.po @@ -62,6 +62,20 @@ msgstr "" "如果域名层数在设定的范围内则匹配,否则不匹配。
域名层数是指 域名共有几段(以点分隔).
例如: www." "example.com 的域名层数为 3,而 internal 的域名层数为 1." +msgid "condition_IpCondition" +msgstr "IP 地址字面量" + +msgid "condition_help_IpCondition" +msgstr "" +"当且仅当主机是字面的 IP 地址,且地址处于某个子网内时匹配。" +"子网使用 CIDR 格式表示。
例如,规则 127.0.0.1/16 会匹配所有类似 " +"127.0.*.* 的地址。
" +"因此地址 127.0.0.1 匹配而地址 127.1.0.0 不匹配。" +"主机名称,例如 localhost 不会被此类规则匹配,因为它们不是 " +"IP 地址字面量。" + msgid "condition_UrlWildcardCondition" msgstr "网址通配符" diff --git a/omega-locales/zh_TW/LC_MESSAGES/omega-web.po b/omega-locales/zh_TW/LC_MESSAGES/omega-web.po index d10d22f..fa90cc0 100644 --- a/omega-locales/zh_TW/LC_MESSAGES/omega-web.po +++ b/omega-locales/zh_TW/LC_MESSAGES/omega-web.po @@ -62,6 +62,20 @@ msgstr "" "如果域名層數在設定的範圍內則匹配,否則不匹配。
域名層數是指 域名共有幾段(以點分隔).
例如: www." "example.com 的域名層數為 3,而 internal 的域名層數為 1." +msgid "condition_IpCondition" +msgstr "IP 地址字面量" + +msgid "condition_help_IpCondition" +msgstr "" +"當且僅當主機是字面的 IP 地址,且地址處於某個子網內時匹配。" +"子網使用 CIDR 格式表示。
例如,規則 127.0.0.1/16 會匹配所有類似" +"127.0.*.* 的地址。
" +"因此地址 127.0.0.1 匹配而地址 127.1.0.0 不匹配。" +"主機名稱,例如 localhost 不會被此類規則匹配,因為它們不是 " +"IP 地址字面量。" + msgid "condition_UrlWildcardCondition" msgstr "網址通配符" diff --git a/omega-pac/package.json b/omega-pac/package.json index b772d4b..42cf7d6 100644 --- a/omega-pac/package.json +++ b/omega-pac/package.json @@ -28,6 +28,7 @@ "uglify-js-real": "./uglifyjs.js" }, "scripts": { - "dev": "npm link" + "dev": "npm link", + "test": "TZ=Europe/London grunt test" } } diff --git a/omega-pac/src/conditions.coffee b/omega-pac/src/conditions.coffee index 54cca9a..655db40 100644 --- a/omega-pac/src/conditions.coffee +++ b/omega-pac/src/conditions.coffee @@ -549,12 +549,13 @@ module.exports = exports = ) str: (condition) -> condition.ip + '/' + condition.prefixLength fromStr: (str, condition) -> - [ip, prefixLength] = str.split('/') - condition.ip = ip - addr = @parseIp ip - condition.ip = '0.0.0.0' unless addr? - condition.prefixLength = parseInt(prefixLength, 10) - condition.prefixLength = 0 unless condition.prefixLength >= 0 + addr = @parseIp str + if addr? + condition.ip = addr.addressMinusSuffix + condition.prefixLength = addr.subnetMask + else + condition.ip = '0.0.0.0' + condition.prefixLength = 0 condition 'HostLevelsCondition': diff --git a/omega-pac/test/conditions.coffee b/omega-pac/test/conditions.coffee index 46bf39e..4fb91c1 100644 --- a/omega-pac/test/conditions.coffee +++ b/omega-pac/test/conditions.coffee @@ -582,6 +582,20 @@ describe 'Conditions', -> ip: '0.0.0.0' prefixLength: 0 ) + it 'should assume full match for IpCondition without prefixLength', -> + cond = Conditions.fromStr('Ip: 127.0.0.1') + cond.should.eql( + conditionType: 'IpCondition' + ip: '127.0.0.1' + prefixLength: 32 + ) + + cond = Conditions.fromStr('Ip: ::1') + cond.should.eql( + conditionType: 'IpCondition' + ip: '::1' + prefixLength: 128 + ) it 'should provide sensible fallbacks for invalid IpCondition', -> cond = Conditions.fromStr('Ip: 0.0.0.0/-233') cond.should.eql( diff --git a/omega-web/src/omega/controllers/switch_profile.coffee b/omega-web/src/omega/controllers/switch_profile.coffee index b2306f2..10e196c 100644 --- a/omega-web/src/omega/controllers/switch_profile.coffee +++ b/omega-web/src/omega/controllers/switch_profile.coffee @@ -77,6 +77,7 @@ angular.module('omega').controller 'SwitchProfileCtrl', ($scope, $rootScope, 'HostWildcardCondition' 'HostRegexCondition' 'HostLevelsCondition' + 'IpCondition' ] } { @@ -193,6 +194,11 @@ angular.module('omega').controller 'SwitchProfileCtrl', ($scope, $rootScope, return pattern.indexOf(':') >= 0 || pattern.indexOf('/') >= 0 return false + $scope.validateIpCondition = (condition, input) -> + return false unless input + ip = OmegaPac.Conditions.parseIp(input) + return ip? + $scope.getWeekdayList = OmegaPac.Conditions.getWeekdayList $scope.updateDay = (condition, i, selected) -> condition.days ||= '-------' diff --git a/omega-web/src/omega/directives.coffee b/omega-web/src/omega/directives.coffee index 12b6a1e..443964c 100644 --- a/omega-web/src/omega/directives.coffee +++ b/omega-web/src/omega/directives.coffee @@ -46,3 +46,18 @@ angular.module('omega').directive 'omegaInt2str', -> parseInt(value) ngModel.$formatters.push (value) -> '' + value +angular.module('omega').directive 'omegaIp2str', -> + restrict: 'A' + priority: 2 # Run post-link after input directive (0) and ngModel (1). + require: 'ngModel' + link: (scope, element, attr, ngModel) -> + ngModel.$parsers.push (value) -> + if value + OmegaPac.Conditions.fromStr('Ip: ' + value) + else + ({conditionType: 'IpCondition', ip: '0.0.0.0', prefixLength: 0}) + ngModel.$formatters.push (value) -> + if value?.ip + OmegaPac.Conditions.str(value).split(' ', 2)[1] + else + '' diff --git a/omega-web/src/partials/profile_switch.jade b/omega-web/src/partials/profile_switch.jade index 0c85f50..2b18e98 100644 --- a/omega-web/src/partials/profile_switch.jade +++ b/omega-web/src/partials/profile_switch.jade @@ -67,6 +67,10 @@ div(ng-controller='SwitchProfileCtrl') span {{'options_hostLevelsBetween' | tr}} = ' ' input.form-control(type='number' max='99' min='1' ng-model='rule.condition.maxValue' required) + span(ng-switch-when='IpCondition') + input.form-control(type='text' ng-model='rule.condition' required + omega-ip2str placeholder='127.0.0.1/8' + ui-validate='{pattern: "validateIpCondition(rule.condition, $value)"}') span.host-levels-details(ng-switch-when='TimeCondition') input.form-control(type='number' min='0' max='23' ng-model='rule.condition.startHour' required) = ' '