Skip to content

Commit

Permalink
#560 treat openssh config values ConnectTimeout and ServerAliveInterv…
Browse files Browse the repository at this point in the history
…al as seconds.
  • Loading branch information
mwiede committed Jan 24, 2025
1 parent fdcdfb8 commit 7179f33
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/main/java/com/jcraft/jsch/OpenSSHConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,15 @@ private String find(String key) {
if (value != null)
break;
}
// TODO: The following change should be applied,
// but it is breaking changes.
// The consensus is required to enable it.
/*
* if(value!=null && (key.equals("SERVERALIVEINTERVAL") || key.equals("CONNECTTIMEOUT"))){ try
* { int timeout = Integer.parseInt(value); value = Integer.toString(timeout*1000); } catch
* (NumberFormatException e) { } }
*/

if (value != null && (key.equals("SERVERALIVEINTERVAL") || key.equals("CONNECTTIMEOUT"))) {
try {
int timeout = Integer.parseInt(value);
value = Integer.toString(timeout * 1000);
} catch (NumberFormatException e) {
logError(originalKey, e);
}
}

if (keysWithListAdoption.contains(key) && value != null
&& (value.startsWith("+") || value.startsWith("-") || value.startsWith("^"))) {
Expand All @@ -264,6 +265,14 @@ private String find(String key) {
return value;
}

private void logError(String originalKey, NumberFormatException e) {
Logger logger = JSch.getLogger();
if (logger != null) {
logger.log(Logger.ERROR, "Error during parsing of " + originalKey + ": " + e.getMessage(),
e);
}
}

private String[] multiFind(String key) {
key = key.toUpperCase(Locale.ROOT);
Vector<String> value = new Vector<>();
Expand Down Expand Up @@ -303,6 +312,7 @@ public int getPort() {
port = Integer.parseInt(foo);
} catch (NumberFormatException e) {
// wrong format
logError("Port", e);
}
return port;
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/com/jcraft/jsch/OpenSSHConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ void parseFileWithNegations() throws IOException, URISyntaxException {
assertUserEquals(openSSHConfig, "my.example.org", "u2");
}

@ParameterizedTest
@ValueSource(strings = {"ConnectTimeout", "ServerAliveInterval"})
void timeoutsAreConvertedToMs(String configKey) throws IOException {
OpenSSHConfig parse = OpenSSHConfig.parse(configKey + " 42");
ConfigRepository.Config config = parse.getConfig("");
assertEquals("42000", config.getValue(configKey));
}

private void assertUserEquals(OpenSSHConfig openSSHConfig, String host, String expected) {
final ConfigRepository.Config config = openSSHConfig.getConfig(host);
assertNotNull(config);
Expand Down

0 comments on commit 7179f33

Please sign in to comment.