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

#560 treat openssh config values ConnectTimeout and ServerAliveInterval as seconds. #755

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 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 @@ -302,7 +311,7 @@ public int getPort() {
try {
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
Loading