diff --git a/src/components/NodeModal.tsx b/src/components/NodeModal.tsx index 48e6e7fb..371ae7ac 100644 --- a/src/components/NodeModal.tsx +++ b/src/components/NodeModal.tsx @@ -5,11 +5,13 @@ import { z } from 'zod' import { DEFAULT_HTTP_FORM_VALUES, + DEFAULT_SOCKS5_FORM_VALUES, DEFAULT_SSR_FORM_VALUES, DEFAULT_SS_FORM_VALUES, DEFAULT_TROJAN_FORM_VALUES, DEFAULT_V2RAY_FORM_VALUES, httpSchema, + socks5Schema, ssSchema, ssrSchema, trojanSchema, @@ -557,8 +559,11 @@ const TrojanForm = () => { } const HTTPForm = () => { - const { onSubmit, getInputProps, reset } = useForm>({ - initialValues: DEFAULT_HTTP_FORM_VALUES, + const { onSubmit, getInputProps, reset } = useForm & { protocol: 'http' | 'https' }>({ + initialValues: { + protocol: 'http', + ...DEFAULT_HTTP_FORM_VALUES, + }, validate: zodResolver(httpSchema), }) @@ -607,16 +612,31 @@ const HTTPForm = () => { } const Socks5Form = () => { - const { onSubmit, getInputProps, reset } = useForm({ - initialValues: {}, + const { onSubmit, getInputProps, reset } = useForm>({ + initialValues: DEFAULT_SOCKS5_FORM_VALUES, + validate: zodResolver(socks5Schema), + }) + + const handleSubmit = onSubmit((values) => { + const generateURLParams: GenerateURLParams = { + protocol: 'socks5', + host: values.host, + port: values.port, + hash: values.name, + } + + if (values.username && values.password) { + Object.assign(generateURLParams, { + username: values.username, + password: values.password, + }) + } + + return generateURL(generateURLParams) }) return ( -
{ - console.log(values) - })} - > + diff --git a/src/constants/default.ts b/src/constants/default.ts index 18cba927..cf77728a 100644 --- a/src/constants/default.ts +++ b/src/constants/default.ts @@ -3,7 +3,7 @@ import { z } from 'zod' import { GlobalInput, Policy } from '~/schemas/gql/graphql' import { DialMode, LogLevel, TLSImplementation, TcpCheckHttpMethod, UTLSImitate } from './misc' -import { httpSchema, ssSchema, ssrSchema, trojanSchema, v2raySchema } from './schema' +import { httpSchema, socks5Schema, ssSchema, ssrSchema, trojanSchema, v2raySchema } from './schema' export const DEFAULT_ENDPOINT_URL = `${location.protocol}//${location.hostname}:2023/graphql` @@ -131,7 +131,14 @@ export const DEFAULT_TROJAN_FORM_VALUES: z.infer = { } export const DEFAULT_HTTP_FORM_VALUES: z.infer = { - protocol: 'http', + host: '', + name: '', + password: '', + port: 0, + username: '', +} + +export const DEFAULT_SOCKS5_FORM_VALUES: z.infer = { host: '', name: '', password: '', diff --git a/src/constants/schema.ts b/src/constants/schema.ts index 96b1d995..826e2c75 100644 --- a/src/constants/schema.ts +++ b/src/constants/schema.ts @@ -103,7 +103,6 @@ export const httpSchema = z.object({ password: z.string().default(''), host: z.string().nonempty(), port: z.number().min(0).max(65535), - protocol: z.enum(['http', 'https']).default('http'), name: z.string().default(''), }) @@ -112,6 +111,5 @@ export const socks5Schema = z.object({ password: z.string().default(''), host: z.string().nonempty(), port: z.number().min(0).max(65535), - protocol: z.literal('socks5'), name: z.string().default(''), })