metacubexd/src/pages/Config.tsx

446 lines
11 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,
ParentComponent,
Show,
children,
createSignal,
onMount,
} from 'solid-js'
2023-08-28 22:13:48 +08:00
import { z } from 'zod'
import { Button } from '~/components'
import {
LANG,
PROXIES_ORDERING_TYPE,
PROXIES_PREVIEW_TYPE,
ROUTES,
TAILWINDCSS_SIZE,
themes,
} from '~/constants'
import {
2023-09-02 19:06:02 +08:00
applyThemeByMode,
autoCloseConns,
2023-09-02 19:06:02 +08:00
autoSwitchTheme,
favDayTheme,
favNightTheme,
latencyTestTimeoutDuration,
proxiesOrderingType,
proxiesPreviewType,
2023-09-02 21:00:20 +08:00
renderInTwoColumn,
renderProxiesInSamePage,
setAutoCloseConns,
2023-09-02 19:06:02 +08:00
setAutoSwitchTheme,
setFavDayTheme,
setFavNightTheme,
setLatencyTestTimeoutDuration,
setProxiesOrderingType,
setProxiesPreviewType,
2023-09-02 21:00:20 +08:00
setRenderInTwoColumn,
setRenderProxiesInSamePage,
setSelectedEndpoint,
setTableSize,
setTwemoji,
2023-09-03 05:40:39 +08:00
setUrlForLatencyTest,
tableSize,
2023-09-03 05:40:39 +08:00
urlForLatencyTest,
useRequest,
useTwemoji,
} from '~/signals'
2023-09-01 14:06:21 +08:00
import type { DNSQuery, Config as IConfig } from '~/types'
2023-08-28 22:13:48 +08:00
const dnsQueryFormSchema = z.object({
name: z.string(),
type: z.string(),
})
const ConfigTitle: ParentComponent = (props) => (
<div class="pb-4 text-lg font-semibold">
{children(() => props.children)()}
</div>
)
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 flex-col gap-2 sm:flex-row">
<input name="name" class="input input-bordered w-full sm:flex-1" />
<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()
2023-08-28 22:13:48 +08:00
const request = useRequest()
2023-09-01 14:06:21 +08:00
const portsList = [
2023-08-28 22:13:48 +08:00
{
label: 'Http Port',
2023-08-28 22:13:48 +08:00
key: 'port',
},
{
label: 'Socks Port',
2023-08-28 22:13:48 +08:00
key: 'socks-port',
},
{
label: 'Redir Port',
2023-08-28 22:13:48 +08:00
key: 'redir-port',
},
{
label: 'Tproxy Port',
2023-08-28 22:13:48 +08:00
key: 'tproxy-port',
},
{
label: 'Mixed Port',
2023-08-28 22:13:48 +08:00
key: 'mixed-port',
},
]
const { form, setInitialValues, reset } = createForm<
z.infer<typeof configFormSchema>
>({ extend: validator({ schema: configFormSchema }) })
2023-09-01 14:06:21 +08:00
2023-08-28 22:13:48 +08:00
onMount(async () => {
const configs = await request.get('configs').json<IConfig>()
setInitialValues(configs)
reset()
})
const [updatingGEODatabases, setUpdatingGEODatabases] = createSignal(false)
const [upgrading, setUpgrading] = createSignal(false)
const [restarting, setRestarting] = createSignal(false)
const onUpdateGEODatabases = async () => {
setUpdatingGEODatabases(true)
try {
await request.post('configs/geo')
} catch {}
setUpdatingGEODatabases(false)
}
const onUpgrade = async () => {
setUpgrading(true)
try {
await request.post('upgrade')
} catch {}
setUpgrading(false)
}
const onRestart = async () => {
setRestarting(true)
try {
await request.post('restart')
} catch {}
setRestarting(false)
}
2023-08-28 22:13:48 +08:00
return (
<div class="flex flex-col gap-4">
<form class="contents" use:form={form}>
<For each={portsList}>
{(item) => (
<div class="form-control w-64 max-w-sm">
<label class="label">
<span class="label-text">{item.label}</span>
</label>
<input
name={item.key}
type="number"
class="input input-bordered"
placeholder={item.label}
/>
</div>
)}
</For>
2023-09-01 14:06:21 +08:00
</form>
<div class="flex flex-wrap items-center gap-2">
<Button loading={updatingGEODatabases()} onClick={onUpdateGEODatabases}>
{t('updateGEODatabases')}
</Button>
<Button loading={upgrading()} onClick={onUpgrade}>
{t('upgradeCore')}
</Button>
<Button loading={restarting()} onClick={onRestart}>
{t('restartCore')}
</Button>
</div>
<div class="flex flex-col">
<ConfigTitle>{t('urlForLatencyTest')}</ConfigTitle>
<input
class="input input-bordered max-w-md"
value={urlForLatencyTest()}
onChange={(e) => setUrlForLatencyTest(e.target.value)}
/>
</div>
<div>
<ConfigTitle>
2023-09-06 11:17:00 +08:00
{t('speedtestTimeoutDuration')} ({t('ms')})
</ConfigTitle>
<input
type="number"
class="input input-bordered w-full max-w-md"
value={latencyTestTimeoutDuration()}
onChange={(e) =>
setLatencyTestTimeoutDuration(Number(e.target.value))
}
/>
</div>
<div>
<ConfigTitle>{t('autoCloseConns')}</ConfigTitle>
<input
class="toggle"
type="checkbox"
checked={autoCloseConns()}
onChange={(e) => setAutoCloseConns(e.target.checked)}
/>
</div>
</div>
)
}
2023-09-01 14:06:21 +08:00
2023-09-02 15:08:35 +08:00
const ConfigForXd = () => {
const [t, { locale }] = useI18n()
const navigate = useNavigate()
const onSwitchEndpointClick = () => {
setSelectedEndpoint('')
navigate(ROUTES.Setup)
}
2023-09-02 14:25:22 +08:00
2023-09-02 15:08:35 +08:00
return (
2023-09-02 19:06:02 +08:00
<div class="grid gap-4">
2023-09-02 21:00:20 +08:00
<div class="flex flex-col">
<ConfigTitle>{t('renderInTwoColumns')}</ConfigTitle>
2023-09-02 21:00:20 +08:00
<input
type="checkbox"
class="toggle"
checked={renderInTwoColumn()}
onChange={(e) => {
setRenderInTwoColumn(e.target.checked)
}}
/>
</div>
<div class="flex flex-col">
<ConfigTitle>{t('renderProxiesInSamePage')}</ConfigTitle>
<input
type="checkbox"
class="toggle"
checked={renderProxiesInSamePage()}
onChange={(e) => {
setRenderProxiesInSamePage(e.target.checked)
}}
/>
</div>
2023-09-02 19:06:02 +08:00
<div class="flex flex-col">
<ConfigTitle>{t('autoSwitchTheme')}</ConfigTitle>
2023-09-02 19:06:02 +08:00
<input
type="checkbox"
class="toggle"
checked={autoSwitchTheme()}
onChange={(e) => {
setAutoSwitchTheme(e.target.checked)
applyThemeByMode()
}}
/>
</div>
2023-09-02 19:06:02 +08:00
<Show when={autoSwitchTheme()}>
<div class="flex flex-col">
<ConfigTitle>{t('favDayTheme')}</ConfigTitle>
2023-09-02 19:06:02 +08:00
<select
class="select select-bordered w-full max-w-xs"
onChange={(e) => {
setFavDayTheme(e.target.value)
applyThemeByMode()
}}
>
<For each={themes}>
{(theme) => (
<option selected={favDayTheme() === theme} value={theme}>
{theme}
</option>
)}
</For>
</select>
</div>
<div class="flex flex-col">
<ConfigTitle>{t('favNightTheme')}</ConfigTitle>
2023-09-02 19:06:02 +08:00
<select
class="select select-bordered w-full max-w-xs"
onChange={(e) => {
setFavNightTheme(e.target.value)
applyThemeByMode()
}}
>
<For each={themes}>
{(theme) => (
<option selected={favNightTheme() === theme} value={theme}>
{theme}
</option>
)}
</For>
</select>
</div>
</Show>
<div>
<ConfigTitle>{t('useTwemoji')}</ConfigTitle>
<input
class="toggle"
type="checkbox"
checked={useTwemoji()}
onChange={(e) => setTwemoji(e.target.checked)}
/>
</div>
<div>
<ConfigTitle>{t('proxiesPreviewType')}</ConfigTitle>
<select
class="select select-bordered w-full max-w-xs"
value={proxiesPreviewType()}
onChange={(e) =>
setProxiesPreviewType(e.target.value as PROXIES_PREVIEW_TYPE)
}
>
<For each={Object.values(PROXIES_PREVIEW_TYPE)}>
{(value) => <option value={value}>{t(value)}</option>}
</For>
</select>
2023-09-02 15:08:35 +08:00
</div>
<div>
<ConfigTitle>{t('proxiesSorting')}</ConfigTitle>
<select
class="select select-bordered w-full max-w-xs"
value={proxiesOrderingType()}
onChange={(e) =>
setProxiesOrderingType(e.target.value as PROXIES_ORDERING_TYPE)
}
>
<For each={Object.values(PROXIES_ORDERING_TYPE)}>
{(value) => (
<option class="flex items-center gap-2" value={value}>
{t(value)}
</option>
)}
</For>
</select>
</div>
2023-09-02 15:08:35 +08:00
<div>
<ConfigTitle>{t('tableSize')}</ConfigTitle>
<select
class="select select-bordered w-full max-w-xs"
value={tableSize()}
onChange={(e) => setTableSize(e.target.value as TAILWINDCSS_SIZE)}
>
<For each={Object.values(TAILWINDCSS_SIZE)}>
{(value) => <option value={value}>{t(value)}</option>}
</For>
</select>
</div>
<div>
<Button
onClick={() => {
const curLocale = locale()
locale(curLocale === LANG.EN ? LANG.ZH : LANG.EN)
}}
>
{t('switchLanguage')}
</Button>
</div>
<div>
<Button onClick={onSwitchEndpointClick}>{t('switchEndpoint')}</Button>
2023-09-02 15:08:35 +08:00
</div>
</div>
2023-09-02 15:08:35 +08:00
)
}
export default () => {
return (
<div class="flex flex-col gap-4">
<DNSQueryForm />
<ConfigForm />
2023-09-02 15:08:35 +08:00
<ConfigForXd />
2023-09-03 18:16:09 +08:00
<kbd class="kbd">{import.meta.env.version}</kbd>
2023-08-28 22:13:48 +08:00
</div>
)
2023-08-24 04:20:53 +08:00
}