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

Update chat message with updated dribble #49

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
31 changes: 20 additions & 11 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/orlandos-nl/BSON.git",
"state" : {
"revision" : "98a2c90988895f1b985ed0066180995df995924c",
"version" : "7.0.28"
"revision" : "944dfb3b0eb028f477c25ba6a071181de8ab903a",
"version" : "8.0.10"
}
},
{
"identity" : "dribble",
"kind" : "remoteSourceControl",
"location" : "https://github.com/orlandos-nl/Dribble.git",
"location" : "https://github.com/needle-tail/Dribble.git",
"state" : {
"revision" : "afbfa0a83bd41880820b8883a765fd0d310eabfd",
"version" : "0.1.0"
"branch" : "main",
"revision" : "3b7ee80ccbf79965cadef58559cb960d0ab3b17d"
}
},
{
Expand All @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics",
"state" : {
"revision" : "919eb1d83e02121cdb434c7bfc1f0c66ef17febe",
"version" : "1.0.2"
"revision" : "cd142fd2f64be2100422d658e7411e39489da985",
"version" : "1.2.0"
}
},
{
Expand Down Expand Up @@ -68,8 +68,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "ddb07e896a2a8af79512543b1c7eb9797f8898a5",
"version" : "1.1.7"
"revision" : "60f13f60c4d093691934dc6cfdf5f508ada1f894",
"version" : "2.6.0"
}
},
{
Expand Down Expand Up @@ -104,8 +104,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "124119f0bb12384cef35aa041d7c3a686108722d",
"version" : "2.40.0"
"revision" : "54c85cb26308b89846d4671f23954dce088da2b0",
"version" : "2.60.0"
}
},
{
Expand All @@ -126,6 +126,15 @@
"version" : "2.17.2"
}
},
{
"identity" : "swift-nio-transport-services",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-transport-services.git",
"state" : {
"revision" : "ebf8b9c365a6ce043bf6e6326a04b15589bd285e",
"version" : "1.20.0"
}
},
{
"identity" : "swift-protobuf",
"kind" : "remoteSourceControl",
Expand Down
11 changes: 6 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/apple/swift-crypto.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-crypto.git", from: "2.6.0"),
// .package(name: "swift-nio", path: "/Users/joannisorlandos/git/joannis/swift-nio"),
// .package(name: "swift-nio-ssl", path: "/Users/joannisorlandos/git/joannis/swift-nio-ssl"),
// .package(name: "Dribble", path: "/Users/joannisorlandos/git/orlandos-nl/Dribble"),
.package(url: "https://github.com/apple/swift-distributed-actors.git", from: "1.0.0-beta.1.1"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.20.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.60.0"),
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.0.0"),
.package(url: "https://github.com/orlandos-nl/Dribble.git", from: "0.1.0"),
.package(url: "https://github.com/orlandos-nl/Dribble.git", from: "0.1.1"),
.package(url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0"),
.package(url: "https://github.com/vapor/websocket-kit.git", from: "2.0.0"),
.package(url: "https://github.com/orlandos-nl/BSON.git", from: "7.0.0"),
.package(url: "https://github.com/orlandos-nl/BSON.git", from: "8.0.9"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"),
.package(url: "https://github.com/Joannis/TaskQueue.git", from: "1.0.0"),
],
Expand Down Expand Up @@ -62,7 +63,7 @@ let package = Package(
.product(name: "NIO", package: "swift-nio"),
.product(name: "_NIOConcurrency", package: "swift-nio"),
.product(name: "NIOFoundationCompat", package: "swift-nio"),
// .product(name: "NIOTransportServices", package: "swift-nio-transport-services"),
.product(name: "NIOTransportServices", package: "swift-nio-transport-services"),
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "BSON", package: "BSON"),
]),
Expand Down
24 changes: 23 additions & 1 deletion Sources/CypherMessaging/Messenger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import Crypto
import TaskQueue
import NIO
import CypherProtocol
#if canImport(NIOTransportServices)
import NIOTransportServices
#endif

public enum DeviceRegisteryMode: Int, Codable, Sendable {
case masterDevice, childDevice, unregistered
Expand Down Expand Up @@ -257,7 +260,11 @@ public final class CypherMessenger: CypherTransportClientDelegate, P2PTransportC
p2pFactories: [P2PTransportClientFactory],
transport: CypherServerTransportClient
) async throws {
#if os(Linux) || os(Android) || os(Windows)
self.eventLoop = MultiThreadedEventLoopGroup(numberOfThreads: 1).next()
#else
self.eventLoop = NIOTSEventLoopGroup().next()
#endif
self.eventHandler = eventHandler
self.username = config.username
self.deviceId = config.deviceKeys.deviceId
Expand Down Expand Up @@ -746,7 +753,8 @@ public final class CypherMessenger: CypherTransportClientDelegate, P2PTransportC

try config.addDeviceConfig(deviceConfig, signedWith: await state.config.deviceKeys.identity)
try await self.transport.publishKeyBundle(config)
let uploadedConfig = config
/// We need to reread the keybundle because at this point in time we have added a new device, and for that to actually be in the uploadedConfig property, we need to fetch it again rather than rely on the original bundle that we read.
let uploadedConfig = try await transport.readKeyBundle(forUsername: self.username)
try await self.updateConfig { appConfig in
appConfig.lastKnownUserConfig = uploadedConfig
}
Expand Down Expand Up @@ -1305,6 +1313,20 @@ public final class CypherMessenger: CypherTransportClientDelegate, P2PTransportC
}
}

