Skip to content

Commit

Permalink
feat: finish http protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
kunish committed Jun 29, 2023
1 parent b41c36b commit 1920b3d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
35 changes: 25 additions & 10 deletions src/components/NodeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import { Base64 } from 'js-base64'
import { z } from 'zod'

import {
DEFAULT_HTTP_FORM_VALUES,
DEFAULT_SSR_FORM_VALUES,
DEFAULT_SS_FORM_VALUES,
DEFAULT_TROJAN_FORM_VALUES,
DEFAULT_V2RAY_FORM_VALUES,
httpSchema,
ssSchema,
ssrSchema,
trojanSchema,
v2raySchema,
} from '~/constants'
import { generateURL } from '~/utils/node'
import { GenerateURLParams, generateURL } from '~/utils/node'

import { FormActions } from './FormActions'

Expand Down Expand Up @@ -555,18 +557,31 @@ const TrojanForm = () => {
}

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

const handleSubmit = onSubmit((values) => {
const generateURLParams: GenerateURLParams = {
protocol: `${values.protocol}-proxy`,
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}>
<Select
label="Protocol"
data={[
Expand Down
11 changes: 10 additions & 1 deletion 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 { ssSchema, ssrSchema, trojanSchema, v2raySchema } from './schema'
import { httpSchema, ssSchema, ssrSchema, trojanSchema, v2raySchema } from './schema'

export const DEFAULT_ENDPOINT_URL = `${location.protocol}//${location.hostname}:2023/graphql`

Expand Down Expand Up @@ -129,3 +129,12 @@ export const DEFAULT_TROJAN_FORM_VALUES: z.infer<typeof trojanSchema> = {
ssCipher: 'aes-128-gcm',
ssPassword: '',
}

export const DEFAULT_HTTP_FORM_VALUES: z.infer<typeof httpSchema> = {
protocol: 'http',
host: '',
name: '',
password: '',
port: 0,
username: '',
}
1 change: 1 addition & 0 deletions src/constants/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const EDITOR_LANGUAGE_ROUTINGA: languages.IMonarchLanguage = {
'dport',
'fallback',
'geoip',
'must_rules',
'geosite',
'ipversion',
'l4proto',
Expand Down
19 changes: 6 additions & 13 deletions src/utils/node.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import URI from 'urijs'

export const generateURL = ({
username,
password,
protocol,
host,
port,
params,
hash,
path,
}: {
username: string
export type GenerateURLParams = {
username?: string
password?: string
protocol: string
host: string
port: number
params: Record<string, unknown>
params?: Record<string, unknown>
hash: string
path?: string
}) => {
}

export const generateURL = ({ username, password, protocol, host, port, params, hash, path }: GenerateURLParams) => {
/**
* 所有参数设置默认值
* 避免方法检测到参数为null/undefine返回该值查询结果
Expand Down

0 comments on commit 1920b3d

Please sign in to comment.