feat: Improve set admin &&

start as admin on windows if was admin before
This commit is contained in:
Nova 2024-11-22 20:48:41 +03:30
parent 228cd81aeb
commit d546ef3483
No known key found for this signature in database
GPG Key ID: 389787EC83F5D73A
3 changed files with 19 additions and 7 deletions

View File

@ -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

View File

@ -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";

View File

@ -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));