-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to exclude host from being proxying #2600 #2614
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,14 @@ | |
class ProxyOptionsConverter { | ||
|
||
static void fromJson(JsonObject json, ProxyOptions obj) { | ||
if (json.getValue("excludedHosts") instanceof JsonArray) { | ||
java.util.LinkedHashSet<java.lang.String> list = new java.util.LinkedHashSet<>(); | ||
json.getJsonArray("excludedHosts").forEach( item -> { | ||
if (item instanceof String) | ||
list.add((String)item); | ||
}); | ||
obj.setExcludedHosts(list); | ||
} | ||
if (json.getValue("host") instanceof String) { | ||
obj.setHost((String)json.getValue("host")); | ||
} | ||
|
@@ -45,6 +53,11 @@ static void fromJson(JsonObject json, ProxyOptions obj) { | |
} | ||
|
||
static void toJson(ProxyOptions obj, JsonObject json) { | ||
if (obj.getExcludedHosts() != null) { | ||
JsonArray array = new JsonArray(); | ||
obj.getExcludedHosts().forEach(item -> array.add(item)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you could use |
||
json.put("excludedHosts", array); | ||
} | ||
if (obj.getHost() != null) { | ||
json.put("host", obj.getHost()); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
import io.vertx.core.http.impl.pool.ConnectionListener; | ||
import io.vertx.core.http.impl.pool.ConnectionProvider; | ||
import io.vertx.core.impl.ContextImpl; | ||
import io.vertx.core.net.ProxyOptions; | ||
import io.vertx.core.net.ProxyType; | ||
import io.vertx.core.net.SocketAddress; | ||
import io.vertx.core.net.impl.ChannelProvider; | ||
|
@@ -116,6 +117,13 @@ public void deactivate(HttpClientConnection conn) { | |
} | ||
} | ||
|
||
private boolean isExcluded(ProxyOptions options, String targetProxy) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this as it's no longer needed; see comment for |
||
if(options.getExcludedHosts() == null || options.getExcludedHosts().isEmpty()) { | ||
return false; | ||
} | ||
return options.getExcludedHosts().contains(targetProxy); | ||
} | ||
|
||
private void doConnect( | ||
ConnectionListener<HttpClientConnection> listener, | ||
ContextImpl context, | ||
|
@@ -129,7 +137,7 @@ private void doConnect( | |
|
||
ChannelProvider channelProvider; | ||
// http proxy requests are handled in HttpClientImpl, everything else can use netty proxy handler | ||
if (options.getProxyOptions() == null || !ssl && options.getProxyOptions().getType()== ProxyType.HTTP ) { | ||
if (options.getProxyOptions() == null || !ssl && options.getProxyOptions().getType() == ProxyType.HTTP || isExcluded(options.getProxyOptions(), host)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment for |
||
channelProvider = ChannelProvider.INSTANCE; | ||
} else { | ||
channelProvider = ProxyChannelProvider.INSTANCE; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1005,7 +1005,7 @@ private HttpClientRequest createRequest(HttpMethod method, String protocol, Stri | |
} | ||
checkClosed(); | ||
HttpClientRequest req; | ||
boolean useProxy = !useSSL && proxyType == ProxyType.HTTP; | ||
boolean useProxy = useProxy(useSSL, options.getProxyOptions(), host); | ||
if (useProxy) { | ||
final int defaultPort = protocol.equals("ftp") ? 21 : 80; | ||
final String addPort = (port != -1 && port != defaultPort) ? (":" + port) : ""; | ||
|
@@ -1030,6 +1030,17 @@ private HttpClientRequest createRequest(HttpMethod method, String protocol, Stri | |
return req; | ||
} | ||
|
||
private boolean useProxy(boolean isSsl, ProxyOptions proxyOptions, String targetHost) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This functionality is duplicated in |
||
if(!isSsl && proxyType == ProxyType.HTTP) { | ||
if(proxyOptions.getExcludedHosts().isEmpty()) { | ||
return true; | ||
} else { | ||
return !proxyOptions.getExcludedHosts().contains(targetHost); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private synchronized void checkClosed() { | ||
if (closed) { | ||
throw new IllegalStateException("Client is closed"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
|
||
package io.vertx.core.net; | ||
|
||
import java.util.Objects; | ||
import java.util.*; | ||
|
||
import io.vertx.codegen.annotations.DataObject; | ||
import io.vertx.core.json.JsonObject; | ||
|
@@ -46,6 +46,7 @@ public class ProxyOptions { | |
private String username; | ||
private String password; | ||
private ProxyType type; | ||
private Set<String> exludedHosts; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo - please rename to |
||
|
||
/** | ||
* Default constructor. | ||
|
@@ -54,6 +55,7 @@ public ProxyOptions() { | |
host = DEFAULT_HOST; | ||
port = DEFAULT_PORT; | ||
type = DEFAULT_TYPE; | ||
exludedHosts = new HashSet<>(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
} | ||
|
||
/** | ||
|
@@ -67,6 +69,7 @@ public ProxyOptions(ProxyOptions other) { | |
username = other.getUsername(); | ||
password = other.getPassword(); | ||
type = other.getType(); | ||
exludedHosts = other.getExcludedHosts(); | ||
} | ||
|
||
/** | ||
|
@@ -199,6 +202,39 @@ public ProxyOptions setType(ProxyType type) { | |
return this; | ||
} | ||
|
||
/** | ||
* Set list of excluded hosts. | ||
* | ||
* @param excludedHosts list of hosts to exclude from proxy logic | ||
* @return a reference to this, so the API can be used fluently | ||
*/ | ||
public ProxyOptions setExcludedHosts(final Set<String> excludedHosts) { | ||
Objects.requireNonNull(excludedHosts, "Excluded host cannot be null"); | ||
this.exludedHosts = excludedHosts; | ||
return this; | ||
} | ||
|
||
/** | ||
* Add host to existing excludedHost list. | ||
* | ||
* @param excludedHost host excluded from proxy logic | ||
* @return a reference to this, so the API can be used fluently | ||
*/ | ||
public ProxyOptions addExcludedHost(final String excludedHost) { | ||
Objects.requireNonNull(excludedHost, "Excluded host cannot be null"); | ||
this.exludedHosts.add(excludedHost); | ||
return this; | ||
} | ||
|
||
/** | ||
* Get excluded hosts. | ||
* | ||
* @return list of hosts that should not be proxied. | ||
*/ | ||
public Set<String> getExcludedHosts() { | ||
return exludedHosts; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
|
@@ -212,6 +248,7 @@ public boolean equals(Object o) { | |
if (port != that.port) return false; | ||
if (!Objects.equals(password, that.password)) return false; | ||
if (!Objects.equals(username, that.username)) return false; | ||
if (!Objects.equals(exludedHosts, that.exludedHosts)) return false; | ||
|
||
return true; | ||
} | ||
|
@@ -224,6 +261,7 @@ public int hashCode() { | |
result = 31 * result + port; | ||
result = 31 * result + (password != null ? password.hashCode() : 0); | ||
result = 31 * result + (username != null ? username.hashCode() : 0); | ||
result = 31 * result + exludedHosts.hashCode(); | ||
return result; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove all changes to this file.