From 0fddcc959c6673c5fefeed679b16f753e331473d Mon Sep 17 00:00:00 2001 From: song_xiao_lin Date: Mon, 22 Apr 2024 14:40:27 +0800 Subject: [PATCH] feat: Contro --- public/background.js | 1 + src/App.tsx | 8 +- src/components/Contro.tsx | 233 ++++++++++++++++++++++------------- src/components/Proxifier.tsx | 5 +- 4 files changed, 153 insertions(+), 94 deletions(-) diff --git a/public/background.js b/public/background.js index d20c41a..93dce4b 100644 --- a/public/background.js +++ b/public/background.js @@ -44,6 +44,7 @@ console.info("Chrome Extenstion Background is loaded") let proxyHost = ""; chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { + console.log(msg); if (msg.action === "connect") { console.info("Start to connect websocket") const host = msg['host'] || "127.0.0.1" diff --git a/src/App.tsx b/src/App.tsx index fc74cd9..3d8761d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,9 +1,9 @@ import React from "react"; import "./App.css"; import { ConfigProvider } from "antd"; -// import { Contro } from "@components/Contro"; +import { Contro } from "@components/Contro"; import { Proxifier } from "@components/Proxifier"; -// import { Controller } from "./components/Controller"; +import { Controller } from "./components/Controller"; function App() { return ( @@ -15,9 +15,9 @@ function App() { }} >
- {/* */} - {/* */} + + {/* */}
); diff --git a/src/components/Contro.tsx b/src/components/Contro.tsx index dace78e..57a5c04 100644 --- a/src/components/Contro.tsx +++ b/src/components/Contro.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from "react"; +import React, { useEffect, useMemo, useState } from "react"; import { Divider, Tooltip, Input } from "antd"; import classNames from "classnames"; import { @@ -8,107 +8,162 @@ import { RefreshIcon, XIcon, } from "@assets/icon/icon"; +import { useUpdateEffect } from "ahooks"; +import { wsc } from "@network/chrome"; import "./Contro.css"; interface ControProps {} export const Contro: React.FC = () => { const [isEdit, setIsEdit] = useState(false); const [connected, setConnected] = useState(false); + const [init, setInit] = React.useState(false); const [autoFindFailedReason, setAutoFindFailedReason] = useState(""); - const [enginePort, setEnginePort] = useState("0"); + const [enginePort, setEnginePort] = useState(""); const [enginePortTemp, setEnginePortTemp] = useState(enginePort); - // useEffect(() => { - // const updateStatus = () => { - // wsc.updateWSCStatus(); - // }; - // updateStatus(); - // const id = setInterval(updateStatus, 500); + useEffect(() => { + const updateStatus = () => { + wsc.updateWSCStatus(); + }; + updateStatus(); + const id = setInterval(updateStatus, 500); - // wsc.onWSCMessage((req: { connected: boolean }) => { - // if (req["connected"] === undefined) { - // return; - // } - // console.log("connected", req.connected); - // setConnected(req.connected); - // }); - // return () => { - // clearInterval(id); - // }; - // }, []); + wsc.onWSCMessage((req: { connected: boolean }) => { + if (req["connected"] === undefined) { + return; + } + setConnected(req.connected); + setInit(true); + }); + return () => { + clearInterval(id); + }; + }, []); + + const findPort = (port: number, max: number) => { + const ws = new WebSocket(`ws://127.0.0.1:${port}`); + ws.onclose = (e: CloseEvent) => { + if (e.reason !== `FoundYakitWebSocketController` && port + 1 <= max) { + setTimeout(() => findPort(port + 1, max), 300); + } + + if (port + 1 > max) { + setAutoFindFailedReason( + "Cannot found Yakit or Yakit WebSocket Controller Port is not right" + ); + } + }; + ws.onopen = () => { + setEnginePort(port + ""); + ws.close(1000, "FoundYakitWebSocketController"); + }; + }; + + useUpdateEffect(() => { + if (!init || connected) { + return; + } + findPort(11212, 11222); + }, [connected, init]); + + const safeConnected = useMemo(() => { + return connected && enginePort; + }, [connected, enginePort]); + + const failConnected = useMemo(() => { + return !connected && autoFindFailedReason; + }, [connected, autoFindFailedReason]); return ( -
-
Yakit 引擎连接状态:
-
- {isEdit ? ( - <> - { - // TODO 需要校验 - const value = e.target.value; - setEnginePortTemp(value); - }} - /> -
- { - setIsEdit(false); - }} - /> - { - setIsEdit(false); - setEnginePort(enginePortTemp); - }} - /> -
- - ) : ( - <> -
- {autoFindFailedReason - ? autoFindFailedReason + "(" + enginePort + ")" - : "已连接(" + enginePort + ")"} -
-
- - { - setIsEdit(true); - setEnginePortTemp(enginePort); + <> + {safeConnected || failConnected ? ( +
+
Yakit 引擎连接状态:
+
+ {isEdit ? ( + <> + { + const value = e.target.value; + setEnginePortTemp(value); }} /> - - - {autoFindFailedReason ? ( - - - - ) : ( - - - - )} -
- - )} -
-
+
+ { + setIsEdit(false); + }} + /> + { + if (enginePortTemp) { + setIsEdit(false); + setEnginePort(enginePortTemp); + wsc.connect(Number(enginePortTemp)); + } + }} + /> +
+ + ) : ( + <> +
+ {safeConnected && "已连接(" + enginePort + ")"} + {failConnected && + (enginePort + ? autoFindFailedReason + "(" + enginePort + ")" + : autoFindFailedReason)} +
+
+ + { + setIsEdit(true); + setEnginePortTemp(enginePort); + }} + /> + + + {safeConnected && ( + + wsc.disconnect()} /> + + )} + {failConnected && ( + + { + if (enginePort) { + wsc.connect(Number(enginePort)); + } else { + findPort(11212, 11222); + } + }} + /> + + )} +
+ + )} +
+
+ ) : null} + ); }; diff --git a/src/components/Proxifier.tsx b/src/components/Proxifier.tsx index 7121edf..05c49d9 100644 --- a/src/components/Proxifier.tsx +++ b/src/components/Proxifier.tsx @@ -68,7 +68,10 @@ export const Proxifier: React.FC = () => { wsc.updateProxyStatus(); wsc.onProxyStatusMessage((msg) => { - if (!msg.proxy || !msg.enable) { + if (msg.proxy === undefined || msg.enable === undefined) { + return; + } + if (msg.proxy === "" || msg.enable === false) { if (proxyList.some((i) => i.open)) { const copyProxyList = [...proxyList]; copyProxyList.forEach((i) => {