metacubexd/src/pages/Config.tsx

393 lines
9.6 KiB
TypeScript
Raw Normal View History

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,
createEffect,
createResource,
createSignal,
onMount,
} from 'solid-js'
2023-08-28 22:13:48 +08:00
import { z } from 'zod'
2023-09-12 21:33:16 +08:00
import {
fetchBackendConfigAPI,
fetchBackendVersionAPI,
flushFakeIPDataAPI,
flushingFakeIPData,
reloadConfigFileAPI,
reloadingConfigFile,
2023-09-12 21:33:16 +08:00
restartBackendAPI,
restartingBackend,
updateBackendConfigAPI,
updateGEODatabasesAPI,
updatingGEODatabases,
upgradeBackendAPI,
upgradingBackend,
} from '~/apis'
2023-09-15 23:43:55 +08:00
import { Button, ConfigTitle } from '~/components'
import { LANG, MODE_OPTIONS, ROUTES, themes } from '~/constants'
import {
2023-09-02 19:06:02 +08:00
autoSwitchTheme,
favDayTheme,
favNightTheme,
setAutoSwitchTheme,
setFavDayTheme,
setFavNightTheme,
setSelectedEndpoint,
2023-09-16 23:55:42 +08:00
setUseTwemoji,
useRequest,
useTwemoji,
} from '~/signals'
2023-09-12 21:33:16 +08:00
import type { DNSQuery } from '~/types'
2023-08-28 22:13:48 +08:00
const dnsQueryFormSchema = z.object({
name: z.string(),
type: z.string(),
})
const DNSQueryForm = () => {
const [t] = useI18n()
const request = useRequest()
2023-09-03 02:22:38 +08:00
const { form, isSubmitting } = createForm<z.infer<typeof dnsQueryFormSchema>>(
{
extend: validator({ schema: dnsQueryFormSchema }),
onSubmit: async (values) => {
request
.get('dns/query', {
searchParams: { name: values.name, type: values.type },
})
.json<DNSQuery>()
.then(({ Answer }) =>
setDNSQueryResult(Answer?.map(({ data }) => data) || []),
)
},
},
2023-09-03 02:22:38 +08:00
)
const [DNSQueryResult, setDNSQueryResult] = createSignal<string[]>([])
return (
2023-09-02 14:02:25 +08:00
<div class="flex flex-col">
<form use:form={form} class="flex gap-2 sm:flex-row">
2023-09-10 16:42:00 +08:00
<input
type="search"
name="name"
class="input input-bordered min-w-0 flex-1"
2023-09-10 16:42:00 +08:00
/>
<div class="flex items-center gap-2">
<select name="type" class="select select-bordered">
<option>A</option>
<option>AAAA</option>
<option>MX</option>
</select>
2023-09-03 02:22:38 +08:00
<Button type="submit" class="btn-primary" loading={isSubmitting()}>
{t('dnsQuery')}
2023-09-03 02:22:38 +08:00
</Button>
</div>
</form>
<Show when={DNSQueryResult().length > 0}>
<div class="flex flex-col p-4">
<For each={DNSQueryResult()}>
{(item) => <div class="py-2">{item}</div>}
</For>
</div>
</Show>
</div>
)
}
const configFormSchema = z.object({
2023-08-28 22:13:48 +08:00
port: z.number(),
'socks-port': z.number(),
'redir-port': z.number(),
'tproxy-port': z.number(),
'mixed-port': z.number(),
})
const ConfigForm = () => {
const [t] = useI18n()
const navigate = useNavigate()
2023-09-01 14:06:21 +08:00
const portList = [
2023-08-28 22:13:48 +08:00
{
2023-09-15 23:43:55 +08:00
label: 'HTTP Port',
2023-08-28 22:13:48 +08:00
key: 'port',
onChange: (e: Event & { target: HTMLInputElement }) =>
void updateBackendConfigAPI('port', Number(e.target.value), refetch),
2023-08-28 22:13:48 +08:00
},
{
label: 'Socks Port',
2023-08-28 22:13:48 +08:00
key: 'socks-port',
onChange: (e: Event & { target: HTMLInputElement }) =>
void updateBackendConfigAPI(
'socks-port',
Number(e.target.value),
refetch,
),
2023-08-28 22:13:48 +08:00
},
{
label: 'Redir Port',
2023-08-28 22:13:48 +08:00
key: 'redir-port',
onChange: (e: Event & { target: HTMLInputElement }) =>
void updateBackendConfigAPI(
'redir-port',
Number(e.target.value),
refetch,
),
2023-08-28 22:13:48 +08:00
},
{
2023-09-15 23:43:55 +08:00
label: 'TProxy Port',
2023-08-28 22:13:48 +08:00
key: 'tproxy-port',
onChange: (e: Event & { target: HTMLInputElement }) =>
void updateBackendConfigAPI(
'tproxy-port',
Number(e.target.value),
refetch,
),
2023-08-28 22:13:48 +08:00
},
{
label: 'Mixed Port',
2023-08-28 22:13:48 +08:00
key: 'mixed-port',
onChange: (e: Event & { target: HTMLInputElement }) =>
void updateBackendConfigAPI(
'mixed-port',
Number(e.target.value),
refetch,
),
2023-08-28 22:13:48 +08:00
},
]
const { form, setInitialValues, reset } = createForm<
z.infer<typeof configFormSchema>
>({ extend: validator({ schema: configFormSchema }) })
2023-09-01 14:06:21 +08:00
const [configsData, { refetch }] = createResource(fetchBackendConfigAPI)
createEffect(() => {
const configs = configsData()
if (configs) {
setInitialValues(configs)
reset()
}
2023-08-28 22:13:48 +08:00
})
const onSwitchEndpointClick = () => {
setSelectedEndpoint('')
navigate(ROUTES.Setup)
}
2023-08-28 22:13:48 +08:00
return (
<div class="flex flex-col gap-4">
2023-09-12 20:50:52 +08:00
<select
class="select select-bordered"
value={configsData()?.mode}
onChange={(e) =>
void updateBackendConfigAPI('mode', e.target.value, refetch)
}
2023-09-12 20:50:52 +08:00
>
<option value={MODE_OPTIONS.Global}>{t('global')}</option>
<option value={MODE_OPTIONS.Rule}>{t('rule')}</option>
<option value={MODE_OPTIONS.Direct}>{t('direct')}</option>
</select>
<form class="grid grid-cols-3 gap-2 sm:grid-cols-5" use:form={form}>
<For each={portList}>
{(item) => (
<div class="form-control">
<label class="label">
<span class="label-text">{item.label}</span>
</label>
<input
name={item.key}
type="number"
class="input input-bordered"
placeholder={item.label}
onChange={item.onChange}
/>
</div>
)}
</For>
2023-09-01 14:06:21 +08:00
</form>
<div class="flex flex-wrap items-center justify-center gap-2">
<Button
class="btn-primary"
loading={reloadingConfigFile()}
onClick={reloadConfigFileAPI}
>
{t('reloadConfig')}
</Button>
2023-09-12 21:33:16 +08:00
<Button
class="btn-secondary"
2023-09-12 21:33:16 +08:00
loading={updatingGEODatabases()}
onClick={updateGEODatabasesAPI}
>
{t('updateGEODatabases')}
</Button>
<Button
class="btn-accent"
loading={flushingFakeIPData()}
onClick={flushFakeIPDataAPI}
>
{t('flushFakeIP')}
</Button>
</div>
<div class="flex flex-wrap items-center justify-center gap-2">
<Button
class="btn-error"
loading={upgradingBackend()}
onClick={upgradeBackendAPI}
>
{t('upgradeCore')}
</Button>
<Button
class="btn-warning"
loading={restartingBackend()}
onClick={restartBackendAPI}
>
{t('restartCore')}
</Button>
<Button class="btn-info" onClick={onSwitchEndpointClick}>
{t('switchEndpoint')}
</Button>
</div>
</div>
)
}
2023-09-01 14:06:21 +08:00
2023-09-02 15:08:35 +08:00
const ConfigForXd = () => {
const [t, { locale }] = useI18n()
2023-09-02 14:25:22 +08:00
2023-09-16 23:55:42 +08:00
return (
2023-09-17 00:05:15 +08:00
<div class="grid grid-cols-2 place-items-center gap-4">
<div class="col-span-2">
<Button
onClick={() => {
const curLocale = locale()
locale(curLocale === LANG.EN ? LANG.ZH : LANG.EN)
}}
>
{t('switchLanguage')}
</Button>
</div>
2023-09-16 23:55:42 +08:00
<div class="flex flex-col gap-4">
<div>
<ConfigTitle>{t('autoSwitchTheme')}</ConfigTitle>
<input
type="checkbox"
class="toggle"
checked={autoSwitchTheme()}
onChange={(e) => setAutoSwitchTheme(e.target.checked)}
/>
</div>
2023-09-16 23:55:42 +08:00
<Show when={autoSwitchTheme()}>
<div class="flex flex-col gap-4">
<div class="flex flex-col">
<ConfigTitle>{t('favDayTheme')}</ConfigTitle>
<select
class="select select-bordered"
onChange={(e) => setFavDayTheme(e.target.value)}
>
<For each={themes}>
{(theme) => (
<option selected={favDayTheme() === theme} value={theme}>
{theme}
</option>
)}
</For>
</select>
</div>
2023-09-16 23:55:42 +08:00
<div class="flex flex-col">
<ConfigTitle>{t('favNightTheme')}</ConfigTitle>
<select
class="select select-bordered"
onChange={(e) => setFavNightTheme(e.target.value)}
>
<For each={themes}>
{(theme) => (
<option selected={favNightTheme() === theme} value={theme}>
{theme}
</option>
)}
</For>
</select>
</div>
</div>
</Show>
2023-09-02 19:06:02 +08:00
</div>
2023-09-16 23:55:42 +08:00
<div>
<ConfigTitle>{t('useTwemoji')}</ConfigTitle>
2023-09-16 23:55:42 +08:00
<input
type="checkbox"
class="toggle"
checked={useTwemoji()}
onChange={(e) => setUseTwemoji(e.target.checked)}
/>
</div>
</div>
2023-09-02 15:08:35 +08:00
)
}
2023-09-10 16:42:00 +08:00
const Versions = () => {
const [backendVersion, setBackendVersion] = createSignal('')
onMount(async () => {
2023-09-12 21:33:16 +08:00
const version = await fetchBackendVersionAPI()
2023-09-10 16:42:00 +08:00
setBackendVersion(version)
})
return (
2023-09-17 00:05:15 +08:00
<div class="flex items-center justify-center gap-4">
2023-09-10 16:42:00 +08:00
<kbd class="kbd">{import.meta.env.version}</kbd>
<kbd class="kbd">{backendVersion()}</kbd>
</div>
)
}
2023-09-02 15:08:35 +08:00
export default () => {
2023-09-17 00:05:15 +08:00
const [t] = useI18n()
return (
2023-09-16 23:55:42 +08:00
<div class="mx-auto flex max-w-screen-md flex-col gap-4">
2023-09-17 00:05:15 +08:00
<ConfigTitle withDivider>{t('dnsQuery')}</ConfigTitle>
<DNSQueryForm />
2023-09-17 00:05:15 +08:00
<ConfigTitle withDivider>{t('coreConfig')}</ConfigTitle>
<ConfigForm />
2023-09-17 00:05:15 +08:00
<ConfigTitle withDivider>{t('xdConfig')}</ConfigTitle>
2023-09-02 15:08:35 +08:00
<ConfigForXd />
2023-09-03 18:16:09 +08:00
2023-09-17 00:05:15 +08:00
<ConfigTitle withDivider>{t('version')}</ConfigTitle>
2023-09-10 16:42:00 +08:00
<Versions />
2023-08-28 22:13:48 +08:00
</div>
)
2023-08-24 04:20:53 +08:00
}