From 5297299677a04d01f96a3c49252ff5a9f2be3f36 Mon Sep 17 00:00:00 2001 From: Ricardo Pinheiro Date: Thu, 13 Jun 2019 11:43:47 +0100 Subject: [PATCH 1/4] Implemented a custom DNS resolver --- .../java/org/java_websocket/DnsResolver.java | 30 ++++++++++++++ .../client/WebSocketClient.java | 39 ++++++++++++++++--- 2 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/java_websocket/DnsResolver.java diff --git a/src/main/java/org/java_websocket/DnsResolver.java b/src/main/java/org/java_websocket/DnsResolver.java new file mode 100644 index 00000000..59f3be3c --- /dev/null +++ b/src/main/java/org/java_websocket/DnsResolver.java @@ -0,0 +1,30 @@ +package org.java_websocket; + +import java.net.InetAddress; +import java.net.URI; +import java.net.UnknownHostException; + +/** + * Users may implement this interface to override the default DNS lookup offered + * by the OS. + * + * @since 1.4.1 + */ +public interface DnsResolver { + + /** + * Resolves the IP address for the given URI. + * + * This method should never return null. If it's not able to resolve the IP + * address then it should throw an UnknownHostException + * + * @param uri The URI to be resolved + * + * @return The resolved IP address + * + * @throws UnknownHostException if no IP address for the uri + * could be found. + */ + InetAddress resolve(URI uri) throws UnknownHostException; + +} diff --git a/src/main/java/org/java_websocket/client/WebSocketClient.java b/src/main/java/org/java_websocket/client/WebSocketClient.java index fc86d813..444416fe 100644 --- a/src/main/java/org/java_websocket/client/WebSocketClient.java +++ b/src/main/java/org/java_websocket/client/WebSocketClient.java @@ -28,10 +28,12 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.Socket; import java.net.URI; +import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.util.Collection; import java.util.Collections; @@ -46,6 +48,7 @@ import javax.net.ssl.SSLSocketFactory; import org.java_websocket.AbstractWebSocket; +import org.java_websocket.DnsResolver; import org.java_websocket.WebSocket; import org.java_websocket.WebSocketImpl; import org.java_websocket.drafts.Draft; @@ -131,6 +134,14 @@ public abstract class WebSocketClient extends AbstractWebSocket implements Runna */ private int connectTimeout = 0; + /** + * DNS resolver that translates a URI to an InetAddress + * + * @see InetAddress + * @since 1.4.1 + */ + private DnsResolver dnsResolver = null; + /** * Constructs a WebSocketClient instance and sets it to the connect to the * specified URI. The channel does not attampt to connect automatically. The connection @@ -195,6 +206,12 @@ public WebSocketClient( URI serverUri , Draft protocolDraft , Map } this.uri = serverUri; this.draft = protocolDraft; + this.dnsResolver = new DnsResolver() { + @Override + public InetAddress resolve(URI uri) throws UnknownHostException { + return InetAddress.getByName(uri.getHost()); + } + }; if(httpHeaders != null) { headers = new TreeMap(String.CASE_INSENSITIVE_ORDER); headers.putAll(httpHeaders); @@ -266,6 +283,17 @@ public void clearHeaders() { headers = null; } + /** + * Sets a custom DNS resolver. + * + * @param dnsResolver The DnsResolver to use. + * + * @since 1.4.1 + */ + public void setDnsResolver(DnsResolver dnsResolver) { + this.dnsResolver = dnsResolver; + } + /** * Reinitiates the websocket connection. This method does not block. * @since 1.3.8 @@ -431,8 +459,9 @@ public void run() { socket.setTcpNoDelay( isTcpNoDelay() ); socket.setReuseAddress( isReuseAddr() ); - if( !socket.isBound() ) { - socket.connect( new InetSocketAddress( uri.getHost(), getPort() ), connectTimeout ); + if (!socket.isBound()) { + InetSocketAddress addr = new InetSocketAddress(dnsResolver.resolve(uri), uri.getPort()); + socket.connect(addr, connectTimeout); } // if the socket is set by others we don't apply any TLS wrapper @@ -509,9 +538,9 @@ private void sendHandshake() throws InvalidHandshakeException { if( part2 != null ) path += '?' + part2; int port = getPort(); - String host = uri.getHost() + ( + String host = uri.getHost() + ( (port != WebSocketImpl.DEFAULT_PORT && port != WebSocketImpl.DEFAULT_WSS_PORT) - ? ":" + port + ? ":" + port : "" ); HandshakeImpl1Client handshake = new HandshakeImpl1Client(); @@ -844,7 +873,7 @@ public InetSocketAddress getLocalSocketAddress() { public InetSocketAddress getRemoteSocketAddress() { return engine.getRemoteSocketAddress(); } - + @Override public String getResourceDescriptor() { return uri.getPath(); From b88de916bdd729de8aedf2d2027ed4ad4b34a048 Mon Sep 17 00:00:00 2001 From: Ricardo Pinheiro Date: Thu, 13 Jun 2019 19:55:50 +0100 Subject: [PATCH 2/4] Use WebSocketClient::getPort instead of URI::getPort --- src/main/java/org/java_websocket/client/WebSocketClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/java_websocket/client/WebSocketClient.java b/src/main/java/org/java_websocket/client/WebSocketClient.java index 444416fe..9b9d4b50 100644 --- a/src/main/java/org/java_websocket/client/WebSocketClient.java +++ b/src/main/java/org/java_websocket/client/WebSocketClient.java @@ -460,7 +460,7 @@ public void run() { socket.setReuseAddress( isReuseAddr() ); if (!socket.isBound()) { - InetSocketAddress addr = new InetSocketAddress(dnsResolver.resolve(uri), uri.getPort()); + InetSocketAddress addr = new InetSocketAddress(dnsResolver.resolve(uri), this.getPort()); socket.connect(addr, connectTimeout); } From 9909046bd1455251aa0b1e092d04b2abfb44f20d Mon Sep 17 00:00:00 2001 From: Ricardo Pinheiro Date: Thu, 13 Jun 2019 20:04:40 +0100 Subject: [PATCH 3/4] Move DnsResolver to org.java_websocket.client and fix code style --- .../java/org/java_websocket/DnsResolver.java | 30 ------------------- .../java_websocket/client/DnsResolver.java | 29 ++++++++++++++++++ .../client/WebSocketClient.java | 1 - 3 files changed, 29 insertions(+), 31 deletions(-) delete mode 100644 src/main/java/org/java_websocket/DnsResolver.java create mode 100644 src/main/java/org/java_websocket/client/DnsResolver.java diff --git a/src/main/java/org/java_websocket/DnsResolver.java b/src/main/java/org/java_websocket/DnsResolver.java deleted file mode 100644 index 59f3be3c..00000000 --- a/src/main/java/org/java_websocket/DnsResolver.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.java_websocket; - -import java.net.InetAddress; -import java.net.URI; -import java.net.UnknownHostException; - -/** - * Users may implement this interface to override the default DNS lookup offered - * by the OS. - * - * @since 1.4.1 - */ -public interface DnsResolver { - - /** - * Resolves the IP address for the given URI. - * - * This method should never return null. If it's not able to resolve the IP - * address then it should throw an UnknownHostException - * - * @param uri The URI to be resolved - * - * @return The resolved IP address - * - * @throws UnknownHostException if no IP address for the uri - * could be found. - */ - InetAddress resolve(URI uri) throws UnknownHostException; - -} diff --git a/src/main/java/org/java_websocket/client/DnsResolver.java b/src/main/java/org/java_websocket/client/DnsResolver.java new file mode 100644 index 00000000..94436fa5 --- /dev/null +++ b/src/main/java/org/java_websocket/client/DnsResolver.java @@ -0,0 +1,29 @@ +package org.java_websocket.client; + +import java.net.InetAddress; +import java.net.URI; +import java.net.UnknownHostException; + +/** + * Users may implement this interface to override the default DNS lookup offered + * by the OS. + * + * @since 1.4.1 + */ +public interface DnsResolver { + + /** + * Resolves the IP address for the given URI. + * + * This method should never return null. If it's not able to resolve the IP + * address then it should throw an UnknownHostException + * + * @param uri The URI to be resolved + * + * @return The resolved IP address + * + * @throws UnknownHostException if no IP address for the uri could be found. + */ + InetAddress resolve(URI uri) throws UnknownHostException; + +} diff --git a/src/main/java/org/java_websocket/client/WebSocketClient.java b/src/main/java/org/java_websocket/client/WebSocketClient.java index 9b9d4b50..f5cd9b12 100644 --- a/src/main/java/org/java_websocket/client/WebSocketClient.java +++ b/src/main/java/org/java_websocket/client/WebSocketClient.java @@ -48,7 +48,6 @@ import javax.net.ssl.SSLSocketFactory; import org.java_websocket.AbstractWebSocket; -import org.java_websocket.DnsResolver; import org.java_websocket.WebSocket; import org.java_websocket.WebSocketImpl; import org.java_websocket.drafts.Draft; From e9c94f22d0df4db4692930722e42ec45e29091bd Mon Sep 17 00:00:00 2001 From: Ricardo Pinheiro Date: Fri, 14 Jun 2019 08:34:42 +0100 Subject: [PATCH 4/4] Add License on DnsResolver.java --- .../java_websocket/client/DnsResolver.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/org/java_websocket/client/DnsResolver.java b/src/main/java/org/java_websocket/client/DnsResolver.java index 94436fa5..016f811b 100644 --- a/src/main/java/org/java_websocket/client/DnsResolver.java +++ b/src/main/java/org/java_websocket/client/DnsResolver.java @@ -1,3 +1,28 @@ +/* + * Copyright (c) 2010-2019 Nathan Rajlich + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + package org.java_websocket.client; import java.net.InetAddress;