Skip to content

Commit

Permalink
feat(ios): add checkReachability for ssh transport
Browse files Browse the repository at this point in the history
  • Loading branch information
Malinskiy committed Nov 23, 2023
1 parent 6cd0e3f commit 18d32a2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ sealed interface Transport {
@JsonProperty("addr") val addr: String,
@JsonProperty("port") val port: Int = 22,
@JsonProperty("authentication") val authentication: SshAuthentication? = null,
@JsonProperty("checkReachability") val checkReachability: Boolean = true,
) : Transport
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ConnectionFactory(private val configuration: Configuration, private val ve

private fun createRemoteCommandExecutor(transport: Transport.Ssh): SshjCommandExecutor? {
return try {
val hostAddress = transport.addr.toInetAddressOrNull() ?: throw DeviceFailureException(DeviceFailureReason.UnreachableHost)
val hostAddress = transport.toInetAddressOrNull() ?: throw DeviceFailureException(DeviceFailureReason.UnreachableHost)
val connectionId = "${hostAddress.hostAddress}:${transport.port}"
val authConfig = transport.authentication ?: vendorConfiguration.ssh.authentication
val sshAuthentication = when (authConfig) {
Expand Down Expand Up @@ -117,23 +117,27 @@ class ConnectionFactory(private val configuration: Configuration, private val ve
}
}

private fun String.toInetAddressOrNull(): InetAddress? {
private fun Transport.Ssh.toInetAddressOrNull(): InetAddress? {
val address = try {
InetAddress.getByName(this)
InetAddress.getByName(this.addr)
} catch (e: UnknownHostException) {
logger.error("Error resolving host $this: $e")
return null
}
return if (try {
address.isReachable(vendorConfiguration.timeoutConfiguration.reachability.toMillis().toInt())
} catch (e: IOException) {
logger.error("Error checking reachability of $this: $e")
false
return if (this.checkReachability) {
if (try {
address.isReachable(vendorConfiguration.timeoutConfiguration.reachability.toMillis().toInt())
} catch (e: IOException) {
logger.error("Error checking reachability of $this: $e")
false
}
) {
address
} else {
null
}
) {
address
} else {
null
address
}
}

Expand Down

0 comments on commit 18d32a2

Please sign in to comment.