Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Update Netty proxy settings #5071

Merged
merged 2 commits into from
Jun 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -19,6 +19,9 @@
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
@@ -45,6 +48,7 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
@@ -53,16 +57,20 @@
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpChunkedInput;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.proxy.HttpProxyHandler;
import io.netty.handler.proxy.ProxyConnectionEvent;
import io.netty.handler.proxy.ProxyHandler;
import io.netty.handler.ssl.ApplicationProtocolConfig;
import io.netty.handler.ssl.ClientAuth;
import io.netty.handler.ssl.IdentityCipherSuiteFilter;
@@ -223,6 +231,8 @@ protected void execute(final ClientRequest jerseyRequest, final Set<URI> redirec
}
}

Integer connectTimeout = jerseyRequest.resolveProperty(ClientProperties.CONNECT_TIMEOUT, 0);

if (chan == null) {
Bootstrap b = new Bootstrap();
b.group(group)
@@ -246,8 +256,29 @@ protected void initChannel(SocketChannel ch) throws Exception {

InetSocketAddress proxyAddr = new InetSocketAddress(u.getHost(),
u.getPort() == -1 ? 8080 : u.getPort());
p.addLast(userName == null ? new HttpProxyHandler(proxyAddr)
: new HttpProxyHandler(proxyAddr, userName, password));
ProxyHandler proxy = createProxyHandler(jerseyRequest, proxyAddr, userName, password, connectTimeout);
p.addLast(proxy);
} else {
ProxySelector sel = ProxySelector.getDefault();
for (Proxy proxy: sel.select(requestUri)) {
if (Proxy.Type.HTTP.equals(proxy.type())) {
SocketAddress proxyAddress = proxy.address();
if (InetSocketAddress.class.isInstance(proxy.address())) {
InetSocketAddress proxyAddr = (InetSocketAddress) proxyAddress;
if (proxyAddr.isUnresolved()
&& proxyAddr.getHostName() != null
&& proxyAddr.getHostName().startsWith("http://")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that starts by https?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, JDK does not support HTTPS proxy

proxyAddress = new InetSocketAddress(
proxyAddr.getHostString().substring(7), proxyAddr.getPort()
);
}
}
ProxyHandler proxyHandler
= createProxyHandler(jerseyRequest, proxyAddress, null, null, connectTimeout);
p.addLast(proxyHandler);
break;
}
}
}

// Enable HTTPS if necessary.
@@ -284,7 +315,6 @@ protected void initChannel(SocketChannel ch) throws Exception {
});

// connect timeout
Integer connectTimeout = jerseyRequest.resolveProperty(ClientProperties.CONNECT_TIMEOUT, 0);
if (connectTimeout > 0) {
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
}
@@ -357,9 +387,7 @@ protected void initChannel(SocketChannel ch) throws Exception {
}

// headers
for (final Map.Entry<String, List<String>> e : jerseyRequest.getStringHeaders().entrySet()) {
nettyRequest.headers().add(e.getKey(), e.getValue());
}
setHeaders(jerseyRequest, nettyRequest.headers());

// host header - http 1.1
nettyRequest.headers().add(HttpHeaderNames.HOST, jerseyRequest.getUri().getHost());
@@ -484,4 +512,24 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
}
}
}

private static ProxyHandler createProxyHandler(ClientRequest jerseyRequest, SocketAddress proxyAddr,
String userName, String password, long connectTimeout) {
HttpHeaders httpHeaders = setHeaders(jerseyRequest, new DefaultHttpHeaders());

ProxyHandler proxy = userName == null ? new HttpProxyHandler(proxyAddr, httpHeaders)
: new HttpProxyHandler(proxyAddr, userName, password, httpHeaders);
if (connectTimeout > 0) {
proxy.setConnectTimeoutMillis(connectTimeout);
}

return proxy;
}

private static HttpHeaders setHeaders(ClientRequest jerseyRequest, HttpHeaders headers) {
for (final Map.Entry<String, List<String>> e : jerseyRequest.getStringHeaders().entrySet()) {
headers.add(e.getKey(), e.getValue());
}
return headers;
}
}
Loading