Skip to content

Commit

Permalink
SwiftNIO: Add TLS support
Browse files Browse the repository at this point in the history
  • Loading branch information
yuroyami committed Apr 5, 2024
1 parent 3922bf6 commit 6e679b1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
30 changes: 21 additions & 9 deletions iosApp/iosApp/SpProtocolIApple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import NIO
import NIOFoundationCompat
import NIOTransportServices
import NIOExtras
import NIOSSL
import shared

@objc class SpProtocolApple: SyncplayProtocol, ChannelInboundHandler {
class SpProtocolApple: SyncplayProtocol, ChannelInboundHandler {
typealias InboundIn = ByteBuffer

private var channel: Channel?
private var eventLoopGroup: EventLoopGroup?

//override let engine = SyncplayProtocol.NetworkEngine.swiftnio

@objc override func connectSocket() {
override func connectSocket() {
let group = NIOTSEventLoopGroup()
eventLoopGroup = group

Expand Down Expand Up @@ -46,15 +47,15 @@ import shared

}

@objc override func isSocketValid() -> Bool {
override func isSocketValid() -> Bool {
return channel?.isActive ?? false
}

@objc override func supportsTLS() -> Bool {
return false
override func supportsTLS() -> Bool {
return true
}

@objc override func endConnection(terminating: Bool) {
override func endConnection(terminating: Bool) {
try? channel?.close().wait()
try? eventLoopGroup?.syncShutdownGracefully()

Expand All @@ -63,7 +64,7 @@ import shared
}
}

@objc override func writeActualString(s: String) {
override func writeActualString(s: String) {
guard let channel = channel else {
syncplayCallback?.onDisconnected()
return
Expand All @@ -82,8 +83,19 @@ import shared
}
}

@objc override func upgradeTls() {
// TLS setup for SwiftNIO
override func upgradeTls() {
if (channel != nil) {
do {
let configuration = TLSConfiguration.makeClientConfiguration()
let sslContext = try NIOSSLContext(configuration: configuration)
let tlsHandler = try NIOSSLClientHandler(context: sslContext, serverHostname: "syncplay.pl")
try channel?.pipeline.addHandler(tlsHandler, position: .first).wait()
} catch {
print("Error initializing TLS: \(error)")
// Handle error appropriately (e.g., call onConnectionFailed())
self.syncplayCallback?.onConnectionFailed()
}
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ val EnStrings = object : Strings {

override val settingNetworkEngineTitle = "Network Engine"
override val settingNetworkEngineSummary = "The network engine serves as the backbone of connectivity. Experiment with each option and choose the one that offers the most stability for your needs."
override val settingNetworkEngineNetty = "Netty (Recommended, TLS)"
override val settingNetworkEngineSwiftNIO = "SwiftNIO (Recommended)"
override val settingNetworkEngineNetty = "Netty (Recommended + TLS)"
override val settingNetworkEngineSwiftNIO = "SwiftNIO (Recommended + TLS)"
override val settingNetworkEngineKtor = "Ktor (Experimental)"

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ class SpProtocolApple {

/** The implementation of the native iOS network client is done in pure Swift
* using SwiftNIO due to its extreme resemblance with Java's Netty.
* It's also as stable and completely reliable, unlike Ktor, which
* keeps causing crashes (I suspect it still doesn't know how to
* do memory management yet, or perhaps I haven't implemented it
* correctly).
* It's also as stable and completely reliable, and supports TLS.
*
* Unlike Ktor, which is contrary to all above:
* keeps causing crashes, doesn't support TLS, terrible concurrency...etc
*
* You can find said SwiftNIO implementation in iosApp native code */
}

0 comments on commit 6e679b1

Please sign in to comment.