From 73f8bee53d7b83ab451b5904d16cd30139411c86 Mon Sep 17 00:00:00 2001 From: Jeremy Norris Date: Mon, 11 Dec 2023 13:18:15 -0600 Subject: [PATCH 1/2] #450 use Socket.connect() with a timeout that has been supported since Java 1.4 instead of using old method of creating a separate thread and joining to that thread with timeout. --- src/main/java/com/jcraft/jsch/Util.java | 61 ++++++------------------- 1 file changed, 14 insertions(+), 47 deletions(-) diff --git a/src/main/java/com/jcraft/jsch/Util.java b/src/main/java/com/jcraft/jsch/Util.java index 85628a19..dde331e7 100644 --- a/src/main/java/com/jcraft/jsch/Util.java +++ b/src/main/java/com/jcraft/jsch/Util.java @@ -26,11 +26,13 @@ package com.jcraft.jsch; -import java.net.Socket; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketTimeoutException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Vector; @@ -369,55 +371,20 @@ static boolean array_equals(byte[] foo, byte bar[]) { } static Socket createSocket(String host, int port, int timeout) throws JSchException { - Socket socket = null; - if (timeout == 0) { - try { - socket = new Socket(host, port); - return socket; - } catch (Exception e) { - String message = e.toString(); - throw new JSchException(message, e); - } - } - final String _host = host; - final int _port = port; - final Socket[] sockp = new Socket[1]; - final Exception[] ee = new Exception[1]; - String message = ""; - Thread tmp = new Thread(() -> { - sockp[0] = null; - try { - sockp[0] = new Socket(_host, _port); - } catch (Exception e) { - ee[0] = e; - if (sockp[0] != null && sockp[0].isConnected()) { - try { - sockp[0].close(); - } catch (Exception eee) { - } - } - sockp[0] = null; - } - }); - tmp.setName("Opening Socket " + host); - tmp.start(); + Socket socket = new Socket(); try { - tmp.join(timeout); - message = "timeout: "; - } catch (InterruptedException eee) { - } - if (sockp[0] != null && sockp[0].isConnected()) { - socket = sockp[0]; - } else { - message += "socket is not established"; - if (ee[0] != null) { - message = ee[0].toString(); + socket.connect(new InetSocketAddress(host, port), timeout); + return socket; + } catch (Exception e) { + try { + socket.close(); + } catch (Exception ignore) { } - tmp.interrupt(); - tmp = null; - throw new JSchException(message, ee[0]); + + String message = + e instanceof SocketTimeoutException ? "timeout: socket is not established" : e.toString(); + throw new JSchException(message, e); } - return socket; } static byte[] str2byte(String str, Charset encoding) { From 5d73d5d5128c7d6335fac0b3bed10a95d5301227 Mon Sep 17 00:00:00 2001 From: Jeremy Norris Date: Mon, 11 Dec 2023 13:18:35 -0600 Subject: [PATCH 2/2] Update dependencies. --- .mvn/wrapper/maven-wrapper.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties index eacdc9ed..346d645f 100644 --- a/.mvn/wrapper/maven-wrapper.properties +++ b/.mvn/wrapper/maven-wrapper.properties @@ -14,5 +14,5 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.5/apache-maven-3.9.5-bin.zip +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar