Explorar o código

feat: add some clash api

GyDi %!s(int64=3) %!d(string=hai) anos
pai
achega
4bb9e10946
Modificáronse 3 ficheiros con 77 adicións e 1 borrados
  1. 19 1
      src/pages/log.tsx
  2. 56 0
      src/services/common.ts
  3. 2 0
      src/services/index.ts

+ 19 - 1
src/pages/log.tsx

@@ -1,5 +1,23 @@
+import { useEffect } from "react";
+import { Box, Typography } from "@mui/material";
+import services from "../services";
+
 const LogPage = () => {
-  return <h1>Log</h1>;
+  useEffect(() => {
+    const sourcePromise = services.getLogs(console.log);
+
+    return () => {
+      sourcePromise.then((src) => src.cancel());
+    };
+  }, []);
+
+  return (
+    <Box sx={{ width: 0.9, maxWidth: "850px", mx: "auto", mb: 2 }}>
+      <Typography variant="h4" component="h1" sx={{ py: 2 }}>
+        Logs
+      </Typography>
+    </Box>
+  );
 };
 
 export default LogPage;

+ 56 - 0
src/services/common.ts

@@ -0,0 +1,56 @@
+import axios from "axios";
+import axiosIns from "./base";
+
+/// Get Version
+export async function getVersion() {
+  return axiosIns.get("/version") as Promise<{
+    premium: boolean;
+    version: string;
+  }>;
+}
+
+export interface ConfigType {
+  port: number;
+  mode: string;
+  "socket-port": number;
+  "allow-lan": boolean;
+  "log-level": string;
+  "mixed-port": number;
+}
+
+/// Get current base configs
+export async function getConfigs() {
+  return axiosIns.get("/configs") as Promise<ConfigType>;
+}
+
+/// Update current configs
+export async function updateConfigs(config: Partial<ConfigType>) {
+  return axiosIns.patch("/configs", config);
+}
+
+interface RuleItem {
+  type: string;
+  payload: string;
+  proxy: string;
+}
+
+/// Get current rules
+export async function getRules() {
+  return axiosIns.get("/rules") as Promise<RuleItem[]>;
+}
+
+/// Get logs stream
+export async function getLogs(callback: (t: any) => void) {
+  const source = axios.CancelToken.source();
+
+  axiosIns.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;
+}

+ 2 - 0
src/services/index.ts

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