From d546ef34837fa9a85f180cece340c76450e3bd2f Mon Sep 17 00:00:00 2001 From: Nova Date: Fri, 22 Nov 2024 20:48:41 +0330 Subject: [PATCH] feat: Improve set admin && start as admin on windows if was admin before --- main/NekoGui.cpp | 2 ++ main/NekoGui_DataStore.hpp | 1 + ui/mainwindow.cpp | 23 ++++++++++++++++------- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/main/NekoGui.cpp b/main/NekoGui.cpp index d52eb84..a7f2bfd 100644 --- a/main/NekoGui.cpp +++ b/main/NekoGui.cpp @@ -308,6 +308,7 @@ namespace NekoGui { _add(new configItem("system_dns_set", &system_dns_set, itemType::boolean)); _add(new configItem("is_dhcp", &is_dhcp, itemType::boolean)); _add(new configItem("system_dns_servers", &system_dns_servers, itemType::stringList)); + _add(new configItem("windows_set_admin", &windows_set_admin, itemType::boolean)); } void DataStore::UpdateStartedId(int id) { @@ -413,6 +414,7 @@ namespace NekoGui { bool admin = false; #ifdef Q_OS_WIN admin = Windows_IsInAdmin(); + dataStore->windows_set_admin = admin; #else admin = QFileInfo(FindNekoBoxCoreRealPath()).groupId() == 0; #endif diff --git a/main/NekoGui_DataStore.hpp b/main/NekoGui_DataStore.hpp index d0b2461..4e55a0e 100644 --- a/main/NekoGui_DataStore.hpp +++ b/main/NekoGui_DataStore.hpp @@ -112,6 +112,7 @@ namespace NekoGui { QStringList remember_spmode = {}; int remember_id = -1919; bool remember_enable = false; + bool windows_set_admin = false; // Socks & HTTP Inbound QString inbound_address = "127.0.0.1"; diff --git a/ui/mainwindow.cpp b/ui/mainwindow.cpp index 25f11ef..3ee9b77 100644 --- a/ui/mainwindow.cpp +++ b/ui/mainwindow.cpp @@ -61,6 +61,15 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi // Load Manager NekoGui::profileManager->LoadManager(); + #ifdef Q_OS_WIN + if (NekoGui::dataStore->windows_set_admin) + { + exit_reason = 5; + on_menu_exit_triggered(); + return; + } + #endif + // Setup misc UI // migrate old themes bool isNum; @@ -706,7 +715,7 @@ void MainWindow::on_menu_exit_triggered() { } auto program = QApplication::applicationFilePath(); - if (exit_reason == 3 || exit_reason == 4) { + if (exit_reason == 3 || exit_reason == 4 || exit_reason == 5) { if (exit_reason == 3) arguments << "-flag_restart_tun_on"; if (exit_reason == 4) arguments << "-flag_restart_dns_set"; #ifdef Q_OS_WIN @@ -749,8 +758,9 @@ bool MainWindow::get_elevated_permissions(int reason) { auto chmodArgs = QString("u+s " + NekoGui::FindNekoBoxCoreRealPath()); ret = Linux_Run_Command("chmod", chmodArgs); if (ret == 0) { - this->exit_reason = reason; - on_menu_exit_triggered(); + NekoGui::IsAdmin(true); + StopVPNProcess(); + return true; } else { MW_show_log(QString("Failed to run chmod %1").arg(chmodArgs)); return false; @@ -768,9 +778,8 @@ bool MainWindow::get_elevated_permissions(int reason) { #ifdef Q_OS_MACOS if (NekoGui::IsAdmin(true)) { - this->exit_reason = reason; - on_menu_exit_triggered(); - return false; + StopVPNProcess(); + return true; } auto n = QMessageBox::warning(GetMessageBoxParent(), software_name, tr("Please give the core root privileges"), QMessageBox::Yes | QMessageBox::No); if (n == QMessageBox::Yes) @@ -785,7 +794,7 @@ bool MainWindow::get_elevated_permissions(int reason) { auto chownCommand = QString("sudo chown root:wheel " + NekoGui::FindNekoBoxCoreRealPath()); ret = Mac_Run_Command(chownCommand); if (ret == 0) { - MessageBoxInfo(tr("Requesting permission"), tr("Please Enter your password in the opened terminals, then try again")); + MessageBoxInfo(tr("Requesting permission"), tr("Please Enter your password in the opened terminal, then try again")); return false; } else { MW_show_log(QString("Failed to run %1 with %2").arg(chownCommand).arg(ret));