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

Fixes Incorrect Socket Timeout #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 13 additions & 13 deletions SwiftSH/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ open class SSHSession {

/// The server host to connect to.
public let host: String

/// The server port to connect to.
public let port: UInt16

/// The logger.
public var log: Logger

/// The version of the underlying SSH library.
public var version: String {
return self.sshLibrary.version
}

public init(sshLibrary: SSHLibrary.Type = Libssh2.self, host: String, port: UInt16 = 22) throws {
self.sshLibrary = sshLibrary
self.host = host
self.port = port
self.log = ConsoleLogger(level: .debug, enabled: true)
self.log = ConsoleLogger(level: .debug, enabled: false)
self.queue = Queue(label: "SSH Queue", concurrent: false)
self.session = try sshLibrary.makeSession()
self.timeout = 10
Expand All @@ -79,7 +79,7 @@ open class SSHSession {

/// The banner received from the remote host.
public fileprivate(set) var remoteBanner: String?

/// The fingerprint received from the remote host.
public fileprivate(set) var fingerprint: [FingerprintHashType: String] = [:]

Expand Down Expand Up @@ -166,7 +166,7 @@ open class SSHSession {
}

// Try to connect to resolved address
if CFSocketConnectToAddress(socket, dataAddress as CFData, Double(self.timeout)/1000) == .success {
if CFSocketConnectToAddress(socket, dataAddress as CFData, Double(self.timeout)) == .success {
self.log.info("Connection to \(ipAddress) on port \(self.port) successful")
self.socket = socket
break
Expand Down Expand Up @@ -202,13 +202,13 @@ open class SSHSession {

// Connection completed successfully
self.connected = true

// Get the remote banner
self.remoteBanner = self.session.banner
if let remoteBanner = self.remoteBanner {
self.log.debug("Remote banner is \(remoteBanner)")
}

// Get the host's fingerprint
self.fingerprint = [:]
for hashType: FingerprintHashType in [.md5, .sha1] {
Expand Down Expand Up @@ -247,13 +247,13 @@ open class SSHSession {
if let socket = self.socket, CFSocketIsValid(socket) {
CFSocketInvalidate(socket)
}

// Clean up state
self.socket = nil
self.connected = false
self.remoteBanner = nil
self.fingerprint = [:]

self.log.debug("Disconnected")
}
}
Expand Down Expand Up @@ -314,20 +314,20 @@ open class SSHSession {
let privateKey = (privateKey as NSString).expandingTildeInPath

try self.session.authenticateByPublicKeyFromFile(username, password: password, publicKey: publicKey, privateKey: privateKey)

case .byPublicKeyFromMemory(let username, let password, let publicKey, let privateKey):
// Public Key authentication
try self.session.authenticateByPublicKeyFromMemory(username, password: password, publicKey: publicKey, privateKey: privateKey)
}
}
}

public func checkFingerprint(_ callback: @escaping ([FingerprintHashType: String]) -> Bool) -> Self {
self.queue.async {
guard self.connected else {
return
}

let fingerprint = self.fingerprint
var disconnect = false

Expand Down