From 6ffad04d4f6e996b9bc0402734fb2c27cceecd3a Mon Sep 17 00:00:00 2001 From: CryptoSax Date: Tue, 31 Aug 2021 15:09:08 -0500 Subject: [PATCH] removed all prints --- Sources/ShadowSwift/AddressReader.swift | 11 - Sources/ShadowSwift/Cipher.swift | 18 +- Sources/ShadowSwift/ShadowConfig.swift | 6 +- Sources/ShadowSwift/ShadowConnection.swift | 5 +- Tests/ShadowSwiftTests/ShadowSwiftTests.swift | 194 +++--------------- 5 files changed, 33 insertions(+), 201 deletions(-) diff --git a/Sources/ShadowSwift/AddressReader.swift b/Sources/ShadowSwift/AddressReader.swift index bc12228..02a0396 100644 --- a/Sources/ShadowSwift/AddressReader.swift +++ b/Sources/ShadowSwift/AddressReader.swift @@ -6,7 +6,6 @@ // import Foundation - // FIXME: Logger class AddressReader @@ -28,7 +27,6 @@ class AddressReader guard let addrType = AddrType(rawValue: Int(buffer[0])) else { - print("Failed to initialize Socks5Addr, AddrType is unknown: \(Int(buffer[0]))") return nil } @@ -43,7 +41,6 @@ class AddressReader guard buffer.count >= addrLength else { - print("Unable to initialize a Socks5Addr. Total data size \(buffer.count) is incorrect for a domain length of \(hostLength)") return nil } @@ -61,14 +58,12 @@ class AddressReader guard let hostString = String(data: hostData, encoding: .utf8) else { - print("Unable to resolve domain name host with provided data: \(String(data: addressData, encoding: .utf8) ?? "could not decode data to string for logging")") return nil } guard let portString = String(data: portData, encoding: .utf8) else { - print("Unable to decode portData to string: \(portData)") return nil } @@ -81,7 +76,6 @@ class AddressReader guard buffer.count >= addressLength else { - print("Received an IPv4 address with an incorrect length: \(buffer.count)") return nil } @@ -96,14 +90,12 @@ class AddressReader guard let hostString = String(data: hostData, encoding: .utf8) else { - print("Failed to decode host data into a string: \(hostData)") return nil } guard let portString = String(data: portData, encoding: .utf8) else { - print("Failed to decode port data into a string: \(portData)") return nil } @@ -116,7 +108,6 @@ class AddressReader guard buffer.count >= addressLength else { - print("Received an IPv6 address with an incorrect length: \(buffer.count)") return nil } @@ -129,14 +120,12 @@ class AddressReader guard let hostString = String(data: hostData, encoding: .utf8) else { - print("Failed to decode host data into a string: \(hostData)") return nil } guard let portString = String(data: portData, encoding: .utf8) else { - print("Failed to decode port data into a string: \(portData)") return nil } diff --git a/Sources/ShadowSwift/Cipher.swift b/Sources/ShadowSwift/Cipher.swift index 7ac461c..16e99cb 100644 --- a/Sources/ShadowSwift/Cipher.swift +++ b/Sources/ShadowSwift/Cipher.swift @@ -139,9 +139,6 @@ class Cipher static func hkdfSHA1(secret: Data, salt: Data, cipherMode: CipherMode) -> Data? { - print("HKDFSHA1") - print("\nsalt") - print(salt.array) let info = Data(string: "ss-subkey") let outputSize = secret.count @@ -149,7 +146,6 @@ class Cipher guard iterations <= 255 else { - print("Key derviation failure: Too many iterations - \(iterations)") return nil } @@ -211,7 +207,7 @@ class Cipher } catch let encryptError { - print("Error running AESGCM encryption: \(encryptError)") + log.error("Error running AESGCM encryption: \(encryptError)") } case .AES_256_GCM: @@ -224,7 +220,7 @@ class Cipher } catch let encryptError { - print("Error running AESGCM encryption: \(encryptError)") + log.error("Error running AESGCM encryption: \(encryptError)") } case .CHACHA20_IETF_POLY1305: @@ -237,7 +233,7 @@ class Cipher } catch let encryptError { - print("Error running ChaChaPoly encryption: \(encryptError)") + log.error("Error running ChaChaPoly encryption: \(encryptError)") } } @@ -272,7 +268,7 @@ class Cipher } catch let decryptError { - print("Error running AESGCM decryption: \(decryptError)") + log.error("Error running AESGCM decryption: \(decryptError)") return nil } case .AES_256_GCM: @@ -283,7 +279,7 @@ class Cipher } catch let decryptError { - print("Error running AESGCM decryption: \(decryptError)") + log.error("Error running AESGCM decryption: \(decryptError)") return nil } case .CHACHA20_IETF_POLY1305: @@ -294,7 +290,7 @@ class Cipher } catch let decryptError { - print("Error running ChaChaPoly decryption: \(decryptError)") + log.error("Error running ChaChaPoly decryption: \(decryptError)") return nil } } @@ -307,8 +303,6 @@ class Cipher // We have 8 bytes, nonce should be 12 counterData.append(contentsOf: [0, 0, 0, 0]) - print("Nonce counter data size: \(counterData.count)") - print("Nonce counter data: \(counterData[0]), \(counterData[11])") // We increment our counter every time nonce is used (encrypt/decrypt) counter += 1 diff --git a/Sources/ShadowSwift/ShadowConfig.swift b/Sources/ShadowSwift/ShadowConfig.swift index cbc44e8..1b6368e 100644 --- a/Sources/ShadowSwift/ShadowConfig.swift +++ b/Sources/ShadowSwift/ShadowConfig.swift @@ -37,9 +37,8 @@ public struct ShadowConfig: Codable let decoded = try decoder.decode(ShadowConfig.self, from: data) self = decoded } - catch let decodeError + catch { - print("Error decoding Shadow Config data: \(decodeError)") return nil } } @@ -53,9 +52,8 @@ public struct ShadowConfig: Codable let data = try Data(contentsOf: url) self.init(from: data) } - catch (let error) + catch { - print("Failed to get data from path \(url.path). \nError: \(error)") return nil } } diff --git a/Sources/ShadowSwift/ShadowConnection.swift b/Sources/ShadowSwift/ShadowConnection.swift index e323710..ea85c37 100644 --- a/Sources/ShadowSwift/ShadowConnection.swift +++ b/Sources/ShadowSwift/ShadowConnection.swift @@ -361,7 +361,6 @@ open class ShadowConnection: Transport.Connection func sendSalt() -> Bool { - print("Sending Salt : \(salt.array)") guard network.write(data: salt) else { @@ -379,7 +378,7 @@ open class ShadowConnection: Transport.Connection guard let serverSalt = maybeSalt else { - print("We did not receive salt from the server.") + self.log.debug("We did not receive salt from the server.") return false } @@ -429,8 +428,6 @@ open class ShadowConnection: Transport.Connection return } - print("Sending address: \(encryptedAddress.count)") - let written = network.write(data: encryptedAddress) if written { diff --git a/Tests/ShadowSwiftTests/ShadowSwiftTests.swift b/Tests/ShadowSwiftTests/ShadowSwiftTests.swift index 398aad0..2d50986 100644 --- a/Tests/ShadowSwiftTests/ShadowSwiftTests.swift +++ b/Tests/ShadowSwiftTests/ShadowSwiftTests.swift @@ -37,7 +37,6 @@ guard let port = NWEndpoint.Port(rawValue: testPort) else { - print("\nUnable to initialize port.\n") XCTFail() return } @@ -63,10 +62,10 @@ switch state { case NWConnection.State.ready: - print("\nConnected state ready\n") + logger.info("\nConnected state ready\n") connected.fulfill() default: - print("\nReceived a state other than ready: \(state)\n") + logger.debug("\nReceived a state other than ready: \(state)\n") return } } @@ -87,7 +86,6 @@ guard let port = NWEndpoint.Port(rawValue: testPort) else { - print("\nUnable to initialize port.\n") XCTFail() return } @@ -113,7 +111,6 @@ switch state { case NWConnection.State.ready: - print("\nConnected state ready\n") connected.fulfill() shadowConnection.send(content: Data("1234"), contentContext: .defaultMessage, isComplete: true, completion: NWConnection.SendCompletion.contentProcessed( @@ -122,7 +119,7 @@ if let sendError = maybeError { - print("Send Error: \(sendError)") + logger.error("Send Error: \(sendError)") XCTFail() return } @@ -130,7 +127,7 @@ sent.fulfill() })) default: - print("\nReceived a state other than ready: \(state)\n") + logger.debug("\nReceived a state other than ready: \(state)\n") return } } @@ -168,7 +165,6 @@ guard let port = NWEndpoint.Port(rawValue: testPort) else { - print("\nUnable to initialize port.\n") XCTFail() return } @@ -192,7 +188,7 @@ switch state { case NWConnection.State.ready: - print("\nConnected state ready\n") + logger.info("\nConnected state ready\n") connected.fulfill() shadowConnection.send(content: Data("GET / HTTP/1.0\r\n\r\n"), contentContext: .defaultMessage, isComplete: true, completion: NWConnection.SendCompletion.contentProcessed( @@ -201,7 +197,7 @@ if let sendError = maybeError { - print("Send Error: \(sendError)") + logger.error("Send Error: \(sendError)") XCTFail() return } @@ -214,21 +210,20 @@ if let receiveError = maybeReceiveError { - print("Got a receive error \(receiveError)") + logger.error("Got a receive error \(receiveError)") //XCTFail() //return } - if let data = maybeData + if maybeData != nil { - print("Received data!!") - print(data.string) + logger.info("Received data!!") received.fulfill() } } })) default: - print("\nReceived a state other than ready: \(state)\n") + logger.debug("\nReceived a state other than ready: \(state)\n") return } } @@ -269,7 +264,6 @@ guard let port = NWEndpoint.Port(rawValue: testPort) else { - print("\nUnable to initialize port.\n") XCTFail() return } @@ -293,7 +287,7 @@ switch state { case NWConnection.State.ready: - print("\nConnected state ready\n") + logger.info("\nConnected state ready\n") connected.fulfill() shadowConnection.send(content: Data("1234"), contentContext: .defaultMessage, isComplete: true, completion: NWConnection.SendCompletion.contentProcessed( @@ -302,7 +296,7 @@ if let sendError = maybeError { - print("Send Error: \(sendError)") + logger.error("Send Error: \(sendError)") XCTFail() return } @@ -315,15 +309,14 @@ if let receiveError = maybeReceiveError { - print("Got a receive error \(receiveError)") + logger.error("Got a receive error \(receiveError)") //XCTFail() //return } - if let data = maybeData + if maybeData != nil { - print("Received data!!") - print(data.string) + logger.info("Received data!!") received.fulfill() shadowConnection.send(content: "Send2", contentContext: .defaultMessage, isComplete: false, completion: NWConnection.SendCompletion.contentProcessed( @@ -332,7 +325,7 @@ if let sendError = maybeSendError { - print("Error on 2nd send: \(sendError)") + logger.error("Error on 2nd send: \(sendError)") XCTFail() return } @@ -345,7 +338,7 @@ if let error = maybeError { - print("Error on 2nd receive: \(error)") + logger.error("Error on 2nd receive: \(error)") XCTFail() } @@ -362,7 +355,7 @@ } })) default: - print("\nReceived a state other than ready: \(state)\n") + logger.debug("\nReceived a state other than ready: \(state)\n") return } } @@ -395,22 +388,18 @@ switch connectionState { case .ready: - print("Server is ready.") - connection.receive(minimumIncompleteLength: 4, maximumLength: 4) { (maybeData, _, _, maybeReceiveError) in - if let receiveError = maybeReceiveError + if maybeReceiveError != nil { - print("Server received an error on receive: \(receiveError)") return } guard let receivedData = maybeData else { - print("Server received nil data.") return } @@ -422,23 +411,20 @@ { (maybeSendError) in - if let sendError = maybeSendError + if maybeSendError != nil { - print("Error sending 'Okay' message: \(sendError)") return } else { - print("Okay message sent!") responseSent.fulfill() connection.receive(minimumIncompleteLength: 5, maximumLength: 5) { (maybeData, _, _, maybeError) in - if let error = maybeError + if maybeError != nil { - print("Server error on 2nd receive: \(error)") XCTFail() return } @@ -451,9 +437,8 @@ connection.send(content: "ServerSend2", contentContext: .defaultMessage, isComplete: false, completion: NWConnection.SendCompletion.contentProcessed( { (maybeError) in - if let error = maybeError + if maybeError != nil { - print("Server error on 2nd send: \(error)") return } @@ -469,8 +454,7 @@ } default: - print("Server state is not ready: \(connectionState)") - } +return } } newConnection.start(queue: .global()) @@ -480,9 +464,8 @@ listener.start(queue: .global()) listening.fulfill() } - catch let listenerError + catch _ { - print("Error running a server: \(listenerError)") return } } @@ -504,134 +487,5 @@ return } } - - // func testHKDF() - // { - // let correct = Data(base64Encoded: "k7qvG929qzyHVF7D2Bxke78qIxk1A8jk/JKSA7K0V40=") - // let secret = Data(base64Encoded: "aFkrPPcQtd5QLc5xBuUhazfDQijc3HVXb974bqnSH4c=") - // let salt = Data(base64Encoded: "rUU438RMMHLlSH0jLMp9FSrFHHuWj4eQw/dq1XQpnJ0=") - // let info = Data(string: "ss-subkey") - // - // guard let result = Cipher(config: <#ShadowConfig#>, salt: <#Data#>, logger: <#Logger#>).hkdfSHA1(secret: secret!, salt: salt!, cipherMode: CipherMode.AES_128_GCM) - // else - // { - // XCTFail() - // return - // } - // - // XCTAssertEqual(correct, result) - // } - // - // // AES.GCM 128 - // func testAES128() - // { - // let nonce = Data(base64Encoded: "GPiavgwl3vKAa6aK") - // let secret = Data(base64Encoded: "x1wXVJg6pM4ML48HB6YyEA==") - // let salt = Data(base64Encoded: "AIZClCKlN3LnoNtETQmh31kScJPT3jCt") - // let info = Data(string: "ss-subkey") - // let key = Cipher().hkdfSHA1(secret: secret!, salt: salt!, info: info) - // //let key = Data(base64Encoded: "vru391Vs32PEhzOuiS325A==") - // - // // Encrypt a thing - // do - // { - // //Seal - // let encrypted = try AES.GCM.seal(plainText, - // using: SymmetricKey(data: key!), - // nonce: AES.GCM.Nonce(data: nonce!)) - // - // - // - // let correct = Data(base64Encoded: "I2lhGBsa1w45XV9I486z4A3j7oro") - // XCTAssertEqual(encrypted.combined![12...], correct) - // } - // catch let error - // { - // print("Error encrypting data: \(error)") - // XCTFail() - // } - // } - // - // // AES.GCM 192 - // func testAES192() - // { - // let nonce = Data(base64Encoded: "9y3YUf37OrbhoESq") - // let secret = Data(base64Encoded: "zxhocnRVToKo5axoM9ZiCRV8ZwU9zRD9") - // let salt = Data(base64Encoded: "biPWP0Uk2UI9tJmwjWACwzU5ltViYJvw") - // let info = Data(string: "ss-subkey") - // let key = Cipher().hkdfSHA1(secret: secret!, salt: salt!, info: info) - // //let key = Data(base64Encoded: "mEl7TEjOwbOBvCpwT9fA6xQuJJ5t8EOC") - // - // // Encrypt a thing - // do - // { - // //Seal - // let encrypted = try AES.GCM.seal(plainText, - // using: SymmetricKey(data: key!), - // nonce: AES.GCM.Nonce(data: nonce!)) - // let correct = Data(base64Encoded: "NJ2FGAA4h35pvqgHqbpqEEwYPmif") - // - // XCTAssertEqual(encrypted.combined![12...], correct) - // } - // catch let error - // { - // print("Error encrypting data: \(error)") - // XCTFail() - // } - // } - // - // // AES.GCM 256 - // func testAES256() - // { - // let nonce = Data(base64Encoded: "MSX/t7/6kgl56xFP") - // let secret = Data(base64Encoded: "aFkrPPcQtd5QLc5xBuUhazfDQijc3HVXb974bqnSH4c=") - // let salt = Data(base64Encoded: "rUU438RMMHLlSH0jLMp9FSrFHHuWj4eQw/dq1XQpnJ0=") - // let info = Data(string: "ss-subkey") - // let key = Cipher().hkdfSHA1(secret: secret!, salt: salt!, info: info) - // //let key = Data(base64Encoded: "k7qvG929qzyHVF7D2Bxke78qIxk1A8jk/JKSA7K0V40=") - // - // // Encrypt a thing - // do - // { - // //Seal - // let encrypted = try AES.GCM.seal(plainText, - // using: SymmetricKey(data: key!), - // nonce: AES.GCM.Nonce(data: nonce!)) - // let correct = Data(base64Encoded: "6k8tJuPU87yBFrtniSage8SX9xiU") - // - // XCTAssertEqual(encrypted.combined![12...], correct) - // } - // catch let error - // { - // print("Error encrypting data: \(error)") - // XCTFail() - // } - // } - // - // // AES.GCM ChaChaPoly - // func testChaChaPoly() - // { - // let nonce = Data(base64Encoded: "kA3DoGigyGYfF2bj") - // let secret = Data(base64Encoded: "q2f5hKCwszOLvvsc4g1cMj1gwho1O3dE2ttwTks8haE=") - // let salt = Data(base64Encoded: "rUU438RMMHLlSH0jLMp9FSrFHHuWj4eQw/dq1XQpnJ0=") - // let info = Data(string: "ss-subkey") - // let key = Cipher().hkdfSHA1(secret: secret!, salt: salt!, info: info) - // //let key = Data(base64Encoded: "mnbqMaGH95dS2jZXaeT9UTszQ0myRcBu1CCjxA+MafY=") - // - // do - // { - // let encrypted = try ChaChaPoly.seal(plainText, - // using: SymmetricKey(data: key!), - // nonce: ChaChaPoly.Nonce(data: nonce!)) - // let correct = Data(base64Encoded: "LJQq3ms5dYIPoN/csclHFuCU1EH0") - // XCTAssertEqual(encrypted.combined[12...], correct) - // } - // catch let error - // { - // print("Error encrypting data: \(error)") - // XCTFail() - // } - // } - }