diff --git a/src/apis/index.ts b/src/apis/index.ts index b436704..c677733 100644 --- a/src/apis/index.ts +++ b/src/apis/index.ts @@ -1,6 +1,6 @@ -import { createSignal } from 'solid-js' +import { createSignal, ResourceActions } from 'solid-js' import { toast } from 'solid-toast' -import { setBackendConfig, useRequest } from '~/signals' +import { useRequest } from '~/signals' import { BackendVersion, Config, @@ -22,11 +22,34 @@ export const closeSingleConnectionAPI = (id: string) => { return request.delete(`connections/${id}`) } +export const [reloadingConfigFile, setReloadingConfigFile] = createSignal(false) export const [updatingGEODatabases, setUpdatingGEODatabases] = createSignal(false) +export const [flushingFakeIPData, setFlushingFakeIPData] = createSignal(false) export const [upgradingBackend, setUpgradingBackend] = createSignal(false) export const [restartingBackend, setRestartingBackend] = createSignal(false) +export const reloadConfigFileAPI = async () => { + const request = useRequest() + setReloadingConfigFile(true) + try { + await request.put('configs', { + searchParams: { force: true }, + json: { path: '', payload: '' }, + }) + } catch {} + setReloadingConfigFile(false) +} + +export const flushFakeIPDataAPI = async () => { + const request = useRequest() + setFlushingFakeIPData(true) + try { + await request.post('cache/fakeip/flush') + } catch {} + setFlushingFakeIPData(false) +} + export const updateGEODatabasesAPI = async () => { const request = useRequest() setUpdatingGEODatabases(true) @@ -63,21 +86,14 @@ export const fetchBackendConfigAPI = () => { export const updateBackendConfigAPI = async ( key: keyof Config, value: Config[keyof Config], + refetch: ResourceActions['refetch'], ) => { try { const request = useRequest() - await request - .patch('configs', { - body: JSON.stringify({ - [key]: value, - }), - }) - .json() + await request.patch('configs', { json: { [key]: value } }).json() - const updatedConfig = await fetchBackendConfigAPI() - - setBackendConfig(updatedConfig) + await refetch() } catch (err) { toast.error((err as Error).message) } diff --git a/src/i18n/en.ts b/src/i18n/en.ts index 59f709e..75fc421 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -87,4 +87,6 @@ export default { closed: 'Closed', sort: 'Sort', hideUnAvailableProxies: 'Hide UnAvailable Proxies', + reloadConfigFile: 'Reload Config File', + flushFakeIPData: 'Flush Fake-IP Data', } diff --git a/src/i18n/zh.ts b/src/i18n/zh.ts index 27bc865..7ae6d7f 100644 --- a/src/i18n/zh.ts +++ b/src/i18n/zh.ts @@ -87,4 +87,6 @@ export default { closed: '已关闭', sort: '排序', hideUnAvailableProxies: '隐藏不可用节点', + reloadConfigFile: '重新加载配置文件', + flushFakeIPData: '清空 Fake-IP 数据', } diff --git a/src/pages/Config.tsx b/src/pages/Config.tsx index 66bf3b4..a722ee5 100644 --- a/src/pages/Config.tsx +++ b/src/pages/Config.tsx @@ -2,11 +2,22 @@ import { createForm } from '@felte/solid' import { validator } from '@felte/validator-zod' import { useI18n } from '@solid-primitives/i18n' import { useNavigate } from '@solidjs/router' -import { For, Show, createSignal, onMount } from 'solid-js' +import { + For, + Show, + createEffect, + createResource, + createSignal, + onMount, +} from 'solid-js' import { z } from 'zod' import { fetchBackendConfigAPI, fetchBackendVersionAPI, + flushFakeIPDataAPI, + flushingFakeIPData, + reloadConfigFileAPI, + reloadingConfigFile, restartBackendAPI, restartingBackend, updateBackendConfigAPI, @@ -19,11 +30,9 @@ import { Button, ConfigTitle } from '~/components' import { LANG, MODE_OPTIONS, ROUTES, themes } from '~/constants' import { autoSwitchTheme, - backendConfig, favDayTheme, favNightTheme, setAutoSwitchTheme, - setBackendConfig, setFavDayTheme, setFavNightTheme, setSelectedEndpoint, @@ -103,27 +112,54 @@ const configFormSchema = z.object({ const ConfigForm = () => { const [t] = useI18n() + const navigate = useNavigate() const portsList = [ { label: 'HTTP Port', key: 'port', + onChange: (e: Event & { target: HTMLInputElement }) => + void updateBackendConfigAPI('port', Number(e.target.value), refetch), }, { label: 'Socks Port', key: 'socks-port', + onChange: (e: Event & { target: HTMLInputElement }) => + void updateBackendConfigAPI( + 'socks-port', + Number(e.target.value), + refetch, + ), }, { label: 'Redir Port', key: 'redir-port', + onChange: (e: Event & { target: HTMLInputElement }) => + void updateBackendConfigAPI( + 'redir-port', + Number(e.target.value), + refetch, + ), }, { label: 'TProxy Port', key: 'tproxy-port', + onChange: (e: Event & { target: HTMLInputElement }) => + void updateBackendConfigAPI( + 'tproxy-port', + Number(e.target.value), + refetch, + ), }, { label: 'Mixed Port', key: 'mixed-port', + onChange: (e: Event & { target: HTMLInputElement }) => + void updateBackendConfigAPI( + 'mixed-port', + Number(e.target.value), + refetch, + ), }, ] @@ -131,19 +167,30 @@ const ConfigForm = () => { z.infer >({ extend: validator({ schema: configFormSchema }) }) - onMount(async () => { - const configs = await fetchBackendConfigAPI() - setBackendConfig(configs) - setInitialValues(configs) - reset() + const [configsData, { refetch }] = createResource(fetchBackendConfigAPI) + + createEffect(() => { + const configs = configsData() + + if (configs) { + setInitialValues(configs) + reset() + } }) + const onSwitchEndpointClick = () => { + setSelectedEndpoint('') + navigate(ROUTES.Setup) + } + return (