From 42a37ef80d669159db1515d350ede6d2d51ea02b Mon Sep 17 00:00:00 2001 From: Keeyou Date: Sun, 31 Dec 2023 17:26:04 +0800 Subject: [PATCH] ios: trying to figure the way to start vpn --- CMakeLists.txt | 15 ++++++-- src/ios/YassAppDelegate.mm | 38 +++++++++++++++++--- src/ios/extensions/Info.plist | 36 +++++++++++++++---- src/ios/extensions/PacketTunnel.entitlements | 12 +++++++ src/ios/yass.entitlements | 10 ++++++ tools/build.go | 2 ++ 6 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 src/ios/yass.entitlements diff --git a/CMakeLists.txt b/CMakeLists.txt index 1591f0b0a..013c836f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4107,6 +4107,8 @@ if (GUI) ) if ((NOT XCODE_CODESIGN_IDENTITY STREQUAL "-") OR XCODE_DEPLOYMENT_TEAM) set_target_properties(${APP_NAME} PROPERTIES + XCODE_ATTRIBUTE_CODE_SIGN_ENTITLEMENTS ${CMAKE_CURRENT_SOURCE_DIR}/src/ios/yass.entitlements + XCODE_ATTRIBUTE_CODE_SIGN_ENTITLEMENTS ${CMAKE_CURRENT_SOURCE_DIR}/src/ios/extensions/PacketTunnel.entitlements XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "${XCODE_CODESIGN_IDENTITY}" XCODE_ATTRIBUTE_DEVELOPMENT_TEAM "${XCODE_DEPLOYMENT_TEAM}" XCODE_ATTRIBUTE_CODE_SIGN_STYLE "Automatic") @@ -4148,10 +4150,12 @@ if (GUI) MACOSX_BUNDLE_GUI_IDENTIFIER "it.gui.ios.yass.PacketTunnel" XCODE_ATTRIBUTE_PRODUCT_NAME "PacketTunnel" XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "it.gui.ios.yass.PacketTunnel" - XCODE_ATTRIBUTE_LD_RUNPATH_SEARCH_PATHS "@executable_path" XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC "YES" - XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++11" + XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++20"; XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++" + XCODE_ATTRIBUTE_CLANG_ENABLE_MODULES YES + XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC YES + XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_WEAK YES XCODE_ATTRIBUTE_PROVISIONING_PROFILE_SPECIFIER "" ) @@ -4169,6 +4173,13 @@ if (GUI) XCODE_ATTRIBUTE_SKIP_INSTALL NO XCODE_ATTRIBUTE_INSTALL_PATH "$(LOCAL_APPS_DIR)" ) + + add_custom_command(TARGET ${APP_NAME} PRE_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory $ex $/Frameworks/PacketTunnel.appex + DEPENDS PacketTunnel + ) + + target_link_libraries(${APP_NAME} PRIVATE ${NetworkExtension_LIBRARY}) endif() if (NOT CMAKE_SKIP_INSTALL_RULES) diff --git a/src/ios/YassAppDelegate.mm b/src/ios/YassAppDelegate.mm index 1546b20b0..e1916a5e3 100644 --- a/src/ios/YassAppDelegate.mm +++ b/src/ios/YassAppDelegate.mm @@ -2,6 +2,8 @@ /* Copyright (c) 2023 Chilledheart */ #import "ios/YassAppDelegate.h" +#import + #include "cli/cli_worker.hpp" #include @@ -29,6 +31,7 @@ @implementation YassAppDelegate { enum YASSState state_; std::string error_msg_; Worker worker_; + NETunnelProviderManager *vpn_manager_; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { @@ -118,11 +121,36 @@ - (void)OnStop:(BOOL)quiet { - (void)OnStarted { state_ = STARTED; config::SaveConfig(); - - YassViewController* viewController = - (YassViewController*) - UIApplication.sharedApplication.keyWindow.rootViewController; - [viewController Started]; + + [NETunnelProviderManager loadAllFromPreferencesWithCompletionHandler:^(NSArray * _Nullable managers, NSError * _Nullable error) { + if (error) { + std::string err_msg = gurl_base::SysNSStringToUTF8([error localizedDescription]); + [self OnStop:true]; + [self OnStartFailed:err_msg]; + return; + } + vpn_manager_ = managers[0]; + NETunnelProviderProtocol* tunnelProtocol = [[NETunnelProviderProtocol alloc] init]; + tunnelProtocol.serverAddress = @""; + tunnelProtocol.providerBundleIdentifier = @"it.gui.ios.PacketTunnel"; + tunnelProtocol.providerConfiguration = @{}; + tunnelProtocol.username = @""; + // tunnelProtocol.passwordReference = @""; + vpn_manager_.protocolConfiguration = tunnelProtocol; + vpn_manager_.localizedDescription = @"YASS VPN"; + vpn_manager_.enabled = TRUE; + BOOL ret = [vpn_manager_.connection startVPNTunnelAndReturnError:&error]; + if (ret == TRUE) { + YassViewController* viewController = + (YassViewController*) + UIApplication.sharedApplication.keyWindow.rootViewController; + [viewController Started]; + } else { + std::string err_msg = gurl_base::SysNSStringToUTF8([error localizedDescription]); + [self OnStop:true]; + [self OnStartFailed:err_msg]; + } + }]; } - (void)OnStartFailed:(std::string)error_msg { diff --git a/src/ios/extensions/Info.plist b/src/ios/extensions/Info.plist index c66ded50b..51735df79 100644 --- a/src/ios/extensions/Info.plist +++ b/src/ios/extensions/Info.plist @@ -2,12 +2,34 @@ - NSExtension - - NSExtensionPointIdentifier - com.apple.networkextension.packet-tunnel - NSExtensionPrincipalClass - PacketTunnelProvider - + CFBundleDevelopmentRegion + en + CFBundleDisplayName + PacketTunnel + CFBundleExecutable + PacketTunnel + CFBundleIdentifier + it.gui.ios.yass.PacketTunnel + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + yass + CFBundlePackageType + XPC! + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + UIRequiredDeviceCapabilities + + arm64 + + NSExtension + + NSExtensionPointIdentifier + com.apple.networkextension.packet-tunnel + NSExtensionPrincipalClass + PacketTunnelProvider + diff --git a/src/ios/extensions/PacketTunnel.entitlements b/src/ios/extensions/PacketTunnel.entitlements index 7810b4b19..0f1bc9174 100644 --- a/src/ios/extensions/PacketTunnel.entitlements +++ b/src/ios/extensions/PacketTunnel.entitlements @@ -6,5 +6,17 @@ group.it.gui.ios.yass + + com.apple.developer.networking.networkextension + + packet-tunnel-provider + app-proxy-provider + content-filter-provider + dns-proxy + + com.apple.developer.networking.vpn.api + + allow-vpn + diff --git a/src/ios/yass.entitlements b/src/ios/yass.entitlements new file mode 100644 index 000000000..7810b4b19 --- /dev/null +++ b/src/ios/yass.entitlements @@ -0,0 +1,10 @@ + + + + + com.apple.security.application-groups + + group.it.gui.ios.yass + + + diff --git a/tools/build.go b/tools/build.go index f04852fd9..90e8e2ffd 100644 --- a/tools/build.go +++ b/tools/build.go @@ -891,6 +891,8 @@ func buildStageGenerateBuildScript() { } else { glog.Fatalf("Invalid archFlag: %s", archFlag); } + cmakeArgs = append(cmakeArgs, fmt.Sprintf("-DXCODE_CODESIGN_IDENTITY=%s", iosCodeSignIdentityFlag)) + cmakeArgs = append(cmakeArgs, fmt.Sprintf("-DXCODE_DEPLOYMENT_TEAM=%s", iosDevelopmentTeamFlag)) glog.Info("No Packaging supported for simulator, disabling...") noPackagingFlag = true } else if subSystemNameFlag != "" {