diff --git a/SecretAgentKit/Agent.swift b/SecretAgentKit/Agent.swift index 3e69037..f9372d2 100644 --- a/SecretAgentKit/Agent.swift +++ b/SecretAgentKit/Agent.swift @@ -21,20 +21,21 @@ public class Agent { extension Agent { - public func handle(reader: FileHandleReader, writer: FileHandleWriter) { + public func handle(reader: FileHandleReader, writer: FileHandleWriter) -> Bool { Logger().debug("Agent handling new data") let data = reader.availableData - guard !data.isEmpty else { return } + guard !data.isEmpty else { return false} let requestTypeInt = data[4] guard let requestType = SSHAgent.RequestType(rawValue: requestTypeInt) else { writer.write(OpenSSHKeyWriter().lengthAndData(of: SSHAgent.ResponseType.agentFailure.data)) Logger().debug("Agent returned \(SSHAgent.ResponseType.agentFailure.debugDescription)") - return + return true } Logger().debug("Agent handling request of type \(requestType.debugDescription)") let subData = Data(data[5...]) let response = handle(requestType: requestType, data: subData, reader: reader) writer.write(response) + return true } func handle(requestType: SSHAgent.RequestType, data: Data, reader: FileHandleReader) -> Data { diff --git a/SecretAgentKit/SocketController.swift b/SecretAgentKit/SocketController.swift index 39e6b80..926e5f8 100644 --- a/SecretAgentKit/SocketController.swift +++ b/SecretAgentKit/SocketController.swift @@ -5,7 +5,7 @@ public class SocketController { private var fileHandle: FileHandle? private var port: SocketPort? - public var handler: ((FileHandleReader, FileHandleWriter) -> Void)? + public var handler: ((FileHandleReader, FileHandleWriter) -> Bool)? public init(path: String) { Logger().debug("Socket controller setting up at \(path)") @@ -52,7 +52,7 @@ public class SocketController { @objc func handleConnectionAccept(notification: Notification) { Logger().debug("Socket controller accepted connection") guard let new = notification.userInfo?[NSFileHandleNotificationFileHandleItem] as? FileHandle else { return } - handler?(new, new) + _ = handler?(new, new) new.waitForDataInBackgroundAndNotify() fileHandle?.acceptConnectionInBackgroundAndNotify(forModes: [RunLoop.current.currentMode!]) } @@ -61,8 +61,12 @@ public class SocketController { Logger().debug("Socket controller has new data available") guard let new = notification.object as? FileHandle else { return } Logger().debug("Socket controller received new file handle") - handler?(new, new) - new.waitForDataInBackgroundAndNotify() + if((handler?(new, new)) == true) { + Logger().debug("Socket controller handled data, wait for more data") + new.waitForDataInBackgroundAndNotify() + } else { + Logger().debug("Socket controller called with empty data, socked closed") + } } }