/
ProxyUtils.java
249 lines (216 loc) · 9.46 KB
/
ProxyUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package org.asynchttpclient.util;
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
import org.asynchttpclient.AsyncHttpClientConfig;
import org.asynchttpclient.Request;
import org.asynchttpclient.proxy.ProxyServer;
import org.asynchttpclient.proxy.ProxyServerSelector;
import org.asynchttpclient.proxy.ProxyServer.Protocol;
import org.asynchttpclient.uri.Uri;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Properties;
/**
* Utilities for Proxy handling.
*
* @author cstamas
*/
public final class ProxyUtils {
private final static Logger log = LoggerFactory.getLogger(ProxyUtils.class);
private static final String PROPERTY_PREFIX = "org.asynchttpclient.AsyncHttpClientConfig.proxy.";
/**
* The host to use as proxy.
*/
public static final String PROXY_HOST = "http.proxyHost";
/**
* The port to use for the proxy.
*/
public static final String PROXY_PORT = "http.proxyPort";
/**
* The protocol to use. Is mapped to the {@link Protocol} enum.
*/
public static final String PROXY_PROTOCOL = PROPERTY_PREFIX + "protocol";
/**
* A specification of non-proxy hosts. See http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html
*/
public static final String PROXY_NONPROXYHOSTS = "http.nonProxyHosts";
/**
* The username to use for authentication for the proxy server.
*/
public static final String PROXY_USER = PROPERTY_PREFIX + "user";
/**
* The password to use for authentication for the proxy server.
*/
public static final String PROXY_PASSWORD = PROPERTY_PREFIX + "password";
private ProxyUtils() {
}
/**
* @param config the global config
* @param request the request
* @return the proxy server to be used for this request (can be null)
*/
public static ProxyServer getProxyServer(AsyncHttpClientConfig config, Request request) {
ProxyServer proxyServer = request.getProxyServer();
if (proxyServer == null) {
ProxyServerSelector selector = config.getProxyServerSelector();
if (selector != null) {
proxyServer = selector.select(request.getUri());
}
}
return ProxyUtils.avoidProxy(proxyServer, request) ? null : proxyServer;
}
/**
* @see #ignoreProxy(ProxyServer, String)
*/
public static boolean avoidProxy(final ProxyServer proxyServer, final Request request) {
return ignoreProxy(proxyServer, request.getUri().getHost());
}
private static boolean matchNonProxyHost(String targetHost, String nonProxyHost) {
if (nonProxyHost.length() > 1) {
if (nonProxyHost.charAt(0) == '*')
return targetHost.regionMatches(true, targetHost.length() - nonProxyHost.length() + 1, nonProxyHost, 1,
nonProxyHost.length() - 1);
else if (nonProxyHost.charAt(nonProxyHost.length() - 1) == '*')
return targetHost.regionMatches(true, 0, nonProxyHost, 0, nonProxyHost.length() - 1);
}
return nonProxyHost.equalsIgnoreCase(targetHost);
}
/**
* Checks whether proxy should be used according to nonProxyHosts settings of it, or we want to go directly to
* target host. If <code>null</code> proxy is passed in, this method returns true -- since there is NO proxy, we
* should avoid to use it. Simple hostname pattern matching using "*" are supported, but only as prefixes.
* See http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html
*
* @param proxyServer
* @param hostname the hostname
* @return true if we have to ignore proxy use (obeying non-proxy hosts settings), false otherwise.
*/
public static boolean ignoreProxy(final ProxyServer proxyServer, final String hostname) {
if (proxyServer != null) {
if (hostname == null)
throw new NullPointerException("hostname");
List<String> nonProxyHosts = proxyServer.getNonProxyHosts();
if (isNonEmpty(nonProxyHosts)) {
for (String nonProxyHost : nonProxyHosts) {
if (matchNonProxyHost(hostname, nonProxyHost))
return true;
}
}
return false;
} else {
return true;
}
}
/**
* Creates a proxy server instance from the given properties.
* Currently the default http.* proxy properties are supported as well as properties specific for AHC.
*
* @param properties the properties to evaluate. Must not be null.
* @return a ProxyServer instance or null, if no valid properties were set.
* @see <a href="http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html">Networking Properties</a>
* @see #PROXY_HOST
* @see #PROXY_PORT
* @see #PROXY_PROTOCOL
* @see #PROXY_NONPROXYHOSTS
*/
public static ProxyServerSelector createProxyServerSelector(Properties properties) {
String host = properties.getProperty(PROXY_HOST);
if (host != null) {
int port = Integer.valueOf(properties.getProperty(PROXY_PORT, "80"));
Protocol protocol;
try {
protocol = Protocol.valueOf(properties.getProperty(PROXY_PROTOCOL, "HTTP"));
} catch (IllegalArgumentException e) {
protocol = Protocol.HTTP;
}
ProxyServer proxyServer = new ProxyServer(protocol, host, port, properties.getProperty(PROXY_USER),
properties.getProperty(PROXY_PASSWORD));
String nonProxyHosts = properties.getProperty(PROXY_NONPROXYHOSTS);
if (nonProxyHosts != null) {
for (String spec : nonProxyHosts.split("\\|")) {
proxyServer.addNonProxyHost(spec);
}
}
return createProxyServerSelector(proxyServer);
}
return ProxyServerSelector.NO_PROXY_SELECTOR;
}
/**
* Get a proxy server selector based on the JDK default proxy selector.
*
* @return The proxy server selector.
*/
public static ProxyServerSelector getJdkDefaultProxyServerSelector() {
return createProxyServerSelector(ProxySelector.getDefault());
}
/**
* Create a proxy server selector based on the passed in JDK proxy selector.
*
* @param proxySelector The proxy selector to use. Must not be null.
* @return The proxy server selector.
*/
public static ProxyServerSelector createProxyServerSelector(final ProxySelector proxySelector) {
return new ProxyServerSelector() {
public ProxyServer select(Uri uri) {
try {
URI javaUri = uri.toJavaNetURI();
List<Proxy> proxies = proxySelector.select(javaUri);
if (proxies != null) {
// Loop through them until we find one that we know how to use
for (Proxy proxy : proxies) {
switch (proxy.type()) {
case HTTP:
if (!(proxy.address() instanceof InetSocketAddress)) {
log.warn("Don't know how to connect to address " + proxy.address());
return null;
} else {
InetSocketAddress address = (InetSocketAddress) proxy.address();
return new ProxyServer(Protocol.HTTP, address.getHostName(), address.getPort());
}
case DIRECT:
return null;
default:
log.warn("ProxySelector returned proxy type that we don't know how to use: " + proxy.type());
break;
}
}
}
return null;
} catch (URISyntaxException e) {
log.warn(uri + " couldn't be turned into a java.net.URI", e);
return null;
}
}
};
}
/**
* Create a proxy server selector that always selects a single proxy server.
*
* @param proxyServer The proxy server to select.
* @return The proxy server selector.
*/
public static ProxyServerSelector createProxyServerSelector(final ProxyServer proxyServer) {
return new ProxyServerSelector() {
public ProxyServer select(Uri uri) {
return proxyServer;
}
};
}
}