mirror of
https://github.com/MatsuriDayo/nekoray.git
synced 2025-12-17 12:34:37 +03:00
wip: tun privilege
This commit is contained in:
@@ -73,6 +73,9 @@ namespace NekoRay {
|
|||||||
_add(new configItem("log_ignore", &log_ignore, itemType::stringList));
|
_add(new configItem("log_ignore", &log_ignore, itemType::stringList));
|
||||||
_add(new configItem("start_minimal", &start_minimal, itemType::boolean));
|
_add(new configItem("start_minimal", &start_minimal, itemType::boolean));
|
||||||
_add(new configItem("max_log_line", &max_log_line, itemType::integer));
|
_add(new configItem("max_log_line", &max_log_line, itemType::integer));
|
||||||
|
#ifndef Q_OS_WIN
|
||||||
|
_add(new configItem("vpn_already_admin", &vpn_already_admin, itemType::boolean));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataStore::UpdateStartedId(int id) {
|
void DataStore::UpdateStartedId(int id) {
|
||||||
|
|||||||
@@ -136,6 +136,7 @@ namespace NekoRay {
|
|||||||
bool vpn_hide_console = false;
|
bool vpn_hide_console = false;
|
||||||
bool vpn_strict_route = false;
|
bool vpn_strict_route = false;
|
||||||
bool vpn_rule_white = false;
|
bool vpn_rule_white = false;
|
||||||
|
bool vpn_already_admin = false; // not saved on Windows
|
||||||
QString vpn_rule_process = "";
|
QString vpn_rule_process = "";
|
||||||
QString vpn_rule_cidr = "";
|
QString vpn_rule_cidr = "";
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ set -e
|
|||||||
set -x
|
set -x
|
||||||
|
|
||||||
if [ "$EUID" -ne 0 ]; then
|
if [ "$EUID" -ne 0 ]; then
|
||||||
echo "Please run as root"
|
echo "[Warning] vpn script not running as root"
|
||||||
exit
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$(uname)" == "Darwin" ]; then
|
if [ "$(uname)" == "Darwin" ]; then
|
||||||
|
|||||||
@@ -4,8 +4,9 @@
|
|||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#include <shlobj.h>
|
||||||
|
|
||||||
void Windows_QWidget_SetForegroundWindow(QWidget* w) {
|
void Windows_QWidget_SetForegroundWindow(QWidget *w) {
|
||||||
HWND hForgroundWnd = GetForegroundWindow();
|
HWND hForgroundWnd = GetForegroundWindow();
|
||||||
DWORD dwForeID = ::GetWindowThreadProcessId(hForgroundWnd, NULL);
|
DWORD dwForeID = ::GetWindowThreadProcessId(hForgroundWnd, NULL);
|
||||||
DWORD dwCurID = ::GetCurrentThreadId();
|
DWORD dwCurID = ::GetCurrentThreadId();
|
||||||
@@ -13,3 +14,11 @@ void Windows_QWidget_SetForegroundWindow(QWidget* w) {
|
|||||||
::SetForegroundWindow((HWND) w->winId());
|
::SetForegroundWindow((HWND) w->winId());
|
||||||
::AttachThreadInput(dwCurID, dwForeID, FALSE);
|
::AttachThreadInput(dwCurID, dwForeID, FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int isThisAdmin = -1; // cached
|
||||||
|
|
||||||
|
bool Windows_IsInAdmin() {
|
||||||
|
if (isThisAdmin >= 0) return isThisAdmin;
|
||||||
|
isThisAdmin = IsUserAnAdmin();
|
||||||
|
return isThisAdmin;
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,3 +3,5 @@
|
|||||||
class QWidget;
|
class QWidget;
|
||||||
|
|
||||||
void Windows_QWidget_SetForegroundWindow(QWidget* w);
|
void Windows_QWidget_SetForegroundWindow(QWidget* w);
|
||||||
|
|
||||||
|
bool Windows_IsInAdmin();
|
||||||
|
|||||||
@@ -630,6 +630,14 @@ https://matsuridayo.github.io/n-configuration/#vpn-tun</source>
|
|||||||
<source>Cancel</source>
|
<source>Cancel</source>
|
||||||
<translation type="unfinished">لغو کردن</translation>
|
<translation type="unfinished">لغو کردن</translation>
|
||||||
</message>
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>Don't ask for privilege elevation</source>
|
||||||
|
<translation type="unfinished"></translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>Already Admin</source>
|
||||||
|
<translation type="unfinished"></translation>
|
||||||
|
</message>
|
||||||
</context>
|
</context>
|
||||||
<context>
|
<context>
|
||||||
<name>EditChain</name>
|
<name>EditChain</name>
|
||||||
|
|||||||
@@ -639,6 +639,14 @@ https://matsuridayo.github.io/n-configuration/#vpn-tun</translation>
|
|||||||
<source>Cancel</source>
|
<source>Cancel</source>
|
||||||
<translation>取消</translation>
|
<translation>取消</translation>
|
||||||
</message>
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>Don't ask for privilege elevation</source>
|
||||||
|
<translation>不请求特权提升</translation>
|
||||||
|
</message>
|
||||||
|
<message>
|
||||||
|
<source>Already Admin</source>
|
||||||
|
<translation>已是管理员</translation>
|
||||||
|
</message>
|
||||||
</context>
|
</context>
|
||||||
<context>
|
<context>
|
||||||
<name>EditChain</name>
|
<name>EditChain</name>
|
||||||
|
|||||||
@@ -18,7 +18,10 @@ DialogVPNSettings::DialogVPNSettings(QWidget *parent) : QDialog(parent), ui(new
|
|||||||
ui->vpn_mtu->setCurrentText(Int2String(NekoRay::dataStore->vpn_mtu));
|
ui->vpn_mtu->setCurrentText(Int2String(NekoRay::dataStore->vpn_mtu));
|
||||||
ui->vpn_ipv6->setChecked(NekoRay::dataStore->vpn_ipv6);
|
ui->vpn_ipv6->setChecked(NekoRay::dataStore->vpn_ipv6);
|
||||||
ui->hide_console->setChecked(NekoRay::dataStore->vpn_hide_console);
|
ui->hide_console->setChecked(NekoRay::dataStore->vpn_hide_console);
|
||||||
#ifndef Q_OS_WIN
|
ui->vpn_already_admin->setChecked(NekoRay::dataStore->vpn_already_admin);
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
ui->vpn_already_admin->setVisible(false);
|
||||||
|
#else
|
||||||
ui->hide_console->setVisible(false);
|
ui->hide_console->setVisible(false);
|
||||||
#endif
|
#endif
|
||||||
ui->strict_route->setChecked(NekoRay::dataStore->vpn_strict_route);
|
ui->strict_route->setChecked(NekoRay::dataStore->vpn_strict_route);
|
||||||
@@ -53,6 +56,7 @@ void DialogVPNSettings::accept() {
|
|||||||
NekoRay::dataStore->vpn_hide_console = ui->hide_console->isChecked();
|
NekoRay::dataStore->vpn_hide_console = ui->hide_console->isChecked();
|
||||||
NekoRay::dataStore->vpn_strict_route = ui->strict_route->isChecked();
|
NekoRay::dataStore->vpn_strict_route = ui->strict_route->isChecked();
|
||||||
NekoRay::dataStore->vpn_rule_white = ui->whitelist_mode->isChecked();
|
NekoRay::dataStore->vpn_rule_white = ui->whitelist_mode->isChecked();
|
||||||
|
NekoRay::dataStore->vpn_already_admin = ui->vpn_already_admin->isChecked();
|
||||||
//
|
//
|
||||||
D_SAVE_STRING_QTEXTEDIT(vpn_rule_cidr)
|
D_SAVE_STRING_QTEXTEDIT(vpn_rule_cidr)
|
||||||
D_SAVE_STRING_QTEXTEDIT(vpn_rule_process)
|
D_SAVE_STRING_QTEXTEDIT(vpn_rule_process)
|
||||||
|
|||||||
@@ -114,6 +114,16 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QCheckBox" name="vpn_already_admin">
|
||||||
|
<property name="toolTip">
|
||||||
|
<string>Don't ask for privilege elevation</string>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Already Admin</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
|||||||
@@ -715,6 +715,9 @@ void MainWindow::refresh_status(const QString &traffic_update) {
|
|||||||
|
|
||||||
auto make_title = [=](bool isTray) {
|
auto make_title = [=](bool isTray) {
|
||||||
QStringList tt;
|
QStringList tt;
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
if (!isTray && Windows_IsInAdmin()) tt << "[Admin]";
|
||||||
|
#endif
|
||||||
if (select_mode) tt << "[" + tr("Select") + "]";
|
if (select_mode) tt << "[" + tr("Select") + "]";
|
||||||
if (!title_error.isEmpty()) tt << "[" + title_error + "]";
|
if (!title_error.isEmpty()) tt << "[" + title_error + "]";
|
||||||
if (NekoRay::dataStore->running_spmode == NekoRay::SystemProxyMode::SYSTEM_PROXY) {
|
if (NekoRay::dataStore->running_spmode == NekoRay::SystemProxyMode::SYSTEM_PROXY) {
|
||||||
@@ -1676,7 +1679,11 @@ bool MainWindow::StartVPNProcess() {
|
|||||||
vpn_process->start("osascript", {"-e", QString("do shell script \"%1\" with administrator privileges")
|
vpn_process->start("osascript", {"-e", QString("do shell script \"%1\" with administrator privileges")
|
||||||
.arg("bash " + scriptPath)});
|
.arg("bash " + scriptPath)});
|
||||||
#else
|
#else
|
||||||
|
if (NekoRay::dataStore->vpn_already_admin) {
|
||||||
|
vpn_process->start("bash", {scriptPath});
|
||||||
|
} else {
|
||||||
vpn_process->start("pkexec", {"bash", scriptPath});
|
vpn_process->start("pkexec", {"bash", scriptPath});
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
vpn_process->waitForStarted();
|
vpn_process->waitForStarted();
|
||||||
vpn_pid = vpn_process->processId(); // actually it's pkexec or bash PID
|
vpn_pid = vpn_process->processId(); // actually it's pkexec or bash PID
|
||||||
@@ -1699,11 +1706,15 @@ bool MainWindow::StopVPNProcess(bool unconditional) {
|
|||||||
p.start("osascript", {"-e", QString("do shell script \"%1\" with administrator privileges")
|
p.start("osascript", {"-e", QString("do shell script \"%1\" with administrator privileges")
|
||||||
.arg("pkill -2 -U 0 nekobox_core")});
|
.arg("pkill -2 -U 0 nekobox_core")});
|
||||||
#else
|
#else
|
||||||
|
if (NekoRay::dataStore->vpn_already_admin) {
|
||||||
|
p.start("bash", {"kill", "-2", Int2String(vpn_pid)});
|
||||||
|
} else {
|
||||||
if (unconditional) {
|
if (unconditional) {
|
||||||
p.start("pkexec", {"killall", "nekobox_core"});
|
p.start("pkexec", {"killall", "-2", "nekobox_core"});
|
||||||
} else {
|
} else {
|
||||||
p.start("pkexec", {"pkill", "-2", "-P", Int2String(vpn_pid)});
|
p.start("pkexec", {"pkill", "-2", "-P", Int2String(vpn_pid)});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
p.waitForFinished();
|
p.waitForFinished();
|
||||||
ok = p.exitCode() == 0;
|
ok = p.exitCode() == 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user