refactor: api and command

This commit is contained in:
GyDi
2021-12-25 22:33:29 +08:00
parent afa56e916e
commit e76855ad0e
22 changed files with 369 additions and 329 deletions

87
src/services/api.ts Normal file
View File

@@ -0,0 +1,87 @@
import axios, { AxiosInstance } from "axios";
import { ApiType } from "./types";
let axiosIns: AxiosInstance = null!;
let server = "127.0.0.1:9090";
let secret = "";
type Callback<T> = (data: T) => void;
/// initialize some infomation
export function initAxios(info: { server?: string; secret?: string }) {
if (info.server) server = info.server;
if (info.secret) secret = info.secret;
axiosIns = axios.create({
baseURL: `http://${server}`,
headers: secret ? { Authorization: `Bearer ${secret}` } : {},
});
axiosIns.interceptors.response.use((r) => r.data);
}
/// get infomation
export function getInfomation() {
return { server, secret };
}
/// Get Version
export async function getVersion() {
return axiosIns.get("/version") as Promise<{
premium: boolean;
version: string;
}>;
}
/// Get current base configs
export async function getClashConfig() {
return axiosIns.get("/configs") as Promise<ApiType.ConfigData>;
}
/// Update current configs
export async function updateConfigs(config: Partial<ApiType.ConfigData>) {
return axiosIns.patch("/configs", config);
}
/// Get current rules
export async function getRules() {
return axiosIns.get("/rules") as Promise<ApiType.RuleItem[]>;
}
/// Update the Proxy Choose
export async function updateProxy(group: string, proxy: string) {
return axiosIns.put(`/proxies/${group}`, { name: proxy });
}
/// Get the Proxy infomation
export async function getProxies() {
const response = await axiosIns.get<any, any>("/proxies");
const proxies = (response?.proxies ?? {}) as Record<
string,
ApiType.ProxyItem
>;
const global = proxies["GLOBAL"];
const order = global?.all;
let groups: ApiType.ProxyGroupItem[] = [];
if (order) {
groups = order
.filter((name) => proxies[name]?.all)
.map((name) => proxies[name])
.map((each) => ({
...each,
all: each.all!.map((item) => proxies[item]),
}));
} else {
groups = Object.values(proxies)
.filter((each) => each.name !== "GLOBAL" && each.all)
.map((each) => ({
...each,
all: each.all!.map((item) => proxies[item]),
}));
groups.sort((a, b) => b.name.localeCompare(a.name));
}
return { global, groups, proxies };
}

View File

@@ -1,26 +0,0 @@
import axios, { AxiosInstance } from "axios";
import { getClashInfo } from "./command";
let axiosIns: AxiosInstance | null = null;
export async function getAxios() {
if (axiosIns) return axiosIns;
let server = "127.0.0.1:9090";
let secret = "";
try {
const info = await getClashInfo();
const { server: server_, secret: secret_ } = info?.controller ?? {};
if (server_) server = server_;
if (secret_) secret = secret_;
} catch {}
axiosIns = axios.create({
baseURL: `http://${server}`,
headers: secret ? { Authorization: `Bearer ${secret}` } : {},
});
axiosIns.interceptors.response.use((r) => r.data);
return axiosIns;
}

49
src/services/cmds.ts Normal file
View File

@@ -0,0 +1,49 @@
import { invoke } from "@tauri-apps/api/tauri";
import { ApiType, CmdType } from "./types";
export async function restartSidecar() {
return invoke<void>("restart_sidecar");
}
export async function getClashInfo() {
return invoke<CmdType.ClashInfo | null>("get_clash_info");
}
export async function patchClashConfig(payload: Partial<ApiType.ConfigData>) {
return invoke<void>("patch_clash_config", { payload });
}
export async function importProfile(url: string) {
return invoke<void>("import_profile", { url });
}
export async function updateProfile(index: number) {
return invoke<void>("update_profile", { index });
}
export async function getProfiles() {
return (await invoke<CmdType.ProfilesConfig>("get_profiles")) ?? {};
}
export async function setProfiles(
current: number,
profile: CmdType.ProfileItem
) {
return invoke<void>("set_profiles", { current, profile });
}
export async function putProfiles(current: number) {
return invoke<void>("put_profiles", { current });
}
export async function setSysProxy(enable: boolean) {
return invoke<void>("set_sys_proxy", { enable });
}
export async function getVergeConfig() {
return invoke<CmdType.VergeConfig>("get_verge_config");
}
export async function patchVergeConfig(payload: CmdType.VergeConfig) {
return invoke<void>("patch_verge_config", { payload });
}

