Skip to content

Commit

Permalink
fix bug crash when run
Browse files Browse the repository at this point in the history
  • Loading branch information
Macbook authored and Macbook committed Dec 30, 2024
1 parent 1a4f1e3 commit bb9c50a
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 117 deletions.
2 changes: 0 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[submodule "client/3rd/OpenVPNAdapter"]
path = client/3rd/OpenVPNAdapter
url = https://github.com/amnezia-vpn/OpenVPNAdapter.git
branch = macos-dirty-build
[submodule "client/3rd/qtkeychain"]
path = client/3rd/qtkeychain
url = https://github.com/frankosterfeld/qtkeychain.git
Expand All @@ -11,7 +10,6 @@
[submodule "client/3rd-prebuilt"]
path = client/3rd-prebuilt
url = https://github.com/amnezia-vpn/3rd-prebuilt
branch = fixbug/mac-network-extension
[submodule "client/3rd/amneziawg-apple"]
path = client/3rd/amneziawg-apple
url = https://github.com/amnezia-vpn/amneziawg-apple
Expand Down
2 changes: 1 addition & 1 deletion client/3rd-prebuilt
Submodule 3rd-prebuilt updated 222 files
24 changes: 24 additions & 0 deletions client/amnezia_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,31 @@ bool AmneziaApplication::parseCommands()

QCommandLineOption c_cleanup { { "c", "cleanup" }, "Cleanup logs" };
m_parser.addOption(c_cleanup);

#if defined(MACOS_NE)
// Handle positional arguments for MacOS NE
QStringList rawArgs = arguments(); // Get the full list of arguments
QStringList positionalArgs;

for (int i = 1; i < rawArgs.size(); ++i) {
if (!rawArgs[i].startsWith("-")) {
positionalArgs << rawArgs[i]; // Collect non-option arguments
}
}

if (!positionalArgs.isEmpty()) {
QString mode = positionalArgs.first();
qInfo() << "Running in mode:" << mode;

if (mode == "DebugMode") {
qInfo() << "Debug Mode activated for MacOS NE";
// Do something specific for DebugMode
} else {
qWarning() << "Unknown mode for MacOS NE:" << mode;
}
}
#endif

m_parser.process(*this);

if (m_parser.isSet(c_cleanup)) {
Expand Down
6 changes: 3 additions & 3 deletions client/cmake/3rdparty.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ if(WIN32)
set(OPENSSL_LIB_CRYPTO_PATH "${OPENSSL_ROOT_DIR}/windows/win32/libcrypto.lib")
endif()
elseif(APPLE AND NOT IOS)
set(LIBSSH_LIB_PATH "${LIBSSH_ROOT_DIR}/macos/x86_64/libssh.a")
set(ZLIB_LIB_PATH "${LIBSSH_ROOT_DIR}/macos/x86_64/libz.a")
set(LIBSSH_INCLUDE_DIR "${LIBSSH_ROOT_DIR}/macos/x86_64")
set(LIBSSH_LIB_PATH "${LIBSSH_ROOT_DIR}/macos/arm64_x86_64/libssh.a")
set(ZLIB_LIB_PATH "${LIBSSH_ROOT_DIR}/macos/arm64_x86_64/libz.a")
set(LIBSSH_INCLUDE_DIR "${LIBSSH_ROOT_DIR}/macos/arm64_x86_64")
set(OPENSSL_INCLUDE_DIR "${OPENSSL_ROOT_DIR}/macos/include")
set(OPENSSL_LIB_SSL_PATH "${OPENSSL_ROOT_DIR}/macos/lib/libssl.a")
set(OPENSSL_LIB_CRYPTO_PATH "${OPENSSL_ROOT_DIR}/macos/lib/libcrypto.a")
Expand Down
12 changes: 6 additions & 6 deletions client/cmake/macos_ne.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ set_target_properties(${PROJECT} PROPERTIES
XCODE_ATTRIBUTE_LD_RUNPATH_SEARCH_PATHS "@executable_path/../Frameworks"
XCODE_EMBED_APP_EXTENSIONS networkextension

XCODE_ATTRIBUTE_CODE_SIGN_STYLE Automatic
# XCODE_ATTRIBUTE_CODE_SIGN_STYLE Manual
# XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "Apple Distribution: Privacy Technologies OU (X7UJ388FXK)"
# XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY[variant=Debug] "Apple Development: TRAN VIET ANH (Y372SYT4WL)"
# XCODE_ATTRIBUTE_PROVISIONING_PROFILE_SPECIFIER "Mac AppStore AmneziaVPN"
# XCODE_ATTRIBUTE_PROVISIONING_PROFILE_SPECIFIER[variant=Debug] "org.amnezia.AmneziaVPNManual"
# XCODE_ATTRIBUTE_CODE_SIGN_STYLE Automatic
XCODE_ATTRIBUTE_CODE_SIGN_STYLE Manual
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "Apple Distribution: Privacy Technologies OU (X7UJ388FXK)"
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY[variant=Debug] "Apple Development: TRAN VIET ANH (Y372SYT4WL)"
XCODE_ATTRIBUTE_PROVISIONING_PROFILE_SPECIFIER "Mac AppStore AmneziaVPN"
XCODE_ATTRIBUTE_PROVISIONING_PROFILE_SPECIFIER[variant=Debug] "org.amnezia.AmneziaVPNManual"
)
set_target_properties(${PROJECT} PROPERTIES
XCODE_ATTRIBUTE_SWIFT_VERSION "5.0"
Expand Down
15 changes: 7 additions & 8 deletions client/macos/networkextension/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ set_target_properties(networkextension PROPERTIES
XCODE_ATTRIBUTE_APPLICATION_EXTENSION_API_ONLY "YES"
XCODE_ATTRIBUTE_ENABLE_BITCODE "NO"

XCODE_ATTRIBUTE_CODE_SIGN_STYLE Automatic
# XCODE_ATTRIBUTE_CODE_SIGN_STYLE Manual
# XCODE_ATTRIBUTE_PROVISIONING_PROFILE_SPECIFIER "Mac AppStore network-extension"
# XCODE_ATTRIBUTE_PROVISIONING_PROFILE_SPECIFIER[variant=Debug] "amnezia.AmneziaVPN.network-extensionManual"
# XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "Apple Distribution: Privacy Technologies OU (X7UJ388FXK)"
# XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY[variant=Debug] "Apple Development: TRAN VIET ANH (Y372SYT4WL)"

# XCODE_ATTRIBUTE_CODE_SIGN_STYLE Automatic
XCODE_ATTRIBUTE_CODE_SIGN_STYLE Manual
XCODE_ATTRIBUTE_PROVISIONING_PROFILE_SPECIFIER "Mac AppStore network-extension"
XCODE_ATTRIBUTE_PROVISIONING_PROFILE_SPECIFIER[variant=Debug] "amnezia.AmneziaVPN.network-extensionManual"
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "Apple Distribution: Privacy Technologies OU (X7UJ388FXK)"
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY[variant=Debug] "Apple Development: TRAN VIET ANH (Y372SYT4WL)"

XCODE_ATTRIBUTE_INFOPLIST_FILE ${CMAKE_CURRENT_SOURCE_DIR}/Info.plist.in
XCODE_ATTRIBUTE_LD_RUNPATH_SEARCH_PATHS "@executable_path/../../../../Frameworks @loader_path/../../../../Frameworks"
Expand Down Expand Up @@ -130,7 +129,7 @@ target_sources(networkextension PRIVATE
target_include_directories(networkextension PRIVATE ${CLIENT_ROOT_DIR})
target_include_directories(networkextension PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

target_link_libraries(networkextension PRIVATE ${CLIENT_ROOT_DIR}/3rd-prebuilt/3rd-prebuilt/wireguard/macos/x86_64/libwg-go.a)
target_link_libraries(networkextension PRIVATE ${CLIENT_ROOT_DIR}/3rd-prebuilt/3rd-prebuilt/wireguard/macos/arm64_x86_64/libwg-go.a)

message(${CLIENT_ROOT_DIR})
message(${CLIENT_ROOT_DIR}/3rd-prebuilt/3rd-prebuilt/xray/HevSocks5Tunnel.xcframework/macos-arm64_x86_64/libhev-socks5-tunnel.a)
Expand Down
4 changes: 2 additions & 2 deletions client/platforms/ios/PacketTunnelProvider+WireGuard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ extension PacketTunnelProvider {
if let lastHandshakeValue = lastHandshakeString, let handshakeValue = Int64(lastHandshakeValue) {
lastHandshake = handshakeValue
} else {
lastHandshake = -2
lastHandshake = -2 // Trả về lỗi nếu không có giá trị last_handshake_time_sec
}

let response: [String: Any] = [
Expand Down Expand Up @@ -194,4 +194,4 @@ extension PacketTunnelProvider {
#endif
}
}
}
}
3 changes: 0 additions & 3 deletions client/platforms/ios/ios_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ class IosController : public QObject
QString openFile();

void requestInetAccess();

void stopForHandshake();
void waitForHandshake();
signals:
void connectionStateChanged(Vpn::ConnectionState state);
void bytesChanged(quint64 receivedBytes, quint64 sentBytes);
Expand Down
107 changes: 15 additions & 92 deletions client/platforms/ios/ios_controller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <QJsonObject>
#include <QThread>
#include <QEventLoop>
#include <QTimer>

#include "../protocols/vpnprotocol.h"
#import "ios_controller_wrapper.h"

Expand Down Expand Up @@ -60,8 +60,6 @@

namespace {
IosController* s_instance = nullptr;
QTimer *m_handshakeTimer = nullptr;
bool is_WireGuard = false;
}

IosController::IosController() : QObject()
Expand Down Expand Up @@ -209,27 +207,21 @@


if (proto == amnezia::Proto::OpenVpn) {
is_WireGuard = false;
return setupOpenVPN();
}
if (proto == amnezia::Proto::Cloak) {
is_WireGuard = false;
return setupCloak();
}
if (proto == amnezia::Proto::WireGuard) {
is_WireGuard = true;
return setupWireGuard();
}
if (proto == amnezia::Proto::Awg) {
is_WireGuard = true;
return setupAwg();
}
if (proto == amnezia::Proto::Xray) {
is_WireGuard = false;
return setupXray();
}
if (proto == amnezia::Proto::SSXray) {
is_WireGuard = false;
return setupSSXray();
}

Expand Down Expand Up @@ -260,98 +252,31 @@
uint64_t txBytes = [response[@"tx_bytes"] intValue];
uint64_t rxBytes = [response[@"rx_bytes"] intValue];

uint64_t last_handshake_time_sec = 0;
if (response[@"last_handshake_time_sec"] && ![response[@"last_handshake_time_sec"] isKindOfClass:[NSNull class]]) {
last_handshake_time_sec = [response[@"last_handshake_time_sec"] intValue];
} else {
qDebug() << "Key last_handshake_time_sec is missing or null";
}

if (last_handshake_time_sec < 0) {
disconnectVpn();
qDebug() << "Invalid handshake time, disconnecting VPN.";
}

emit bytesChanged(rxBytes - m_rxBytes, txBytes - m_txBytes);
m_rxBytes = rxBytes;
m_txBytes = txBytes;
});
}

void IosController::stopForHandshake() {
if (m_handshakeTimer) {
if (m_handshakeTimer->isActive()) {
m_handshakeTimer->stop();
}
m_handshakeTimer->deleteLater();
m_handshakeTimer = nullptr;

qDebug() << "Handshake monitoring stopped.";
} else {
qDebug() << "No active handshake monitoring to stop.";
}
}


void IosController::waitForHandshake() {
qDebug() << "Waiting for last_handshake_time_sec to be greater than 0...";

// Initialize the timer if it's null
if (!m_handshakeTimer) {
m_handshakeTimer = new QTimer(this);

// Connect the timer's timeout signal to perform handshake checking
connect(m_handshakeTimer, &QTimer::timeout, this, [this]() {
// Prepare the message to check status
NSString *actionKey = [NSString stringWithUTF8String:MessageKey::action];
NSString *actionValue = [NSString stringWithUTF8String:Action::getStatus];
NSString *tunnelIdKey = [NSString stringWithUTF8String:MessageKey::tunnelId];
NSString *tunnelIdValue = !m_tunnelId.isEmpty() ? m_tunnelId.toNSString() : @"";

NSDictionary *message = @{actionKey: actionValue, tunnelIdKey: tunnelIdValue};

// Lambda to handle the response
auto checkHandshake = [this](NSDictionary *response) {
uint64_t last_handshake_time_sec = 0;
if (response && response[@"last_handshake_time_sec"] &&
![response[@"last_handshake_time_sec"] isKindOfClass:[NSNull class]]) {
last_handshake_time_sec = [response[@"last_handshake_time_sec"] unsignedLongLongValue];
}

qDebug() << "last_handshake_time_sec:" << last_handshake_time_sec;

if (last_handshake_time_sec > 0) {
// Handshake successful, update state
qDebug() << "Handshake detected, updating state to CONNECTED.";
emit connectionStateChanged(Vpn::ConnectionState::Connected);
stopForHandshake();
return;
} else {
if (last_handshake_time_sec == 0) {
// Keep retrying
emit connectionStateChanged(Vpn::ConnectionState::Connecting);
} else {
// Handle handshake failure and stop monitoring
emit connectionStateChanged(Vpn::ConnectionState::Disconnected);
stopForHandshake();
return;
}
}
};

// Send the message to the VPN extension
sendVpnExtensionMessage(message, checkHandshake);
});

qDebug() << "Handshake timer initialized.";
}

// Start the timer only if it's not already active
if (m_handshakeTimer && !m_handshakeTimer->isActive()) {
m_handshakeTimer->start(1000); // Retry every 1 second
qDebug() << "Handshake timer Retry every 1 second";
}
}

void IosController::vpnStatusDidChange(void *pNotification)
{
NETunnelProviderSession *session = (NETunnelProviderSession *)pNotification;

if (session /* && session == TunnelManager.session */ ) {
qDebug() << "IosController::vpnStatusDidChange" << iosStatusToState(session.status) << session;
if (is_WireGuard && session.status == NEVPNStatusConnected)
{
// use last_handshake_time
return;
}

if (session.status == NEVPNStatusDisconnected) {
if (@available(iOS 16.0, *)) {
[session fetchLastDisconnectErrorWithCompletionHandler:^(NSError * _Nullable error) {
Expand Down Expand Up @@ -446,7 +371,6 @@
} else {
qDebug() << "Disconnect error is unavailable on iOS < 16.0";
}
stopForHandshake();
}

emit connectionStateChanged(iosStatusToState(session.status));
Expand Down Expand Up @@ -731,7 +655,7 @@
tunnelProtocol.serverAddress = m_serverAddress;

m_currentTunnel.protocolConfiguration = tunnelProtocol;
waitForHandshake();

startTunnel();
}

Expand Down Expand Up @@ -955,4 +879,3 @@
}];
[task resume];
}

0 comments on commit bb9c50a

Please sign in to comment.