import { NativeSelect } from '@/components/form/NativeSelect';
import { useId } from 'react';
import { useTranslation } from 'react-i18next';
import {
Alert,
Button,
Form,
Input,
InputNumber,
Radio,
Select,
Space,
Switch,
Typography,
} from 'antd';
import {
CloudDownloadOutlined,
FileProtectOutlined,
MinusOutlined,
PlusOutlined,
} from '@ant-design/icons';
import { useFieldArray, useFormContext, useWatch } from 'react-hook-form';
import { CipherSuitesSelect, selectTagRender } from '@/components/form';
import DynamicEchHelper from '@/components/form/DynamicEchHelper';
import { FormField } from '@/components/form/rhf';
import {
ALPN_OPTION,
DOMAIN_STRATEGY_OPTION,
TLS_VERSION_OPTION,
USAGE_OPTION,
UTLS_FINGERPRINT,
} from '@/schemas/primitives';
import { SockoptStreamSettingsSchema } from '@/schemas/protocols/stream/sockopt';
const { TextArea } = Input;
const CERT_LINES_TRANSFORM = {
input: (v: unknown) => (Array.isArray(v) ? v.join('\n') : v),
output: (raw: unknown) => (typeof raw === 'string' ? raw.split('\n') : raw),
};
interface TlsFormProps {
saving: boolean;
setCertFromPanel: (certName: number) => void;
clearCertFiles: (certName: number) => void;
pinFromCert: () => void;
pinFromRemote: () => void;
getNewEchCert: () => void;
clearEchCert: () => void;
}
interface CertRowProps {
index: number;
total: number;
saving: boolean;
onRemove: () => void;
setCertFromPanel: (certName: number) => void;
clearCertFiles: (certName: number) => void;
}
function CertRow({
index,
total,
saving,
onRemove,
setCertFromPanel,
clearCertFiles,
}: CertRowProps) {
const { t } = useTranslation();
const { control } = useFormContext();
const useFile = useWatch({
control,
name: `streamSettings.tlsSettings.certificates.${index}.useFile`,
});
const usage = useWatch({
control,
name: `streamSettings.tlsSettings.certificates.${index}.usage`,
});
return (
{t('pages.inbounds.certificatePath')}
{t('pages.inbounds.certificateContent')}
{total > 1 && (
)}
{useFile ? (
<>
>
) : (
<>
>
)}
({ value: u, label: u }))}
/>
{usage === 'issue' && (
)}
);
}
function EchSockoptSection() {
const { t } = useTranslation();
const uid = useId();
const { control, setValue } = useFormContext();
const echSockopt = useWatch({ control, name: 'streamSettings.tlsSettings.echSockopt' });
const on = !!echSockopt;
return (
<>
setValue(
'streamSettings.tlsSettings.echSockopt',
v ? SockoptStreamSettingsSchema.parse({}) : undefined,
)
}
/>
{t('pages.inbounds.form.echSockoptTip')}
{on && (
<>
({ value: v, label: v }))}
/>
>
)}
>
);
}
interface EchKeySectionProps {
saving: boolean;
getNewEchCert: () => void;
clearEchCert: () => void;
}
function EchKeySection({ saving, getNewEchCert, clearEchCert }: EchKeySectionProps) {
const { t } = useTranslation();
const { control, setValue } = useFormContext();
const localKeys = useWatch({ control, name: 'streamSettings.tlsSettings.echServerKeys' });
const echConfig = useWatch({
control,
name: 'streamSettings.tlsSettings.settings.echConfigList',
});
const serverName: string =
useWatch({ control, name: 'streamSettings.tlsSettings.serverName' }) ?? '';
const helper = (
setValue('streamSettings.tlsSettings.settings.echConfigList', next, {
shouldDirty: true,
})
}
onClearLocalKeys={() => setValue('streamSettings.tlsSettings.echServerKeys', '')}
/>
);
return (
<>
{t('pages.inbounds.form.echConfigListHint')}
}
>
{helper}
>
);
}
export default function TlsForm({
saving,
setCertFromPanel,
clearCertFiles,
pinFromCert,
pinFromRemote,
getNewEchCert,
clearEchCert,
}: TlsFormProps) {
const { t } = useTranslation();
const uid = useId();
const { control } = useFormContext();
const { fields, append, remove } = useFieldArray({
control,
name: 'streamSettings.tlsSettings.certificates',
});
const cipherSuites = useWatch({ control, name: 'streamSettings.tlsSettings.cipherSuites' });
const fingerprint = useWatch({
control,
name: 'streamSettings.tlsSettings.settings.fingerprint',
});
// An empty fingerprint is fine: share links pin it to unsafe whenever cs is set.
const cipherSuitesIgnoredByClients =
Boolean(cipherSuites) && Boolean(fingerprint) && fingerprint !== UTLS_FINGERPRINT.UTLS_UNSAFE;
return (
<>
{t('pages.inbounds.form.cipherSuitesNeedUnsafe')}
) : undefined
}
>
({ value: v, label: v }))}
/>
({ value: v, label: v }))}
/>
({ value: fp, label: fp })),
]}
/>
{fields.map((field, idx) => (
remove(idx)}
setCertFromPanel={setCertFromPanel}
clearCertFiles={clearCertFiles}
/>
))}
}
onClick={pinFromCert}
loading={saving}
aria-label={t('pages.inbounds.form.pinFromCert')}
title={t('pages.inbounds.form.pinFromCert')}
/>
}
onClick={() => pinFromRemote()}
loading={saving}
aria-label={t('pages.inbounds.form.pinFromRemote')}
title={t('pages.inbounds.form.pinFromRemote')}
/>
{t('pages.inbounds.form.pinnedPeerCertSha256Tip')}
>
);
}