Skip to content

Commit

Permalink
ios: trying to figure the way to start vpn
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilledheart committed Dec 31, 2023
1 parent f487e95 commit 4a3ddce
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 14 deletions.
14 changes: 12 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4107,6 +4107,7 @@ 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_IDENTITY "${XCODE_CODESIGN_IDENTITY}"
XCODE_ATTRIBUTE_DEVELOPMENT_TEAM "${XCODE_DEPLOYMENT_TEAM}"
XCODE_ATTRIBUTE_CODE_SIGN_STYLE "Automatic")
Expand Down Expand Up @@ -4148,10 +4149,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 ""
)

Expand All @@ -4169,6 +4172,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 $<TARGET_FILE_DIR:PacketTunnel>ex $<TARGET_FILE_DIR:${APP_NAME}>/Frameworks/PacketTunnel.appex
DEPENDS PacketTunnel
)

target_link_libraries(${APP_NAME} PRIVATE ${NetworkExtension_LIBRARY})
endif()

if (NOT CMAKE_SKIP_INSTALL_RULES)
Expand Down
38 changes: 33 additions & 5 deletions src/ios/YassAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/* Copyright (c) 2023 Chilledheart */
#import "ios/YassAppDelegate.h"

#import <NetworkExtension/NetworkExtension.h>

#include "cli/cli_worker.hpp"

#include <pthread.h>
Expand Down Expand Up @@ -29,6 +31,7 @@ @implementation YassAppDelegate {
enum YASSState state_;
std::string error_msg_;
Worker worker_;
NETunnelProviderManager *vpn_manager_;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey,id> *)launchOptions {
Expand Down Expand Up @@ -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<NETunnelProviderManager *> * _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 {
Expand Down
32 changes: 25 additions & 7 deletions src/ios/extensions/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,30 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.networkextension.packet-tunnel</string>
<key>NSExtensionPrincipalClass</key>
<string>PacketTunnelProvider</string>
</dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>PacketTunnel</string>
<key>CFBundleExecutable</key>
<string>PacketTunnel</string>
<key>CFBundleIdentifier</key>
<string>it.gui.ios.yass.PacketTunnel</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>yass</string>
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.networkextension.packet-tunnel</string>
<key>NSExtensionPrincipalClass</key>
<string>PacketTunnelProvider</string>
</dict>
</dict>
</plist>
9 changes: 9 additions & 0 deletions src/ios/extensions/PacketTunnel.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,14 @@
<array>
<string>group.it.gui.ios.yass</string>
</array>

<key>com.apple.developer.networking.networkextension</key>
<array>
<string>packet-tunnel-provider</string>
</array>
<key>com.apple.developer.networking.vpn.api</key>
<array>
<string>allow-vpn</string>
</array>
</dict>
</plist>
10 changes: 10 additions & 0 deletions src/ios/yass.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.it.gui.ios.yass</string>
</array>
</dict>
</plist>

0 comments on commit 4a3ddce

Please sign in to comment.