-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import VPN configuration file into module (#1238)
Add feature to WireGuard, and unify behavior in OpenVPN. Always allow import, even if a configuration exists in the module. The ImportModifier might be reused later if more modules need it (there is some degree of duplication).
- Loading branch information
Showing
11 changed files
with
173 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
Packages/App/Sources/AppUIMain/Views/Modules/WireGuard/WireGuardView+Import.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// | ||
// WireGuardView+Import.swift | ||
// Passepartout | ||
// | ||
// Created by Davide De Rosa on 3/7/25. | ||
// Copyright (c) 2025 Davide De Rosa. All rights reserved. | ||
// | ||
// https://github.com/passepartoutvpn | ||
// | ||
// This file is part of Passepartout. | ||
// | ||
// Passepartout is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Passepartout is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>. | ||
// | ||
|
||
import CommonLibrary | ||
import CommonUtils | ||
import PassepartoutKit | ||
import SwiftUI | ||
|
||
extension WireGuardView { | ||
struct ImportModifier: ViewModifier { | ||
|
||
@ObservedObject | ||
var draft: ModuleDraft<WireGuardModule.Builder> | ||
|
||
let impl: WireGuardModule.Implementation? | ||
|
||
@Binding | ||
var isImporting: Bool | ||
|
||
@ObservedObject | ||
var errorHandler: ErrorHandler | ||
|
||
@State | ||
private var importURL: URL? | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.fileImporter( | ||
isPresented: $isImporting, | ||
allowedContentTypes: [.item], | ||
onCompletion: importConfiguration | ||
) | ||
} | ||
} | ||
} | ||
|
||
private extension WireGuardView.ImportModifier { | ||
func importConfiguration(from result: Result<URL, Error>) { | ||
do { | ||
let url = try result.get() | ||
guard url.startAccessingSecurityScopedResource() else { | ||
throw AppError.permissionDenied | ||
} | ||
defer { | ||
url.stopAccessingSecurityScopedResource() | ||
} | ||
importURL = url | ||
|
||
guard let impl else { | ||
fatalError("Requires WireGuardModule implementation") | ||
} | ||
let parsed: Module | ||
do { | ||
parsed = try impl.importer.module(fromURL: url, object: nil) | ||
} catch let error as PassepartoutError { | ||
pp_log(.app, .error, "Unable to parse URL: \(error)") | ||
|
||
switch error.code { | ||
case .unknownImportedModule: | ||
throw PassepartoutError(.parsing) | ||
|
||
default: | ||
throw error | ||
} | ||
} | ||
guard let module = parsed as? WireGuardModule else { | ||
throw PassepartoutError(.parsing) | ||
} | ||
draft.module.configurationBuilder = module.configuration?.builder() | ||
} catch { | ||
pp_log(.app, .error, "Unable to import WireGuard configuration: \(error)") | ||
errorHandler.handle(error, title: draft.module.moduleType.localizedDescription) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
Packages/App/Sources/UILibrary/Views/Modules/ModuleImportSection.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// ModuleImportSection.swift | ||
// Passepartout | ||
// | ||
// Created by Davide De Rosa on 3/7/25. | ||
// Copyright (c) 2025 Davide De Rosa. All rights reserved. | ||
// | ||
// https://github.com/passepartoutvpn | ||
// | ||
// This file is part of Passepartout. | ||
// | ||
// Passepartout is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Passepartout is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public struct ModuleImportSection: View { | ||
|
||
@Binding | ||
private var isImporting: Bool | ||
|
||
public init(isImporting: Binding<Bool>) { | ||
_isImporting = isImporting | ||
} | ||
|
||
public var body: some View { | ||
Section { | ||
Button(Strings.Modules.General.Rows.importFromFile.forMenu) { | ||
isImporting = true | ||
} | ||
} | ||
} | ||
} |