metacubexd/src/pages/Config.tsx

184 lines
4.5 KiB
TypeScript
Raw Normal View History

import { createForm } from '@felte/solid'
import { validator } from '@felte/validator-zod'
import { useI18n } from '@solid-primitives/i18n'
import { makePersisted } from '@solid-primitives/storage'
2023-09-01 14:06:21 +08:00
import { For, Show, createSignal, onMount } from 'solid-js'
2023-08-28 22:13:48 +08:00
import { z } from 'zod'
import { PROXIES_PREVIEW_TYPE } from '~/config/enum'
import { useRequest } 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 DNSQueryForm = () => {
const [t] = useI18n()
const request = useRequest()
const { form } = 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) || []),
)
},
})
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 items-center gap-2">
<input name="name" class="input input-bordered flex-1" />
<select name="type" class="select select-bordered">
<option>A</option>
<option>AAAA</option>
<option>MX</option>
</select>
<button type="submit" class="btn btn-primary">
{t('dnsQuery')}
</button>
</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 = () => {
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()
})
return (
<div>
<form class="contents" use:form={form}>
<For each={portsList}>
{(item) => (
<div class="form-control w-64 max-w-xs">
<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>
)
}
2023-09-01 14:06:21 +08:00
export const [proxiesPreviewType, setProxiesPreviewType] = makePersisted(
createSignal(PROXIES_PREVIEW_TYPE.BAR),
{ name: 'proxiesPreviewType', storage: localStorage },
)
2023-09-02 15:08:35 +08:00
export const [urlForDelayTest, setUrlForDelayTest] = makePersisted(
createSignal('https://www.gstatic.com/generate_204'),
{ name: 'urlForDelayTest', storage: localStorage },
)
2023-09-02 15:08:35 +08:00
const ConfigForXd = () => {
2023-09-02 14:25:22 +08:00
const [t] = useI18n()
2023-09-02 15:08:35 +08:00
return (
<>
<div>{t('proxiesPreviewType')}</div>
<div class="flex">
<For each={Object.values(PROXIES_PREVIEW_TYPE)}>
{(value) => (
<label class="flex items-center">
{t(value)}
<input
class="radio m-4"
aria-label={value}
type="radio"
name="proxiesPreviewType"
checked={value === proxiesPreviewType()}
onChange={() => setProxiesPreviewType(value)}
/>
</label>
)}
</For>
</div>
<div>{t('urlForDelayTest')}</div>
<div>
<input
class="input input-bordered w-96"
value={urlForDelayTest()}
onChange={(e) => setUrlForDelayTest(e.target?.value!)}
/>
</div>
</>
)
}
export default () => {
return (
<div class="flex flex-col gap-4">
<DNSQueryForm />
<ConfigForm />
2023-09-02 15:08:35 +08:00
<ConfigForXd />
2023-08-28 22:13:48 +08:00
</div>
)
2023-08-24 04:20:53 +08:00
}