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

feat: add network type finder for iOS #49

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 12 additions & 11 deletions composeApp/src/iosMain/kotlin/org/ooni/probe/SetupDependencies.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.ooni.probe

import app.cash.sqldelight.driver.native.NativeSqliteDriver
import org.ooni.engine.NetworkTypeFinder
import org.ooni.engine.OonimkallBridge
import org.ooni.engine.models.NetworkType
import org.ooni.probe.di.Dependencies
import org.ooni.probe.shared.Platform
import org.ooni.probe.shared.PlatformInfo
Expand All @@ -17,16 +17,17 @@ import platform.UIKit.UIDevice
* See link for `baseFileDir` https://github.com/ooni/probe-ios/blob/2145bbd5eda6e696be216e3bce97e8d5fb33dcea/ooniprobe/Engine/Engine.m#L54
* See link for `cacheDir` https://github.com/ooni/probe-ios/blob/2145bbd5eda6e696be216e3bce97e8d5fb33dcea/ooniprobe/Engine/Engine.m#L66
*/
fun setupDependencies(bridge: OonimkallBridge) =
Dependencies(
platformInfo = platformInfo,
oonimkallBridge = bridge,
baseFileDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).first().toString(),
cacheDir = NSTemporaryDirectory(),
databaseDriverFactory = ::buildDatabaseDriver,
// TODO
networkTypeFinder = { NetworkType.Unknown("") },
)
fun setupDependencies(
bridge: OonimkallBridge,
networkTypeFinder: NetworkTypeFinder,
) = Dependencies(
platformInfo = platformInfo,
oonimkallBridge = bridge,
baseFileDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).first().toString(),
cacheDir = NSTemporaryDirectory(),
databaseDriverFactory = ::buildDatabaseDriver,
networkTypeFinder = networkTypeFinder,
)

private val platformInfo get() =
object : PlatformInfo {
Expand Down
6 changes: 6 additions & 0 deletions iosApp/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
058557D9273AAEEB004C7B11 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 058557D8273AAEEB004C7B11 /* Preview Assets.xcassets */; };
2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2152FB032600AC8F00CF470E /* iOSApp.swift */; };
7555FF83242A565900829871 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7555FF82242A565900829871 /* ContentView.swift */; };
79B9D19A2C6552DE004DCEE6 /* IosNetworkTypeFinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79B9D1992C6552DE004DCEE6 /* IosNetworkTypeFinder.swift */; };
79B9D19B2C6552DE004DCEE6 /* IosNetworkTypeFinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79B9D1992C6552DE004DCEE6 /* IosNetworkTypeFinder.swift */; };
79FBD00D2C5A70AF004E041C /* iOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2152FB032600AC8F00CF470E /* iOSApp.swift */; };
79FBD00E2C5A70AF004E041C /* IosOonimkallBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93E977702C4FCCE3009CCABC /* IosOonimkallBridge.swift */; };
79FBD00F2C5A70AF004E041C /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7555FF82242A565900829871 /* ContentView.swift */; };
Expand All @@ -32,6 +34,7 @@
7555FF7B242A565900829871 /* OONI Probe.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "OONI Probe.app"; sourceTree = BUILT_PRODUCTS_DIR; };
7555FF82242A565900829871 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
7555FF8C242A565B00829871 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
79B9D1992C6552DE004DCEE6 /* IosNetworkTypeFinder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IosNetworkTypeFinder.swift; sourceTree = "<group>"; };
79FBD01A2C5A70AF004E041C /* NewsMediaScan.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NewsMediaScan.app; sourceTree = BUILT_PRODUCTS_DIR; };
79FBD01B2C5A70AF004E041C /* iosApp copy-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "iosApp copy-Info.plist"; path = "/Users/aanorbel/Code/ooni/new/probe-multiplatform/iosApp/iosApp copy-Info.plist"; sourceTree = "<absolute>"; };
8918F4B39DEEFE89FDF20821 /* Pods-iosApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iosApp.debug.xcconfig"; path = "Target Support Files/Pods-iosApp/Pods-iosApp.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -130,6 +133,7 @@
isa = PBXGroup;
children = (
93E977702C4FCCE3009CCABC /* IosOonimkallBridge.swift */,
79B9D1992C6552DE004DCEE6 /* IosNetworkTypeFinder.swift */,
);
path = engine;
sourceTree = "<group>";
Expand Down Expand Up @@ -434,6 +438,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
79B9D19A2C6552DE004DCEE6 /* IosNetworkTypeFinder.swift in Sources */,
2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */,
93E977712C4FCCE3009CCABC /* IosOonimkallBridge.swift in Sources */,
7555FF83242A565900829871 /* ContentView.swift in Sources */,
Expand All @@ -444,6 +449,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
79B9D19B2C6552DE004DCEE6 /* IosNetworkTypeFinder.swift in Sources */,
79FBD00D2C5A70AF004E041C /* iOSApp.swift in Sources */,
79FBD00E2C5A70AF004E041C /* IosOonimkallBridge.swift in Sources */,
79FBD00F2C5A70AF004E041C /* ContentView.swift in Sources */,
Expand Down
69 changes: 69 additions & 0 deletions iosApp/iosApp/engine/IosNetworkTypeFinder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import composeApp
import Foundation
import Network

class IosNetworkTypeFinder : NetworkTypeFinder {
private let queue = DispatchQueue.global(qos: .utility)

private var pathMonitor: NWPathMonitor?

init() {
_ = ensurePathMonitor()
}

@discardableResult
private func ensurePathMonitor() -> NWPathMonitor {
if (pathMonitor == nil) {
let pathMonitor = NWPathMonitor()
pathMonitor.start(queue: queue)
self.pathMonitor = pathMonitor
}
return self.pathMonitor!
}

func invoke() -> any NetworkType {
if VpnChecker.isVpnActive() {
return NetworkTypeVPN()
}

let path = pathMonitor?.currentPath
if let path = path, path.status == .satisfied {
if path.usesInterfaceType(.wifi) {
return NetworkTypeWifi()
} else if path.usesInterfaceType(.cellular) {
return NetworkTypeMobile()
} else if path.usesInterfaceType(.wiredEthernet) {
return NetworkTypeUnknown(value: "wired_ethernet")
} else {
return NetworkTypeUnknown(value: "")
}
}
return NetworkTypeUnknown(value: "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this one be NoInternet?

}

}

// https://stackoverflow.com/a/61231348
struct VpnChecker {

private static let vpnProtocolsKeysIdentifiers = [
"tap", "tun", "ppp", "ipsec", "utun"
]

static func isVpnActive() -> Bool {
guard let cfDict = CFNetworkCopySystemProxySettings() else { return false }
let nsDict = cfDict.takeRetainedValue() as NSDictionary
guard let keys = nsDict["__SCOPED__"] as? NSDictionary,
let allKeys = keys.allKeys as? [String] else { return false }

// Checking for tunneling protocols in the keys
for key in allKeys {
for protocolId in vpnProtocolsKeysIdentifiers
where key.starts(with: protocolId) {
// I use start(with:), so I can cover also `ipsec4`, `ppp0`, `utun0` etc...
return true
}
}
return false
}
}
5 changes: 4 additions & 1 deletion iosApp/iosApp/iOSApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import composeApp

@main
struct iOSApp: App {
let dependencies = SetupDependenciesKt.setupDependencies(bridge: IosOonimkallBridge())
let dependencies = SetupDependenciesKt.setupDependencies(
bridge: IosOonimkallBridge(),
networkTypeFinder: IosNetworkTypeFinder()
)

var body: some Scene {
WindowGroup {
Expand Down