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+
packageorg.java_websocket;
2+
3+
importjava.net.InetAddress;
4+
importjava.net.URI;
5+
importjava.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+
publicinterfaceDnsResolver{
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+
InetAddressresolve(URIuri) throwsUnknownHostException;
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
importjava.io.IOException;
2929
importjava.io.InputStream;
3030
importjava.io.OutputStream;
31+
importjava.net.InetAddress;
3132
importjava.net.InetSocketAddress;
3233
importjava.net.Proxy;
3334
importjava.net.Socket;
3435
importjava.net.URI;
36+
importjava.net.UnknownHostException;
3537
importjava.nio.ByteBuffer;
3638
importjava.util.Collection;
3739
importjava.util.Collections;
@@ -46,6 +48,7 @@
4648
importjavax.net.ssl.SSLSocketFactory;
4749

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

137+
/**
138+
* DNS resolver that translates a URI to an InetAddress
139+
*
140+
* @see InetAddress
141+
* @since 1.4.1
142+
*/
143+
privateDnsResolverdnsResolver = 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 = newDnsResolver(){
210+
@Override
211+
publicInetAddressresolve(URIuri) throwsUnknownHostException{
212+
returnInetAddress.getByName(uri.getHost());
213+
}
214+
};
198215
if(httpHeaders != null){
199216
headers = newTreeMap<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+
publicvoidsetDnsResolver(DnsResolverdnsResolver){
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( newInetSocketAddress( uri.getHost(), getPort() ), connectTimeout );
462+
if (!socket.isBound()){
463+
InetSocketAddressaddr = newInetSocketAddress(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
intport = getPort();
512-
Stringhost = uri.getHost() + (
541+
Stringhost = uri.getHost() + (
513542
(port != WebSocketImpl.DEFAULT_PORT && port != WebSocketImpl.DEFAULT_WSS_PORT)
514-
? ":" + port
543+
? ":" + port
515544
: "" );
516545

517546
HandshakeImpl1Clienthandshake = newHandshakeImpl1Client();
@@ -844,7 +873,7 @@ public InetSocketAddress getLocalSocketAddress(){
844873
publicInetSocketAddressgetRemoteSocketAddress(){
845874
returnengine.getRemoteSocketAddress();
846875
}
847-
876+
848877
@Override
849878
publicStringgetResourceDescriptor(){
850879
returnuri.getPath();

0 commit comments

Comments
(0)