feat: Contro

This commit is contained in:
song_xiao_lin
2024-04-22 17:35:43 +08:00
parent 19015fae87
commit 3ab46d55c0
4 changed files with 153 additions and 94 deletions
+1
View File
@@ -44,6 +44,7 @@ console.info("Chrome Extenstion Background is loaded")
let proxyHost = ""; let proxyHost = "";
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
console.log(msg);
if (msg.action === "connect") { if (msg.action === "connect") {
console.info("Start to connect websocket") console.info("Start to connect websocket")
const host = msg['host'] || "127.0.0.1" const host = msg['host'] || "127.0.0.1"
+4 -4
View File
@@ -1,9 +1,9 @@
import React from "react"; import React from "react";
import "./App.css"; import "./App.css";
import { ConfigProvider } from "antd"; import { ConfigProvider } from "antd";
// import { Contro } from "@components/Contro"; import { Contro } from "@components/Contro";
import { Proxifier } from "@components/Proxifier"; import { Proxifier } from "@components/Proxifier";
// import { Controller } from "./components/Controller"; import { Controller } from "./components/Controller";
function App() { function App() {
return ( return (
@@ -15,9 +15,9 @@ function App() {
}} }}
> >
<div className="App"> <div className="App">
{/* <Contro /> */} <Contro />
{/* <Controller/> */}
<Proxifier/> <Proxifier/>
{/* <Controller/> */}
</div> </div>
</ConfigProvider> </ConfigProvider>
); );
+144 -89
View File
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react"; import React, { useEffect, useMemo, useState } from "react";
import { Divider, Tooltip, Input } from "antd"; import { Divider, Tooltip, Input } from "antd";
import classNames from "classnames"; import classNames from "classnames";
import { import {
@@ -8,107 +8,162 @@ import {
RefreshIcon, RefreshIcon,
XIcon, XIcon,
} from "@assets/icon/icon"; } from "@assets/icon/icon";
import { useUpdateEffect } from "ahooks";
import { wsc } from "@network/chrome";
import "./Contro.css"; import "./Contro.css";
interface ControProps {} interface ControProps {}
export const Contro: React.FC<ControProps> = () => { export const Contro: React.FC<ControProps> = () => {
const [isEdit, setIsEdit] = useState<boolean>(false); const [isEdit, setIsEdit] = useState<boolean>(false);
const [connected, setConnected] = useState(false); const [connected, setConnected] = useState(false);
const [init, setInit] = React.useState(false);
const [autoFindFailedReason, setAutoFindFailedReason] = useState<string>(""); const [autoFindFailedReason, setAutoFindFailedReason] = useState<string>("");
const [enginePort, setEnginePort] = useState<string>("0"); const [enginePort, setEnginePort] = useState<string>("");
const [enginePortTemp, setEnginePortTemp] = useState<string>(enginePort); const [enginePortTemp, setEnginePortTemp] = useState<string>(enginePort);
// useEffect(() => { useEffect(() => {
// const updateStatus = () => { const updateStatus = () => {
// wsc.updateWSCStatus(); wsc.updateWSCStatus();
// }; };
// updateStatus(); updateStatus();
// const id = setInterval(updateStatus, 500); const id = setInterval(updateStatus, 500);
// wsc.onWSCMessage((req: { connected: boolean }) => { wsc.onWSCMessage((req: { connected: boolean }) => {
// if (req["connected"] === undefined) { if (req["connected"] === undefined) {
// return; return;
// } }
// console.log("connected", req.connected); setConnected(req.connected);
// setConnected(req.connected); setInit(true);
// }); });
// return () => { return () => {
// clearInterval(id); 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 ( return (
<div <>
className={classNames("Contro", { {safeConnected || failConnected ? (
["Contro-success-bg"]: !autoFindFailedReason, <div
["Contro-error-bg"]: autoFindFailedReason, className={classNames("Contro", {
})} ["Contro-success-bg"]: safeConnected,
> ["Contro-error-bg"]: failConnected,
<div className="Contro-lable">Yakit 引擎连接状态:</div> })}
<div className="Contro-cont"> >
{isEdit ? ( <div className="Contro-lable">Yakit 引擎连接状态:</div>
<> <div className="Contro-cont">
<Input {isEdit ? (
rootClassName="Contro-cont-input" <>
value={enginePortTemp} <Input
onChange={(e) => { rootClassName="Contro-cont-input"
// TODO 需要校验 value={enginePortTemp}
const value = e.target.value; placeholder="输入范围 11212 - 11222"
setEnginePortTemp(value); onChange={(e) => {
}} const value = e.target.value;
/> setEnginePortTemp(value);
<div className="Contro-handle-icon">
<XIcon
className="icon-p"
onClick={() => {
setIsEdit(false);
}}
/>
<CheckIcon
className="contro-handle-icon-check icon-p"
onClick={() => {
setIsEdit(false);
setEnginePort(enginePortTemp);
}}
/>
</div>
</>
) : (
<>
<div
className={classNames("Contro-cont-text", {
["Contro-cont-text-success"]: !autoFindFailedReason,
["Contro-cont-text-error"]: autoFindFailedReason,
})}
>
{autoFindFailedReason
? autoFindFailedReason + "(" + enginePort + ")"
: "已连接(" + enginePort + ")"}
</div>
<div className="Contro-handle-icon">
<Tooltip title="修改监听端口">
<PencilAltIcon
className="icon-p"
onClick={() => {
setIsEdit(true);
setEnginePortTemp(enginePort);
}} }}
/> />
</Tooltip> <div className="Contro-handle-icon">
<Divider type="vertical" style={{ height: 16 }} /> <XIcon
{autoFindFailedReason ? ( className="icon-p"
<Tooltip title="重新连接"> onClick={() => {
<RefreshIcon /> setIsEdit(false);
</Tooltip> }}
) : ( />
<Tooltip title="断开连接"> <CheckIcon
<ExitIcon /> className="contro-handle-icon-check icon-p"
</Tooltip> onClick={() => {
)} if (enginePortTemp) {
</div> setIsEdit(false);
</> setEnginePort(enginePortTemp);
)} wsc.connect(Number(enginePortTemp));
</div> }
</div> }}
/>
</div>
</>
) : (
<>
<div
className={classNames("Contro-cont-text", {
["Contro-cont-text-success"]: safeConnected,
["Contro-cont-text-error"]: failConnected,
})}
>
{safeConnected && "已连接(" + enginePort + ")"}
{failConnected &&
(enginePort
? autoFindFailedReason + "(" + enginePort + ")"
: autoFindFailedReason)}
</div>
<div className="Contro-handle-icon">
<Tooltip title="修改监听端口">
<PencilAltIcon
className="icon-p"
onClick={() => {
setIsEdit(true);
setEnginePortTemp(enginePort);
}}
/>
</Tooltip>
<Divider type="vertical" style={{ height: 16 }} />
{safeConnected && (
<Tooltip title="断开连接">
<ExitIcon onClick={() => wsc.disconnect()} />
</Tooltip>
)}
{failConnected && (
<Tooltip title="重新连接">
<RefreshIcon
onClick={() => {
if (enginePort) {
wsc.connect(Number(enginePort));
} else {
findPort(11212, 11222);
}
}}
/>
</Tooltip>
)}
</div>
</>
)}
</div>
</div>
) : null}
</>
); );
}; };
+4 -1
View File
@@ -68,7 +68,10 @@ export const Proxifier: React.FC<ProxifierProps> = () => {
wsc.updateProxyStatus(); wsc.updateProxyStatus();
wsc.onProxyStatusMessage((msg) => { 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)) { if (proxyList.some((i) => i.open)) {
const copyProxyList = [...proxyList]; const copyProxyList = [...proxyList];
copyProxyList.forEach((i) => { copyProxyList.forEach((i) => {