diff --git a/Sources/ReplicantSwift/Polish/Polish.swift b/Sources/ReplicantSwift/Polish/Polish.swift index 3f8f9f3..cb9e8a0 100644 --- a/Sources/ReplicantSwift/Polish/Polish.swift +++ b/Sources/ReplicantSwift/Polish/Polish.swift @@ -8,7 +8,7 @@ import Crypto import Foundation -import Transport +import Transmission public protocol PolishConnection { diff --git a/Sources/ReplicantSwift/Polish/PolishErrors.swift b/Sources/ReplicantSwift/Polish/PolishErrors.swift index 4ea6f96..c6822cb 100644 --- a/Sources/ReplicantSwift/Polish/PolishErrors.swift +++ b/Sources/ReplicantSwift/Polish/PolishErrors.swift @@ -16,4 +16,5 @@ enum HandshakeError: Error case clientKeyDataIncorrectSize case unableToDecryptData case dataCreationError + case writeError } diff --git a/Sources/ReplicantSwift/Polish/Silver/SilverClientConnection.swift b/Sources/ReplicantSwift/Polish/Silver/SilverClientConnection.swift index 62dcd2a..a2d3208 100644 --- a/Sources/ReplicantSwift/Polish/Silver/SilverClientConnection.swift +++ b/Sources/ReplicantSwift/Polish/Silver/SilverClientConnection.swift @@ -7,7 +7,7 @@ import Foundation import Logging -import Transport +import Transmission import Network import Crypto @@ -62,53 +62,43 @@ public class SilverClientConnection // } } +public enum PolishError: Error +{ + case noData + case failedDecrypt + case writeError + case readError +} + extension SilverClientConnection: PolishConnection { public func handshake(connection: Connection, completion: @escaping (Error?) -> Void) { log.debug("\n🤝 Client handshake initiation.") log.debug("\n🤝 Sending Public Key Data") - let paddedKeyData = controller.generatePaddedKeyData(publicKey: publicKey, chunkSize: chunkSize) - connection.send(content: paddedKeyData, contentContext: .defaultMessage, isComplete: false, completion: NWConnection.SendCompletion.contentProcessed( + guard let paddedKeyData = controller.generatePaddedKeyData(publicKey: publicKey, chunkSize: chunkSize) else + { + completion(PolishError.noData) + return + } + + guard connection.write(data: paddedKeyData) else { - (maybeError) in - self.log.error("\n🤝 Handshake: Returned from sending our public key to the server.\n") - guard maybeError == nil - else - { - self.log.error("\n🤝 Received error from server when sending our key: \(maybeError!)") - completion(maybeError!) - return - } - - let replicantChunkSize = Int(self.chunkSize) - connection.receive(minimumIncompleteLength: replicantChunkSize, maximumLength: replicantChunkSize, completion: - { - (maybeResponse1Data, maybeResponse1Context, _, maybeResponse1Error) in - - self.log.debug("\n🤝 Callback from handshake network.receive called.") - guard maybeResponse1Error == nil - else - { - self.log.error("\n🤝 Received an error while waiting for response from server after sending key: \(maybeResponse1Error!)") - completion(maybeResponse1Error!) - return - } - - // This data is meaningless it can be discarded - guard let reponseData = maybeResponse1Data - else - { - self.log.error("\n🤝 Server key response did not contain data.") - completion(nil) - return - } - - self.log.debug("\n🤝 Received response data from the server during handshake: \(reponseData)\n") - completion(nil) - }) - })) + completion(HandshakeError.writeError) + return + } + + let replicantChunkSize = Int(self.chunkSize) + guard let responseData = connection.read(size: replicantChunkSize) else + { + self.log.debug("\n🤝 Callback from handshake network.receive called.") + completion(HandshakeError.writeError) + return + } + + self.log.debug("\n🤝 Received response data from the server during handshake: \(responseData)\n") + completion(nil) } public func polish(inputData: Data) -> Data? diff --git a/Sources/ReplicantSwift/Polish/Silver/SilverServer.swift b/Sources/ReplicantSwift/Polish/Silver/SilverServer.swift index cb48779..dc346d6 100644 --- a/Sources/ReplicantSwift/Polish/Silver/SilverServer.swift +++ b/Sources/ReplicantSwift/Polish/Silver/SilverServer.swift @@ -6,7 +6,7 @@ // import Foundation -import Transport +import Transmission import Logging import Crypto diff --git a/Sources/ReplicantSwift/Polish/Silver/SilverServerConfig.swift b/Sources/ReplicantSwift/Polish/Silver/SilverServerConfig.swift index a7ead65..cd099f6 100644 --- a/Sources/ReplicantSwift/Polish/Silver/SilverServerConfig.swift +++ b/Sources/ReplicantSwift/Polish/Silver/SilverServerConfig.swift @@ -15,7 +15,6 @@ public struct SilverServerConfig: PolishServerConfig, Codable public let chunkSize: UInt16 public let chunkTimeout: Int - public func construct(logger: Logger) -> PolishServer? { let silverServer = SilverServer(logger: logger, chunkSize: chunkSize, chunkTimeout: chunkTimeout) diff --git a/Sources/ReplicantSwift/Polish/Silver/SilverServerConnection.swift b/Sources/ReplicantSwift/Polish/Silver/SilverServerConnection.swift index 28e30ea..4c9b275 100644 --- a/Sources/ReplicantSwift/Polish/Silver/SilverServerConnection.swift +++ b/Sources/ReplicantSwift/Polish/Silver/SilverServerConnection.swift @@ -7,7 +7,7 @@ import Foundation import Logging -import Transport +import Transmission import Network import Crypto @@ -52,69 +52,45 @@ extension SilverServerConnection: PolishConnection print("\n🤝 Replicant Server handshake called.") let replicantChunkSize = chunkSize - //Call receive first - connection.receive(minimumIncompleteLength: Int(replicantChunkSize), maximumLength: Int(replicantChunkSize)) + //Call read first + guard let clientPaddedData = connection.read(size: Int(replicantChunkSize)) else { - (maybeResponse1Data, maybeResponse1Context, _, maybeResponse1Error) in - - print("\n🤝 network.receive callback from handshake.") - print("\n🤝 Data received: \(String(describing: maybeResponse1Data?.bytes))") - - // Parse received public key and store it - guard maybeResponse1Error == nil - else - { - print("\n\n🤝 Received an error while waiting for response from server acfter sending key: \(maybeResponse1Error!)\n") - completion(maybeResponse1Error!) - return - } - - // Make sure we have data - guard let clientPaddedData = maybeResponse1Data - else - { - print("\nClient introduction did not contain data.\n") - completion(HandshakeError.noClientKeyData) - return - } - - // Key data is the first chunk of keyDataSize - let clientKeyData = clientPaddedData[.. Data?