- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFakeDNS.java
More file actions
Latest commit
188 lines (168 loc) · 7.1 KB
/
Copy pathFakeDNS.java
File metadata and controls
188 lines (168 loc) · 7.1 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
packageorg.apache.kudu.client;
importjava.lang.reflect.Field;
importjava.lang.reflect.InvocationHandler;
importjava.lang.reflect.Method;
importjava.lang.reflect.Proxy;
importjava.net.InetAddress;
importjava.net.UnknownHostException;
importjava.util.Arrays;
importjava.util.HashMap;
importjava.util.Map;
importjavax.annotation.concurrent.GuardedBy;
importcom.google.common.base.Throwables;
importcom.google.common.net.InetAddresses;
/**
* Fake DNS resolver which allows our tests to work well even though we use
* strange loopback IP addresses (127.x.y.z) with no corresponding reverse
* DNS.
*
* This overrides the reverse lookups for such IPs to return the same address
* in String form.
*
* Without this class, reverse DNS lookups for such addresses often take
* 5 seconds to return, causing timeouts and overall test slowness.
*
* In the future this class might also be extended to test more interesting
* DNS-related scenarios.
*/
publicclassFakeDNS {
staticFakeDNSinstance = newFakeDNS();
@GuardedBy("this")
privateMap<String, InetAddress> forwardResolutions = newHashMap<>();
@GuardedBy("this")
privateMap<InetAddress, String> reverseResolutions = newHashMap<>();
/** whether the fake resolver has been installed */
@GuardedBy("this")
privatebooleaninstalled = false;
privateFakeDNS() {}
publicstaticFakeDNSgetInstance() {
returninstance;
}
publicsynchronizedvoidaddForwardResolution(Stringhostname, InetAddressip) {
forwardResolutions.put(hostname, ip);
}
publicsynchronizedvoidaddReverseResolution(InetAddressip, Stringhostname) {
reverseResolutions.put(ip, hostname);
}
/**
* Install the fake DNS resolver into the Java runtime.
*/
publicsynchronizedvoidinstall() {
if (installed) return;
try {
try {
// Override the NameService in Java 9 or later.
finalClass<?> nameServiceInterface = Class.forName("java.net.InetAddress$NameService");
Fieldfield = InetAddress.class.getDeclaredField("nameService");
// Get the default NameService to fallback to.
Methodmethod = InetAddress.class.getDeclaredMethod("createNameService");
method.setAccessible(true);
ObjectfallbackNameService = method.invoke(null);
// Create a proxy instance to set on the InetAddress field which will handle
// all NameService calls.
Objectproxy = Proxy.newProxyInstance(nameServiceInterface.getClassLoader(),
newClass<?>[]{nameServiceInterface}, newNameServiceListener(fallbackNameService));
field.setAccessible(true);
field.set(InetAddress.class, proxy);
} catch (finalClassNotFoundException | NoSuchFieldExceptione) {
// Override the NameService in Java 8 or earlier.
finalClass<?> nameServiceInterface = Class.forName("sun.net.spi.nameservice.NameService");
Fieldfield = InetAddress.class.getDeclaredField("nameServices");
// Get the default NameService to fallback to.
Methodmethod = InetAddress.class.getDeclaredMethod("createNSProvider", String.class);
method.setAccessible(true);
ObjectfallbackNameService = method.invoke(null, "default");
// Create a proxy instance to set on the InetAddress field which will handle
// all NameService calls.
Objectproxy = Proxy.newProxyInstance(nameServiceInterface.getClassLoader(),
newClass<?>[]{nameServiceInterface}, newNameServiceListener(fallbackNameService));
field.setAccessible(true);
// Java 8 or earlier takes a list of NameServices
field.set(InetAddress.class, Arrays.asList(proxy));
}
} catch (Exceptione) {
thrownewRuntimeException(e);
}
installed = true;
}
/**
* The NameService in all versions of Java has the same interface, so we
* can use the same InvocationHandler as our proxy instance for both
* java.net.InetAddress$NameService and sun.net.spi.nameservice.NameService.
*/
privateclassNameServiceListenerimplementsInvocationHandler {
privatefinalObjectfallbackNameService;
// Creates a NameServiceListener with a NameService implementation to
// fallback to. The parameter is untyped so we can handle the NameService
// type in all versions of Java with reflection.
NameServiceListener(ObjectfallbackNameService) {
this.fallbackNameService = fallbackNameService;
}
privateInetAddress[] lookupAllHostAddr(Stringhost) throwsUnknownHostException {
InetAddressinetAddress;
synchronized(FakeDNS.this) {
inetAddress = forwardResolutions.get(host);
}
if (inetAddress != null) {
returnnewInetAddress[]{inetAddress};
}
try {
Methodmethod = fallbackNameService.getClass()
.getDeclaredMethod("lookupAllHostAddr", String.class);
method.setAccessible(true);
return (InetAddress[]) method.invoke(fallbackNameService, host);
} catch (ReflectiveOperationExceptione) {
Throwables.propagateIfPossible(e.getCause(), UnknownHostException.class);
thrownewAssertionError("unexpected reflection issue", e);
}
}
privateStringgetHostByAddr(byte[] addr) throwsUnknownHostException {
if (addr[0] == 127) {
returnInetAddresses.toAddrString(InetAddress.getByAddress(addr));
}
Stringhostname;
synchronized (FakeDNS.this) {
hostname = reverseResolutions.get(InetAddress.getByAddress(addr));
}
if (hostname != null) {
returnhostname;
}
try {
Methodmethod = fallbackNameService.getClass()
.getDeclaredMethod("getHostByAddr", byte[].class);
method.setAccessible(true);
return (String) method.invoke(fallbackNameService, (Object) addr);
} catch (ReflectiveOperationExceptione) {
Throwables.propagateIfPossible(e.getCause(), UnknownHostException.class);
thrownewAssertionError("unexpected reflection issue", e);
}
}
@Override
publicObjectinvoke(Objectproxy, Methodmethod, Object[] args) throwsThrowable {
switch (method.getName()) {
case"lookupAllHostAddr":
returnlookupAllHostAddr((String) args[0]);
case"getHostByAddr":
returngetHostByAddr((byte[]) args[0]);
default:
thrownewUnsupportedOperationException();
}
}
}
}