diff --git a/.eslintrc.yml b/.eslintrc.yml index 340a7254..05b23a82 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -43,6 +43,9 @@ rules: - blankLine: always prev: '*' next: switch + - blankLine: always + prev: switch + next: '*' react/prop-types: off react/display-name: off react/self-closing-comp: error diff --git a/src/components/NodeModal.tsx b/src/components/NodeModal.tsx index 569158a3..f699ea5d 100644 --- a/src/components/NodeModal.tsx +++ b/src/components/NodeModal.tsx @@ -6,9 +6,11 @@ import { z } from 'zod' import { DEFAULT_SSR_FORM_VALUES, DEFAULT_SS_FORM_VALUES, + DEFAULT_TROJAN_FORM_VALUES, DEFAULT_V2RAY_FORM_VALUES, ssSchema, ssrSchema, + trojanSchema, v2raySchema, } from '~/constants' import { generateURL } from '~/utils/node' @@ -446,19 +448,50 @@ const SSRForm = () => { } const TrojanForm = () => { - const { values, onSubmit, getInputProps, reset } = useForm({ - initialValues: { - method: 'origin', - obfs: 'none', - }, + const { values, onSubmit, getInputProps, reset } = useForm>({ + initialValues: DEFAULT_TROJAN_FORM_VALUES, + validate: zodResolver(trojanSchema), + }) + + const handleSubmit = onSubmit((values) => { + const query: Record = { + allowInsecure: values.allowInsecure, + } + + if (values.peer !== '') { + query.sni = values.peer + } + + let protocol = 'trojan' + + if (values.method !== 'origin' || values.obfs !== 'none') { + protocol = 'trojan-go' + query.type = values.obfs === 'none' ? 'original' : 'ws' + + if (values.method === 'shadowsocks') { + query.encryption = `ss;${values.ssCipher};${values.ssPassword}` + } + + if (query.type === 'ws') { + query.host = values.host || '' + query.path = values.path || '/' + } + + delete query.allowInsecure + } + + return generateURL({ + protocol: protocol, + username: values.password, + host: values.server, + port: values.port, + hash: values.name, + params: query, + }) }) return ( -
{ - console.log(values) - })} - > + diff --git a/src/constants/default.ts b/src/constants/default.ts index 4533d38e..79da9af5 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 { ssSchema, ssrSchema, v2raySchema } from './schema' +import { ssSchema, ssrSchema, trojanSchema, v2raySchema } from './schema' export const DEFAULT_ENDPOINT_URL = `${location.protocol}//${location.hostname}:2023/graphql` @@ -114,3 +114,18 @@ export const DEFAULT_SSR_FORM_VALUES: z.infer = { protoParam: '', server: '', } + +export const DEFAULT_TROJAN_FORM_VALUES: z.infer = { + method: 'origin', + obfs: 'none', + allowInsecure: false, + host: '', + name: '', + password: '', + path: '', + peer: '', + port: 0, + server: '', + ssCipher: 'aes-128-gcm', + ssPassword: '', +}