1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.jboss.netty.handler.ipfilter;
18
19 import org.jboss.netty.logging.InternalLogger;
20 import org.jboss.netty.logging.InternalLoggerFactory;
21 import org.jboss.netty.util.internal.StringUtil;
22
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
25 import java.util.regex.Pattern;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class PatternRule implements IpFilterRule, Comparable<Object> {
52 private static final InternalLogger logger = InternalLoggerFactory.getInstance(PatternRule.class);
53
54 private Pattern ipPattern;
55
56 private Pattern namePattern;
57
58 private boolean isAllowRule = true;
59
60 private boolean localhost;
61
62 private final String pattern;
63
64
65
66
67
68
69
70 public PatternRule(boolean allow, String pattern) {
71 isAllowRule = allow;
72 this.pattern = pattern;
73 parse(pattern);
74 }
75
76
77
78
79
80
81 public String getPattern() {
82 return pattern;
83 }
84
85 public boolean isAllowRule() {
86 return isAllowRule;
87 }
88
89 public boolean isDenyRule() {
90 return !isAllowRule;
91 }
92
93 public boolean contains(InetAddress inetAddress) {
94 if (localhost) {
95 if (isLocalhost(inetAddress)) {
96 return true;
97 }
98 }
99 if (ipPattern != null) {
100 if (ipPattern.matcher(inetAddress.getHostAddress()).matches()) {
101 return true;
102 }
103 }
104 if (namePattern != null) {
105 if (namePattern.matcher(inetAddress.getHostName()).matches()) {
106 return true;
107 }
108 }
109 return false;
110 }
111
112 private void parse(String pattern) {
113 if (pattern == null) {
114 return;
115 }
116
117 String[] acls = StringUtil.split(pattern, ',');
118
119 String ip = "";
120 String name = "";
121 for (String c : acls) {
122 c = c.trim();
123 if (c.equals("n:localhost")) {
124 localhost = true;
125 } else if (c.startsWith("n:")) {
126 name = addRule(name, c.substring(2));
127 } else if (c.startsWith("i:")) {
128 ip = addRule(ip, c.substring(2));
129 }
130 }
131 if (ip.length() != 0) {
132 ipPattern = Pattern.compile(ip);
133 }
134 if (name.length() != 0) {
135 namePattern = Pattern.compile(name);
136 }
137 }
138
139 private static String addRule(String pattern, String rule) {
140 if (rule == null || rule.length() == 0) {
141 return pattern;
142 }
143 if (pattern.length() != 0) {
144 pattern += "|";
145 }
146 rule = rule.replaceAll("\\.", "\\\\.");
147 rule = rule.replaceAll("\\*", ".*");
148 rule = rule.replaceAll("\\?", ".");
149 pattern += '(' + rule + ')';
150 return pattern;
151 }
152
153 private static boolean isLocalhost(InetAddress address) {
154 try {
155 if (address.equals(InetAddress.getLocalHost())) {
156 return true;
157 }
158 } catch (UnknownHostException e) {
159 if (logger.isInfoEnabled()) {
160 logger.info("error getting ip of localhost", e);
161 }
162 }
163 try {
164 InetAddress[] addrs = InetAddress.getAllByName("127.0.0.1");
165 for (InetAddress addr : addrs) {
166 if (addr.equals(address)) {
167 return true;
168 }
169 }
170 } catch (UnknownHostException e) {
171 if (logger.isInfoEnabled()) {
172 logger.info("error getting ip of localhost", e);
173 }
174 }
175 return false;
176
177 }
178
179 public int compareTo(Object o) {
180 if (o == null) {
181 return -1;
182 }
183 if (!(o instanceof PatternRule)) {
184 return -1;
185 }
186 PatternRule p = (PatternRule) o;
187 if (p.isAllowRule() && !isAllowRule) {
188 return -1;
189 }
190 if (pattern == null && p.pattern == null) {
191 return 0;
192 }
193 if (pattern != null) {
194 return pattern.compareTo(p.getPattern());
195 }
196 return -1;
197 }
198
199 }