Files
Netcatty/domain/quickConnect.ts
T
LAPTOP-O016UC3M\Qi Chen 4e9e5da13c Localizes UI and improves dynamic translation support
Integrates all user-facing strings into the translation system and
expands locale files for comprehensive coverage, enabling dynamic
i18n across the interface. Replaces hardcoded labels, button texts,
and prompts with translation keys, benefiting maintainability and
future language support. Improves translation ergonomics in forms,
dialogs, wizards, and settings, ensuring consistent user experience
in both English and Chinese.
2025-12-17 14:25:05 +08:00

53 lines
1.4 KiB
TypeScript

export interface QuickConnectTarget {
hostname: string;
username?: string;
port?: number;
}
// Parse user@host:port format
export function parseQuickConnectInput(
input: string,
): QuickConnectTarget | null {
const trimmed = input.trim();
if (!trimmed) return null;
// Pattern: [user@]hostname[:port]
// Hostname can be IP (v4 or v6) or domain name
const regex = /^(?:([^@]+)@)?([^\s:]+|\[[^\]]+\])(?::(\d+))?$/;
const match = trimmed.match(regex);
if (!match) return null;
const [, username, hostname, portStr] = match;
// Validate hostname looks like an IP or domain
const ipv4Regex = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
const ipv6Regex = /^\[?[a-fA-F0-9:]+\]?$/;
const domainRegex =
/^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*$/;
if (
!ipv4Regex.test(hostname) &&
!ipv6Regex.test(hostname) &&
!domainRegex.test(hostname)
) {
return null;
}
const port = portStr ? parseInt(portStr, 10) : undefined;
if (port !== undefined && (isNaN(port) || port < 1 || port > 65535)) {
return null;
}
return {
hostname: hostname.replace(/^\[|\]$/g, ""), // Remove IPv6 brackets
username: username || undefined,
port,
};
}
// Check if input looks like a quick connect address
export function isQuickConnectInput(input: string): boolean {
return parseQuickConnectInput(input) !== null;
}