Skip to content

Commit

Permalink
fix: not save clash overrides config, close #1295
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Aug 4, 2024
1 parent 5030fc9 commit 723b58f
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 12 deletions.
1 change: 1 addition & 0 deletions backend/tauri/src/config/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl IRuntime {

// 这里只更改 allow-lan | ipv6 | log-level
pub fn patch_config(&mut self, patch: Mapping) {
tracing::debug!("patching runtime config: {:?}", patch);
if let Some(config) = self.config.as_mut() {
["allow-lan", "ipv6", "log-level"]
.into_iter()
Expand Down
3 changes: 2 additions & 1 deletion backend/tauri/src/enhance/tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ macro_rules! append {
};
}

#[tracing::instrument]
pub fn use_tun(mut config: Mapping, enable: bool) -> Mapping {
let tun_key = Value::from("tun");
let tun_val = config.get(&tun_key);

tracing::debug!("tun_val: {:?}", tun_val);
if !enable && tun_val.is_none() {
return config;
}
Expand Down
7 changes: 6 additions & 1 deletion backend/tauri/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,13 @@ pub fn get_runtime_logs() -> CmdResult<IndexMap<String, Logs>> {
}

#[tauri::command]
#[tracing_attributes::instrument]
pub async fn patch_clash_config(payload: Mapping) -> CmdResult {
wrap_err!(feat::patch_clash(payload).await)?;
tracing::debug!("patch_clash_config: {payload:?}");
if let Err(e) = feat::patch_clash(payload).await {
tracing::error!("{e}");
return Err(format!("{e}"));
}
feat::update_proxies_buff(None);
Ok(())
}
Expand Down
4 changes: 3 additions & 1 deletion backend/tauri/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tracing_attributes::instrument;
use url::Url;
pub static SERVER_PORT: Lazy<u16> = Lazy::new(|| port_scanner::request_open_port().unwrap());

#[derive(Deserialize)]
#[derive(Debug, Deserialize)]
struct CacheIcon {
/// should be encoded as base64
url: String,
Expand Down Expand Up @@ -63,6 +63,7 @@ async fn cache_icon_inner<'n>(url: &str) -> Result<CacheFile<'n>> {
Ok(data)
}

#[tracing_attributes::instrument]
async fn cache_icon(query: Query<CacheIcon>) -> Response<Body> {
match cache_icon_inner(&query.url).await {
Ok(data) => {
Expand All @@ -73,6 +74,7 @@ async fn cache_icon(query: Query<CacheIcon>) -> Response<Body> {
response
}
Err(e) => {
tracing::error!("{}", e);
let mut response = Response::new(Body::from(e.to_string()));
*response.status_mut() = StatusCode::BAD_REQUEST;
response
Expand Down
2 changes: 2 additions & 0 deletions frontend/interface/ipc/useClash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export const useClash = () => {
await tauri.patchClashInfo(payload);

await getClashInfo.mutate();
} catch (e) {
console.error(e);
} finally {
return getClashInfo.data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const SchemeProvider = () => {
});
}
});
}, []);
}, [navigate]);

return null;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export default function RuntimeConfigDiffDialog({
const run = async () => {
const { monaco } = await import("@/services/monaco");
monacoRef.current = monaco;
console.log(domRef.current);
editorRef.current = monaco.editor.createDiffEditor(domRef.current!, {
theme: mode === "light" ? "vs" : "vs-dark",
minimap: { enabled: false },
Expand All @@ -68,7 +67,6 @@ export default function RuntimeConfigDiffDialog({
editorRef.current?.dispose();
};
}, [mode, open, runtimeConfig, profileConfig]);
console.log(currentProfileUid);
if (!currentProfileUid) {
return null;
}
Expand Down
1 change: 0 additions & 1 deletion frontend/nyanpasu/src/components/proxies/group-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const IconRender = memo(function IconRender({ icon }: { icon: string }) {
}
return `http://localhost:${serverPort}/cache/icon?url=${btoa(src)}`;
}, [src, serverPort]);
console.log(serverPort, isLoading, error);
if (isLoading || error) {
return null;
}
Expand Down
28 changes: 23 additions & 5 deletions frontend/nyanpasu/src/components/setting/modules/create-props.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useGlobalMutation } from "@/utils/mutation";
import { SwitchProps } from "@mui/material/Switch/Switch";
import { Clash, useClash, useNyanpasu, VergeConfig } from "@nyanpasu/interface";
import { MenuItemProps } from "@nyanpasu/ui";
Expand Down Expand Up @@ -27,12 +28,20 @@ export const clash = {
[K in keyof Clash.Config]: Clash.Config[K] extends boolean ? K : never;
}[keyof Clash.Config],
): SwitchProps => {
const { getConfigs, setConfigs } = useClash();

const { getConfigs, setConfigs, setClashInfo } = useClash();
const mutate = useGlobalMutation();
return {
checked: getConfigs.data?.[propName] || false,
onChange: () => {
setConfigs({ [propName]: !getConfigs.data?.[propName] });
Promise.all([
setClashInfo({ [propName]: !getConfigs.data?.[propName] }),
setConfigs({ [propName]: !getConfigs.data?.[propName] }),
]).finally(() => {
mutate(
(key) =>
typeof key === "string" && key.includes("/getRuntimeConfigYaml"),
);
});
},
};
},
Expand All @@ -56,13 +65,22 @@ export const clash = {
propName: keyof Clash.Config,
{ options, fallbackSelect }: CreateMenuPropsOptions,
): Omit<MenuItemProps, "label"> => {
const { getConfigs, setConfigs } = useClash();
const { getConfigs, setConfigs, setClashInfo } = useClash();
const mutate = useGlobalMutation();

return {
options,
selected: getConfigs.data?.[propName] || fallbackSelect,
onSelected: (value: OptionValue) => {
setConfigs({ [propName]: value });
Promise.all([
setClashInfo({ [propName]: !getConfigs.data?.[propName] }),
setConfigs({ [propName]: value }),
]).finally(() => {
mutate(
(key) =>
typeof key === "string" && key.includes("/getRuntimeConfigYaml"),
);
});
},
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { useCoreType } from "@/hooks/use-store";
import getSystem from "@/utils/get-system";
import { useGlobalMutation } from "@/utils/mutation";
import { message } from "@/utils/notification";
import { Button, List, ListItem, ListItemText } from "@mui/material";
import { pullupUWPTool, useNyanpasu, VergeConfig } from "@nyanpasu/interface";
Expand Down Expand Up @@ -45,6 +46,8 @@ export const SettingClashBase = () => {
const stack = nyanpasuConfig?.tun_stack || "gvisor";
return stack in tunStackOptions ? stack : "gvisor";
}, [nyanpasuConfig?.tun_stack, tunStackOptions]);
const mutate = useGlobalMutation();

return (
<BaseCard label={t("Clash Setting")}>
<List disablePadding>
Expand All @@ -68,6 +71,11 @@ export const SettingClashBase = () => {
payload.enable_tun_mode = true; // just to reload clash config
}
setNyanpasuConfig(payload);
mutate(
(key) =>
typeof key === "string" &&
key.includes("/getRuntimeConfigYaml"),
);
}}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChangeEvent, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { useGlobalMutation } from "@/utils/mutation";
import { message } from "@/utils/notification";
import Done from "@mui/icons-material/Done";
import LoadingButton from "@mui/lab/LoadingButton";
Expand Down Expand Up @@ -37,6 +38,7 @@ export const SettingClashExternal = () => {
const { nyanpasuConfig, setNyanpasuConfig } = useNyanpasu();

const { getClashInfo, setClashInfo } = useClash();
const mutate = useGlobalMutation();

// What even are these fields?????
// I had to write the shit code to make it run like a pile of crap.
Expand Down Expand Up @@ -88,6 +90,10 @@ export const SettingClashExternal = () => {

setTimeout(() => {
setLoading(false);
mutate(
(key) =>
typeof key === "string" && key.includes("/getRuntimeConfigYaml"),
);
}, 300);
}
};
Expand Down

0 comments on commit 723b58f

Please sign in to comment.