mirror of
https://github.com/itdoginfo/podkop.git
synced 2026-07-22 15:23:27 +03:00
fix: correct link validation & some points on dash
This commit is contained in:
@@ -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') };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user