feat: disable running with admin permission and check service mode
Some checks are pending
Alpha Build / alpha (macos-latest, aarch64-apple-darwin) (push) Waiting to run
Alpha Build / alpha (macos-latest, x86_64-apple-darwin) (push) Waiting to run
Alpha Build / alpha (windows-latest, aarch64-pc-windows-msvc) (push) Waiting to run
Alpha Build / alpha (windows-latest, i686-pc-windows-msvc) (push) Waiting to run
Alpha Build / alpha (windows-latest, x86_64-pc-windows-msvc) (push) Waiting to run
Alpha Build / alpha-for-linux (ubuntu-latest, aarch64-unknown-linux-gnu) (push) Waiting to run
Alpha Build / alpha-for-linux (ubuntu-latest, armv7-unknown-linux-gnueabihf) (push) Waiting to run
Alpha Build / alpha-for-linux (ubuntu-latest, i686-unknown-linux-gnu) (push) Waiting to run
Alpha Build / alpha-for-linux (ubuntu-latest, x86_64-unknown-linux-gnu) (push) Waiting to run
Alpha Build / alpha-for-fixed-webview2 (arm64, windows-latest, aarch64-pc-windows-msvc) (push) Waiting to run
Alpha Build / alpha-for-fixed-webview2 (x64, windows-latest, x86_64-pc-windows-msvc) (push) Waiting to run
Alpha Build / alpha-for-fixed-webview2 (x86, windows-latest, i686-pc-windows-msvc) (push) Waiting to run
Alpha Build / Update tag (push) Blocked by required conditions

This commit is contained in:
MystiPanda 2024-06-14 22:35:43 +08:00
parent 1b8c4cb832
commit a0f9fb90ee
No known key found for this signature in database
4 changed files with 94 additions and 79 deletions

View File

@ -88,8 +88,10 @@ pub fn toggle_system_proxy() {
pub fn toggle_tun_mode() {
let enable = Config::verge().data().enable_tun_mode;
let enable = enable.unwrap_or(false);
tauri::async_runtime::spawn(async move {
if !enable {
if let Ok(res) = service::check_service().await {
if res.code == 0 {
match patch_verge(IVerge {
enable_tun_mode: Some(!enable),
..IVerge::default()
@ -99,6 +101,25 @@ pub fn toggle_tun_mode() {
Ok(_) => handle::Handle::refresh_verge(),
Err(err) => log::error!(target: "app", "{err}"),
}
return;
}
}
tauri::api::dialog::message(
None::<&tauri::Window>,
"Please install and enable service mode",
"Service mode is required for Tun mode",
);
} else {
match patch_verge(IVerge {
enable_tun_mode: Some(!enable),
..IVerge::default()
})
.await
{
Ok(_) => handle::Handle::refresh_verge(),
Err(err) => log::error!(target: "app", "{err}"),
}
}
});
}
@ -403,3 +424,19 @@ pub async fn test_delay(url: String) -> Result<u32> {
}
}
}
pub fn check_permission() -> Result<()> {
#[cfg(target_os = "windows")]
{
let hklm = winreg::RegKey::predef(winreg::enums::HKEY_LOCAL_MACHINE);
if let Ok(reg) = hklm.open_subkey_with_flags(
"SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run",
winreg::enums::KEY_SET_VALUE,
) {
reg.delete_value("Clash Verge").unwrap_or_default();
return Ok(());
}
}
Err(anyhow::anyhow!("permission denied"))
}

View File

@ -19,6 +19,16 @@ fn main() -> std::io::Result<()> {
println!("app exists");
return Ok(());
}
// 权限检测
if feat::check_permission().is_ok() {
println!("please do not run with admin permission");
tauri::api::dialog::blocking::message(
None::<&tauri::Window>,
"Please do not run with admin permission",
"If you want to use Tun mode, please enable service mode instead",
);
return Ok(());
}
#[cfg(target_os = "linux")]
std::env::set_var("WEBKIT_DISABLE_DMABUF_RENDERER", "1");

View File

@ -1,12 +1,4 @@
import { useTranslation } from "react-i18next";
import { Button, ButtonGroup, Tooltip } from "@mui/material";
import { checkService } from "@/services/cmds";
import { useVerge } from "@/hooks/use-verge";
import getSystem from "@/utils/get-system";
import useSWR from "swr";
import { useEffect } from "react";
const isWIN = getSystem() === "windows";
import { Button, ButtonGroup } from "@mui/material";
interface Props {
value?: string;
@ -15,39 +7,12 @@ interface Props {
export const StackModeSwitch = (props: Props) => {
const { value, onChange } = props;
const { verge } = useVerge();
const { enable_service_mode } = verge ?? {};
// service mode
const { data: serviceStatus, mutate: mutateCheck } = useSWR(
isWIN ? "checkService" : null,
checkService,
{
revalidateIfStale: false,
shouldRetryOnError: false,
}
);
const { t } = useTranslation();
useEffect(() => {
mutateCheck();
}, []);
return (
<Tooltip
title={
isWIN && (serviceStatus !== "active" || !enable_service_mode)
? t("System and Mixed Can Only be Used in Service Mode")
: ""
}
>
<ButtonGroup size="small" sx={{ my: "4px" }}>
<Button
variant={value?.toLowerCase() === "system" ? "contained" : "outlined"}
onClick={() => onChange?.("system")}
disabled={
isWIN && (serviceStatus !== "active" || !enable_service_mode)
}
sx={{ textTransform: "capitalize" }}
>
System
@ -63,14 +28,10 @@ export const StackModeSwitch = (props: Props) => {
<Button
variant={value?.toLowerCase() === "mixed" ? "contained" : "outlined"}
onClick={() => onChange?.("mixed")}
disabled={
isWIN && (serviceStatus !== "active" || !enable_service_mode)
}
sx={{ textTransform: "capitalize" }}
>
Mixed
</Button>
</ButtonGroup>
</Tooltip>
);
};

View File

@ -22,11 +22,15 @@ const SettingSystem = ({ onError }: Props) => {
const { verge, mutateVerge, patchVerge } = useVerge();
// service mode
const { data: serviceStatus } = useSWR("checkService", checkService, {
const { data: serviceStatus, mutate: mutateCheck } = useSWR(
"checkService",
checkService,
{
revalidateIfStale: false,
shouldRetryOnError: false,
focusThrottleInterval: 36e5, // 1 hour
});
}
);
const serviceRef = useRef<DialogRef>(null);
const sysproxyRef = useRef<DialogRef>(null);
@ -84,7 +88,7 @@ const SettingSystem = ({ onError }: Props) => {
onChange={(e) => onChangeData({ enable_tun_mode: e })}
onGuard={(e) => patchVerge({ enable_tun_mode: e })}
>
<Switch edge="end" />
<Switch disabled={serviceStatus !== "active"} edge="end" />
</GuardState>
</SettingItem>
@ -109,7 +113,10 @@ const SettingSystem = ({ onError }: Props) => {
onCatch={onError}
onFormat={onSwitchFormat}
onChange={(e) => onChangeData({ enable_service_mode: e })}
onGuard={(e) => patchVerge({ enable_service_mode: e })}
onGuard={(e) => {
setTimeout(() => mutateCheck(), 1000);
return patchVerge({ enable_service_mode: e });
}}
>
<Switch
edge="end"