From 7579780886cc0f2569237999f12f43de9c15e954 Mon Sep 17 00:00:00 2001 From: Kenta Kubo <601636+kkk669@users.noreply.github.com> Date: Thu, 23 Jul 2020 23:21:26 +0900 Subject: [PATCH] Add the implementation that configures 1.1.1.1 to NEDNSSettingsManager --- CustomDNS.xcodeproj/project.pbxproj | 24 ++++++++++++++++++-- CustomDNS/ContentView.swift | 34 ++++++++++++++++++++++++++--- CustomDNS/CustomDNS.entitlements | 14 ++++++++++++ CustomDNS/CustomDNSApp.swift | 31 +++++++++++++++++++++++++- CustomDNS/Info.plist | 2 ++ 5 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 CustomDNS/CustomDNS.entitlements diff --git a/CustomDNS.xcodeproj/project.pbxproj b/CustomDNS.xcodeproj/project.pbxproj index b5bc7fb..3dc647a 100644 --- a/CustomDNS.xcodeproj/project.pbxproj +++ b/CustomDNS.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 8940024324ACBD2800EBE74B /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8940024224ACBD2800EBE74B /* Preview Assets.xcassets */; }; 8940024E24ACBD2800EBE74B /* CustomDNSTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8940024D24ACBD2800EBE74B /* CustomDNSTests.swift */; }; 8940025924ACBD2800EBE74B /* CustomDNSUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8940025824ACBD2800EBE74B /* CustomDNSUITests.swift */; }; + 8940026924ACBE4900EBE74B /* NetworkExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8940026824ACBE4900EBE74B /* NetworkExtension.framework */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -33,6 +34,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 8924EDF324C9CDF1004AF871 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 8940023824ACBD2700EBE74B /* CustomDNS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CustomDNS.app; sourceTree = BUILT_PRODUCTS_DIR; }; 8940023B24ACBD2700EBE74B /* CustomDNSApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomDNSApp.swift; sourceTree = ""; }; 8940023D24ACBD2700EBE74B /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; @@ -45,6 +47,8 @@ 8940025424ACBD2800EBE74B /* CustomDNSUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CustomDNSUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 8940025824ACBD2800EBE74B /* CustomDNSUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomDNSUITests.swift; sourceTree = ""; }; 8940025A24ACBD2800EBE74B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 8940026624ACBE4900EBE74B /* CustomDNS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = CustomDNS.entitlements; sourceTree = ""; }; + 8940026824ACBE4900EBE74B /* NetworkExtension.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = NetworkExtension.framework; path = System/Library/Frameworks/NetworkExtension.framework; sourceTree = SDKROOT; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -52,6 +56,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 8940026924ACBE4900EBE74B /* NetworkExtension.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -75,10 +80,12 @@ 8940022F24ACBD2700EBE74B = { isa = PBXGroup; children = ( + 8924EDF324C9CDF1004AF871 /* README.md */, 8940023A24ACBD2700EBE74B /* CustomDNS */, 8940024C24ACBD2800EBE74B /* CustomDNSTests */, 8940025724ACBD2800EBE74B /* CustomDNSUITests */, 8940023924ACBD2700EBE74B /* Products */, + 8940026724ACBE4900EBE74B /* Frameworks */, ); sourceTree = ""; }; @@ -95,6 +102,7 @@ 8940023A24ACBD2700EBE74B /* CustomDNS */ = { isa = PBXGroup; children = ( + 8940026624ACBE4900EBE74B /* CustomDNS.entitlements */, 8940023B24ACBD2700EBE74B /* CustomDNSApp.swift */, 8940023D24ACBD2700EBE74B /* ContentView.swift */, 8940023F24ACBD2800EBE74B /* Assets.xcassets */, @@ -130,6 +138,14 @@ path = CustomDNSUITests; sourceTree = ""; }; + 8940026724ACBE4900EBE74B /* Frameworks */ = { + isa = PBXGroup; + children = ( + 8940026824ACBE4900EBE74B /* NetworkExtension.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -417,6 +433,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_ENTITLEMENTS = CustomDNS/CustomDNS.entitlements; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_ASSET_PATHS = "\"CustomDNS/Preview Content\""; DEVELOPMENT_TEAM = X4678G5DL2; @@ -429,8 +446,9 @@ ); PRODUCT_BUNDLE_IDENTIFIER = xyz.kebo.CustomDNS; PRODUCT_NAME = "$(TARGET_NAME)"; + SUPPORTS_MACCATALYST = YES; SWIFT_VERSION = 5.0; - TARGETED_DEVICE_FAMILY = "1,2"; + TARGETED_DEVICE_FAMILY = "1,2,6"; }; name = Debug; }; @@ -439,6 +457,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_ENTITLEMENTS = CustomDNS/CustomDNS.entitlements; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_ASSET_PATHS = "\"CustomDNS/Preview Content\""; DEVELOPMENT_TEAM = X4678G5DL2; @@ -451,8 +470,9 @@ ); PRODUCT_BUNDLE_IDENTIFIER = xyz.kebo.CustomDNS; PRODUCT_NAME = "$(TARGET_NAME)"; + SUPPORTS_MACCATALYST = YES; SWIFT_VERSION = 5.0; - TARGETED_DEVICE_FAMILY = "1,2"; + TARGETED_DEVICE_FAMILY = "1,2,6"; }; name = Release; }; diff --git a/CustomDNS/ContentView.swift b/CustomDNS/ContentView.swift index 20a033d..2780358 100644 --- a/CustomDNS/ContentView.swift +++ b/CustomDNS/ContentView.swift @@ -5,11 +5,39 @@ // Created by Kenta Kubo on 7/1/20. // +import Combine +import NetworkExtension import SwiftUI -struct ContentView: View { - var body: some View { - Text("Hello, world!").padding() +struct ContentView { + @State var isEnabled = false + var cancellable: Cancellable? + + init() { + self.cancellable = Future { resolve in + let manager = NEDNSSettingsManager.shared() + manager.loadFromPreferences { + if let err = $0 { + resolve(.failure(err)) + } else { + resolve(.success(manager.isEnabled)) + } + } + } + .mapError { fatalError("\($0.localizedDescription)") } + .assign(to: \.isEnabled, on: self) + } +} + +extension ContentView: View { + @ViewBuilder var body: some View { + if self.isEnabled { + Text("Enabled") + .foregroundColor(.green) + } else { + Text("Disabled") + .foregroundColor(.secondary) + } } } diff --git a/CustomDNS/CustomDNS.entitlements b/CustomDNS/CustomDNS.entitlements new file mode 100644 index 0000000..5889260 --- /dev/null +++ b/CustomDNS/CustomDNS.entitlements @@ -0,0 +1,14 @@ + + + + + com.apple.developer.networking.networkextension + + dns-settings + + com.apple.security.app-sandbox + + com.apple.security.network.client + + + diff --git a/CustomDNS/CustomDNSApp.swift b/CustomDNS/CustomDNSApp.swift index 6acd6c6..03143c7 100644 --- a/CustomDNS/CustomDNSApp.swift +++ b/CustomDNS/CustomDNSApp.swift @@ -5,10 +5,39 @@ // Created by Kenta Kubo on 7/1/20. // +import NetworkExtension import SwiftUI @main -struct CustomDNSApp: App { +struct CustomDNSApp { + init() { + // Create a DNS configuration + let manager = NEDNSSettingsManager.shared() + manager.loadFromPreferences { loadError in + if let loadError = loadError { + print(loadError) + return + } + let dotSettings = NEDNSOverTLSSettings(servers: [ + "1.1.1.1", + "1.0.0.1", + "2606:4700:4700::64", + "2606:4700:4700::6400", + ]) + dotSettings.serverName = "cloudflare-dns.com" + manager.dnsSettings = dotSettings + manager.saveToPreferences { saveError in + if let saveError = saveError { + print(saveError) + return + } + print("saved") + } + } + } +} + +extension CustomDNSApp: App { var body: some Scene { WindowGroup { ContentView() diff --git a/CustomDNS/Info.plist b/CustomDNS/Info.plist index efc211a..2e8adb5 100644 --- a/CustomDNS/Info.plist +++ b/CustomDNS/Info.plist @@ -18,6 +18,8 @@ 1.0 CFBundleVersion 1 + LSApplicationCategoryType + public.app-category.utilities LSRequiresIPhoneOS UIApplicationSceneManifest