feat: profile enhanced mode
This commit is contained in:
@@ -9,6 +9,10 @@ export async function syncProfiles() {
|
||||
return invoke<void>("sync_profiles");
|
||||
}
|
||||
|
||||
export async function enhanceProfiles() {
|
||||
return invoke<void>("enhance_profiles");
|
||||
}
|
||||
|
||||
export async function createProfile(item: Partial<CmdType.ProfileItem>) {
|
||||
return invoke<void>("create_profile", { item });
|
||||
}
|
||||
@@ -40,8 +44,8 @@ export async function selectProfile(index: string) {
|
||||
return invoke<void>("select_profile", { index });
|
||||
}
|
||||
|
||||
export async function restartSidecar() {
|
||||
return invoke<void>("restart_sidecar");
|
||||
export async function changeProfileChain(chain?: string[]) {
|
||||
return invoke<void>("change_profile_chain", { chain });
|
||||
}
|
||||
|
||||
export async function getClashInfo() {
|
||||
@@ -64,6 +68,10 @@ export async function getSystemProxy() {
|
||||
return invoke<any>("get_sys_proxy");
|
||||
}
|
||||
|
||||
export async function restartSidecar() {
|
||||
return invoke<void>("restart_sidecar");
|
||||
}
|
||||
|
||||
export async function killSidecars() {
|
||||
return invoke<any>("kill_sidecars");
|
||||
}
|
||||
|
||||
@@ -1,22 +1,98 @@
|
||||
import { emit, listen } from "@tauri-apps/api/event";
|
||||
import { CmdType } from "./types";
|
||||
|
||||
function toMerge(
|
||||
merge: CmdType.ProfileMerge,
|
||||
data: CmdType.ProfileData
|
||||
): CmdType.ProfileData {
|
||||
if (!merge) return data;
|
||||
|
||||
const newData = { ...data };
|
||||
|
||||
// rules
|
||||
if (Array.isArray(merge["prepend-rules"])) {
|
||||
if (!newData.rules) newData.rules = [];
|
||||
newData.rules.unshift(...merge["prepend-rules"]);
|
||||
}
|
||||
if (Array.isArray(merge["append-rules"])) {
|
||||
if (!newData.rules) newData.rules = [];
|
||||
newData.rules.push(...merge["append-rules"]);
|
||||
}
|
||||
|
||||
// proxies
|
||||
if (Array.isArray(merge["prepend-proxies"])) {
|
||||
if (!newData.proxies) newData.proxies = [];
|
||||
newData.proxies.unshift(...merge["prepend-proxies"]);
|
||||
}
|
||||
if (Array.isArray(merge["append-proxies"])) {
|
||||
if (!newData.proxies) newData.proxies = [];
|
||||
newData.proxies.push(...merge["append-proxies"]);
|
||||
}
|
||||
|
||||
// proxy-groups
|
||||
if (Array.isArray(merge["prepend-proxy-groups"])) {
|
||||
if (!newData["proxy-groups"]) newData["proxy-groups"] = [];
|
||||
newData["proxy-groups"].unshift(...merge["prepend-proxy-groups"]);
|
||||
}
|
||||
if (Array.isArray(merge["append-proxy-groups"])) {
|
||||
if (!newData["proxy-groups"]) newData["proxy-groups"] = [];
|
||||
newData["proxy-groups"].push(...merge["append-proxy-groups"]);
|
||||
}
|
||||
|
||||
return newData;
|
||||
}
|
||||
|
||||
function toScript(
|
||||
script: string,
|
||||
data: CmdType.ProfileData
|
||||
): Promise<CmdType.ProfileData> {
|
||||
if (!script) {
|
||||
throw new Error("miss the main function");
|
||||
}
|
||||
|
||||
const paramsName = `__verge${Math.floor(Math.random() * 1000)}`;
|
||||
const code = `'use strict';${script};return main(${paramsName});`;
|
||||
const func = new Function(paramsName, code);
|
||||
return func(data); // support async main function
|
||||
}
|
||||
|
||||
export default function setup() {
|
||||
listen("script-handler", (event) => {
|
||||
listen("script-handler", async (event) => {
|
||||
const payload = event.payload as CmdType.EnhancedPayload;
|
||||
console.log(payload);
|
||||
|
||||
// setTimeout(() => {
|
||||
// try {
|
||||
// const fn = eval(payload.script + "\n\nmixin");
|
||||
// console.log(fn);
|
||||
let pdata = payload.current || {};
|
||||
|
||||
// const result = fn(payload.params || {});
|
||||
// console.log("result", result);
|
||||
// emit(payload.callback, JSON.stringify(result)).catch(console.error);
|
||||
// } catch (err) {
|
||||
// console.error(err);
|
||||
// }
|
||||
// }, 3000);
|
||||
for (const each of payload.chain) {
|
||||
try {
|
||||
// process script
|
||||
if (each.item.type === "script") {
|
||||
pdata = await toScript(each.script!, pdata);
|
||||
}
|
||||
|
||||
// process merge
|
||||
else if (each.item.type === "merge") {
|
||||
pdata = toMerge(each.merge!, pdata);
|
||||
}
|
||||
|
||||
// invalid type
|
||||
else {
|
||||
throw new Error(`invalid enhanced profile type "${each.item.type}"`);
|
||||
}
|
||||
|
||||
console.log("step", pdata);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
const result: CmdType.EnhancedResult = {
|
||||
data: pdata,
|
||||
status: "success",
|
||||
};
|
||||
|
||||
emit(payload.callback, JSON.stringify(result)).catch(console.error);
|
||||
});
|
||||
|
||||
// enhanceProfiles();
|
||||
}
|
||||
|
||||
@@ -124,14 +124,32 @@ export namespace CmdType {
|
||||
system_proxy_bypass?: string;
|
||||
}
|
||||
|
||||
export type ProfileMerge = Record<string, any>;
|
||||
|
||||
// partial of the clash config
|
||||
export type ProfileData = Partial<{
|
||||
rules: any[];
|
||||
proxies: any[];
|
||||
"proxy-groups": any[];
|
||||
"proxy-providers": any[];
|
||||
"rule-providers": any[];
|
||||
}>;
|
||||
|
||||
export interface ChainItem {
|
||||
item: ProfileItem;
|
||||
merge?: object;
|
||||
merge?: ProfileMerge;
|
||||
script?: string;
|
||||
}
|
||||
|
||||
export interface EnhancedPayload {
|
||||
chain: ChainItem[];
|
||||
current: object;
|
||||
current: ProfileData;
|
||||
callback: string;
|
||||
}
|
||||
|
||||
export interface EnhancedResult {
|
||||
data: ProfileData;
|
||||
status: string;
|
||||
error?: string;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user