Skip to content

Commit 5297299

Browse files
committed
Implemented a custom DNS resolver
1 parent f3d299f commit 5297299

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.java_websocket;
2+
3+
import java.net.InetAddress;
4+
import java.net.URI;
5+
import java.net.UnknownHostException;
6+
7+
/**
8+
* Users may implement this interface to override the default DNS lookup offered
9+
* by the OS.
10+
*
11+
* @since 1.4.1
12+
*/
13+
public interface DnsResolver {
14+
15+
/**
16+
* Resolves the IP address for the given URI.
17+
*
18+
* This method should never return null. If it's not able to resolve the IP
19+
* address then it should throw an UnknownHostException
20+
*
21+
* @param uri The URI to be resolved
22+
*
23+
* @return The resolved IP address
24+
*
25+
* @throws UnknownHostException if no IP address for the <code>uri</code>
26+
* could be found.
27+
*/
28+
InetAddress resolve(URI uri) throws UnknownHostException;
29+
30+
}

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

Lines changed: 34 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;
@@ -46,6 +48,7 @@
4648
import javax.net.ssl.SSLSocketFactory;
4749

4850
import org.java_websocket.AbstractWebSocket;
51+
import org.java_websocket.DnsResolver;
4952
import org.java_websocket.WebSocket;
5053
import org.java_websocket.WebSocketImpl;
5154
import org.java_websocket.drafts.Draft;
@@ -131,6 +134,14 @@ public abstract class WebSocketClient extends AbstractWebSocket implements Runna
131134
*/
132135
private int connectTimeout = 0;
133136

137+
/**
138+
* DNS resolver that translates a URI to an InetAddress
139+
*
140+
* @see InetAddress
141+
* @since 1.4.1
142+
*/
143+
private DnsResolver dnsResolver = null;
144+
134145
/**
135146
* Constructs a WebSocketClient instance and sets it to the connect to the
136147
* specified URI. The channel does not attampt to connect automatically. The connection
@@ -195,6 +206,12 @@ public WebSocketClient( URI serverUri , Draft protocolDraft , Map<String,String>
195206
}
196207
this.uri = serverUri;
197208
this.draft = protocolDraft;
209+
this.dnsResolver = new DnsResolver() {
210+
@Override
211+
public InetAddress resolve(URI uri) throws UnknownHostException {
212+
return InetAddress.getByName(uri.getHost());
213+
}
214+
};
198215
if(httpHeaders != null) {
199216
headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
200217
headers.putAll(httpHeaders);
@@ -266,6 +283,17 @@ public void clearHeaders() {
266283
headers = null;
267284
}
268285

286+
/**
287+
* Sets a custom DNS resolver.
288+
*
289+
* @param dnsResolver The DnsResolver to use.
290+
*
291+
* @since 1.4.1
292+
*/
293+
public void setDnsResolver(DnsResolver dnsResolver) {
294+
this.dnsResolver = dnsResolver;
295+
}
296+
269297
/**
270298
* Reinitiates the websocket connection. This method does not block.
271299
* @since 1.3.8
@@ -431,8 +459,9 @@ public void run() {
431459
socket.setTcpNoDelay( isTcpNoDelay() );
432460
socket.setReuseAddress( isReuseAddr() );
433461

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

438467
// if the socket is set by others we don't apply any TLS wrapper
@@ -509,9 +538,9 @@ private void sendHandshake() throws InvalidHandshakeException {
509538
if( part2 != null )
510539
path += '?' + part2;
511540
int port = getPort();
512-
String host = uri.getHost() + (
541+
String host = uri.getHost() + (
513542
(port != WebSocketImpl.DEFAULT_PORT && port != WebSocketImpl.DEFAULT_WSS_PORT)
514-
? ":" + port
543+
? ":" + port
515544
: "" );
516545

517546
HandshakeImpl1Client handshake = new HandshakeImpl1Client();
@@ -844,7 +873,7 @@ public InetSocketAddress getLocalSocketAddress() {
844873
public InetSocketAddress getRemoteSocketAddress() {
845874
return engine.getRemoteSocketAddress();
846875
}
847-
876+
848877
@Override
849878
public String getResourceDescriptor() {
850879
return uri.getPath();

0 commit comments

Comments
 (0)