mirror of
https://github.com/itdoginfo/podkop.git
synced 2025-12-08 20:46:50 +03:00
refactor: change vless validation logic
This commit is contained in:
@@ -2,62 +2,68 @@ import { ValidationResult } from './types';
|
||||
|
||||
// TODO refactor current validation and add tests
|
||||
export function validateVlessUrl(url: string): ValidationResult {
|
||||
if (!url.startsWith('vless://')) {
|
||||
return {
|
||||
valid: false,
|
||||
message: 'Invalid VLESS URL: must start with vless://',
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const uuid = url.split('/')[2]?.split('@')[0];
|
||||
const parsedUrl = new URL(url);
|
||||
|
||||
if (!uuid) {
|
||||
if (!url || /\s/.test(url)) {
|
||||
return {
|
||||
valid: false,
|
||||
message: 'Invalid VLESS URL: must not contain spaces',
|
||||
};
|
||||
}
|
||||
|
||||
if (parsedUrl.protocol !== 'vless:') {
|
||||
return {
|
||||
valid: false,
|
||||
message: 'Invalid VLESS URL: must start with vless://',
|
||||
};
|
||||
}
|
||||
|
||||
if (!parsedUrl.username) {
|
||||
return { valid: false, message: 'Invalid VLESS URL: missing UUID' };
|
||||
}
|
||||
|
||||
const serverPart = url.split('@')[1];
|
||||
|
||||
if (!serverPart) {
|
||||
return {
|
||||
valid: false,
|
||||
message: 'Invalid VLESS URL: missing server address',
|
||||
};
|
||||
}
|
||||
|
||||
const [server, portAndRest] = serverPart.split(':');
|
||||
|
||||
if (!server) {
|
||||
if (!parsedUrl.hostname) {
|
||||
return { valid: false, message: 'Invalid VLESS URL: missing server' };
|
||||
}
|
||||
|
||||
const port = portAndRest ? portAndRest.split(/[/?#]/)[0] : null;
|
||||
|
||||
if (!port) {
|
||||
if (!parsedUrl.port) {
|
||||
return { valid: false, message: 'Invalid VLESS URL: missing port' };
|
||||
}
|
||||
|
||||
const portNum = parseInt(port, 10);
|
||||
|
||||
if (isNaN(portNum) || portNum < 1 || portNum > 65535) {
|
||||
if (
|
||||
isNaN(+parsedUrl.port) ||
|
||||
+parsedUrl.port < 1 ||
|
||||
+parsedUrl.port > 65535
|
||||
) {
|
||||
return {
|
||||
valid: false,
|
||||
message: 'Invalid port number. Must be between 1 and 65535',
|
||||
message:
|
||||
'Invalid VLESS URL: invalid port number. Must be between 1 and 65535',
|
||||
};
|
||||
}
|
||||
|
||||
const queryString = url.split('?')[1];
|
||||
|
||||
if (!queryString) {
|
||||
if (!parsedUrl.search) {
|
||||
return {
|
||||
valid: false,
|
||||
message: 'Invalid VLESS URL: missing query parameters',
|
||||
};
|
||||
}
|
||||
|
||||
const params = new URLSearchParams(queryString.split('#')[0]);
|
||||
const params = new URLSearchParams(parsedUrl.search);
|
||||
|
||||
const type = params.get('type');
|
||||
const validTypes = ['tcp', 'raw', 'udp', 'grpc', 'http', 'ws'];
|
||||
const validTypes = [
|
||||
'tcp',
|
||||
'raw',
|
||||
'udp',
|
||||
'grpc',
|
||||
'http',
|
||||
'httpupgrade',
|
||||
'xhttp',
|
||||
'ws',
|
||||
'kcp',
|
||||
];
|
||||
|
||||
if (!type || !validTypes.includes(type)) {
|
||||
return {
|
||||
@@ -94,9 +100,9 @@ export function validateVlessUrl(url: string): ValidationResult {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return { valid: true, message: 'Valid' };
|
||||
} catch (_e) {
|
||||
return { valid: false, message: 'Invalid VLESS URL: parsing failed' };
|
||||
}
|
||||
|
||||
return { valid: true, message: 'Valid' };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user