Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to configure connection timeout #94

Open
morco opened this issue Apr 13, 2023 · 1 comment
Open

Make it possible to configure connection timeout #94

morco opened this issue Apr 13, 2023 · 1 comment

Comments

@morco
Copy link

morco commented Apr 13, 2023

Big fan of this project, works very good in general, however one thing I found painfully missing when working with it is the possibility to set a custom value for the connection timeout. The wrapped upstream library JSch allows this principally, but unfortunately as far as I can tell not as a configuration map option, but only by directly manipulating the session object like this:

session.connect(int connectTimeout)

Or probably better like this:

session.setTimeout(int timeout)

To my understanding it is atm not possible to make sshoogr set this timeout parameter in clean fashion. You actually expose the session object inside the session delegate which is quite nice, but unfortunately doesn't help here as for setting the timeout to be meaningful it must obviously happen between the time frame of the session object being created and connect method being called on it, which is atm a "solid, non-interferable" block.
I think a workable solution could look similar to this monkey-patched version of the SessionDelegate connect method we use as a workaround for the moment:

void connect_with_to() {
  try {
    if (session == null || !session.connected || changed) {

      disconnect()

      if (host == null) {
        throw new SshException('Host is required.')
      }
      if (username == null) {
        throw new SshException('Username is required.')
      }
      if (keyFile == null && password == null) {
        throw new SshException('Password or key file is required.')
      }

      session = jsch.getSession(username, host, port)
      if (keyFile != null) {
        if (passPhrase) {
          jsch.addIdentity(keyFile.absolutePath, passPhrase)
        } else {
          jsch.addIdentity(keyFile.absolutePath)
        }
      }

      if (password) session.setPassword(password as String)

      if (proxyHost?.trim() && proxyPort?.trim()) {
        session.proxy = new ProxyHTTP(proxyHost, Integer.parseInt(proxyPort))
      }

      if (options.verbose) {
        logger.info(">>> Connecting to $host")
      }

// the following if-clause is the new interesting bit
      if (options.connectionTimeout != null) {
        session.setTimeout(con_timeout)
      }

      session.connect()
    }
  } finally {
    changed = false
  }
}
@github-actions
Copy link

Thank you for your contribution! Welcome to sshoogr!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant