Fix usages of writeFatalError
This commit is contained in:
@@ -3,3 +3,17 @@ import Foundation
|
|||||||
public enum FullDiskAccessPermissionRequired: Error {
|
public enum FullDiskAccessPermissionRequired: Error {
|
||||||
case error
|
case error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ExtensionStartupError: Error {
|
||||||
|
let message: String
|
||||||
|
|
||||||
|
init(_ message: String) {
|
||||||
|
self.message = message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension ExtensionStartupError: LocalizedError {
|
||||||
|
public var errorDescription: String? {
|
||||||
|
message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -357,12 +357,12 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func serviceStop() throws {
|
public func serviceStop() throws {
|
||||||
reset()
|
tunnel.stopService()
|
||||||
}
|
}
|
||||||
|
|
||||||
public func serviceReload() throws {
|
public func serviceReload() throws {
|
||||||
runBlocking { [self] in
|
try runBlocking { [self] in
|
||||||
await tunnel.reloadService()
|
try await tunnel.reloadService()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,15 +23,13 @@ open class ExtensionProvider: NEPacketTunnelProvider {
|
|||||||
var setupError: NSError?
|
var setupError: NSError?
|
||||||
LibboxSetup(options, &setupError)
|
LibboxSetup(options, &setupError)
|
||||||
if let setupError {
|
if let setupError {
|
||||||
writeFatalError("(packet-tunnel) error: setup service: \(setupError.localizedDescription)")
|
throw ExtensionStartupError("(packet-tunnel) error: setup service: \(setupError.localizedDescription)")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var stderrError: NSError?
|
var stderrError: NSError?
|
||||||
LibboxRedirectStderr(FilePath.cacheDirectory.appendingPathComponent("stderr.log").relativePath, &stderrError)
|
LibboxRedirectStderr(FilePath.cacheDirectory.appendingPathComponent("stderr.log").relativePath, &stderrError)
|
||||||
if let stderrError {
|
if let stderrError {
|
||||||
writeFatalError("(packet-tunnel) redirect stderr error: \(stderrError.localizedDescription)")
|
throw ExtensionStartupError("(packet-tunnel) redirect stderr error: \(stderrError.localizedDescription)")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await LibboxSetMemoryLimit(!SharedPreferences.ignoreMemoryLimit.get())
|
await LibboxSetMemoryLimit(!SharedPreferences.ignoreMemoryLimit.get())
|
||||||
@@ -42,17 +40,15 @@ open class ExtensionProvider: NEPacketTunnelProvider {
|
|||||||
var error: NSError?
|
var error: NSError?
|
||||||
commandServer = LibboxNewCommandServer(platformInterface, platformInterface, &error)
|
commandServer = LibboxNewCommandServer(platformInterface, platformInterface, &error)
|
||||||
if let error {
|
if let error {
|
||||||
writeFatalError("(packet-tunnel): create command server error: \(error.localizedDescription)")
|
throw ExtensionStartupError("(packet-tunnel): create command server error: \(error.localizedDescription)")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
try commandServer.start()
|
try commandServer.start()
|
||||||
} catch {
|
} catch {
|
||||||
writeFatalError("(packet-tunnel): start command server error: \(error.localizedDescription)")
|
throw ExtensionStartupError("(packet-tunnel): start command server error: \(error.localizedDescription)")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
writeMessage("(packet-tunnel): Here I stand")
|
writeMessage("(packet-tunnel): Here I stand")
|
||||||
await startService()
|
try await startService()
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
if #available(iOS 18.0, *) {
|
if #available(iOS 18.0, *) {
|
||||||
ControlCenter.shared.reloadControls(ofKind: ExtensionProfile.controlKind)
|
ControlCenter.shared.reloadControls(ofKind: ExtensionProfile.controlKind)
|
||||||
@@ -76,31 +72,27 @@ open class ExtensionProvider: NEPacketTunnelProvider {
|
|||||||
cancelTunnelWithError(nil)
|
cancelTunnelWithError(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func startService() async {
|
private func startService() async throws {
|
||||||
let profile: Profile?
|
let profile: Profile?
|
||||||
do {
|
do {
|
||||||
profile = try await ProfileManager.get(Int64(SharedPreferences.selectedProfileID.get()))
|
profile = try await ProfileManager.get(Int64(SharedPreferences.selectedProfileID.get()))
|
||||||
} catch {
|
} catch {
|
||||||
writeFatalError("(packet-tunnel) error: read selected profile: \(error.localizedDescription)")
|
throw ExtensionStartupError("(packet-tunnel) error: read selected profile: \(error.localizedDescription)")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
guard let profile else {
|
guard let profile else {
|
||||||
writeFatalError("(packet-tunnel) error: missing selected profile")
|
throw ExtensionStartupError("(packet-tunnel) error: missing selected profile")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
let configContent: String
|
let configContent: String
|
||||||
do {
|
do {
|
||||||
configContent = try profile.read()
|
configContent = try profile.read()
|
||||||
} catch {
|
} catch {
|
||||||
writeFatalError("(packet-tunnel) error: read config file \(profile.path): \(error.localizedDescription)")
|
throw ExtensionStartupError("(packet-tunnel) error: read config file \(profile.path): \(error.localizedDescription)")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
let options = LibboxOverrideOptions()
|
let options = LibboxOverrideOptions()
|
||||||
do {
|
do {
|
||||||
try commandServer.startOrReloadService(configContent, options: options)
|
try commandServer.startOrReloadService(configContent, options: options)
|
||||||
} catch {
|
} catch {
|
||||||
writeFatalError("(packet-tunnel) error: start service: \(error.localizedDescription)")
|
throw ExtensionStartupError("(packet-tunnel) error: start service: \(error.localizedDescription)")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
await SharedPreferences.startedByUser.set(true)
|
await SharedPreferences.startedByUser.set(true)
|
||||||
@@ -132,7 +124,7 @@ open class ExtensionProvider: NEPacketTunnelProvider {
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private func stopService() {
|
func stopService() {
|
||||||
do {
|
do {
|
||||||
try commandServer.closeService()
|
try commandServer.closeService()
|
||||||
} catch {
|
} catch {
|
||||||
@@ -143,13 +135,13 @@ open class ExtensionProvider: NEPacketTunnelProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func reloadService() async {
|
func reloadService() async throws {
|
||||||
writeMessage("(packet-tunnel) reloading service")
|
writeMessage("(packet-tunnel) reloading service")
|
||||||
reasserting = true
|
reasserting = true
|
||||||
defer {
|
defer {
|
||||||
reasserting = false
|
reasserting = false
|
||||||
}
|
}
|
||||||
await startService()
|
try await startService()
|
||||||
}
|
}
|
||||||
|
|
||||||
override open func stopTunnel(with reason: NEProviderStopReason) async {
|
override open func stopTunnel(with reason: NEProviderStopReason) async {
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ import System
|
|||||||
class PacketTunnelProvider: ExtensionProvider {
|
class PacketTunnelProvider: ExtensionProvider {
|
||||||
override func startTunnel(options: [String: NSObject]?) async throws {
|
override func startTunnel(options: [String: NSObject]?) async throws {
|
||||||
guard let usernameObject = options?["username"] else {
|
guard let usernameObject = options?["username"] else {
|
||||||
writeFatalError("missing start options")
|
throw ExtensionStartupError("missing start options")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
let username = usernameObject as! NSString
|
let username = usernameObject as! NSString
|
||||||
FilePath.sharedDirectory = URL(filePath: "/Users/\(username)/Library/Group Containers/\(FilePath.groupName)")
|
FilePath.sharedDirectory = URL(filePath: "/Users/\(username)/Library/Group Containers/\(FilePath.groupName)")
|
||||||
|
|||||||
Reference in New Issue
Block a user