2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// OnDemand+Rules.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/14/22.
|
|
|
|
// Copyright (c) 2022 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 Foundation
|
|
|
|
import NetworkExtension
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutCore
|
|
|
|
import PassepartoutUtils
|
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)
|
|
|
|
// FIXME: Catalyst, missing enum case, try hardcoding
|
|
|
|
// https://developer.apple.com/documentation/networkextension/neondemandruleinterfacetype/ethernet
|
|
|
|
NEOnDemandRuleInterfaceType(rawValue: 1)
|
|
|
|
#elseif os(macOS)
|
|
|
|
.ethernet
|
|
|
|
#else
|
|
|
|
nil
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
extension Profile.OnDemand {
|
2022-04-13 17:12:16 +00:00
|
|
|
func rules(withCustomRules: Bool) -> [NEOnDemandRule] {
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-04-14 05:24:03 +00:00
|
|
|
// TODO: on-demand, drop hardcoding when "trusted networks" -> "on-demand"
|
|
|
|
// isEnabled = true
|
|
|
|
// policy = .excluding
|
2022-04-12 13:09:14 +00:00
|
|
|
assert(policy == .excluding)
|
2022-04-14 05:24:03 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
var rules: [NEOnDemandRule] = []
|
2022-04-13 17:12:16 +00:00
|
|
|
if withCustomRules {
|
2022-04-19 12:18:26 +00:00
|
|
|
#if os(iOS)
|
2022-05-17 09:32:19 +00:00
|
|
|
if Utils.hasCellularData() && withMobileNetwork {
|
2022-04-13 17:12:16 +00:00
|
|
|
let rule = policyRule
|
|
|
|
rule.interfaceTypeMatch = .cellular
|
|
|
|
rules.append(rule)
|
|
|
|
}
|
2022-04-19 12:18:26 +00:00
|
|
|
#endif
|
2022-05-17 09:32:19 +00:00
|
|
|
if Utils.hasEthernet() && withEthernetNetwork {
|
2022-04-19 11:29:16 +00:00
|
|
|
if let compatibleEthernet = NEOnDemandRuleInterfaceType.compatibleEthernet {
|
|
|
|
let rule = policyRule
|
|
|
|
rule.interfaceTypeMatch = compatibleEthernet
|
|
|
|
rules.append(rule)
|
|
|
|
} else {
|
|
|
|
pp_log.warning("Unable to add rule for NEOnDemandRuleInterfaceType.ethernet (not compatible)")
|
|
|
|
}
|
2022-04-13 17:12:16 +00:00
|
|
|
}
|
|
|
|
let SSIDs = Array(withSSIDs.filter { $1 }.keys)
|
|
|
|
if !SSIDs.isEmpty {
|
|
|
|
let rule = policyRule
|
|
|
|
rule.interfaceTypeMatch = .wiFi
|
|
|
|
rule.ssidMatch = SSIDs
|
|
|
|
rules.append(rule)
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
let connection = NEOnDemandRuleConnect()
|
|
|
|
connection.interfaceTypeMatch = .any
|
|
|
|
rules.append(connection)
|
|
|
|
return rules
|
|
|
|
}
|
|
|
|
|
|
|
|
private var policyRule: NEOnDemandRule {
|
2022-09-04 18:09:31 +00:00
|
|
|
disconnectsIfNotMatching ? NEOnDemandRuleDisconnect() : NEOnDemandRuleIgnore()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
}
|