Skip to content

Commit

Permalink
Update Netty proxy settings
Browse files Browse the repository at this point in the history
Allow to pass headers to the proxy
Allow to set proxy using the JDK -Dhttp.proxyHost setting

Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol committed May 27, 2022
1 parent 31f6557 commit 0605efd
Show file tree
Hide file tree
Showing 4 changed files with 412 additions and 153 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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().equals(Proxy.Type.HTTP)) {
SocketAddress proxyAddress = proxy.address();
if (InetSocketAddress.class.isInstance(proxy.address())) {
InetSocketAddress proxyAddr = (InetSocketAddress) proxyAddress;
if (proxyAddr.isUnresolved()
&& proxyAddr.getHostName() != null
&& proxyAddr.getHostName().startsWith("http://")) {
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.
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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

0 comments on commit 0605efd

Please sign in to comment.