feat(config): add setting, requestTimeoutDuration

This commit is contained in:
kunish 2023-09-05 22:32:47 +08:00
parent 2b46e5fbb2
commit d01d148cff
No known key found for this signature in database
GPG Key ID: 647A12B4F782C430
6 changed files with 61 additions and 26 deletions

View File

@ -40,7 +40,7 @@ export default {
auto: 'Auto', auto: 'Auto',
off: 'Off', off: 'Off',
proxiesPreviewType: 'Proxies preview type', proxiesPreviewType: 'Proxies preview type',
urlForLatencyTest: 'Url for latency test', urlForLatencyTest: 'URL for latency test',
autoCloseConns: 'Automatically close all connections', autoCloseConns: 'Automatically close all connections',
useTwemoji: 'Use Twemoji Mozilla Font', useTwemoji: 'Use Twemoji Mozilla Font',
autoSwitchTheme: 'Automatically switch theme', autoSwitchTheme: 'Automatically switch theme',
@ -66,4 +66,5 @@ export default {
lg: 'Large size', lg: 'Large size',
switchEndpoint: 'Switch Endpoint', switchEndpoint: 'Switch Endpoint',
switchLanguage: 'Switch Language', switchLanguage: 'Switch Language',
requestTimeoutDuration: 'Request Timeout Duration',
} }

View File

@ -66,4 +66,5 @@ export default {
lg: '超大尺寸', lg: '超大尺寸',
switchEndpoint: '切换后端', switchEndpoint: '切换后端',
switchLanguage: '切换语言', switchLanguage: '切换语言',
requestTimeoutDuration: '请求超时时间',
} }

View File

