fix: correct link validation & some points on dash

This commit is contained in:
divocat
2025-10-09 18:17:43 +03:00
parent 9d89258c0c
commit 9bc2b5ffef
9 changed files with 422 additions and 154 deletions

View File

@@ -1,6 +1,5 @@
import { ValidationResult } from './types';
// TODO refactor current validation and add tests
export function validateTrojanUrl(url: string): ValidationResult {
try {
if (!url.startsWith('trojan://')) {
@@ -17,17 +16,42 @@ export function validateTrojanUrl(url: string): ValidationResult {
};
}
const refinedURL = url.replace('trojan://', 'https://');
const parsedUrl = new URL(refinedURL);
const body = url.slice('trojan://'.length);
const [mainPart] = body.split('#');
const [userHostPort] = mainPart.split('?');
if (!parsedUrl.username || !parsedUrl.hostname || !parsedUrl.port) {
const [userPart, hostPortPart] = userHostPort.split('@');
if (!userHostPort)
return {
valid: false,
message: _(
'Invalid Trojan URL: must contain username, hostname and port',
),
message: 'Invalid Trojan URL: missing credentials and host',
};
if (!userPart)
return { valid: false, message: 'Invalid Trojan URL: missing password' };
if (!hostPortPart)
return {
valid: false,
message: 'Invalid Trojan URL: missing hostname and port',
};
const [host, port] = hostPortPart.split(':');
if (!host)
return { valid: false, message: 'Invalid Trojan URL: missing hostname' };
if (!port)
return { valid: false, message: 'Invalid Trojan URL: missing port' };
const portNum = Number(port);
if (!Number.isInteger(portNum) || portNum < 1 || portNum > 65535)
return {
valid: false,
message: 'Invalid Trojan URL: invalid port number',
};
}
} catch (_e) {
return { valid: false, message: _('Invalid Trojan URL: parsing failed') };
}