2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// OnDemand+Rules.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/14/22.
|
2023-03-17 15:56:19 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// 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 Foundation
|
|
|
|
import NetworkExtension
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutCore
|
2023-05-24 16:19:47 +00:00
|
|
|
import PassepartoutVPN
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-04-19 11:29:16 +00:00
|
|
|
extension NEOnDemandRuleInterfaceType {
|
|
|
|
static var compatibleEthernet: NEOnDemandRuleInterfaceType? {
|
|
|
|
#if targetEnvironment(macCatalyst)
|
2023-07-23 06:44:46 +00:00
|
|
|
// XXX: Catalyst, missing enum case, try hardcoding
|
2022-04-19 11:29:16 +00:00
|
|
|
// https://developer.apple.com/documentation/networkextension/neondemandruleinterfacetype/ethernet
|
|
|
|
NEOnDemandRuleInterfaceType(rawValue: 1)
|
|
|
|
#elseif os(macOS)
|
|
|
|
.ethernet
|
|
|
|
#else
|
|
|
|
nil
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:19:47 +00:00
|
|
|
extension VPNConfigurationParameters {
|
|
|
|
var onDemandRules: [NEOnDemandRule] {
|
|
|
|
profile.onDemand.rules(isInteractive: profile.account.authenticationMethod == .interactive, withCustomRules: withCustomRules)
|
2023-03-17 15:49:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension Profile.OnDemand {
|
|
|
|
func rules(isInteractive: Bool, withCustomRules: Bool) -> [NEOnDemandRule] {
|
|
|
|
guard isEnabled && !isInteractive else {
|
|
|
|
return []
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
var rules: [NEOnDemandRule] = []
|
2023-07-23 06:44:46 +00:00
|
|
|
|
|
|
|
// apply exceptions (unless .any)
|
|
|
|
if withCustomRules && policy != .any {
|
2023-12-20 19:43:39 +00:00
|
|
|
#if os(iOS)
|
2022-05-17 09:32:19 +00:00
|
|
|
if Utils.hasCellularData() && withMobileNetwork {
|
2023-07-23 06:44:46 +00:00
|
|
|
rules.append(cellularRule())
|
2022-04-13 17:12:16 +00:00
|
|
|
}
|
2023-12-20 19:43:39 +00:00
|
|
|
#endif
|
2024-01-10 19:13:08 +00:00
|
|
|
#if targetEnvironment(macCatalyst) || os(macOS)
|
2022-05-17 09:32:19 +00:00
|
|
|
if Utils.hasEthernet() && withEthernetNetwork {
|
2023-07-23 06:44:46 +00:00
|
|
|
if let rule = ethernetRule() {
|
2022-04-19 11:29:16 +00:00
|
|
|
rules.append(rule)
|
|
|
|
} else {
|
|
|
|
pp_log.warning("Unable to add rule for NEOnDemandRuleInterfaceType.ethernet (not compatible)")
|
|
|
|
}
|
2022-04-13 17:12:16 +00:00
|
|
|
}
|
2023-12-20 19:43:39 +00:00
|
|
|
#endif
|
2022-04-13 17:12:16 +00:00
|
|
|
let SSIDs = Array(withSSIDs.filter { $1 }.keys)
|
|
|
|
if !SSIDs.isEmpty {
|
2023-07-23 06:44:46 +00:00
|
|
|
rules.append(wifiRule(SSIDs: SSIDs))
|
2022-04-13 17:12:16 +00:00
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-23 06:44:46 +00:00
|
|
|
|
|
|
|
// IMPORTANT: append fallback rule last
|
|
|
|
rules.append(globalRule())
|
|
|
|
|
2024-01-10 18:43:35 +00:00
|
|
|
pp_log.debug("On-demand rules:")
|
|
|
|
rules.forEach {
|
|
|
|
pp_log.debug("\($0)")
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
return rules
|
|
|
|
}
|
2023-07-23 06:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension Profile.OnDemand {
|
|
|
|
func globalRule() -> NEOnDemandRule {
|
|
|
|
let rule: NEOnDemandRule
|
|
|
|
switch policy {
|
|
|
|
case .any, .excluding:
|
|
|
|
rule = NEOnDemandRuleConnect()
|
|
|
|
|
|
|
|
case .including:
|
|
|
|
rule = NEOnDemandRuleDisconnect()
|
|
|
|
}
|
|
|
|
rule.interfaceTypeMatch = .any
|
|
|
|
return rule
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkRule(matchingInterface interfaceType: NEOnDemandRuleInterfaceType) -> NEOnDemandRule {
|
|
|
|
let rule: NEOnDemandRule
|
|
|
|
switch policy {
|
|
|
|
case .any, .excluding:
|
|
|
|
rule = NEOnDemandRuleDisconnect()
|
|
|
|
|
|
|
|
case .including:
|
|
|
|
rule = NEOnDemandRuleConnect()
|
|
|
|
}
|
|
|
|
rule.interfaceTypeMatch = interfaceType
|
|
|
|
return rule
|
|
|
|
}
|
|
|
|
|
2023-12-20 19:43:39 +00:00
|
|
|
#if os(iOS)
|
2023-07-23 06:44:46 +00:00
|
|
|
func cellularRule() -> NEOnDemandRule {
|
|
|
|
networkRule(matchingInterface: .cellular)
|
|
|
|
}
|
2023-12-20 19:43:39 +00:00
|
|
|
#endif
|
2023-07-23 06:44:46 +00:00
|
|
|
|
2024-01-10 19:13:08 +00:00
|
|
|
#if targetEnvironment(macCatalyst) || os(macOS)
|
2023-07-23 06:44:46 +00:00
|
|
|
func ethernetRule() -> NEOnDemandRule? {
|
|
|
|
guard let compatibleEthernet = NEOnDemandRuleInterfaceType.compatibleEthernet else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return networkRule(matchingInterface: compatibleEthernet)
|
|
|
|
}
|
2023-12-20 19:43:39 +00:00
|
|
|
#endif
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-07-23 06:44:46 +00:00
|
|
|
func wifiRule(SSIDs: [String]) -> NEOnDemandRule {
|
|
|
|
let rule = networkRule(matchingInterface: .wiFi)
|
|
|
|
rule.ssidMatch = SSIDs
|
|
|
|
return rule
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|