View File

@@ -1,78 +0,0 @@
import { invoke } from "@tauri-apps/api/tauri";
import { ConfigType } from "./common";
export async function restartSidecar() {
return invoke<void>("restart_sidecar");
}
export interface ClashInfo {
status: string;
controller?: { server?: string; secret?: string };
message?: string;
}
export async function getClashInfo() {
return invoke<ClashInfo | null>("get_clash_info");
}
export async function patchClashConfig(payload: Partial<ConfigType>) {
return invoke<void>("patch_clash_config", { payload });
}
export async function importProfile(url: string) {
return invoke<void>("import_profile", { url });
}
export async function updateProfile(index: number) {
return invoke<void>("update_profile", { index });
}
export interface ProfileItem {
name?: string;
file?: string;
mode?: string;
url?: string;
updated?: number;
selected?: { name?: string; now?: string }[];
extra?: {
upload: number;
download: number;
total: number;
expire: number;
};
}
export interface ProfilesConfig {
current?: number;
items?: ProfileItem[];
}
export async function getProfiles() {
return (await invoke<ProfilesConfig>("get_profiles")) ?? {};
}
export async function setProfiles(current: number, profile: ProfileItem) {
return invoke<void>("set_profiles", { current, profile });
}
export async function putProfiles(current: number) {
return invoke<void>("put_profiles", { current });
}
export async function setSysProxy(enable: boolean) {
return invoke<void>("set_sys_proxy", { enable });
}
export interface VergeConfig {
theme_mode?: "light" | "dark";
enable_self_startup?: boolean;
enable_system_proxy?: boolean;
}
export async function getVergeConfig() {
return invoke<VergeConfig>("get_verge_config");
}
export async function patchVergeConfig(payload: VergeConfig) {
return invoke<void>("patch_verge_config", { payload });
}

View File

@@ -1,60 +0,0 @@
import axios from "axios";
import { getAxios } from "./base";
/// Get Version
export async function getVersion() {
return (await getAxios()).get("/version") as Promise<{
premium: boolean;
version: string;
}>;
}
export interface ConfigType {
port: number;
mode: string;
ipv6: boolean;
"socket-port": number;
"allow-lan": boolean;
"log-level": string;
"mixed-port": number;
"redir-port": number;
"socks-port": number;
"tproxy-port": number;
}
/// Get current base configs
export async function getClashConfig() {
return (await getAxios()).get("/configs") as Promise<ConfigType>;
}
/// Update current configs
export async function updateConfigs(config: Partial<ConfigType>) {
return (await getAxios()).patch("/configs", config);
}
interface RuleItem {
type: string;
payload: string;
proxy: string;
}
/// Get current rules
export async function getRules() {
return (await getAxios()).get("/rules") as Promise<RuleItem[]>;
}
/// Get logs stream
export async function getLogs(callback: (t: any) => void) {
const source = axios.CancelToken.source();
(await getAxios()).get("/logs", {
cancelToken: source.token,
onDownloadProgress: (progressEvent) => {
const data = progressEvent.currentTarget.response || "";
const lastData = data.slice(data.trim().lastIndexOf("\n") + 1);
callback(JSON.parse(lastData));
},
});
return source;
}

View File

@@ -1,9 +0,0 @@
import * as common from "./common";
import * as proxy from "./proxy";
import * as traffic from "./traffic";
export default {
...common,
...proxy,
...traffic,
};

View File

