From 4652c4ce4eeb87e5c0358457e686d6d2825c3dd3 Mon Sep 17 00:00:00 2001 From: Arsh Anwar <45912425+d4rk-lucif3r@users.noreply.github.com> Date: Sat, 1 Jun 2024 04:18:27 +0530 Subject: [PATCH 1/3] test url, phone and clipboard --- NearbyShare/InboundNearbyConnection.swift | 58 ++++++++++++++++------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/NearbyShare/InboundNearbyConnection.swift b/NearbyShare/InboundNearbyConnection.swift index c49990a..dceb0a3 100644 --- a/NearbyShare/InboundNearbyConnection.swift +++ b/NearbyShare/InboundNearbyConnection.swift @@ -24,7 +24,7 @@ class InboundNearbyConnection: NearbyConnection{ private var textPayloadID:Int64=0 enum State{ - case initial, receivedConnectionRequest, sentUkeyServerInit, receivedUkeyClientFinish, sentConnectionResponse, sentPairedKeyResult, receivedPairedKeyResult, waitingForUserConsent, receivingFiles, disconnected + case initial, receivedConnectionRequest, sentUkeyServerInit, receivedUkeyClientFinish, sentConnectionResponse, sentPairedKeyResult, receivedPairedKeyResult, waitingForUserConsent, receivingFiles, receivingText, disconnected } override init(connection: NWConnection, id:String) { @@ -103,7 +103,7 @@ class InboundNearbyConnection: NearbyConnection{ guard frame.payloadChunk.offset==currentOffset else { throw NearbyError.protocolError("Invalid offset into file \(frame.payloadChunk.offset), expected \(currentOffset)") } guard currentOffset+Int64(frame.payloadChunk.body.count)<=fileInfo.meta.size else { throw NearbyError.protocolError("Transferred file size exceeds previously specified value") } if frame.payloadChunk.body.count>0{ - fileInfo.fileHandle?.write(frame.payloadChunk.body) + try fileInfo.fileHandle?.write(contentsOf: frame.payloadChunk.body) transferredFiles[id]!.bytesTransferred+=Int64(frame.payloadChunk.body.count) fileInfo.progress?.completedUnitCount=transferredFiles[id]!.bytesTransferred }else if (frame.payloadChunk.flags & 1)==1{ @@ -118,9 +118,19 @@ class InboundNearbyConnection: NearbyConnection{ } override func processBytesPayload(payload: Data, id: Int64) throws -> Bool { - if id==textPayloadID{ - if let urlStr=String(data: payload, encoding: .utf8), let url=URL(string: urlStr){ - NSWorkspace.shared.open(url) + if id == textPayloadID { + if currentState == .receivingText { + if let text=String(data: payload, encoding: .utf8) { + let pasteboard = NSPasteboard.general + pasteboard.clearContents() // Clear the clipboard + if !pasteboard.setString(text, forType: .string) { + print("Could not setString in pasteboard") + } + } + } else { + if let urlStr=String(data: payload, encoding: .utf8), let url=URL(string: urlStr){ + NSWorkspace.shared.open(url) + } } try sendDisconnectionAndDisconnect() return true @@ -129,7 +139,7 @@ class InboundNearbyConnection: NearbyConnection{ } private func processConnectionRequestFrame(_ frame:Location_Nearby_Connections_OfflineFrame) throws{ - guard frame.hasV1 && frame.v1.hasConnectionRequest && frame.v1.connectionRequest.hasEndpointInfo else { throw NearbyError.requiredFieldMissing("connectionRequest.endpointInfo") } + guard frame.hasV1 && frame.v1.hasConnectionRequest && frame.v1.connectionRequest.hasEndpointInfo else { throw NearbyError.requiredFieldMissing } guard case .connectionRequest = frame.v1.type else { throw NearbyError.protocolError("Unexpected frame type \(frame.v1.type)") } let endpointInfo=frame.v1.connectionRequest.endpointInfo guard endpointInfo.count>17 else { throw NearbyError.protocolError("Endpoint info too short") } @@ -142,7 +152,7 @@ class InboundNearbyConnection: NearbyConnection{ } private func processUkey2ClientInit(_ msg:Securegcm_Ukey2Message) throws{ - guard msg.hasMessageType, msg.hasMessageData else { throw NearbyError.requiredFieldMissing("clientInit ukey2message.type|data") } + guard msg.hasMessageType, msg.hasMessageData else { throw NearbyError.requiredFieldMissing } guard case .clientInit = msg.messageType else{ sendUkey2Alert(type: .badMessageType) throw NearbyError.ukey2 @@ -206,7 +216,7 @@ class InboundNearbyConnection: NearbyConnection{ } private func processUkey2ClientFinish(_ msg:Securegcm_Ukey2Message, raw:Data) throws{ - guard msg.hasMessageType, msg.hasMessageData else { throw NearbyError.requiredFieldMissing("clientFinish ukey2message.type|data") } + guard msg.hasMessageType, msg.hasMessageData else { throw NearbyError.requiredFieldMissing } guard case .clientFinish = msg.messageType else { throw NearbyError.ukey2 } var sha=SHA512() @@ -214,7 +224,7 @@ class InboundNearbyConnection: NearbyConnection{ guard cipherCommitment==Data(sha.finalize()) else { throw NearbyError.ukey2 } let clientFinish=try Securegcm_Ukey2ClientFinished(serializedData: msg.messageData) - guard clientFinish.hasPublicKey else {throw NearbyError.requiredFieldMissing("ukey2clientFinish.publicKey") } + guard clientFinish.hasPublicKey else {throw NearbyError.requiredFieldMissing } let clientKey=try Securemessage_GenericPublicKey(serializedData: clientFinish.publicKey) try finalizeKeyExchange(peerKey: clientKey) @@ -223,7 +233,7 @@ class InboundNearbyConnection: NearbyConnection{ } private func processConnectionResponseFrame(_ frame:Location_Nearby_Connections_OfflineFrame) throws{ - guard frame.hasV1, frame.v1.hasType else { throw NearbyError.requiredFieldMissing("offlineFrame.v1.type") } + guard frame.hasV1, frame.v1.hasType else { throw NearbyError.requiredFieldMissing } if case .connectionResponse = frame.v1.type { var resp=Location_Nearby_Connections_OfflineFrame() resp.version = .v1 @@ -254,7 +264,7 @@ class InboundNearbyConnection: NearbyConnection{ } private func processPairedKeyEncryptionFrame(_ frame:Sharing_Nearby_Frame) throws{ - guard frame.hasV1, frame.v1.hasPairedKeyEncryption else { throw NearbyError.requiredFieldMissing("shareNearbyFrame.v1.pairedKeyEncryption") } + guard frame.hasV1, frame.v1.hasPairedKeyEncryption else { throw NearbyError.requiredFieldMissing } var pairedResult=Sharing_Nearby_Frame() pairedResult.version = .v1 pairedResult.v1=Sharing_Nearby_V1Frame() @@ -266,12 +276,12 @@ class InboundNearbyConnection: NearbyConnection{ } private func processPairedKeyResultFrame(_ frame:Sharing_Nearby_Frame) throws{ - guard frame.hasV1, frame.v1.hasPairedKeyResult else { throw NearbyError.requiredFieldMissing("shareNearbyFrame.v1.pairedKeyResult") } + guard frame.hasV1, frame.v1.hasPairedKeyResult else { throw NearbyError.requiredFieldMissing } currentState = .receivedPairedKeyResult } private func processIntroductionFrame(_ frame:Sharing_Nearby_Frame) throws{ - guard frame.hasV1, frame.v1.hasIntroduction else { throw NearbyError.requiredFieldMissing("shareNearbyFrame.v1.introduction") } + guard frame.hasV1, frame.v1.hasIntroduction else { throw NearbyError.requiredFieldMissing } currentState = .waitingForUserConsent if frame.v1.introduction.fileMetadata.count>0 && frame.v1.introduction.textMetadata.isEmpty{ let downloadsDirectory=(try FileManager.default.url(for: .downloadsDirectory, in: .userDomainMask, appropriateFor: nil, create: true)).resolvingSymlinksInPath() @@ -308,7 +318,19 @@ class InboundNearbyConnection: NearbyConnection{ DispatchQueue.main.async { self.delegate?.obtainUserConsent(for: metadata, from: self.remoteDeviceInfo!, connection: self) } - }else{ + } else if case .phoneNumber=meta.type{ + let metadata=TransferMetadata(files: [], id: id, pinCode: pinCode, textDescription: meta.textTitle) + textPayloadID=meta.payloadID + DispatchQueue.main.async { + self.delegate?.obtainUserConsent(for: metadata, from: self.remoteDeviceInfo!, connection: self) + } + } else if case .text=meta.type{ + let metadata=TransferMetadata(files: [], id: id, pinCode: pinCode, textDescription: meta.textTitle) + textPayloadID=meta.payloadID + DispatchQueue.main.async { + self.delegate?.obtainUserConsent(for: metadata, from: self.remoteDeviceInfo!, connection: self) + } + } else{ rejectTransfer(with: .unsupportedAttachmentType) } }else{ @@ -346,7 +368,11 @@ class InboundNearbyConnection: NearbyConnection{ frame.version = .v1 frame.v1.type = .response frame.v1.connectionResponse.status = .accept - currentState = .receivingFiles + if (transferredFiles.isEmpty) { + currentState = .receivingText + } else { + currentState = .receivingFiles + } try sendTransferSetupFrame(frame) }catch{ lastError=error @@ -379,4 +405,4 @@ class InboundNearbyConnection: NearbyConnection{ protocol InboundNearbyConnectionDelegate{ func obtainUserConsent(for transfer:TransferMetadata, from device:RemoteDeviceInfo, connection:InboundNearbyConnection) func connectionWasTerminated(connection:InboundNearbyConnection, error:Error?) -} +} \ No newline at end of file From 2b010d357b866ee98d47ea26df6a75f556846aaa Mon Sep 17 00:00:00 2001 From: Arsh Date: Sat, 1 Jun 2024 04:45:15 +0530 Subject: [PATCH 2/3] Fixed Errors and Done Validation Signed-off-by: Arsh --- NearbyShare/InboundNearbyConnection.swift | 26 +++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/NearbyShare/InboundNearbyConnection.swift b/NearbyShare/InboundNearbyConnection.swift index dceb0a3..ba89f1e 100644 --- a/NearbyShare/InboundNearbyConnection.swift +++ b/NearbyShare/InboundNearbyConnection.swift @@ -102,10 +102,10 @@ class InboundNearbyConnection: NearbyConnection{ let currentOffset=fileInfo.bytesTransferred guard frame.payloadChunk.offset==currentOffset else { throw NearbyError.protocolError("Invalid offset into file \(frame.payloadChunk.offset), expected \(currentOffset)") } guard currentOffset+Int64(frame.payloadChunk.body.count)<=fileInfo.meta.size else { throw NearbyError.protocolError("Transferred file size exceeds previously specified value") } - if frame.payloadChunk.body.count>0{ - try fileInfo.fileHandle?.write(contentsOf: frame.payloadChunk.body) - transferredFiles[id]!.bytesTransferred+=Int64(frame.payloadChunk.body.count) - fileInfo.progress?.completedUnitCount=transferredFiles[id]!.bytesTransferred + if frame.payloadChunk.body.count>0{ + fileInfo.fileHandle?.write(frame.payloadChunk.body) + transferredFiles[id]!.bytesTransferred+=Int64(frame.payloadChunk.body.count) + fileInfo.progress?.completedUnitCount=transferredFiles[id]!.bytesTransferred }else if (frame.payloadChunk.flags & 1)==1{ try fileInfo.fileHandle?.close() transferredFiles[id]!.fileHandle=nil @@ -139,7 +139,7 @@ class InboundNearbyConnection: NearbyConnection{ } private func processConnectionRequestFrame(_ frame:Location_Nearby_Connections_OfflineFrame) throws{ - guard frame.hasV1 && frame.v1.hasConnectionRequest && frame.v1.connectionRequest.hasEndpointInfo else { throw NearbyError.requiredFieldMissing } + guard frame.hasV1 && frame.v1.hasConnectionRequest && frame.v1.connectionRequest.hasEndpointInfo else { throw NearbyError.requiredFieldMissing("connectionRequest.endpointInfo") } guard case .connectionRequest = frame.v1.type else { throw NearbyError.protocolError("Unexpected frame type \(frame.v1.type)") } let endpointInfo=frame.v1.connectionRequest.endpointInfo guard endpointInfo.count>17 else { throw NearbyError.protocolError("Endpoint info too short") } @@ -152,7 +152,7 @@ class InboundNearbyConnection: NearbyConnection{ } private func processUkey2ClientInit(_ msg:Securegcm_Ukey2Message) throws{ - guard msg.hasMessageType, msg.hasMessageData else { throw NearbyError.requiredFieldMissing } + guard msg.hasMessageType, msg.hasMessageData else { throw NearbyError.requiredFieldMissing("clientInit ukey2message.type|data") } guard case .clientInit = msg.messageType else{ sendUkey2Alert(type: .badMessageType) throw NearbyError.ukey2 @@ -216,7 +216,7 @@ class InboundNearbyConnection: NearbyConnection{ } private func processUkey2ClientFinish(_ msg:Securegcm_Ukey2Message, raw:Data) throws{ - guard msg.hasMessageType, msg.hasMessageData else { throw NearbyError.requiredFieldMissing } + guard msg.hasMessageType, msg.hasMessageData else { throw NearbyError.requiredFieldMissing("clientFinish ukey2message.type|data") } guard case .clientFinish = msg.messageType else { throw NearbyError.ukey2 } var sha=SHA512() @@ -224,7 +224,7 @@ class InboundNearbyConnection: NearbyConnection{ guard cipherCommitment==Data(sha.finalize()) else { throw NearbyError.ukey2 } let clientFinish=try Securegcm_Ukey2ClientFinished(serializedData: msg.messageData) - guard clientFinish.hasPublicKey else {throw NearbyError.requiredFieldMissing } + guard clientFinish.hasPublicKey else {throw NearbyError.requiredFieldMissing("ukey2clientFinish.publicKey") } let clientKey=try Securemessage_GenericPublicKey(serializedData: clientFinish.publicKey) try finalizeKeyExchange(peerKey: clientKey) @@ -233,7 +233,7 @@ class InboundNearbyConnection: NearbyConnection{ } private func processConnectionResponseFrame(_ frame:Location_Nearby_Connections_OfflineFrame) throws{ - guard frame.hasV1, frame.v1.hasType else { throw NearbyError.requiredFieldMissing } + guard frame.hasV1, frame.v1.hasType else { throw NearbyError.requiredFieldMissing("offlineFrame.v1.type") } if case .connectionResponse = frame.v1.type { var resp=Location_Nearby_Connections_OfflineFrame() resp.version = .v1 @@ -264,7 +264,7 @@ class InboundNearbyConnection: NearbyConnection{ } private func processPairedKeyEncryptionFrame(_ frame:Sharing_Nearby_Frame) throws{ - guard frame.hasV1, frame.v1.hasPairedKeyEncryption else { throw NearbyError.requiredFieldMissing } + guard frame.hasV1, frame.v1.hasPairedKeyEncryption else { throw NearbyError.requiredFieldMissing("shareNearbyFrame.v1.pairedKeyEncryption") } var pairedResult=Sharing_Nearby_Frame() pairedResult.version = .v1 pairedResult.v1=Sharing_Nearby_V1Frame() @@ -276,12 +276,12 @@ class InboundNearbyConnection: NearbyConnection{ } private func processPairedKeyResultFrame(_ frame:Sharing_Nearby_Frame) throws{ - guard frame.hasV1, frame.v1.hasPairedKeyResult else { throw NearbyError.requiredFieldMissing } + guard frame.hasV1, frame.v1.hasPairedKeyResult else { throw NearbyError.requiredFieldMissing("shareNearbyFrame.v1.pairedKeyResult") } currentState = .receivedPairedKeyResult } private func processIntroductionFrame(_ frame:Sharing_Nearby_Frame) throws{ - guard frame.hasV1, frame.v1.hasIntroduction else { throw NearbyError.requiredFieldMissing } + guard frame.hasV1, frame.v1.hasIntroduction else { throw NearbyError.requiredFieldMissing("shareNearbyFrame.v1.introduction") } currentState = .waitingForUserConsent if frame.v1.introduction.fileMetadata.count>0 && frame.v1.introduction.textMetadata.isEmpty{ let downloadsDirectory=(try FileManager.default.url(for: .downloadsDirectory, in: .userDomainMask, appropriateFor: nil, create: true)).resolvingSymlinksInPath() @@ -405,4 +405,4 @@ class InboundNearbyConnection: NearbyConnection{ protocol InboundNearbyConnectionDelegate{ func obtainUserConsent(for transfer:TransferMetadata, from device:RemoteDeviceInfo, connection:InboundNearbyConnection) func connectionWasTerminated(connection:InboundNearbyConnection, error:Error?) -} \ No newline at end of file +} From c38b482b0aacd5c6fbfaf8447e508d4f957d4385 Mon Sep 17 00:00:00 2001 From: Arsh Anwar <45912425+d4rk-lucif3r@users.noreply.github.com> Date: Sat, 1 Jun 2024 16:49:30 +0530 Subject: [PATCH 3/3] Create swift.yml --- .github/workflows/swift.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/workflows/swift.yml diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml new file mode 100644 index 0000000..5645dcd --- /dev/null +++ b/.github/workflows/swift.yml @@ -0,0 +1,22 @@ +# This workflow will build a Swift project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-swift + +name: Swift + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + build: + + runs-on: macos-latest + + steps: + - uses: actions/checkout@v4 + - name: Build + run: swift build -v + - name: Run tests + run: swift test -v