@ -2,7 +2,14 @@ import { createForm } from '@felte/solid'
import { validator } from '@felte/validator-zod' import { validator } from '@felte/validator-zod'
import { useI18n } from '@solid-primitives/i18n' import { useI18n } from '@solid-primitives/i18n'
import { useNavigate } from '@solidjs/router' import { useNavigate } from '@solidjs/router'
import { For, Show, createSignal, onMount } from 'solid-js' import {
For,
ParentComponent,
Show,
children,
createSignal,
onMount,
} from 'solid-js'
import { z } from 'zod' import { z } from 'zod'
import { Button } from '~/components' import { Button } from '~/components'
import { import {
@ -23,6 +30,7 @@ import {
proxiesPreviewType, proxiesPreviewType,
renderInTwoColumn, renderInTwoColumn,
renderProxiesInSamePage, renderProxiesInSamePage,
requestTimeoutDuration,
setAutoCloseConns, setAutoCloseConns,
setAutoSwitchTheme, setAutoSwitchTheme,
setFavDayTheme, setFavDayTheme,
@ -31,6 +39,7 @@ import {
setProxiesPreviewType, setProxiesPreviewType,
setRenderInTwoColumn, setRenderInTwoColumn,
setRenderProxiesInSamePage, setRenderProxiesInSamePage,
setRequestTimeoutDuration,
setSelectedEndpoint, setSelectedEndpoint,
setTableSize, setTableSize,
setTwemoji, setTwemoji,
@ -47,6 +56,12 @@ const dnsQueryFormSchema = z.object({
type: z.string(), type: z.string(),
}) })
const ConfigTitle: ParentComponent = (props) => (
<div class="pb-4 text-lg font-semibold">
{children(() => props.children)()}
</div>
)
const DNSQueryForm = () => { const DNSQueryForm = () => {
const [t] = useI18n() const [t] = useI18n()
const request = useRequest() const request = useRequest()
@ -73,6 +88,7 @@ const DNSQueryForm = () => {
<div class="flex flex-col"> <div class="flex flex-col">
<form use:form={form} class="flex flex-col gap-2 sm:flex-row"> <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" /> <input name="name" class="input input-bordered w-full sm:flex-1" />
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<select name="type" class="select select-bordered"> <select name="type" class="select select-bordered">
<option>A</option> <option>A</option>
@ -207,17 +223,30 @@ const ConfigForm = () => {
</div> </div>
<div class="flex flex-col"> <div class="flex flex-col">
<div class="pb-4 text-lg font-semibold">{t('urlForLatencyTest')}</div> <ConfigTitle>{t('urlForLatencyTest')}</ConfigTitle>
<input <input
class="w-100 input input-bordered max-w-md" class="input input-bordered max-w-md"
value={urlForLatencyTest()} value={urlForLatencyTest()}
onChange={(e) => setUrlForLatencyTest(e.target.value)} onChange={(e) => setUrlForLatencyTest(e.target.value)}
/> />
</div> </div>
<div> <div>
<div class="pb-4 text-lg font-semibold">{t('autoCloseConns')}</div> <ConfigTitle>
{t('requestTimeoutDuration')} ({t('ms')})
</ConfigTitle>
<input
type="number"
class="input input-bordered w-full max-w-md"
value={requestTimeoutDuration()}
onChange={(e) => setRequestTimeoutDuration(Number(e.target.value))}
/>
</div>
<div>
<ConfigTitle>{t('autoCloseConns')}</ConfigTitle>
<input <input
class="toggle" class="toggle"
@ -242,7 +271,8 @@ const ConfigForXd = () => {
return ( return (
<div class="grid gap-4"> <div class="grid gap-4">
<div class="flex flex-col"> <div class="flex flex-col">
<div class="pb-4 text-lg font-semibold">{t('renderInTwoColumns')}</div> <ConfigTitle>{t('renderInTwoColumns')}</ConfigTitle>
<input <input
type="checkbox" type="checkbox"
class="toggle" class="toggle"
@ -253,9 +283,8 @@ const ConfigForXd = () => {
/> />
</div> </div>
<div class="flex flex-col"> <div class="flex flex-col">
<div class="pb-4 text-lg font-semibold"> <ConfigTitle>{t('renderProxiesInSamePage')}</ConfigTitle>
{t('renderProxiesInSamePage')}
</div>
<input <input
type="checkbox" type="checkbox"
class="toggle" class="toggle"
@ -265,8 +294,10 @@ const ConfigForXd = () => {
}} }}
/> />
</div> </div>
<div class="flex flex-col"> <div class="flex flex-col">
<div class="pb-4 text-lg font-semibold">{t('autoSwitchTheme')}</div> <ConfigTitle>{t('autoSwitchTheme')}</ConfigTitle>
<input <input
type="checkbox" type="checkbox"
class="toggle" class="toggle"
@ -277,9 +308,11 @@ const ConfigForXd = () => {
}} }}
/> />
</div> </div>
<Show when={autoSwitchTheme()}> <Show when={autoSwitchTheme()}>
<div class="flex flex-col"> <div class="flex flex-col">
<div class="pb-4 text-lg font-semibold">{t('favDayTheme')}</div> <ConfigTitle>{t('favDayTheme')}</ConfigTitle>
<select <select
class="select select-bordered w-full max-w-xs" class="select select-bordered w-full max-w-xs"
onChange={(e) => { onChange={(e) => {
@ -297,7 +330,7 @@ const ConfigForXd = () => {
</select> </select>
</div> </div>
<div class="flex flex-col"> <div class="flex flex-col">
<div class="pb-4 text-lg font-semibold">{t('favNightTheme')}</div> <ConfigTitle>{t('favNightTheme')}</ConfigTitle>
<select <select
class="select select-bordered w-full max-w-xs" class="select select-bordered w-full max-w-xs"
@ -318,7 +351,7 @@ const ConfigForXd = () => {
</Show> </Show>
<div> <div>
<div class="pb-4 text-lg font-semibold">{t('useTwemoji')}</div> <ConfigTitle>{t('useTwemoji')}</ConfigTitle>
<input <input
class="toggle" class="toggle"
@ -329,7 +362,7 @@ const ConfigForXd = () => {
</div> </div>
<div> <div>
<div class="pb-4 text-lg font-semibold">{t('proxiesPreviewType')}</div> <ConfigTitle>{t('proxiesPreviewType')}</ConfigTitle>
<select <select
class="select select-bordered w-full max-w-xs" class="select select-bordered w-full max-w-xs"
@ -345,7 +378,7 @@ const ConfigForXd = () => {
</div> </div>
<div> <div>
<div class="pb-4 text-lg font-semibold">{t('proxiesSorting')}</div> <ConfigTitle>{t('proxiesSorting')}</ConfigTitle>
<select <select
class="select select-bordered w-full max-w-xs" class="select select-bordered w-full max-w-xs"
@ -365,7 +398,7 @@ const ConfigForXd = () => {
</div> </div>
<div> <div>
<div class="pb-4 text-lg font-semibold">{t('tableSize')}</div> <ConfigTitle>{t('tableSize')}</ConfigTitle>
<select <select
class="select select-bordered w-full max-w-xs" class="select select-bordered w-full max-w-xs"

View File

@ -54,6 +54,11 @@ export const [tableSize, setTableSize] = makePersisted(
createSignal<TAILWINDCSS_SIZE>(TAILWINDCSS_SIZE.XS), createSignal<TAILWINDCSS_SIZE>(TAILWINDCSS_SIZE.XS),
{ name: 'tableSize', storage: localStorage }, { name: 'tableSize', storage: localStorage },
) )
export const [requestTimeoutDuration, setRequestTimeoutDuration] =
makePersisted(createSignal(10000), {
name: 'requestTimeoutDuration',
storage: localStorage,
})
export const isLatencyTestByHttps = () => export const isLatencyTestByHttps = () =>
urlForLatencyTest().startsWith('https') urlForLatencyTest().startsWith('https')

View File

@ -91,10 +91,7 @@ export const useProxies = () => {
const latencyTestByProxyGroupName = async (proxyGroupName: string) => { const latencyTestByProxyGroupName = async (proxyGroupName: string) => {
const data: Record<string, number> = await request const data: Record<string, number> = await request
.get(`group/${proxyGroupName}/delay`, { .get(`group/${proxyGroupName}/delay`, {
searchParams: { searchParams: { url: urlForLatencyTest() },
url: urlForLatencyTest(),
timeout: 2000,
},
}) })
.json() .json()
@ -121,9 +118,7 @@ export const useProxies = () => {
} }
const healthCheckByProviderName = async (providerName: string) => { const healthCheckByProviderName = async (providerName: string) => {
await request.get(`providers/proxies/${providerName}/healthcheck`, { await request.get(`providers/proxies/${providerName}/healthcheck`)
timeout: 30 * 1000, // this api is a little bit slow sometimes...
})
await updateProxies() await updateProxies()
} }

View File

@ -1,6 +1,7 @@
import { makePersisted } from '@solid-primitives/storage' import { makePersisted } from '@solid-primitives/storage'
import ky from 'ky' import ky from 'ky'
import { createSignal } from 'solid-js' import { createSignal } from 'solid-js'
import { requestTimeoutDuration } from '~/signals/config'
export const [selectedEndpoint, setSelectedEndpoint] = makePersisted( export const [selectedEndpoint, setSelectedEndpoint] = makePersisted(
createSignal(''), createSignal(''),
@ -26,9 +27,8 @@ export const useRequest = () => {
return ky.create({ return ky.create({
prefixUrl: e?.url, prefixUrl: e?.url,
headers: { timeout: requestTimeoutDuration(),
Authorization: e?.secret ? `Bearer ${e.secret}` : '', headers: { Authorization: e?.secret ? `Bearer ${e.secret}` : '' },
},
}) })
} }