feat: Add mac os permanent permission &

fix windows build
This commit is contained in:
Nova 2024-11-21 14:37:44 +03:30
parent d1c9c5b4c3
commit f61a317970
No known key found for this signature in database
GPG Key ID: 389787EC83F5D73A
11 changed files with 62 additions and 21 deletions

View File

@ -100,7 +100,7 @@ jobs:
# ========================================================================================================= Qt Install
- name: Install Qt
if: matrix.platform != 'windows-2022'
uses: jurplel/install-qt-action@v4.0.0
uses: jurplel/install-qt-action@v4.1.1
with:
version: ${{ matrix.qt_version }}
setup-python: true
@ -108,9 +108,8 @@ jobs:
cache-key-prefix: QtCache-${{ matrix.platform }}-${{ matrix.arch }}
- name: Install Qt Windows
if: matrix.platform == 'windows-2022'
uses: jurplel/install-qt-action@v4.0.0
uses: jurplel/install-qt-action@v4.1.1
with:
arch: win64_msvc2022_64
version: ${{ matrix.qt_version }}
cache: true
cache-key-prefix: QtCache-${{ matrix.platform }}-${{ matrix.arch }}
@ -209,11 +208,13 @@ jobs:
zip -r $version_standalone-windows64.zip nekoray
rm -rf nekoray
####
mv macos-arm64/nekoray.dmg $version_standalone-macos-arm64.dmg
rm -rf macos-arm64/nekoray.dmg
mv macos-arm64/nekoray.app nekoray
zip -r $version_standalone-macos-arm64.zip nekoray
rm -rf nekoray
####
mv macos-amd64/nekoray.dmg $version_standalone-macos-amd64.dmg
rm -rf macos-amd64/nekoray.dmg
mv macos-amd64/nekoray.app nekoray
zip -r $version_standalone-macos-amd64.zip nekoray
rm -rf nekoray
- name: Clean Up
run: |
cd deployment

View File

@ -195,6 +195,8 @@ set(PROJECT_SOURCES
ui/edit/edit_ssh.h
ui/edit/edit_ssh.ui
fmt/SSHBean.h
sys/macos/MacOS.cpp
sys/macos/MacOS.h
)
# Qt exe

View File

@ -0,0 +1,3 @@
find_library(SECURITY_FRAMEWORK Security)
set(PLATFORM_SOURCES sys/macos/MacOS.cpp)
set(PLATFORM_LIBRARIES ${SECURITY_FRAMEWORK})

View File

@ -61,6 +61,8 @@ $cmake .. -GNinja \
-Dprotobuf_MSVC_STATIC_RUNTIME=OFF \
-Dprotobuf_BUILD_TESTS=OFF \
-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \
-Dprotobuf_BUILD_PROTOBUF_BINARIES=ON \
-Dprotobuf_BUILD_LIBUPB=OFF \
-DCMAKE_CXX_STANDARD=17
ninja && ninja install

View File

@ -31,6 +31,4 @@ popd
codesign --force --deep --sign - $BUILD/nekoray.app
#### pack dmg ###
sudo npm install -g appdmg
appdmg appdmg.json $DEST/nekoray.dmg
mv $BUILD/nekoray.app $DEST

View File

@ -407,19 +407,15 @@ namespace NekoGui {
short isAdminCache = -1;
// IsAdmin 主要判断:有无权限启动 Tun
bool IsAdmin() {
if (isAdminCache >= 0) return isAdminCache;
bool IsAdmin(bool forceRenew) {
if (isAdminCache >= 0 && !forceRenew) return isAdminCache;
bool admin = false;
#ifdef Q_OS_WIN
admin = Windows_IsInAdmin();
#endif
#ifdef Q_OS_LINUX
#else
admin = QFileInfo(FindNekoBoxCoreRealPath()).groupId() == 0;
#endif
#ifdef Q_OS_MACOS
admin = geteuid() == 0;
#endif
isAdminCache = admin;
return admin;

View File

@ -14,7 +14,7 @@ namespace NekoGui {
QString FindNekoBoxCoreRealPath();
bool IsAdmin();
bool IsAdmin(bool forceRenew=false);
QString GetBasePath();

View File

@ -53,7 +53,6 @@ namespace NekoGui_sys {
}
QProcess::setEnvironment(env);
QProcess::start(program, arguments);
}

9
sys/macos/MacOS.cpp Normal file
View File

@ -0,0 +1,9 @@
#include <QFile>
#include <QFileInfo>
#include <QString>
#include <main/NekoGui_Utils.hpp>
int Mac_Run_Command(QString command) {
auto cmd = QString("osascript -e 'tell application \"Terminal\" to activate' -e 'tell application \"Terminal\" to do script \"%1; exit;\"' with administrator privileges").arg(command);
return system(cmd.toStdString().c_str());
}

5
sys/macos/MacOS.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include <QString>
int Mac_Run_Command(QString command);

View File

@ -46,6 +46,8 @@
#include <QStyleHints>
#include <main/HTTPRequestHelper.hpp>
#include "sys/macos/MacOS.h"
void UI_InitMainWindow() {
mainwindow = new MainWindow;
}
@ -764,9 +766,33 @@ bool MainWindow::get_elevated_permissions(int reason) {
#endif
#ifdef Q_OS_MACOS
MessageBoxWarning("Need administrator privilege", "Enabling TUN mode requires elevated privileges, please run Nekoray as root.");
if (NekoGui::IsAdmin(true))
{
this->exit_reason = reason;
on_menu_exit_triggered();
return false;
}
auto n = QMessageBox::warning(GetMessageBoxParent(), software_name, tr("Please give the core root privileges"), QMessageBox::Yes | QMessageBox::No);
if (n == QMessageBox::Yes)
{
auto chmodCommand = QString("chmod u+s " + NekoGui::FindNekoBoxCoreRealPath());
auto ret = Mac_Run_Command(chmodCommand);
if (ret != 0)
{
MW_show_log(QString("Failed to run %1 with code %2").arg(chmodCommand).arg(ret));
return false;
}
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"));
return false;
} else {
MW_show_log(QString("Failed to run %1 with %2").arg(chownCommand).arg(ret));
return false;
}
}
#endif
return false;
}