Skip to content

Commit c827fcd

Browse files
authored
#560 treat openssh config values ConnectTimeout and ServerAliveInterval as seconds. (#755)
* #560 treat openssh config values ConnectTimeout and ServerAliveInterval as seconds. * cleanup
1 parent 1fd3a2c commit c827fcd

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/main/java/com/jcraft/jsch/OpenSSHConfig.java

+18-9
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,15 @@ private String find(String key) {
233233
if (value != null)
234234
break;
235235
}
236-
// TODO: The following change should be applied,
237-
// but it is breaking changes.
238-
// The consensus is required to enable it.
239-
/*
240-
* if(value!=null && (key.equals("SERVERALIVEINTERVAL") || key.equals("CONNECTTIMEOUT"))){ try
241-
* { int timeout = Integer.parseInt(value); value = Integer.toString(timeout*1000); } catch
242-
* (NumberFormatException e) { } }
243-
*/
236+
237+
if (value != null && (key.equals("SERVERALIVEINTERVAL") || key.equals("CONNECTTIMEOUT"))) {
238+
try {
239+
int timeout = Integer.parseInt(value);
240+
value = Integer.toString(timeout * 1000);
241+
} catch (NumberFormatException e) {
242+
logError(originalKey, e);
243+
}
244+
}
244245

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

268+
private void logError(String originalKey, NumberFormatException e) {
269+
Logger logger = JSch.getLogger();
270+
if (logger != null) {
271+
logger.log(Logger.ERROR, "Error during parsing of " + originalKey + ": " + e.getMessage(),
272+
e);
273+
}
274+
}
275+
267276
private String[] multiFind(String key) {
268277
key = key.toUpperCase(Locale.ROOT);
269278
Vector<String> value = new Vector<>();
@@ -302,7 +311,7 @@ public int getPort() {
302311
try {
303312
port = Integer.parseInt(foo);
304313
} catch (NumberFormatException e) {
305-
// wrong format
314+
logError("Port", e);
306315
}
307316
return port;
308317
}

src/test/java/com/jcraft/jsch/OpenSSHConfigTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ void parseFileWithNegations() throws IOException, URISyntaxException {
102102
assertUserEquals(openSSHConfig, "my.example.org", "u2");
103103
}
104104

105+
@ParameterizedTest
106+
@ValueSource(strings = {"ConnectTimeout", "ServerAliveInterval"})
107+
void timeoutsAreConvertedToMs(String configKey) throws IOException {
108+
OpenSSHConfig parse = OpenSSHConfig.parse(configKey + " 42");
109+
ConfigRepository.Config config = parse.getConfig("");
110+
assertEquals("42000", config.getValue(configKey));
111+
}
112+
105113
private void assertUserEquals(OpenSSHConfig openSSHConfig, String host, String expected) {
106114
final ConfigRepository.Config config = openSSHConfig.getConfig(host);
107115
assertNotNull(config);

0 commit comments

Comments
 (0)