forked from AsyncHttpClient/async-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxyServer.java
189 lines (159 loc) · 6.12 KB
/
ProxyServer.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
/*
* Copyright 2010 Ning, Inc.
*
* This program is licensed to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
package org.asynchttpclient.proxy;
import io.netty.handler.codec.http.HttpHeaders;
import org.asynchttpclient.Realm;
import org.asynchttpclient.Request;
import org.jspecify.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import static java.util.Objects.requireNonNull;
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
/**
* Represents a proxy server.
*/
public class ProxyServer {
private final String host;
private final int port;
private final int securedPort;
private final @Nullable Realm realm;
private final List<String> nonProxyHosts;
private final ProxyType proxyType;
private final @Nullable Function<Request, HttpHeaders> customHeaders;
public ProxyServer(String host, int port, int securedPort, @Nullable Realm realm, List<String> nonProxyHosts, ProxyType proxyType,
@Nullable Function<Request, HttpHeaders> customHeaders) {
this.host = host;
this.port = port;
this.securedPort = securedPort;
this.realm = realm;
this.nonProxyHosts = nonProxyHosts;
this.proxyType = proxyType;
this.customHeaders = customHeaders;
}
public ProxyServer(String host, int port, int securedPort, Realm realm, List<String> nonProxyHosts, ProxyType proxyType) {
this(host, port, securedPort, realm, nonProxyHosts, proxyType, null);
}
public String getHost() {
return host;
}
public int getPort() {
return port;
}
public int getSecuredPort() {
return securedPort;
}
public List<String> getNonProxyHosts() {
return nonProxyHosts;
}
public @Nullable Realm getRealm() {
return realm;
}
public ProxyType getProxyType() {
return proxyType;
}
public @Nullable Function<Request, HttpHeaders> getCustomHeaders() {
return customHeaders;
}
/**
* Checks whether proxy should be used according to nonProxyHosts settings of
* it, or we want to go directly to target host. If {@code null} 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.
*
* @param hostname the hostname
* @return true if we have to ignore proxy use (obeying non-proxy hosts
* settings), false otherwise.
* @see <a href=
* "https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html">Networking
* Properties</a>
*/
public boolean isIgnoredForHost(String hostname) {
requireNonNull(hostname, "hostname");
if (isNonEmpty(nonProxyHosts)) {
for (String nonProxyHost : nonProxyHosts) {
if (matchNonProxyHost(hostname, nonProxyHost)) {
return true;
}
}
}
return false;
}
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);
}
public static class Builder {
private final String host;
private final int port;
private int securedPort;
private @Nullable Realm realm;
private @Nullable List<String> nonProxyHosts;
private @Nullable ProxyType proxyType;
private @Nullable Function<Request, HttpHeaders> customHeaders;
public Builder(String host, int port) {
this.host = host;
this.port = port;
securedPort = port;
}
public Builder setSecuredPort(int securedPort) {
this.securedPort = securedPort;
return this;
}
public Builder setRealm(@Nullable Realm realm) {
this.realm = realm;
return this;
}
public Builder setRealm(Realm.Builder realm) {
this.realm = realm.build();
return this;
}
public Builder setNonProxyHost(String nonProxyHost) {
if (nonProxyHosts == null) {
nonProxyHosts = new ArrayList<>(1);
}
nonProxyHosts.add(nonProxyHost);
return this;
}
public Builder setNonProxyHosts(List<String> nonProxyHosts) {
this.nonProxyHosts = nonProxyHosts;
return this;
}
public Builder setProxyType(ProxyType proxyType) {
this.proxyType = proxyType;
return this;
}
public Builder setCustomHeaders(Function<Request, HttpHeaders> customHeaders) {
this.customHeaders = customHeaders;
return this;
}
public ProxyServer build() {
List<String> nonProxyHosts = this.nonProxyHosts != null ? Collections.unmodifiableList(this.nonProxyHosts) : Collections.emptyList();
ProxyType proxyType = this.proxyType != null ? this.proxyType : ProxyType.HTTP;
return new ProxyServer(host, port, securedPort, realm, nonProxyHosts, proxyType, customHeaders);
}
}
}