Files
app/ios/DModem/Sources/VpnTunnel.swift
T
Claude 9915decc6d Comments for every function: the last batch; CI checks it
The UI tests, Gradle's script, the iOS app and the native test hosts
(batch 6 of 8, comment lines only) and the checker's own. Every
function in the app is now documented; android.yml's docs job runs
tools/doc_check.py first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
2026-10-27 08:23:54 +00:00

63 lines
2.4 KiB
Swift

// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Neo-Desktop
import DModemShared
import Foundation
import NetworkExtension
/// The phone's traffic through the dial-up link: the app's packet tunnel
/// (the DModemTunnel extension) started with the link's address and DNS,
/// told where the shared Kotlin's UdpPacketTunnel listens for its packets.
/// The first time, iOS asks the user to allow the VPN configuration.
final class VpnTunnel: NSObject, TunnelStarter, @unchecked Sendable {
static let extensionId = "com.neodesktop.dmodem.tunnel"
private var manager: NETunnelProviderManager?
/// (on the link's thread, never the main one: it waits for the system)
func start(address: String, dns: String, mtu: Int32, port: Int32) -> Bool {
let done = DispatchSemaphore(value: 0)
let result = Started()
Task {
do {
let m = try await Self.manager()
try m.connection.startVPNTunnel(options: [
"address": address as NSString, "dns": dns as NSString,
"mtu": NSNumber(value: mtu), "port": NSNumber(value: port),
])
self.manager = m
result.ok = true
} catch {
NSLog("vpn: \(error)")
}
done.signal()
}
done.wait()
return result.ok
}
/// (whether it started, set by the task the start waits for)
private final class Started: @unchecked Sendable { var ok = false }
/// the packet tunnel stopped (if it was started)
func stop() {
manager?.connection.stopVPNTunnel()
manager = nil
}
/// the app's VPN configuration: found, or made (and saved, which the user allows)
private static func manager() async throws -> NETunnelProviderManager {
let all = try await NETunnelProviderManager.loadAllFromPreferences()
let m = all.first { ($0.protocolConfiguration as? NETunnelProviderProtocol)?.providerBundleIdentifier == extensionId }
?? NETunnelProviderManager()
let p = NETunnelProviderProtocol()
p.providerBundleIdentifier = extensionId
p.serverAddress = "Dial-up"
m.protocolConfiguration = p
m.localizedDescription = "D-Modem dial-up"
m.isEnabled = true
try await m.saveToPreferences()
// (loaded again after a save, or the start fails)
try await m.loadFromPreferences()
return m
}
}