tunnelkit/Sources/TunnelKitWireGuardAppExtension/Internal/ErrorNotifier.swift
Davide De Rosa fda232edcb
Add WireGuard package (#236)
* Add WireGuard packages

- Use eduVPN script for WireGuardKitGo
- Wrap WireGuardKit entities into Configuration
- Split demo into OpenVPN/WireGuard controllers

* Rewrite README with multiple VPN protocols
2021-12-01 13:54:00 +01:00

43 lines
1.3 KiB
Swift

import TunnelKitWireGuardCore
import TunnelKitWireGuardManager
// SPDX-License-Identifier: MIT
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
import NetworkExtension
class ErrorNotifier {
private let appGroupId: String
private var sharedFolderURL: URL? {
guard let sharedFolderURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupId) else {
wg_log(.error, message: "Cannot obtain shared folder URL")
return nil
}
return sharedFolderURL
}
init(appGroupId: String) {
self.appGroupId = appGroupId
removeLastErrorFile()
}
func notify(_ error: WireGuardProviderError) {
guard let lastErrorFilePath = networkExtensionLastErrorFileURL?.path else {
return
}
let errorMessageData = "\(error)".data(using: .utf8)
FileManager.default.createFile(atPath: lastErrorFilePath, contents: errorMessageData, attributes: nil)
}
func removeLastErrorFile() {
if let lastErrorFileURL = networkExtensionLastErrorFileURL {
try? FileManager.default.removeItem(at: lastErrorFileURL)
}
}
private var networkExtensionLastErrorFileURL: URL? {
return sharedFolderURL?.appendingPathComponent("last-error.txt")
}
}