diff options
author | zpencer <spencerfang@google.com> | 2017-10-17 19:26:11 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-17 19:26:11 -0700 |
commit | 7df9ae97538d73576455b92753d55c58cd23817f (patch) | |
tree | 63fe8d66c18f592da35debc71cc159c472783380 /netty | |
parent | b9f6590084d3820997773c6d26bf4d3aab38aa85 (diff) | |
download | grpc-grpc-java-7df9ae97538d73576455b92753d55c58cd23817f.tar.gz |
core,netty,okhttp: detect proxy via ProxySelector (#3021)
This lets us specify the proxy using `-Dhttps.proxyHost=host -Dhttps.proxyPort=port`
along with auth info like username and password.
Diffstat (limited to 'netty')
4 files changed, 42 insertions, 23 deletions
diff --git a/netty/src/main/java/io/grpc/netty/InternalNettyChannelBuilder.java b/netty/src/main/java/io/grpc/netty/InternalNettyChannelBuilder.java index 9c1ad8983..a1bce2867 100644 --- a/netty/src/main/java/io/grpc/netty/InternalNettyChannelBuilder.java +++ b/netty/src/main/java/io/grpc/netty/InternalNettyChannelBuilder.java @@ -17,6 +17,7 @@ package io.grpc.netty; import io.grpc.Internal; +import io.grpc.internal.ProxyParameters; import java.net.SocketAddress; /** @@ -44,7 +45,8 @@ public final class InternalNettyChannelBuilder { extends NettyChannelBuilder.TransportCreationParamsFilterFactory { @Override TransportCreationParamsFilter create( - SocketAddress targetServerAddress, String authority, String userAgent); + SocketAddress targetServerAddress, String authority, String userAgent, + ProxyParameters proxy); } /** diff --git a/netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java b/netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java index 9a023ba11..a97a9600a 100644 --- a/netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java +++ b/netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java @@ -36,6 +36,7 @@ import io.grpc.internal.ClientTransportFactory; import io.grpc.internal.ConnectionClientTransport; import io.grpc.internal.GrpcUtil; import io.grpc.internal.KeepAliveManager; +import io.grpc.internal.ProxyParameters; import io.grpc.internal.SharedResourceHolder; import io.netty.channel.Channel; import io.netty.channel.ChannelOption; @@ -341,18 +342,13 @@ public final class NettyChannelBuilder static ProtocolNegotiator createProtocolNegotiator( String authority, NegotiationType negotiationType, - SslContext sslContext) { + SslContext sslContext, + ProxyParameters proxy) { ProtocolNegotiator negotiator = createProtocolNegotiatorByType(authority, negotiationType, sslContext); - String proxy = System.getenv("GRPC_PROXY_EXP"); if (proxy != null) { - String[] parts = proxy.split(":", 2); - int port = 80; - if (parts.length > 1) { - port = Integer.parseInt(parts[1]); - } - InetSocketAddress proxyAddress = new InetSocketAddress(parts[0], port); - negotiator = ProtocolNegotiators.httpProxy(proxyAddress, null, null, negotiator); + negotiator = ProtocolNegotiators.httpProxy( + proxy.proxyAddress, proxy.username, proxy.password, negotiator); } return negotiator; } @@ -406,7 +402,10 @@ public final class NettyChannelBuilder interface TransportCreationParamsFilterFactory { @CheckReturnValue TransportCreationParamsFilter create( - SocketAddress targetServerAddress, String authority, @Nullable String userAgent); + SocketAddress targetServerAddress, + String authority, + @Nullable String userAgent, + @Nullable ProxyParameters proxy); } @CheckReturnValue @@ -472,11 +471,12 @@ public final class NettyChannelBuilder @Override public ConnectionClientTransport newClientTransport( - SocketAddress serverAddress, String authority, @Nullable String userAgent) { + SocketAddress serverAddress, String authority, @Nullable String userAgent, + @Nullable ProxyParameters proxy) { checkState(!closed, "The transport factory is closed."); TransportCreationParamsFilter dparams = - transportCreationParamsFilterFactory.create(serverAddress, authority, userAgent); + transportCreationParamsFilterFactory.create(serverAddress, authority, userAgent, proxy); final AtomicBackoff.State keepAliveTimeNanosState = keepAliveTimeNanos.getState(); Runnable tooManyPingsRunnable = new Runnable() { @@ -528,8 +528,12 @@ public final class NettyChannelBuilder @Override public TransportCreationParamsFilter create( - SocketAddress targetServerAddress, String authority, String userAgent) { - return new DynamicNettyTransportParams(targetServerAddress, authority, userAgent); + SocketAddress targetServerAddress, + String authority, + String userAgent, + ProxyParameters proxyParams) { + return new DynamicNettyTransportParams( + targetServerAddress, authority, userAgent, proxyParams); } @CheckReturnValue @@ -538,12 +542,17 @@ public final class NettyChannelBuilder private final SocketAddress targetServerAddress; private final String authority; @Nullable private final String userAgent; + private ProxyParameters proxyParams; private DynamicNettyTransportParams( - SocketAddress targetServerAddress, String authority, String userAgent) { + SocketAddress targetServerAddress, + String authority, + String userAgent, + ProxyParameters proxyParams) { this.targetServerAddress = targetServerAddress; this.authority = authority; this.userAgent = userAgent; + this.proxyParams = proxyParams; } @Override @@ -563,7 +572,7 @@ public final class NettyChannelBuilder @Override public ProtocolNegotiator getProtocolNegotiator() { - return createProtocolNegotiator(authority, negotiationType, sslContext); + return createProtocolNegotiator(authority, negotiationType, sslContext, proxyParams); } } } diff --git a/netty/src/test/java/io/grpc/netty/NettyChannelBuilderTest.java b/netty/src/test/java/io/grpc/netty/NettyChannelBuilderTest.java index 8bcfad034..2198f1c02 100644 --- a/netty/src/test/java/io/grpc/netty/NettyChannelBuilderTest.java +++ b/netty/src/test/java/io/grpc/netty/NettyChannelBuilderTest.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import io.grpc.ManagedChannel; +import io.grpc.internal.ProxyParameters; import io.grpc.netty.InternalNettyChannelBuilder.OverrideAuthorityChecker; import io.grpc.netty.ProtocolNegotiators.TlsNegotiator; import io.netty.handler.ssl.SslContext; @@ -39,6 +40,7 @@ public class NettyChannelBuilderTest { @Rule public final ExpectedException thrown = ExpectedException.none(); private final SslContext noSslContext = null; + private final ProxyParameters noProxy = null; private void shutdown(ManagedChannel mc) throws Exception { mc.shutdownNow(); @@ -141,7 +143,8 @@ public class NettyChannelBuilderTest { ProtocolNegotiator negotiator = NettyChannelBuilder.createProtocolNegotiator( "authority", NegotiationType.PLAINTEXT, - noSslContext); + noSslContext, + noProxy); // just check that the classes are the same, and that negotiator is not null. assertTrue(negotiator instanceof ProtocolNegotiators.PlaintextNegotiator); } @@ -151,7 +154,8 @@ public class NettyChannelBuilderTest { ProtocolNegotiator negotiator = NettyChannelBuilder.createProtocolNegotiator( "authority", NegotiationType.PLAINTEXT_UPGRADE, - noSslContext); + noSslContext, + noProxy); // just check that the classes are the same, and that negotiator is not null. assertTrue(negotiator instanceof ProtocolNegotiators.PlaintextUpgradeNegotiator); } @@ -162,7 +166,8 @@ public class NettyChannelBuilderTest { NettyChannelBuilder.createProtocolNegotiator( "authority:1234", NegotiationType.TLS, - noSslContext); + noSslContext, + noProxy); } @Test @@ -170,7 +175,8 @@ public class NettyChannelBuilderTest { ProtocolNegotiator negotiator = NettyChannelBuilder.createProtocolNegotiator( "authority:1234", NegotiationType.TLS, - GrpcSslContexts.forClient().build()); + GrpcSslContexts.forClient().build(), + noProxy); assertTrue(negotiator instanceof ProtocolNegotiators.TlsNegotiator); ProtocolNegotiators.TlsNegotiator n = (TlsNegotiator) negotiator; @@ -184,7 +190,8 @@ public class NettyChannelBuilderTest { ProtocolNegotiator negotiator = NettyChannelBuilder.createProtocolNegotiator( "bad_authority", NegotiationType.TLS, - GrpcSslContexts.forClient().build()); + GrpcSslContexts.forClient().build(), + noProxy); assertTrue(negotiator instanceof ProtocolNegotiators.TlsNegotiator); ProtocolNegotiators.TlsNegotiator n = (TlsNegotiator) negotiator; diff --git a/netty/src/test/java/io/grpc/netty/NettyTransportTest.java b/netty/src/test/java/io/grpc/netty/NettyTransportTest.java index 22f16750b..8d2a528a0 100644 --- a/netty/src/test/java/io/grpc/netty/NettyTransportTest.java +++ b/netty/src/test/java/io/grpc/netty/NettyTransportTest.java @@ -76,7 +76,8 @@ public class NettyTransportTest extends AbstractTransportTest { return clientFactory.newClientTransport( new InetSocketAddress("localhost", port), testAuthority(server), - null /* agent */); + null /* agent */, + null /* proxy */); } @Test |