• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc;
18 
19 import java.io.IOException;
20 import java.net.SocketAddress;
21 import javax.annotation.Nullable;
22 
23 /**
24  * A utility class to detect which proxy, if any, should be used for a given
25  * {@link java.net.SocketAddress}. This class performs network requests to resolve address names,
26  * and should only be used in places that are expected to do IO such as the
27  * {@link io.grpc.NameResolver}.
28  *
29  * <h1>How Proxies work in gRPC</h1>
30  *
31  * <p>In order for gRPC to use a proxy, {@link NameResolver}, {@link ProxyDetector} and the
32  * underlying transport need to work together.
33  *
34  * <p>The {@link NameResolver} should invoke the {@link ProxyDetector} retrieved from the {@link
35  * NameResolver.Args#getProxyDetector}, and pass the returned {@link ProxiedSocketAddress} to
36  * {@link NameResolver.Listener#onAddresses}.  The DNS name resolver shipped with gRPC is already
37  * doing so.
38  *
39  * <p>The default {@code ProxyDetector} uses Java's standard {@link java.net.ProxySelector} and
40  * {@link java.net.Authenticator} to detect proxies and authentication credentials and produce
41  * {@link HttpConnectProxiedSocketAddress}, which is for using an HTTP CONNECT proxy.  A custom
42  * {@code ProxyDetector} can be passed to {@link ManagedChannelBuilder#proxyDetector}.
43  *
44  * <p>The {@link ProxiedSocketAddress} is then handled by the transport.  The transport needs to
45  * support whatever type of {@code ProxiedSocketAddress} returned by {@link ProxyDetector}.  The
46  * Netty transport and the OkHttp transport currently only support {@link
47  * HttpConnectProxiedSocketAddress} which is returned by the default {@code ProxyDetector}.
48  */
49 public interface ProxyDetector {
50   /**
51    * Given a target address, returns a proxied address if a proxy should be used. If no proxy should
52    * be used, then return value will be {@code null}.
53    *
54    * <p>If the returned {@code ProxiedSocketAddress} contains any address that needs to be resolved
55    * locally, it should be resolved before it's returned, and this method throws if unable to
56    * resolve it.
57    *
58    * @param targetServerAddress the target address, which is generally unresolved, because the proxy
59    *                            will resolve it.
60    */
61   @Nullable
proxyFor(SocketAddress targetServerAddress)62   ProxiedSocketAddress proxyFor(SocketAddress targetServerAddress) throws IOException;
63 }
64