From bb584fc7d12b1af863c8a127ac1f3f3dabc7e959 Mon Sep 17 00:00:00 2001 From: v1ll4n Date: Thu, 21 Mar 2024 11:08:42 +0800 Subject: [PATCH] add proxy --- public/background.js | 26 +++++++++++ src/App.tsx | 3 +- src/components/Controller.tsx | 4 +- src/components/Proxifier.tsx | 85 +++++++++++++++++++++++++++++++++++ src/network/chrome.ts | 24 +++++++++- 5 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 src/components/Proxifier.tsx diff --git a/public/background.js b/public/background.js index d9fcbf8..d20c41a 100644 --- a/public/background.js +++ b/public/background.js @@ -41,6 +41,8 @@ setInterval(heartbeat, 3000) console.info("Chrome Extenstion Background is loaded") +let proxyHost = ""; + chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { if (msg.action === "connect") { console.info("Start to connect websocket") @@ -55,5 +57,29 @@ chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { } else { chrome.runtime.sendMessage({connected: false}) } + } else if (msg.action === 'setproxy') { + chrome.proxy.settings.set({ + value: { + mode: "fixed_servers", + rules: { + singleProxy: { + scheme: msg.scheme, + host: msg.host, + port: parseInt(`${msg.port}`) + } + } + }, + scope: 'regular', + }) + proxyHost = `${msg.scheme}://${msg.host}:${msg.port}` + } else if (msg.action === 'clearproxy') { + chrome.proxy.settings.clear({}) + proxyHost = "" + } else if (msg.action === 'proxystatus') { + if (proxyHost !== '') { + chrome.runtime.sendMessage({enable: !!proxyHost, proxy: proxyHost}) + } else { + chrome.runtime.sendMessage({enable: false, proxy: ""}) + } } }) diff --git a/src/App.tsx b/src/App.tsx index 5771624..d8f097f 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,6 +2,7 @@ import React from 'react'; import './App.css'; import {Layout, Row} from 'antd'; import {Controller} from "./components/Controller"; +import {Proxifier} from "./components/Proxifier"; function App() { return ( @@ -11,7 +12,7 @@ function App() { - PROXY + diff --git a/src/components/Controller.tsx b/src/components/Controller.tsx index 1ed86ad..f35f9c9 100644 --- a/src/components/Controller.tsx +++ b/src/components/Controller.tsx @@ -18,12 +18,12 @@ export const Controller: React.FC = (props) => { useEffect(() => { // control status const updateStatus = () => { - wsc.getStatus() + wsc.updateWSCStatus() } updateStatus() const id = setInterval(updateStatus, 1000) - wsc.listen((req: { connected: boolean }) => { + wsc.onWSCMessage((req: { connected: boolean }) => { setConnected(req.connected) setTimeout(() => { setInit(true) diff --git a/src/components/Proxifier.tsx b/src/components/Proxifier.tsx new file mode 100644 index 0000000..2b32a48 --- /dev/null +++ b/src/components/Proxifier.tsx @@ -0,0 +1,85 @@ +import React, {useEffect, useState} from "react"; +import {Button, Card, Form, Input, InputNumber, Select, Space} from "antd"; +import {wsc} from "../network/chrome"; + +export interface ProxifierProp { + +} + +const {Compact} = Space; + +interface ProxyConfig { + scheme: "http" | "socks5"; + host: string; + port: number; +} + +export const Proxifier: React.FC = (props) => { + const [config, setConfig] = useState({ + scheme: "http", + host: "127.0.0.1", + port: 8083 + }); + const [init, setInit] = useState(false); + const [proxyEnable, setProxyEnable] = useState(false); + const [currentProxy, setCurrentProxy] = useState(""); + + useEffect(() => { + if (init) { + return + } + + wsc.onProxyStatusMessage(msg => { + setInit(true) + setProxyEnable(msg.enable) + setCurrentProxy(msg.proxy) + }) + }, [init]) + + // proxy + useEffect(() => { + const update = () => { + wsc.updateProxyStatus() + } + update() + const id = setInterval(update, 1000) + return () => { + clearInterval(id) + } + }, []) + + return +
{ + e.preventDefault() + setInit(false) + wsc.setproxy(config.scheme, config.host, config.port) + }} disabled={!init}> + + + + (setConfig({...config, host: e.target.value}))}/> + (setConfig({...config, port: e}))} + /> + + + + + +
+
+}; \ No newline at end of file diff --git a/src/network/chrome.ts b/src/network/chrome.ts index 0baf0ef..6727193 100644 --- a/src/network/chrome.ts +++ b/src/network/chrome.ts @@ -15,13 +15,33 @@ export namespace wsc { }); } - export function getStatus() { + export function updateWSCStatus() { chrome.runtime.sendMessage({ action: 'status', }); } - export function listen(onMessage: (message: any) => void) { + export function updateProxyStatus() { + chrome.runtime.sendMessage({ + action: 'proxystatus', + }); + } + + export function onWSCMessage(onMessage: (message: any) => void) { chrome.runtime.onMessage.addListener(onMessage); } + + export function onProxyStatusMessage(onMessage: (message: { enable: boolean, proxy: string }) => any) { + chrome.runtime.onMessage.addListener(onMessage) + } + + export function setproxy(scheme: string, host: string, port: number) { + chrome.runtime.sendMessage({action: "setproxy", scheme, host, port}) + } + + export function clearproxy() { + chrome.runtime.sendMessage({action: 'clearproxy'}) + } + + }