Skip to content

Commit

Permalink
feat: finish socks5 protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
kunish committed Jun 29, 2023
1 parent 1920b3d commit cd4c3d4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
38 changes: 29 additions & 9 deletions src/components/NodeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -557,8 +559,11 @@ const TrojanForm = () => {
}

const HTTPForm = () => {
const { onSubmit, getInputProps, reset } = useForm<z.infer<typeof httpSchema>>({
initialValues: DEFAULT_HTTP_FORM_VALUES,
const { onSubmit, getInputProps, reset } = useForm<z.infer<typeof httpSchema> & { protocol: 'http' | 'https' }>({
initialValues: {
protocol: 'http',
...DEFAULT_HTTP_FORM_VALUES,
},
validate: zodResolver(httpSchema),
})

Expand Down Expand Up @@ -607,16 +612,31 @@ const HTTPForm = () => {
}

const Socks5Form = () => {
const { onSubmit, getInputProps, reset } = useForm({
initialValues: {},
const { onSubmit, getInputProps, reset } = useForm<z.infer<typeof socks5Schema>>({
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 (
<form
onSubmit={onSubmit((values) => {
console.log(values)
})}
>
<form onSubmit={handleSubmit}>
<TextInput label="Name" {...getInputProps('name')} />

<TextInput label="Host" withAsterisk {...getInputProps('host')} />
Expand Down
11 changes: 9 additions & 2 deletions src/constants/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -131,7 +131,14 @@ export const DEFAULT_TROJAN_FORM_VALUES: z.infer<typeof trojanSchema> = {
}

export const DEFAULT_HTTP_FORM_VALUES: z.infer<typeof httpSchema> = {
protocol: 'http',
host: '',
name: '',
password: '',
port: 0,
username: '',
}

export const DEFAULT_SOCKS5_FORM_VALUES: z.infer<typeof socks5Schema> = {
host: '',
name: '',
password: '',
Expand Down
2 changes: 0 additions & 2 deletions src/constants/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(''),
})

Expand All @@ -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(''),
})

0 comments on commit cd4c3d4

Please sign in to comment.