extension CypherMessenger {
@CryptoActor
public func updateChatMessage(_ message: DecryptedModel<ChatMessageModel>) async throws {
try await self.cachedStore.updateChatMessage(message.encrypted)
await self.eventHandler.onMessageChange(
AnyChatMessage(
target: message.props.message.target,
messenger: self,
raw: message
)
)
}
}

extension Contact {
@CypherTextKitActor public func listDevices() async throws -> [UserDevice] {
return try await messenger._fetchDeviceIdentities(for: username).map { device in
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Dribble
import NIO
//import NIOTransportServices
#if canImport(NIOTransportServices)
import NIOTransportServices
#endif

public enum IPv6TCPP2PError: Error {
case reconnectFailed, timeout, socketCreationFailed
Expand Down Expand Up @@ -102,10 +104,15 @@ public final class IPv6TCPP2PTransportClientFactory: P2PTransportClientFactory {
public let transportLayerIdentifier = "_ipv6-tcp"
public let isMeshEnabled = false
public weak var delegate: P2PTransportFactoryDelegate?

#if os(Linux) || os(Android) || os(Windows)
let eventLoop = MultiThreadedEventLoopGroup(numberOfThreads: 1).next()
#else
let eventLoop = NIOTSEventLoopGroup().next()
#endif
let stun: StunConfig?

public init(stun: StunConfig? = nil) {
public init(stun: StunConfig? = nil) async {
self.stun = stun
}

Expand Down Expand Up @@ -189,17 +196,18 @@ public final class IPv6TCPP2PTransportClientFactory: P2PTransportClientFactory {

guard
let localAddress = channel.localAddress,
let port = localAddress.port
let port = localAddress.port,
let ip = address.ipAddress
else {
promise.fail(IPv6TCPP2PError.socketCreationFailed)
return self.eventLoop.makeFailedFuture(IPv6TCPP2PError.socketCreationFailed)
}

return channel.eventLoop.executeAsync {
try await handle.sendMessage("", metadata: [
"ip": address.ipAddress,
"ip": ip,
"port": port
])
] as! Document)
}
}.whenFailure { error in
debugLog("Failed to host IPv6 Server", error)
Expand Down
2 changes: 1 addition & 1 deletion Sources/CypherMessaging/Primitives/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Joannis Orlandos on 19/04/2021.
//

typealias CacheActor = MainActor
public typealias CacheActor = MainActor

public protocol CacheKey {
associatedtype Value
Expand Down
7 changes: 7 additions & 0 deletions Sources/CypherMessaging/TestSupport/SpoofTransport.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import CypherProtocol
import Foundation
import NIO
#if canImport(NIOTransportServices)
import NIOTransportServices
#endif

public enum SpoofTransportClientSettings {
public enum PacketType: Sendable {
Expand All @@ -25,7 +28,11 @@ public enum SpoofTransportClientSettings {
}

fileprivate final class SpoofServer {
#if os(Linux) || os(Android) || os(Windows)
fileprivate let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
#else
fileprivate let elg = NIOTSEventLoopGroup()
#endif
fileprivate var onlineDevices = [SpoofTransportClient]()
private var backlog = [DeviceId: [CypherServerEvent]]()
private var userDevices = [Username: Set<DeviceId>]()
Expand Down
3 changes: 2 additions & 1 deletion Sources/MessagingHelpers/VaporTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import CypherProtocol
import CypherMessaging
import JWTKit
import WebSocketKit
import NIOTransportServices

// TODO: Secondary servers

Expand Down Expand Up @@ -209,7 +210,7 @@ public final class VaporTransport: CypherServerTransportClient {
self.host = host
self.username = username
self.deviceId = deviceId
self.eventLoop = MultiThreadedEventLoopGroup(numberOfThreads: 1).next()
self.eventLoop = NIOTSEventLoopGroup().next()
self.httpClient = httpClient
self.signer = signer
}
Expand Down