@@ -1,54 +0,0 @@
import { getAxios } from "./base";
export interface ProxyItem {
name: string;
type: string;
udp: boolean;
history: {
time: string;
delay: number;
}[];
all?: string[];
now?: string;
}
export type ProxyGroupItem = Omit<ProxyItem, "all"> & {
all: ProxyItem[];
};
/// Get the Proxy infomation
export async function getProxies() {
const axiosIns = await getAxios();
const response = await axiosIns.get<any, any>("/proxies");
const proxies = (response?.proxies ?? {}) as Record<string, ProxyItem>;
const global = proxies["GLOBAL"];
const order = global?.all;
let groups: ProxyGroupItem[] = [];
if (order) {
groups = order
.filter((name) => proxies[name]?.all)
.map((name) => proxies[name])
.map((each) => ({
...each,
all: each.all!.map((item) => proxies[item]),
}));
} else {
groups = Object.values(proxies)
.filter((each) => each.name !== "GLOBAL" && each.all)
.map((each) => ({
...each,
all: each.all!.map((item) => proxies[item]),
}));
groups.sort((a, b) => b.name.localeCompare(a.name));
}
return { global, groups, proxies };
}
/// Update the Proxy Choose
export async function updateProxy(group: string, proxy: string) {
return (await getAxios()).put(`/proxies/${group}`, { name: proxy });
}

View File

@@ -1,29 +0,0 @@
import axios from "axios";
import { getAxios } from "./base";
export interface TrafficData {
up: number;
down: number;
}
/// Get the traffic stream
export async function getTraffic(callback: (data: TrafficData) => void) {
const source = axios.CancelToken.source();
(await getAxios()).get("/traffic", {
cancelToken: source.token,
onDownloadProgress: (progressEvent) => {
const data = progressEvent.currentTarget.response || "";
const lastData = data.slice(data.trim().lastIndexOf("\n") + 1);
if (!lastData) callback({ up: 0, down: 0 });
try {
callback(JSON.parse(lastData) as TrafficData);
} catch {
callback({ up: 0, down: 0 });
}
},
});
return source;
}

115
src/services/types.ts Normal file
View File

@@ -0,0 +1,115 @@
/**
* Some interface for clash api
*/
export namespace ApiType {
export interface ConfigData {
port: number;
mode: string;
ipv6: boolean;
"socket-port": number;
"allow-lan": boolean;
"log-level": string;
"mixed-port": number;
"redir-port": number;
"socks-port": number;
"tproxy-port": number;
}
export interface RuleItem {
type: string;
payload: string;
proxy: string;
}
export interface ProxyItem {
name: string;
type: string;
udp: boolean;
history: {
time: string;
delay: number;
}[];
all?: string[];
now?: string;
}
export type ProxyGroupItem = Omit<ProxyItem, "all"> & {
all: ProxyItem[];
};
export interface TrafficItem {
up: number;
down: number;
}
export interface LogItem {
type: string;
time?: string;
payload: string;
}
export interface ConnectionsItem {
id: string;
metadata: {
network: string;
type: string;
host: string;
sourceIP: string;
sourcePort: string;
destinationPort: string;
destinationIP?: string;
};
upload: number;
download: number;
start: string;
chains: string[];
rule: string;
rulePayload: string;
}
export interface Connections {
downloadTotal: number;
uploadTotal: number;
connections: ConnectionsItem[];
}
}
/**
* Some interface for command
*/
export namespace CmdType {
export interface ClashInfo {
status: string;
controller?: { server?: string; secret?: string };
message?: string;
}
export interface ProfileItem {
name?: string;
file?: string;
mode?: string;
url?: string;
updated?: number;
selected?: {
name?: string;
now?: string;
}[];
extra?: {
upload: number;
download: number;
total: number;
expire: number;
};
}
export interface ProfilesConfig {
current?: number;
items?: ProfileItem[];
}
export interface VergeConfig {
theme_mode?: "light" | "dark";
enable_self_startup?: boolean;
enable_system_proxy?: boolean;
}
}