From e5a6393fa8cdf4c92b2e68b5d2f4c7633ca5d690 Mon Sep 17 00:00:00 2001 From: yaling888 <73897884+yaling888@users.noreply.github.com> Date: Sat, 7 May 2022 02:43:42 +0800 Subject: feat: add tun stack config to config page --- src/api/configs.ts | 6 +-- src/components/Config.tsx | 100 +++++++++++++++++++++++++++++++--------------- src/store/configs.ts | 28 ++++++++++--- src/store/types.ts | 16 ++++++++ 4 files changed, 109 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/api/configs.ts b/src/api/configs.ts index e084261..ed2b1e9 100644 --- a/src/api/configs.ts +++ b/src/api/configs.ts @@ -1,5 +1,5 @@ import { getURLAndInit } from 'src/misc/request-helper'; -import { ClashGeneralConfig } from 'src/store/types'; +import { ClashGeneralConfig, TunPartial } from 'src/store/types'; import { ClashAPIConfig } from 'src/types'; const endpoint = '/configs'; @@ -14,7 +14,7 @@ export async function fetchConfigs(apiConfig: ClashAPIConfig) { // req body // { Path: string } -type ClashConfigPartial = Partial; +type ClashConfigPartial = TunPartial; function configsPatchWorkaround(o: ClashConfigPartial) { // backward compatibility for older clash using `socket-port` if ('socks-port' in o) { @@ -32,7 +32,7 @@ export async function updateConfigs( return await fetch(url + endpoint, { ...init, body, method: 'PATCH' }); } -export async function reloadConfigs( +export async function reloadConfigFile( apiConfig: ClashAPIConfig ) { const { url, init } = getURLAndInit(apiConfig); diff --git a/src/components/Config.tsx b/src/components/Config.tsx index 3d9e36e..808ffd9 100644 --- a/src/components/Config.tsx +++ b/src/components/Config.tsx @@ -11,7 +11,13 @@ import { getLatencyTestUrl, getSelectedChartStyleIndex, } from '../store/app'; -import { fetchConfigs, getConfigs, updateConfigs, reloadConfigs, flushFakeIPPool } from '../store/configs'; +import { + fetchConfigs, + flushFakeIPPool, + getConfigs, + reloadConfigFile, + updateConfigs, +} from '../store/configs'; import { openModal } from '../store/modals'; import Button from './Button'; import s0 from './Config.module.scss'; @@ -40,7 +46,7 @@ const portFields = [ { key: 'socks-port', label: 'SOCKS5 Proxy Port' }, { key: 'mixed-port', label: 'Mixed Port' }, { key: 'redir-port', label: 'Redir Port' }, - { key: 'tproxy-port', label: 'tProxy Port' }, + { key: 'tproxy-port', label: 'TProxy Port' }, { key: 'mitm-port', label: 'MITM Port' }, ]; @@ -50,10 +56,15 @@ const langOptions = [ ]; const modeOptions = [ - ['Global', 'Global'], - ['Rule', 'Rule'], - ['Script', 'Script'], - ['Direct', 'Direct'], + ['rule', 'Rule'], + ['script', 'Script'], + ['direct', 'Direct'], + ['global', 'Global'], +]; + +const tunStackOptions = [ + ['gVisor', 'gVisor'], + ['System', 'System'], ]; const mapState = (s: State) => ({ @@ -112,6 +123,14 @@ function ConfigImpl({ [configState] ); + const setTunConfigState = useCallback( + (name, val) => { + const tun = {...configState.tun, [name]: val } + setConfigStateInternal({ ...configState, tun: {...tun}}); + }, + [configState] + ); + const handleSwitchOnChange = useCallback( (checked: boolean) => { const name = 'allow-lan'; @@ -145,11 +164,15 @@ function ConfigImpl({ } setConfigState(name, value); break; + case 'stack': + setTunConfigState(name, value); + dispatch(updateConfigs(apiConfig, { 'tun': {[name]: value }})); + break; default: return; } }, - [apiConfig, dispatch, setConfigState] + [apiConfig, dispatch, setConfigState, setTunConfigState] ); const handleInputOnChange = useCallback( @@ -186,8 +209,8 @@ function ConfigImpl({ [apiConfig, dispatch, updateAppConfig] ); - const handleReloadConfigs = useCallback(() => { - dispatch(reloadConfigs(apiConfig)); + const handleReloadConfigFile = useCallback(() => { + dispatch(reloadConfigFile(apiConfig)); },[apiConfig, dispatch]); const handleFlushFakeIPPool = useCallback(() => { @@ -242,6 +265,17 @@ function ConfigImpl({ /> +
+
TUN Stack
+