From 95e9bc5fb268be782ed5b36f45cc08ec0bcba8f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 11 Mar 2024 17:38:04 +0800 Subject: [PATCH] Add options to exclude APNs route --- .../Views/Setting/ProfileOverrideView.swift | 13 +++++++++++++ Library/Database/SharedPreferences.swift | 2 ++ Library/Network/ExtensionPlatformInterface.swift | 11 +++++++++++ 3 files changed, 26 insertions(+) diff --git a/ApplicationLibrary/Views/Setting/ProfileOverrideView.swift b/ApplicationLibrary/Views/Setting/ProfileOverrideView.swift index a148d57..1cd996b 100644 --- a/ApplicationLibrary/Views/Setting/ProfileOverrideView.swift +++ b/ApplicationLibrary/Views/Setting/ProfileOverrideView.swift @@ -5,6 +5,7 @@ public struct ProfileOverrideView: View { @State private var isLoading = true @State private var excludeDefaultRoute = false @State private var autoRouteUseSubRangesByDefault = false + @State private var excludeAPNsRoute = false public init() {} public var body: some View { @@ -39,6 +40,17 @@ public struct ProfileOverrideView: View { Text("By default, segment routing is used in `auto_route` instead of global routing. If `*_` exists in the configuration, this item will not take effect on the corresponding network. (commonly used to resolve HomeKit compatibility issues)") } + FormSection { + Toggle("Exclude APNs Route", isOn: $excludeAPNsRoute) + .onChangeCompat(of: excludeAPNsRoute) { newValue in + Task { + await SharedPreferences.excludeAPNsRoute.set(newValue) + } + } + } footer: { + Text("Append `push.apple.com` to `bypass_domain`, and `17.0.0.0/8` to `inet4_route_exclude_address`.") + } + FormButton { Task { await SharedPreferences.resetProfileOverride() @@ -60,6 +72,7 @@ public struct ProfileOverrideView: View { private func loadSettings() async { excludeDefaultRoute = await SharedPreferences.excludeDefaultRoute.get() autoRouteUseSubRangesByDefault = await SharedPreferences.autoRouteUseSubRangesByDefault.get() + excludeAPNsRoute = await SharedPreferences.excludeAPNsRoute.get() isLoading = false } } diff --git a/Library/Database/SharedPreferences.swift b/Library/Database/SharedPreferences.swift index 5d2634f..cdc1379 100644 --- a/Library/Database/SharedPreferences.swift +++ b/Library/Database/SharedPreferences.swift @@ -62,10 +62,12 @@ public enum SharedPreferences { public static let excludeDefaultRoute = Preference("exclude_default_route", defaultValue: false) public static let autoRouteUseSubRangesByDefault = Preference("auto_route_use_sub_ranges_by_default", defaultValue: false) + public static let excludeAPNsRoute = Preference("exclude_apple_push_notification_services", defaultValue: false) public static func resetProfileOverride() async { await excludeDefaultRoute.set(nil) await autoRouteUseSubRangesByDefault.set(nil) + await excludeAPNsRoute.set(nil) } #if DEBUG diff --git a/Library/Network/ExtensionPlatformInterface.swift b/Library/Network/ExtensionPlatformInterface.swift index 285d2cd..97f6d8e 100644 --- a/Library/Network/ExtensionPlatformInterface.swift +++ b/Library/Network/ExtensionPlatformInterface.swift @@ -28,6 +28,7 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc } let autoRouteUseSubRangesByDefault = await SharedPreferences.autoRouteUseSubRangesByDefault.get() + let excludeAPNs = await SharedPreferences.excludeAPNsRoute.get() let settings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "127.0.0.1") if options.getAutoRoute() { @@ -84,6 +85,13 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc ipv4ExcludeRoutes.append(NEIPv4Route(destinationAddress: "0.0.0.0", subnetMask: "255.255.255.254")) } } + if excludeAPNs, !ipv4Routes.isEmpty { + if !ipv4ExcludeRoutes.contains(where: { it in + it.destinationAddress == "17.0.0.0" && it.destinationSubnetMask == "255.0.0.0" + }) { + ipv4ExcludeRoutes.append(NEIPv4Route(destinationAddress: "17.0.0.0", subnetMask: "255.0.0.0")) + } + } ipv4Settings.includedRoutes = ipv4Routes ipv4Settings.excludedRoutes = ipv4ExcludeRoutes @@ -140,6 +148,9 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc proxySettings.httpEnabled = true proxySettings.httpsEnabled = true } + if excludeAPNs { + proxySettings.exceptionList = ["push.apple.com"] + } settings.proxySettings = proxySettings }