|
| 1 | +/*** |
| 2 | + Copyright (c) 2017 CommonsWare, LLC |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | + not use this file except in compliance with the License. You may obtain |
| 6 | + a copy of the License at |
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + Unless required by applicable law or agreed to in writing, software |
| 9 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + See the License for the specific language governing permissions and |
| 12 | + limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.commonsware.cwac.netsecurity; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.List; |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +/** |
| 23 | + * Represents a rule for identifying matching domain names. This is used |
| 24 | + * by MemorizingTrustManager to limit the scope of memorization to only |
| 25 | + * domains matching a rule. |
| 26 | + * |
| 27 | + * The whitelist() and blacklist() static methods implement two common |
| 28 | + * patterns (accept only domains in a list or deny a list of domains and |
| 29 | + * accept everything else). The other static methods allow you to assemble |
| 30 | + * other scenarios. These methods are designed to be used as static imports, |
| 31 | + * akin to Hamcrest matchers. |
| 32 | + */ |
| 33 | +abstract public class DomainMatchRule { |
| 34 | + abstract public boolean matches(String host); |
| 35 | + |
| 36 | + /** |
| 37 | + * Implements a logical AND: only domains matching all of the supplied |
| 38 | + * rules are considered to be a match. |
| 39 | + * |
| 40 | + * @param rules Rules to apply and AND together |
| 41 | + * @return Rule implementing the AND logic |
| 42 | + */ |
| 43 | + public static DomainMatchRule allOf(DomainMatchRule... rules) { |
| 44 | + return(new Composite(false, rules)); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Implements a logical AND: only domains matching all of the supplied |
| 49 | + * rules are considered to be a match. |
| 50 | + * |
| 51 | + * @param rules Rules to apply and AND together |
| 52 | + * @return Rule implementing the AND logic |
| 53 | + */ |
| 54 | + public static DomainMatchRule allOf(List<DomainMatchRule> rules) { |
| 55 | + return(new Composite(false, rules)); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Implements a logical OR: any domains matching at least one of the supplied |
| 60 | + * rules are considered to be a match. |
| 61 | + * |
| 62 | + * @param rules Rules to apply and OR together |
| 63 | + * @return Rule implementing the OR logic |
| 64 | + */ |
| 65 | + public static DomainMatchRule anyOf(DomainMatchRule... rules) { |
| 66 | + return(new Composite(true, rules)); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Implements a logical OR: any domains matching at least one of the supplied |
| 71 | + * rules are considered to be a match. |
| 72 | + * |
| 73 | + * @param rules Rules to apply and OR together |
| 74 | + * @return Rule implementing the OR logic |
| 75 | + */ |
| 76 | + public static DomainMatchRule anyOf(List<DomainMatchRule> rules) { |
| 77 | + return(new Composite(true, rules)); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Inverts a rule, by applying a logical NOT to whatever it returns |
| 82 | + * from matches() |
| 83 | + * |
| 84 | + * @param rule Rules to invert |
| 85 | + * @return Rule implementing the NOT logic |
| 86 | + */ |
| 87 | + public static DomainMatchRule not(DomainMatchRule rule) { |
| 88 | + return(new Not(rule)); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * A concrete matcher, comparing domain names against the supplied |
| 93 | + * regular expression. |
| 94 | + * |
| 95 | + * @param pattern Pattern to compare against domain names |
| 96 | + * @return Rule implementing the pattern-match rule |
| 97 | + */ |
| 98 | + public static DomainMatchRule is(Pattern pattern) { |
| 99 | + return(new Regex(pattern)); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * A concrete matcher, comparing domain names against a "glob"-style |
| 104 | + * expression. So, "foo.com" as a glob will only match "foo.com" as a |
| 105 | + * domain. But "*.foo.com" as a glob will match "www.foo.com", "bar.foo.com", |
| 106 | + * "www.bar.foo.com", and so on. |
| 107 | + * |
| 108 | + * @param glob Glob to compare against domain names |
| 109 | + * @return Rule implementing the glob-match rule |
| 110 | + */ |
| 111 | + public static DomainMatchRule is(String glob) { |
| 112 | + return(new Regex(glob)); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Accept any of the supplied globs, and reject anything else (see is(String) |
| 117 | + * for what "glob" means). |
| 118 | + * |
| 119 | + * @param globs Globs to compare against domain names |
| 120 | + * @return Rule implementing the whitelist |
| 121 | + */ |
| 122 | + public static DomainMatchRule whitelist(String... globs) { |
| 123 | + List<DomainMatchRule> rules=new ArrayList<>(); |
| 124 | + |
| 125 | + for (String glob : globs) { |
| 126 | + rules.add(is(glob)); |
| 127 | + } |
| 128 | + |
| 129 | + return(anyOf(rules)); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Reject all of the supplied globs, and accept anything else (see is(String) |
| 134 | + * for what "glob" means). |
| 135 | + * |
| 136 | + * @param globs Globs to compare against domain names |
| 137 | + * @return Rule implementing the blacklist |
| 138 | + */ |
| 139 | + public static DomainMatchRule blacklist(String... globs) { |
| 140 | + List<DomainMatchRule> rules=new ArrayList<>(); |
| 141 | + |
| 142 | + for (String glob : globs) { |
| 143 | + rules.add(not(is(glob))); |
| 144 | + } |
| 145 | + |
| 146 | + return(allOf(rules)); |
| 147 | + } |
| 148 | + |
| 149 | + private static class Composite extends DomainMatchRule { |
| 150 | + private final List<DomainMatchRule> rules; |
| 151 | + private final boolean isOr; |
| 152 | + |
| 153 | + Composite(boolean isOr, DomainMatchRule... rules) { |
| 154 | + this(isOr, Arrays.asList(rules)); |
| 155 | + } |
| 156 | + |
| 157 | + Composite(boolean isOr, List<DomainMatchRule> rules) { |
| 158 | + this.isOr=isOr; |
| 159 | + this.rules=rules; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public boolean matches(String host) { |
| 164 | + for (DomainMatchRule rule : rules) { |
| 165 | + boolean match=rule.matches(host); |
| 166 | + |
| 167 | + if (match && isOr) { |
| 168 | + return(true); |
| 169 | + } |
| 170 | + else if (!match && !isOr) { |
| 171 | + return(false); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return(!isOr); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private static class Not extends DomainMatchRule { |
| 180 | + private final DomainMatchRule rule; |
| 181 | + |
| 182 | + Not(DomainMatchRule rule) { |
| 183 | + this.rule=rule; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public boolean matches(String host) { |
| 188 | + return(!rule.matches(host)); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private static class Regex extends DomainMatchRule { |
| 193 | + private final Pattern pattern; |
| 194 | + |
| 195 | + Regex(String glob) { |
| 196 | + this(Pattern.compile(glob.replaceAll("\\.", "\\\\.") |
| 197 | + .replaceAll("\\*", "\\.\\*"))); |
| 198 | + } |
| 199 | + |
| 200 | + Regex(Pattern pattern) { |
| 201 | + this.pattern=pattern; |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public boolean matches(String host) { |
| 206 | + return(pattern.matcher(host).matches()); |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments