mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-23 02:00:20 +07:00
feat: add network status check for awg/wg protocol (#1894)
* Add network status check for AWG/WG protocol * Use service for PingSender * Cleanup unused code * Use networkchecker for all protocols * fix android build * add delay for ping checker stop * handle for interafe problems on windows * Restart IpcClient after OS suspend * Add DBus network checker for Linux * Use ping check for tun interfce * Windows suspend mode handler * MacOS suspend mode handler draft * Add delay for Linux wakeup reconnect * Add delay for Linux wakeup reconnect * Fix macOS wakeup/sleep prob Fix macOS not receiving wakeup/sleep events * fix done * Update deploy.yml fix CICD * Update vpnconnection.cpp update fix build CICD * Update vpnconnection.cpp update fix build cicd macos * Update deploy.yml fix CICD build macos * Update deploy.yml fix CICD macos * feat: implement SCP write buffer, improve network check and refactor macOS OpenGL support * feat: add tunnel addresses updated signal and handle network check based on gateway and local address availability * refactor: improve IpcClient connection handling and instance management * fix: scp revert. * fix: cmake reverted. * fix: submodules updated --------- Co-authored-by: Mykola Baibuz <mykola.baibuz@gmail.com> Co-authored-by: Yaroslav Yashin <yaroslav.yashin@gmail.com> Co-authored-by: vkamn <vk@amnezia.org>
This commit is contained in:
@@ -6,6 +6,11 @@
|
||||
#include "leakdetector.h"
|
||||
#include "logger.h"
|
||||
|
||||
#include <QProcess>
|
||||
#include <QMetaObject>
|
||||
#include <pthread.h>
|
||||
#include <iostream>
|
||||
|
||||
#import <CoreWLAN/CoreWLAN.h>
|
||||
#import <Network/Network.h>
|
||||
|
||||
@@ -13,6 +18,37 @@ namespace {
|
||||
Logger logger("MacOSNetworkWatcher");
|
||||
}
|
||||
|
||||
// Global variables for CFRunLoop thread
|
||||
static pthread_t g_powerThread;
|
||||
static CFRunLoopRef g_powerRunLoop = nullptr;
|
||||
static bool g_shouldStopPowerThread = false;
|
||||
static PowerNotificationsListener* g_powerListener = nullptr;
|
||||
|
||||
// Thread function for dedicated CFRunLoop
|
||||
void* powerMonitoringThread(void* arg) {
|
||||
logger.debug() << "Power monitoring thread started";
|
||||
|
||||
PowerNotificationsListener* listener = static_cast<PowerNotificationsListener*>(arg);
|
||||
|
||||
// Get the runloop for this thread
|
||||
g_powerRunLoop = CFRunLoopGetCurrent();
|
||||
|
||||
// Register for power notifications in this thread
|
||||
listener->registerForNotifications();
|
||||
|
||||
// Run the CFRunLoop - this will block until CFRunLoopStop is called
|
||||
while (!g_shouldStopPowerThread) {
|
||||
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0, true);
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
listener->cleanup();
|
||||
g_powerRunLoop = nullptr;
|
||||
|
||||
logger.debug() << "Power monitoring thread finished";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@interface MacOSNetworkWatcherDelegate : NSObject <CWEventDelegate> {
|
||||
MacOSNetworkWatcher* m_watcher;
|
||||
}
|
||||
@@ -33,17 +69,145 @@ Logger logger("MacOSNetworkWatcher");
|
||||
|
||||
if (m_watcher) {
|
||||
m_watcher->checkInterface();
|
||||
// Emit networkChanged signal when BSSID changes
|
||||
emit m_watcher->networkChanged(QString::fromNSString(interfaceName));
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
MacOSNetworkWatcher::MacOSNetworkWatcher(QObject* parent) : IOSNetworkWatcher(parent) {
|
||||
void PowerNotificationsListener::registerForNotifications()
|
||||
{
|
||||
logger.debug() << "Registering for system power notifications in dedicated thread";
|
||||
|
||||
rootPowerDomain = IORegisterForSystemPower(this, ¬ifyPortRef, sleepWakeupCallBack, ¬ifierObj);
|
||||
if (rootPowerDomain == IO_OBJECT_NULL) {
|
||||
logger.error() << "Failed to register for system power notifications!";
|
||||
return;
|
||||
}
|
||||
|
||||
// Add the notification port to the current runloop (dedicated thread)
|
||||
CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notifyPortRef), kCFRunLoopCommonModes);
|
||||
logger.debug() << "Power notifications registered successfully";
|
||||
}
|
||||
|
||||
void PowerNotificationsListener::cleanup()
|
||||
{
|
||||
if (notifyPortRef != nullptr) {
|
||||
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notifyPortRef), kCFRunLoopCommonModes);
|
||||
IONotificationPortDestroy(notifyPortRef);
|
||||
notifyPortRef = nullptr;
|
||||
}
|
||||
|
||||
if (notifierObj != IO_OBJECT_NULL) {
|
||||
IODeregisterForSystemPower(¬ifierObj);
|
||||
notifierObj = IO_OBJECT_NULL;
|
||||
}
|
||||
|
||||
if (rootPowerDomain != IO_OBJECT_NULL) {
|
||||
IOServiceClose(rootPowerDomain);
|
||||
rootPowerDomain = IO_OBJECT_NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void PowerNotificationsListener::sleepWakeupCallBack(void *refParam, io_service_t service, natural_t messageType, void *messageArgument)
|
||||
{
|
||||
Q_UNUSED(service)
|
||||
|
||||
auto listener = static_cast<PowerNotificationsListener *>(refParam);
|
||||
|
||||
logger.debug() << "Power callback received, messageType:" << messageType;
|
||||
switch (messageType) {
|
||||
case kIOMessageCanSystemSleep:
|
||||
/* Idle sleep is about to kick in. This message will not be sent for forced sleep.
|
||||
* Applications have a chance to prevent sleep by calling IOCancelPowerChange.
|
||||
* Most applications should not prevent idle sleep. Power Management waits up to
|
||||
* 30 seconds for you to either allow or deny idle sleep. If you don’t acknowledge
|
||||
* this power change by calling either IOAllowPowerChange or IOCancelPowerChange,
|
||||
* the system will wait 30 seconds then go to sleep.
|
||||
*/
|
||||
|
||||
logger.debug() << "System power message: can system sleep?";
|
||||
|
||||
// Uncomment to cancel idle sleep
|
||||
// IOCancelPowerChange(thiz->rootPowerDomain, reinterpret_cast<long>(messageArgument));
|
||||
|
||||
// Allow idle sleep
|
||||
IOAllowPowerChange(listener->rootPowerDomain, reinterpret_cast<long>(messageArgument));
|
||||
break;
|
||||
|
||||
case kIOMessageSystemWillNotSleep:
|
||||
/* Announces that the system has retracted a previous attempt to sleep; it
|
||||
* follows `kIOMessageCanSystemSleep`.
|
||||
*/
|
||||
logger.debug() << "System power message: system will NOT sleep.";
|
||||
break;
|
||||
|
||||
case kIOMessageSystemWillSleep:
|
||||
/* The system WILL go to sleep. If you do not call IOAllowPowerChange or
|
||||
* IOCancelPowerChange to acknowledge this message, sleep will be delayed by
|
||||
* 30 seconds.
|
||||
*
|
||||
* NOTE: If you call IOCancelPowerChange to deny sleep it returns kIOReturnSuccess,
|
||||
* however the system WILL still go to sleep.
|
||||
*/
|
||||
|
||||
logger.debug() << "System power message: system WILL sleep";
|
||||
IOAllowPowerChange(listener->rootPowerDomain, reinterpret_cast<long>(messageArgument));
|
||||
break;
|
||||
|
||||
case kIOMessageSystemWillPowerOn:
|
||||
/* Announces that the system is beginning to power the device tree; most devices
|
||||
* are still unavailable at this point.
|
||||
*/
|
||||
/* From the documentation:
|
||||
*
|
||||
* - kIOMessageSystemWillPowerOn is delivered at early wakeup time, before most hardware
|
||||
* has been powered on. Be aware that any attempts to access disk, network, the display,
|
||||
* etc. may result in errors or blocking your process until those resources become
|
||||
* available.
|
||||
*
|
||||
* So we do NOT log this event.
|
||||
*/
|
||||
break;
|
||||
|
||||
case kIOMessageSystemHasPoweredOn:
|
||||
/* Announces that the system and its devices have woken up. */
|
||||
logger.debug() << "System has powered on - emitting sleepMode signal from dedicated CFRunLoop thread";
|
||||
if (listener->m_watcher) {
|
||||
// Use QMetaObject::invokeMethod for thread-safe signal emission
|
||||
QMetaObject::invokeMethod(listener->m_watcher, "sleepMode", Qt::QueuedConnection);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
logger.debug() << "System power message: other event: " << messageType;
|
||||
/* Not a system sleep and wake notification. */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
MacOSNetworkWatcher::MacOSNetworkWatcher(QObject* parent) : IOSNetworkWatcher(parent), m_powerlistener(this) {
|
||||
MZ_COUNT_CTOR(MacOSNetworkWatcher);
|
||||
}
|
||||
|
||||
MacOSNetworkWatcher::~MacOSNetworkWatcher() {
|
||||
MZ_COUNT_DTOR(MacOSNetworkWatcher);
|
||||
|
||||
// Stop the dedicated power monitoring thread
|
||||
if (g_powerListener) {
|
||||
logger.debug() << "Stopping dedicated power monitoring thread";
|
||||
g_shouldStopPowerThread = true;
|
||||
|
||||
if (g_powerRunLoop) {
|
||||
CFRunLoopStop(g_powerRunLoop);
|
||||
}
|
||||
|
||||
// Wait for thread to finish
|
||||
pthread_join(g_powerThread, nullptr);
|
||||
g_powerListener = nullptr;
|
||||
}
|
||||
|
||||
if (m_delegate) {
|
||||
CWWiFiClient* client = CWWiFiClient.sharedWiFiClient;
|
||||
if (!client) {
|
||||
@@ -66,6 +230,20 @@ void MacOSNetworkWatcher::start() {
|
||||
logger.debug() << "Delegate already registered";
|
||||
return;
|
||||
}
|
||||
|
||||
// Start dedicated power monitoring thread with CFRunLoop
|
||||
if (!g_powerListener) {
|
||||
g_powerListener = &m_powerlistener;
|
||||
g_shouldStopPowerThread = false;
|
||||
|
||||
int result = pthread_create(&g_powerThread, nullptr, powerMonitoringThread, &m_powerlistener);
|
||||
if (result != 0) {
|
||||
logger.error() << "Failed to create power monitoring thread:" << result;
|
||||
g_powerListener = nullptr;
|
||||
} else {
|
||||
logger.debug() << "Power monitoring enabled";
|
||||
}
|
||||
}
|
||||
|
||||
CWWiFiClient* client = CWWiFiClient.sharedWiFiClient;
|
||||
if (!client) {
|
||||
@@ -77,6 +255,8 @@ void MacOSNetworkWatcher::start() {
|
||||
m_delegate = [[MacOSNetworkWatcherDelegate alloc] initWithObject:this];
|
||||
[client setDelegate:static_cast<MacOSNetworkWatcherDelegate*>(m_delegate)];
|
||||
[client startMonitoringEventWithType:CWEventTypeBSSIDDidChange error:nullptr];
|
||||
|
||||
logger.debug() << "MacOSNetworkWatcher started successfully";
|
||||
}
|
||||
|
||||
void MacOSNetworkWatcher::checkInterface() {
|
||||
@@ -87,42 +267,70 @@ void MacOSNetworkWatcher::checkInterface() {
|
||||
return;
|
||||
}
|
||||
|
||||
CWWiFiClient* client = CWWiFiClient.sharedWiFiClient;
|
||||
if (!client) {
|
||||
logger.debug() << "Unable to retrieve the CWWiFiClient shared instance";
|
||||
// Use wdutil to get reliable WiFi information
|
||||
QProcess process;
|
||||
process.start("wdutil", QStringList() << "info");
|
||||
process.waitForFinished(5000);
|
||||
|
||||
QString output = process.readAllStandardOutput();
|
||||
QString errorOutput = process.readAllStandardError();
|
||||
|
||||
logger.debug() << "wdutil exit code:" << process.exitCode();
|
||||
|
||||
if (process.exitCode() != 0) {
|
||||
logger.debug() << "wdutil failed with exit code:" << process.exitCode();
|
||||
return;
|
||||
}
|
||||
|
||||
CWInterface* interface = [client interface];
|
||||
if (!interface) {
|
||||
logger.debug() << "No default wifi interface";
|
||||
return;
|
||||
|
||||
// Parse wdutil output to find WiFi connection info
|
||||
QStringList lines = output.split('\n');
|
||||
QString ssid, interfaceName, security;
|
||||
bool wifiSectionFound = false;
|
||||
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
QString trimmedLine = lines[i].trimmed();
|
||||
|
||||
if (trimmedLine == "WIFI") {
|
||||
wifiSectionFound = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (wifiSectionFound) {
|
||||
// Stop parsing when we reach next section header (all caps after separator line)
|
||||
if (trimmedLine.startsWith("————————")) {
|
||||
if (i + 1 < lines.size()) {
|
||||
QString nextLine = lines[i + 1].trimmed();
|
||||
if (!nextLine.isEmpty() && nextLine.length() > 2 && nextLine.toUpper() == nextLine && nextLine != "WIFI") {
|
||||
break;
|
||||
}
|
||||
}
|
||||
continue; // Skip separator lines
|
||||
}
|
||||
|
||||
if (trimmedLine.startsWith("Interface Name")) {
|
||||
QStringList parts = trimmedLine.split(":");
|
||||
if (parts.size() >= 2) {
|
||||
interfaceName = parts[1].trimmed();
|
||||
}
|
||||
} else if (trimmedLine.startsWith("SSID")) {
|
||||
QStringList parts = trimmedLine.split(":");
|
||||
if (parts.size() >= 2) {
|
||||
ssid = parts[1].trimmed();
|
||||
}
|
||||
} else if (trimmedLine.startsWith("Security")) {
|
||||
QStringList parts = trimmedLine.split(":");
|
||||
if (parts.size() >= 2) {
|
||||
security = parts[1].trimmed();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (![interface powerOn]) {
|
||||
logger.debug() << "The interface is off";
|
||||
return;
|
||||
|
||||
if (!ssid.isEmpty() && !interfaceName.isEmpty()) {
|
||||
logger.debug() << "Found active WiFi connection on" << interfaceName
|
||||
<< "SSID:" << ssid << "Security:" << security;
|
||||
} else {
|
||||
logger.debug() << "No active WiFi connection found";
|
||||
}
|
||||
|
||||
NSString* ssidNS = [interface ssid];
|
||||
if (!ssidNS) {
|
||||
logger.debug() << "WiFi is not in used";
|
||||
return;
|
||||
}
|
||||
|
||||
QString ssid = QString::fromNSString(ssidNS);
|
||||
if (ssid.isEmpty()) {
|
||||
logger.debug() << "WiFi doesn't have a valid SSID";
|
||||
return;
|
||||
}
|
||||
|
||||
CWSecurity security = [interface security];
|
||||
if (security == kCWSecurityNone || security == kCWSecurityWEP) {
|
||||
logger.debug() << "Unsecured network found!";
|
||||
emit unsecuredNetwork(ssid, ssid);
|
||||
return;
|
||||
}
|
||||
|
||||
logger.debug() << "Secure WiFi interface";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user