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