diff --git a/Library/Database/SharedPreferences.swift b/Library/Database/SharedPreferences.swift index 791acd3..6ad27c1 100644 --- a/Library/Database/SharedPreferences.swift +++ b/Library/Database/SharedPreferences.swift @@ -58,6 +58,11 @@ public enum SharedPreferences { public static let systemProxyEnabled = Preference("system_proxy_enabled", defaultValue: true) + #if os(tvOS) + public static let commandServerPort = Preference("tv_command_server_port", defaultValue: 0) + public static let commandServerSecret = Preference("tv_command_server_secret", defaultValue: "") + #endif + // Profile Override public static let excludeDefaultRoute = Preference("exclude_default_route", defaultValue: false) diff --git a/Library/Network/ExtensionProvider.swift b/Library/Network/ExtensionProvider.swift index f6f9255..168013c 100644 --- a/Library/Network/ExtensionProvider.swift +++ b/Library/Network/ExtensionProvider.swift @@ -19,6 +19,12 @@ open class ExtensionProvider: NEPacketTunnelProvider { options.workingPath = FilePath.workingDirectory.relativePath options.tempPath = FilePath.cacheDirectory.relativePath options.logMaxLines = 3000 + + #if os(tvOS) + options.commandServerListenPort = await SharedPreferences.commandServerPort.get() + options.commandServerSecret = await SharedPreferences.commandServerSecret.get() + #endif + var setupError: NSError? LibboxSetup(options, &setupError) if let setupError { @@ -61,16 +67,6 @@ open class ExtensionProvider: NEPacketTunnelProvider { } } - public func writeFatalError(_ message: String) { - #if DEBUG - NSLog(message) - #endif - if let commandServer { - commandServer.setError(message) - } - cancelTunnelWithError(nil) - } - private func startService() async throws { let profile: Profile? do { diff --git a/SFT/ApplicationDelegate.swift b/SFT/ApplicationDelegate.swift index 66b6c4f..e58e9d3 100644 --- a/SFT/ApplicationDelegate.swift +++ b/SFT/ApplicationDelegate.swift @@ -11,6 +11,23 @@ class ApplicationDelegate: NSObject, UIApplicationDelegate { options.basePath = FilePath.sharedDirectory.relativePath options.workingPath = FilePath.workingDirectory.relativePath options.tempPath = FilePath.cacheDirectory.relativePath + var port = SharedPreferences.commandServerPort.getBlocking() + var secret = SharedPreferences.commandServerSecret.getBlocking() + if port == 0 || secret.isEmpty { + var error: NSError? + LibboxAvailablePort(7990, &port, &error) + if let error { + port = 7990 + NSLog("Failed to get available port for control server: \(error.localizedDescription)") + } + secret = LibboxRandomHex(16)!.value + Task { + await SharedPreferences.commandServerPort.set(port) + await SharedPreferences.commandServerSecret.set(secret) + } + } + options.commandServerListenPort = port + options.commandServerSecret = secret var error: NSError? LibboxSetup(options, &error) LibboxSetLocale(Locale.current.identifier)