From fd99ba6255731e8019d5eb4e25a968db4f45bf93 Mon Sep 17 00:00:00 2001 From: GyDi Date: Thu, 7 Apr 2022 01:20:44 +0800 Subject: [PATCH] feat: handle remote clash config fields --- src-tauri/src/cmds.rs | 17 +++++ src-tauri/src/core/profiles.rs | 13 ++++ src-tauri/src/main.rs | 1 + src/components/profile/enhanced.tsx | 113 ++++++++++++++++++++++++++-- src/services/cmds.ts | 4 + src/services/enhance.ts | 3 +- src/services/types.ts | 2 + 7 files changed, 146 insertions(+), 7 deletions(-) diff --git a/src-tauri/src/cmds.rs b/src-tauri/src/cmds.rs index 70b7d12..bf1e852 100644 --- a/src-tauri/src/cmds.rs +++ b/src-tauri/src/cmds.rs @@ -125,6 +125,23 @@ pub fn change_profile_chain( wrap_err!(clash.activate_enhanced(&profiles, false, false)) } +/// change the profile valid fields +#[tauri::command] +pub fn change_profile_valid( + valid: Option>, + app_handle: tauri::AppHandle, + clash_state: State<'_, ClashState>, + profiles_state: State<'_, ProfilesState>, +) -> Result<(), String> { + let mut clash = clash_state.0.lock().unwrap(); + let mut profiles = profiles_state.0.lock().unwrap(); + + profiles.put_valid(valid); + clash.set_window(app_handle.get_window("main")); + + wrap_err!(clash.activate_enhanced(&profiles, false, false)) +} + /// manually exec enhanced profile #[tauri::command] pub fn enhance_profiles( diff --git a/src-tauri/src/core/profiles.rs b/src-tauri/src/core/profiles.rs index 075a584..c84eb71 100644 --- a/src-tauri/src/core/profiles.rs +++ b/src-tauri/src/core/profiles.rs @@ -316,6 +316,9 @@ pub struct Profiles { /// same as PrfConfig.chain chain: Option>, + /// record valid fields for clash + valid: Option>, + /// profile list items: Option>, } @@ -399,6 +402,11 @@ impl Profiles { self.chain = chain; } + /// just change the `field` + pub fn put_valid(&mut self, valid: Option>) { + self.valid = valid; + } + /// find the item by the uid pub fn get_item(&self, uid: &String) -> Result<&PrfItem> { if self.items.is_some() { @@ -599,9 +607,12 @@ impl Profiles { None => vec![], }; + let valid = self.valid.clone().unwrap_or(vec![]); + Ok(PrfEnhanced { current, chain, + valid, callback, }) } @@ -613,6 +624,8 @@ pub struct PrfEnhanced { pub chain: Vec, + pub valid: Vec, + pub callback: String, } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index cada75f..6e401b3 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -119,6 +119,7 @@ fn main() -> std::io::Result<()> { cmds::sync_profiles, cmds::enhance_profiles, cmds::change_profile_chain, + cmds::change_profile_valid, cmds::read_profile_file, cmds::save_profile_file ]); diff --git a/src/components/profile/enhanced.tsx b/src/components/profile/enhanced.tsx index dc603c3..2a5ab6a 100644 --- a/src/components/profile/enhanced.tsx +++ b/src/components/profile/enhanced.tsx @@ -1,26 +1,49 @@ import useSWR from "swr"; +import { useState } from "react"; import { useLockFn } from "ahooks"; -import { Box, Grid, IconButton, Stack } from "@mui/material"; -import { RestartAltRounded } from "@mui/icons-material"; +import { + Box, + Divider, + Grid, + IconButton, + ListItemIcon, + ListItemText, + Menu, + MenuItem, + Stack, +} from "@mui/material"; +import { + AddchartRounded, + CheckRounded, + MenuRounded, + RestartAltRounded, +} from "@mui/icons-material"; import { getProfiles, deleteProfile, enhanceProfiles, changeProfileChain, + changeProfileValid, } from "../../services/cmds"; import { CmdType } from "../../services/types"; -import Notice from "../base/base-notice"; +import getSystem from "../../utils/get-system"; import ProfileMore from "./profile-more"; +import Notice from "../base/base-notice"; interface Props { items: CmdType.ProfileItem[]; chain: string[]; } +const OS = getSystem(); + const EnhancedMode = (props: Props) => { const { items, chain } = props; - const { mutate } = useSWR("getProfiles", getProfiles); + const { data, mutate } = useSWR("getProfiles", getProfiles); + const valid = data?.valid || []; + + const [anchorEl, setAnchorEl] = useState(null); // handler const onEnhance = useLockFn(async () => { @@ -74,6 +97,19 @@ const EnhancedMode = (props: Props) => { mutate((conf = {}) => ({ ...conf, chain: newChain }), true); }); + // update valid list + const onToggleValid = useLockFn(async (key: string) => { + try { + const newValid = valid.includes(key) + ? valid.filter((i) => i !== key) + : valid.concat(key); + await changeProfileValid(newValid); + mutate(); + } catch (err: any) { + Notice.error(err.message || err.toString()); + } + }); + return ( { - {/* + setAnchorEl(e.currentTarget)} + > - */} + + + setAnchorEl(null)} + transitionDuration={225} + TransitionProps={ + OS === "macos" ? { style: { transitionDuration: "225ms" } } : {} + } + MenuListProps={{ + dense: true, + "aria-labelledby": "profile-use-button", + }} + onContextMenu={(e) => { + setAnchorEl(null); + e.preventDefault(); + }} + > + + + + + Use Clash Fields + + + + + {[ + "tun", + "dns", + "hosts", + "script", + "profile", + "payload", + "interface-name", + "routing-mark", + ].map((key) => { + const has = valid.includes(key); + + return ( + onToggleValid(key)} + > + {has && ( + + + + )} + {key} + + ); + })} + diff --git a/src/services/cmds.ts b/src/services/cmds.ts index cf47295..bc4555e 100644 --- a/src/services/cmds.ts +++ b/src/services/cmds.ts @@ -66,6 +66,10 @@ export async function changeProfileChain(chain?: string[]) { return invoke("change_profile_chain", { chain }); } +export async function changeProfileValid(valid?: string[]) { + return invoke("change_profile_valid", { valid }); +} + export async function getClashInfo() { return invoke("get_clash_info"); } diff --git a/src/services/enhance.ts b/src/services/enhance.ts index ee746d3..a1ebf5a 100644 --- a/src/services/enhance.ts +++ b/src/services/enhance.ts @@ -145,11 +145,12 @@ class Enhance { // enhanced mode runner private async runner(payload: CmdType.EnhancedPayload) { const chain = payload.chain || []; + const valid = payload.valid || []; if (!Array.isArray(chain)) throw new Error("unhandle error"); let pdata = payload.current || {}; - let useList = [] as string[]; + let useList = valid; for (const each of chain) { const { uid, type = "" } = each.item; diff --git a/src/services/types.ts b/src/services/types.ts index 0d5bf3c..50b81fc 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -116,6 +116,7 @@ export namespace CmdType { export interface ProfilesConfig { current?: string; chain?: string[]; + valid?: string[]; items?: ProfileItem[]; } @@ -191,6 +192,7 @@ export namespace CmdType { export interface EnhancedPayload { chain: ChainItem[]; + valid: string[]; current: ProfileData; callback: string; }