diff options
author | Brian P. Hinz <bphinz@users.sf.net> | 2019-11-04 20:12:57 -0500 |
---|---|---|
committer | Brian P. Hinz <bphinz@users.sf.net> | 2019-11-04 20:14:53 -0500 |
commit | 512d940e7e0392993446822b7d67adb9aaf374ca (patch) | |
tree | 6cfa590396397c6e1186bfc96c31069e2bcd7663 /java/com/tigervnc/vncviewer | |
parent | ac61fd85b598284e512500699607871939ae3f34 (diff) | |
download | tigervnc-512d940e7e0392993446822b7d67adb9aaf374ca.tar.gz tigervnc-512d940e7e0392993446822b7d67adb9aaf374ca.zip |
Poll local socket to make sure SSH tunnel is ready before connecting
Diffstat (limited to 'java/com/tigervnc/vncviewer')
-rw-r--r-- | java/com/tigervnc/vncviewer/Tunnel.java | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/java/com/tigervnc/vncviewer/Tunnel.java b/java/com/tigervnc/vncviewer/Tunnel.java index 9b8af136..2d0c61f5 100644 --- a/java/com/tigervnc/vncviewer/Tunnel.java +++ b/java/com/tigervnc/vncviewer/Tunnel.java @@ -29,6 +29,8 @@ import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.IOException; +import java.net.*; import java.util.*; import com.tigervnc.rdr.*; @@ -209,13 +211,38 @@ public class Tunnel { try { Thread t = new Thread(new ExtProcess(cmd, vlog, true)); t.start(); - // wait for the ssh process to start - Thread.sleep(1000); + // try for up to 5s + for (int i=0;i<50;i++) { + if (isTunnelReady(localPort)) + return; + else + Thread.sleep(100); + } + throw new Exception("SSH Tunnel not ready"); } catch (java.lang.Exception e) { throw new Exception(e.getMessage()); } } + private static boolean isTunnelReady(int localPort) throws Exception { + // test the local forwarding socket to make + // sure the tunnel is up before connecting + SocketAddress sockAddr = + new InetSocketAddress("localhost", localPort); + java.net.Socket socket = new java.net.Socket(); + boolean ready = false; + try { + socket.connect(sockAddr); + ready = socket.isConnected(); + socket.close(); + } catch (IOException e) { + // expected until tunnel is up + } catch (java.lang.Exception e) { + throw new Exception(e.getMessage()); + } + return ready; + } + private static String fillCmdPattern(String pattern, String gatewayHost, String remoteHost, int remotePort, int localPort) { |