Added error handling for libssh
This commit is contained in:
parent
193755454e
commit
24eac6c2b3
5 changed files with 61 additions and 6 deletions
|
@ -40,8 +40,30 @@ class GRemoteSession @Inject constructor(
|
|||
fun setup(uri: URIish) {
|
||||
val session = processSession.get()
|
||||
session.setOptions(LibSshOptions.SSH_OPTIONS_HOST, uri.host)
|
||||
session.setOptions(LibSshOptions.SSH_OPTIONS_USER, uri.user)
|
||||
session.loadOptionsFromConfig()
|
||||
|
||||
session.connect()
|
||||
session.userAuthPublicKeyAuto(uri.user, null)
|
||||
var result = session.userAuthPublicKeyAuto(null, null)
|
||||
|
||||
if(result == 1) {
|
||||
credentialsStateManager.updateState(CredentialsState.SshCredentialsRequested)
|
||||
|
||||
var credentials = credentialsStateManager.currentCredentialsState
|
||||
while (credentials is CredentialsState.CredentialsRequested) {
|
||||
credentials = credentialsStateManager.currentCredentialsState
|
||||
}
|
||||
|
||||
val password = if (credentials !is CredentialsState.SshCredentialsAccepted)
|
||||
null
|
||||
else
|
||||
credentials.password
|
||||
|
||||
result = session.userAuthPublicKeyAuto(null, password)
|
||||
}
|
||||
|
||||
if(result != 0)
|
||||
throw Exception("Something went wrong with authentication")
|
||||
|
||||
this.session = session
|
||||
}
|
||||
|
|
10
src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/Enums.kt
Normal file
10
src/main/kotlin/com/jetpackduba/gitnuro/ssh/libssh/Enums.kt
Normal file
|
@ -0,0 +1,10 @@
|
|||
package com.jetpackduba.gitnuro.ssh.libssh
|
||||
|
||||
enum class sshAuthE(val value: Int) {
|
||||
SSH_AUTH_SUCCESS(0),
|
||||
SSH_AUTH_DENIED(1),
|
||||
SSH_AUTH_PARTIAL(2),
|
||||
SSH_AUTH_INFO(3),
|
||||
SSH_AUTH_AGAIN(4),
|
||||
SSH_AUTH_ERROR(-1)
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package com.jetpackduba.gitnuro.ssh.libssh
|
||||
|
||||
import com.jetpackduba.gitnuro.ssh.libssh.streams.checkValidResult
|
||||
import javax.inject.Inject
|
||||
|
||||
class LibSshSession @Inject constructor() {
|
||||
|
@ -8,19 +9,25 @@ class LibSshSession @Inject constructor() {
|
|||
private var session: ssh_session = sshLib.ssh_new()
|
||||
private var channel: LibSshChannel? = null
|
||||
|
||||
|
||||
fun setOptions(option: LibSshOptions, value: String) {
|
||||
sshLib.ssh_options_set(session, option.ordinal, value)
|
||||
}
|
||||
|
||||
fun loadOptionsFromConfig() {
|
||||
checkValidResult(sshLib.ssh_options_parse_config(session, null))
|
||||
}
|
||||
|
||||
fun connect() {
|
||||
sshLib.ssh_connect(session)
|
||||
}
|
||||
|
||||
fun userAuthPublicKeyAuto(username: String?, password: String?): Int {
|
||||
val result = sshLib.ssh_userauth_publickey_auto(session, username, password)
|
||||
|
||||
if(result != 0)
|
||||
println("RESULT is $result. ERROR IS: ${getError()}")
|
||||
|
||||
fun userAuthPublicKeyAuto(username: String?, password: String?) {
|
||||
sshLib.ssh_userauth_publickey_auto(session, username, password)
|
||||
return result
|
||||
}
|
||||
|
||||
fun createChannel(): LibSshChannel {
|
||||
|
@ -31,8 +38,13 @@ class LibSshSession @Inject constructor() {
|
|||
return newChannel
|
||||
}
|
||||
|
||||
private fun getError(): String {
|
||||
return sshLib.ssh_get_error(session)
|
||||
}
|
||||
|
||||
fun disconnect() {
|
||||
sshLib.ssh_disconnect(session)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -12,10 +12,14 @@ interface SSHLibrary : Library {
|
|||
fun ssh_new(): ssh_session
|
||||
fun ssh_disconnect(session: ssh_session): ssh_session
|
||||
fun ssh_options_set(session: ssh_session, enumValue: Int, value: String)
|
||||
|
||||
fun ssh_options_parse_config(session: ssh_session, fileName: String?): Int
|
||||
|
||||
fun ssh_connect(session: ssh_session) : Int
|
||||
|
||||
fun ssh_userauth_password(session: ssh_session, username: String, password: String): Int
|
||||
fun ssh_userauth_agent(session: ssh_session, username: String?): Int
|
||||
fun ssh_userauth_publickey_auto(session: ssh_session, username: String?, password: String?): Int
|
||||
fun ssh_get_error(session: ssh_session): String
|
||||
|
||||
fun ssh_channel_new(sshSession: ssh_session): ssh_channel
|
||||
|
||||
|
|
|
@ -31,4 +31,11 @@ class LibSshChannelOutputStream(private val sshChannel: ssh_channel) : OutputStr
|
|||
fun checkValidResult(result: Int) {
|
||||
if (result != 0)
|
||||
throw Exception("Result is $result")
|
||||
}
|
||||
|
||||
fun checkValidResult(result: Int, callback: (Int) -> Unit) {
|
||||
if (result != 0) {
|
||||
callback(result)
|
||||
throw Exception("Result is $result")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue