Skip to content

Commit c9e7533

Browse files
authored
Merge pull request TooTallNate#906 from maiph/dns-resolver
Implemented a custom DNS resolver, see TooTallNate#859
2 parents 4c0a555 + e9c94f2 commit c9e7533

2 files changed

Lines changed: 87 additions & 5 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2010-2019 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.java_websocket.client;
27+
28+
import java.net.InetAddress;
29+
import java.net.URI;
30+
import java.net.UnknownHostException;
31+
32+
/**
33+
* Users may implement this interface to override the default DNS lookup offered
34+
* by the OS.
35+
*
36+
* @since 1.4.1
37+
*/
38+
public interface DnsResolver {
39+
40+
/**
41+
* Resolves the IP address for the given URI.
42+
*
43+
* This method should never return null. If it's not able to resolve the IP
44+
* address then it should throw an UnknownHostException
45+
*
46+
* @param uri The URI to be resolved
47+
*
48+
* @return The resolved IP address
49+
*
50+
* @throws UnknownHostException if no IP address for the <code>uri</code> could be found.
51+
*/
52+
InetAddress resolve(URI uri) throws UnknownHostException;
53+
54+
}

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
import java.io.IOException;
2929
import java.io.InputStream;
3030
import java.io.OutputStream;
31+
import java.net.InetAddress;
3132
import java.net.InetSocketAddress;
3233
import java.net.Proxy;
3334
import java.net.Socket;
3435
import java.net.URI;
36+
import java.net.UnknownHostException;
3537
import java.nio.ByteBuffer;
3638
import java.util.Collection;
3739
import java.util.Collections;
@@ -131,6 +133,14 @@ public abstract class WebSocketClient extends AbstractWebSocket implements Runna
131133
*/
132134
private int connectTimeout = 0;
133135

136+
/**
137+
* DNS resolver that translates a URI to an InetAddress
138+
*
139+
* @see InetAddress
140+
* @since 1.4.1
141+
*/
142+
private DnsResolver dnsResolver = null;
143+
134144
/**
135145
* Constructs a WebSocketClient instance and sets it to the connect to the
136146
* specified URI. The channel does not attampt to connect automatically. The connection
@@ -195,6 +205,12 @@ public WebSocketClient( URI serverUri , Draft protocolDraft , Map<String,String>
195205
}
196206
this.uri = serverUri;
197207
this.draft = protocolDraft;
208+
this.dnsResolver = new DnsResolver() {
209+
@Override
210+
public InetAddress resolve(URI uri) throws UnknownHostException {
211+
return InetAddress.getByName(uri.getHost());
212+
}
213+
};
198214
if(httpHeaders != null) {
199215
headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
200216
headers.putAll(httpHeaders);
@@ -266,6 +282,17 @@ public void clearHeaders() {
266282
headers = null;
267283
}
268284

285+
/**
286+
* Sets a custom DNS resolver.
287+
*
288+
* @param dnsResolver The DnsResolver to use.
289+
*
290+
* @since 1.4.1
291+
*/
292+
public void setDnsResolver(DnsResolver dnsResolver) {
293+
this.dnsResolver = dnsResolver;
294+
}
295+
269296
/**
270297
* Reinitiates the websocket connection. This method does not block.
271298
* @since 1.3.8
@@ -431,8 +458,9 @@ public void run() {
431458
socket.setTcpNoDelay( isTcpNoDelay() );
432459
socket.setReuseAddress( isReuseAddr() );
433460

434-
if( !socket.isBound() ) {
435-
socket.connect( new InetSocketAddress( uri.getHost(), getPort() ), connectTimeout );
461+
if (!socket.isBound()) {
462+
InetSocketAddress addr = new InetSocketAddress(dnsResolver.resolve(uri), this.getPort());
463+
socket.connect(addr, connectTimeout);
436464
}
437465

438466
// if the socket is set by others we don't apply any TLS wrapper
@@ -509,9 +537,9 @@ private void sendHandshake() throws InvalidHandshakeException {
509537
if( part2 != null )
510538
path += '?' + part2;
511539
int port = getPort();
512-
String host = uri.getHost() + (
540+
String host = uri.getHost() + (
513541
(port != WebSocketImpl.DEFAULT_PORT && port != WebSocketImpl.DEFAULT_WSS_PORT)
514-
? ":" + port
542+
? ":" + port
515543
: "" );
516544

517545
HandshakeImpl1Client handshake = new HandshakeImpl1Client();
@@ -844,7 +872,7 @@ public InetSocketAddress getLocalSocketAddress() {
844872
public InetSocketAddress getRemoteSocketAddress() {
845873
return engine.getRemoteSocketAddress();
846874
}
847-
875+
848876
@Override
849877
public String getResourceDescriptor() {
850878
return uri.getPath();

0 commit comments

Comments
 (0)