VmessURL constructor
VmessURL({
- required String url,
Creates a VmessURL by parsing the provided vmess share link string.
Throws ArgumentError if the url does not start with vmess:// or
cannot be decoded into a valid JSON config payload.
Implementation
VmessURL({required super.url}) {
if (!url.startsWith('vmess://')) {
throw ArgumentError('url is invalid');
}
var raw = url.substring(8);
if (raw.length % 4 > 0) {
raw += '=' * (4 - raw.length % 4);
}
try {
rawConfig = jsonDecode(utf8.decode(base64Decode(raw)));
} catch (_) {
throw ArgumentError('url is invalid');
}
final sni = super.populateTransportSettings(
transport: rawConfig['net'],
headerType: rawConfig['type'],
host: rawConfig['host'],
path: rawConfig['path'],
seed: rawConfig['path'],
quicSecurity: rawConfig['host'],
key: rawConfig['path'],
mode: rawConfig['type'],
serviceName: rawConfig['path'],
);
final String? fingerprint =
(rawConfig['fp'] != null && rawConfig['fp'] != '')
? rawConfig['fp']
: streamSetting['tlsSettings']?['fingerprint'];
super.populateTlsSettings(
streamSecurity: rawConfig['tls'],
sni: rawConfig['sni']?.toString().isNotEmpty == true
? rawConfig['sni'].toString()
: sni,
fingerprint: fingerprint,
alpns: rawConfig['alpn'],
publicKey: null,
shortId: null,
spiderX: null,
pinnedPeerCertSha256: (rawConfig['pcs'] ??
rawConfig['pinSHA256'] ??
rawConfig['pinnedPeerCertSha256'])
?.toString(),
verifyPeerCertByName:
(rawConfig['vcn'] ?? rawConfig['verifyPeerCertByName'])?.toString(),
);
}