Skip to content

Commit

Permalink
feat: finish trojan protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
kunish committed Jun 29, 2023
1 parent 73a4d49 commit b41c36b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 43 additions & 10 deletions src/components/NodeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<z.infer<typeof trojanSchema>>({
initialValues: DEFAULT_TROJAN_FORM_VALUES,
validate: zodResolver(trojanSchema),
})

const handleSubmit = onSubmit((values) => {
const query: Record<string, unknown> = {
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 (
<form
onSubmit={onSubmit((values) => {
console.log(values)
})}
>
<form onSubmit={handleSubmit}>
<TextInput label="Name" {...getInputProps('name')} />

<TextInput label="Host" withAsterisk {...getInputProps('server')} />
Expand Down
17 changes: 16 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, v2raySchema } from './schema'
import { ssSchema, ssrSchema, trojanSchema, v2raySchema } from './schema'

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

Expand Down Expand Up @@ -114,3 +114,18 @@ export const DEFAULT_SSR_FORM_VALUES: z.infer<typeof ssrSchema> = {
protoParam: '',
server: '',
}

export const DEFAULT_TROJAN_FORM_VALUES: z.infer<typeof trojanSchema> = {
method: 'origin',
obfs: 'none',
allowInsecure: false,
host: '',
name: '',
password: '',
path: '',
peer: '',
port: 0,
server: '',
ssCipher: 'aes-128-gcm',
ssPassword: '',
}

0 comments on commit b41c36b

Please sign in to comment.