import 'dart:convert'; import 'dart:io' show ZLibCodec, gzip, zlib; import '../models/runtime_config.dart'; /// Разбор ссылок и подписок всех протоколов и форматов, поддерживаемых `tachyon-core`: /// - URI: `vless://`, `vmess://` (URI и V2RayN Base64 JSON), `trojan://`, /// `ss://` / `shadowsocks://` (SIP002, Legacy Base64, SS-2022, плагины, UoT), /// `hy2://` / `hysteria2://`, `hysteria://`, `tuic://`, /// `wg://` / `wireguard://` / `amneziawg://` / `awg://` (WireGuard + AmneziaWG), /// `anytls://`, `naive+https://` / `naive+quic://` / `naive://`, /// `snell://`, `shadowtls://`, `fptn:` / `fptnb:`, /// `socks://` / `socks5://` / `socks5h://` / `socks4://`, `http://` / `https://` (прокси), /// `ssh://`, `mtproto://` / `tg://proxy`, `mieru://`, `masque://`, `trusttunnel://`, `openconnect://`. /// - Подписки и конфиги: /// - Списки URI (сырые и в Base64 / URL-safe Base64) /// - Sing-box JSON (`outbounds` или массив / одиночный аутбаунд) /// - Xray / V2Ray JSON (`outbounds` с `protocol`, `settings`, `streamSettings`) /// - SIP008 Shadowsocks JSON (`servers`) /// - Clash / Mihomo YAML (`proxies:`) /// - WireGuard / AmneziaWG `.conf` INI (`[Interface]` + `[Peer]`) class LinkParser { const LinkParser(); /// Разбирает список ссылок или многострочный конфиг. List parseMany(String content, {String subscriptionId = ''}) { final trimmed = content.trim().replaceFirst('\uFEFF', ''); if (trimmed.isEmpty) return const []; // 1. Токены FPTN (fptn: / fptnb: или JSON токена FPTN) if (isFptnPayload(trimmed)) { final fptnProfiles = _parseFptnToken(trimmed, subscriptionId: subscriptionId); if (fptnProfiles.isNotEmpty) return fptnProfiles; } // 2. JSON: sing-box, Xray/V2Ray, SIP008 if (trimmed.startsWith('{') || trimmed.startsWith('[')) { final jsonProfiles = _parseJsonPayload(trimmed, subscriptionId: subscriptionId); if (jsonProfiles.isNotEmpty) return jsonProfiles; } // 3. WireGuard / AmneziaWG INI-конфиг ([Interface] ... [Peer]) if (looksLikeWireGuardConf(trimmed)) { final wgProfiles = parseWireGuardConfAll(trimmed, subscriptionId: subscriptionId); if (wgProfiles.isNotEmpty) return wgProfiles; } // 3b. OpenVPN статический ключ (.ovpn) if (looksLikeOpenVpnConf(trimmed)) { final ovpnProfile = parseOpenVpnConf(trimmed, subscriptionId: subscriptionId); if (ovpnProfile != null) return [ovpnProfile]; } // 4. Clash / Mihomo YAML (секция proxies:) if (RegExp(r'(^|\n)\s*proxies\s*:', multiLine: true).hasMatch(trimmed)) { final clashProfiles = _parseClashYaml(trimmed, subscriptionId: subscriptionId); if (clashProfiles.isNotEmpty) return clashProfiles; } // 5. Построчный разбор URI, Surge/Quantumult X и одиночных JSON-строк final profiles = []; for (final raw in trimmed.split(RegExp(r'[\r\n]+'))) { final line = raw.trim(); if (line.isEmpty || line.startsWith('#') || line.startsWith('//') || line.startsWith(';') || line.startsWith('[')) { continue; } if (line.toLowerCase().startsWith('vpn://')) { profiles.addAll(_parseAmneziaVpnContainer(line, subscriptionId: subscriptionId)); continue; } if (_isFptnToken(line)) { profiles.addAll(_parseFptnToken(line, subscriptionId: subscriptionId)); continue; } final profile = parse(line, subscriptionId: subscriptionId); if (profile != null) profiles.add(profile); } return profiles; } /// Автоопределение и разбор любого содержимого подписки (URI, Base64, JSON, Clash YAML, WG .conf, FPTN). List parseSubscription(String content, {String subscriptionId = ''}) { final trimmed = content.trim().replaceFirst('\uFEFF', ''); if (trimmed.isEmpty) return const []; // Прямой разбор (JSON, YAML, WG .conf, FPTN, либо список URI) final direct = parseMany(trimmed, subscriptionId: subscriptionId); if (direct.isNotEmpty) return direct; // Попытка декодировать Base64 (внутри может быть список URI, JSON, Clash YAML или WG .conf) final normalized = _normalizeBase64(trimmed); if (normalized.isNotEmpty) { try { final decoded = utf8.decode(base64.decode(normalized), allowMalformed: true); final parsed = parseMany(decoded, subscriptionId: subscriptionId); if (parsed.isNotEmpty) return parsed; } catch (_) {} try { final decoded = latin1.decode(base64.decode(normalized)); final parsed = parseMany(decoded, subscriptionId: subscriptionId); if (parsed.isNotEmpty) return parsed; } catch (_) {} } return const []; } /// Конвертирует узлы `nodes` и `primary_tags`, разобранные нативным движком `tachyon-core`, /// в список `Profile` с сохранением транзитных цепочек `detour` (`chain_outbounds`). List parseEngineNodes( List rawNodes, List rawPrimaryTags, { String subscriptionId = '', }) { final nodes = >[]; final byTag = >{}; for (final item in rawNodes) { if (item is! Map) continue; final map = Map.from(item); nodes.add(map); final tag = (map['tag'] ?? '').toString().trim(); if (tag.isNotEmpty) { byTag[tag] = map; } } if (nodes.isEmpty) return const []; final primarySet = rawPrimaryTags .map((e) => e.toString().trim()) .where((s) => s.isNotEmpty) .toSet(); final primaryNodes = primarySet.isNotEmpty ? nodes.where((n) => primarySet.contains((n['tag'] ?? '').toString().trim())).toList() : nodes; final out = []; for (final rawNode in primaryNodes) { final mainNode = Map.from(rawNode); final firstHopTag = (mainNode['detour'] ?? '').toString().trim(); if (firstHopTag.isNotEmpty && byTag.containsKey(firstHopTag)) { final chainOutbounds = >[]; final visited = {}; var currTag = firstHopTag; while (currTag.isNotEmpty && byTag.containsKey(currTag) && visited.add(currTag)) { final hop = Map.from(byTag[currTag]!); chainOutbounds.add(hop); final nextTag = (hop['detour'] ?? '').toString().trim(); if (nextTag.isNotEmpty && byTag.containsKey(nextTag)) { currTag = nextTag; } else { hop.remove('detour'); currTag = ''; } } if (chainOutbounds.isNotEmpty) { mainNode['chain_outbounds'] = chainOutbounds; } } final profile = _outboundMapToProfile(mainNode, subscriptionId: subscriptionId); if (profile != null) { out.add(profile); } } return out; } /// Разбирает одну ссылку или одиночный JSON-объект. Profile? parse(String link, {String subscriptionId = ''}) { final value = link.trim().replaceFirst('\uFEFF', ''); if (value.isEmpty) return null; if (isFptnPayload(value)) { final list = _parseFptnToken(value, subscriptionId: subscriptionId); return list.isNotEmpty ? list.first : null; } if (looksLikeWireGuardConf(value)) { return _parseWireGuardConf(value, subscriptionId: subscriptionId); } if (looksLikeOpenVpnConf(value)) { return parseOpenVpnConf(value, subscriptionId: subscriptionId); } final lower = value.toLowerCase(); if (lower.startsWith('vless://')) return _parseVless(value, subscriptionId: subscriptionId); if (lower.startsWith('vmess://')) return _parseVmess(value, subscriptionId: subscriptionId); if (lower.startsWith('trojan://')) return _parseTrojan(value, subscriptionId: subscriptionId); if (lower.startsWith('ss://') || lower.startsWith('shadowsocks://')) { return _parseShadowsocks(value, subscriptionId: subscriptionId); } if (lower.startsWith('hysteria2://') || lower.startsWith('hy2://')) { return _parseHysteria2(value, subscriptionId: subscriptionId); } if (lower.startsWith('hysteria://')) return _parseHysteria1(value, subscriptionId: subscriptionId); if (lower.startsWith('tuic://')) return _parseTuic(value, subscriptionId: subscriptionId); if (lower.startsWith('wg://') || lower.startsWith('wireguard://') || lower.startsWith('amneziawg://') || lower.startsWith('awg://')) { return _parseWireGuardUri(value, subscriptionId: subscriptionId); } if (lower.startsWith('anytls://')) return _parseAnytls(value, subscriptionId: subscriptionId); if (lower.startsWith('naive+https://') || lower.startsWith('naive+http2://') || lower.startsWith('naive+h2://') || lower.startsWith('naive+quic://') || lower.startsWith('naive+h3://') || lower.startsWith('naive://')) { return _parseNaive(value, subscriptionId: subscriptionId); } if (lower.startsWith('snell://')) return _parseSnell(value, subscriptionId: subscriptionId); if (lower.startsWith('shadowtls://')) return _parseShadowTls(value, subscriptionId: subscriptionId); if (lower.startsWith('socks5://') || lower.startsWith('socks5h://') || lower.startsWith('socks4://') || lower.startsWith('socks://')) { return _parseSocks(value, subscriptionId: subscriptionId); } if (lower.startsWith('http://') || lower.startsWith('https://')) { if (lower.contains('://t.me/proxy') || lower.contains('://telegram.me/proxy')) { return _parseMtproto(value, subscriptionId: subscriptionId); } return _parseHttpProxy(value, subscriptionId: subscriptionId); } if (lower.startsWith('ssh://')) return _parseSsh(value, subscriptionId: subscriptionId); if (lower.startsWith('mtproto://') || lower.startsWith('tg://proxy')) { return _parseMtproto(value, subscriptionId: subscriptionId); } if (lower.startsWith('mieru://') || lower.startsWith('mierus://')) { return _parseMieru(value, subscriptionId: subscriptionId); } if (lower.startsWith('tewfai://')) { return _parseTewfai(value, subscriptionId: subscriptionId); } if (lower.startsWith('masque://')) return _parseMasque(value, subscriptionId: subscriptionId); if (lower.startsWith('trusttunnel://')) { return _parseTrustTunnel(value, subscriptionId: subscriptionId); } if (lower.startsWith('openconnect://') || lower.startsWith('anyconnect://')) { return _parseOpenConnect(value, subscriptionId: subscriptionId); } if (lower.startsWith('wdtt://') || lower.startsWith('qwdtt://') || lower.startsWith('csqtt://')) { return _parseWdtt(value, subscriptionId: subscriptionId); } if (lower.startsWith('sudoku://')) { return _parseSudoku(value, subscriptionId: subscriptionId); } if (lower.startsWith('vpn://')) { final list = _parseAmneziaVpnContainer(value, subscriptionId: subscriptionId); return list.isNotEmpty ? list.first : null; } if (value.startsWith('{') || value.startsWith('[')) { final list = _parseJsonPayload(value, subscriptionId: subscriptionId); return list.isNotEmpty ? list.first : null; } return _parseSurgeOrQxLine(value, subscriptionId: subscriptionId); } // --------------------------------------------------------------------------- // VLESS // --------------------------------------------------------------------------- Profile? _parseVless(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final uuid = Uri.decodeComponent(uri.userInfo); if (uuid.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; final rawType = (query['type'] ?? query['net'] ?? 'tcp').toLowerCase(); final transportType = _normalizeTransportName(rawType); final rawSec = (query['security'] ?? '').trim().toLowerCase(); final pbk = query['pbk'] ?? query['publicKey'] ?? ''; final sid = query['sid'] ?? query['shortId'] ?? ''; final sni = query['sni'] ?? query['peer'] ?? query['serverName'] ?? ''; final security = rawSec.isNotEmpty ? rawSec : (pbk.isNotEmpty ? 'reality' : (sni.isNotEmpty ? 'tls' : 'none')); final extra = { 'security': security, if ((query['fp'] ?? query['fingerprint'] ?? '').isNotEmpty) 'fp': normalizeUtlsFingerprint(query['fp'] ?? query['fingerprint'] ?? ''), }; final encryption = query['encryption'] ?? ''; if (encryption.isNotEmpty && encryption != 'none') { extra['encryption'] = encryption; } final packetEncoding = query['packetEncoding'] ?? query['packet_encoding'] ?? ''; if (packetEncoding.isNotEmpty) { extra['packet_encoding'] = packetEncoding; } if (_isTruthy(query['pq'] ?? query['support_x25519mlkem768'] ?? query['support-x25519mlkem768'])) { extra['pq'] = true; } if (_isTruthy(query['fragment'] ?? query['tls_fragment'])) { extra['tls_fragment'] = true; } _extractCertPin(extra, query['pcs'] ?? query['pinSHA256']); final trMap = _buildTransportFromQuery(transportType, query); if (trMap != null) { extra['transport'] = trMap; } final resolvedName = _name(query, uri.fragment, '$server:$port'); return Profile( id: _id('vless', '$server:$port:$uuid:$resolvedName:${query['path'] ?? ''}'), name: resolvedName, type: 'vless', server: server, port: port, uuid: uuid, transport: transportType, sni: sni, alpn: _splitCsv(query['alpn']), allowInsecure: _isTruthy(query['allowInsecure'] ?? query['insecure']), flow: normalizeVlessFlow(query['flow'] ?? ''), publicKey: pbk.trim(), shortId: normalizeRealityShortId(sid), subscriptionId: subscriptionId, extra: extra, ); } // --------------------------------------------------------------------------- // VMess (стандартный URI и V2RayN Base64 JSON) // --------------------------------------------------------------------------- Profile? _parseVmess(String link, {String subscriptionId = ''}) { final payload = link.substring('vmess://'.length).trim(); if (payload.contains('@')) { final uri = Uri.tryParse(link); if (uri != null && uri.host.isNotEmpty && uri.userInfo.isNotEmpty) { final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; final uuid = Uri.decodeComponent(uri.userInfo); final transportType = _normalizeTransportName(query['type'] ?? query['net'] ?? 'tcp'); final rawSec = (query['security'] ?? '').trim().toLowerCase(); final sni = query['sni'] ?? query['peer'] ?? ''; final pbk = query['pbk'] ?? ''; final security = rawSec.isNotEmpty ? rawSec : (pbk.isNotEmpty ? 'reality' : (sni.isNotEmpty ? 'tls' : 'none')); final extra = { 'security': security, 'vmess_security': query['encryption'] ?? query['scy'] ?? 'auto', if ((query['fp'] ?? '').isNotEmpty) 'fp': query['fp'], }; _extractCertPin(extra, query['pcs'] ?? query['pinSHA256']); final trMap = _buildTransportFromQuery(transportType, query); if (trMap != null) extra['transport'] = trMap; final resolvedName = _name(query, uri.fragment, '$server:$port'); return Profile( id: _id('vmess', '$server:$port:$uuid:$resolvedName'), name: resolvedName, type: 'vmess', server: server, port: port, uuid: uuid, transport: transportType, sni: sni, alpn: _splitCsv(query['alpn']), allowInsecure: _isTruthy(query['allowInsecure'] ?? query['insecure']), publicKey: pbk, shortId: query['sid'] ?? '', subscriptionId: subscriptionId, extra: extra, ); } } try { final rawB64 = payload.split('#').first; final decoded = utf8.decode(base64.decode(_normalizeBase64(rawB64)), allowMalformed: true); final map = jsonDecode(decoded); if (map is! Map) return null; final server = _cleanHost((map['add'] ?? '').toString()); if (server.isEmpty) return null; final port = _toInt(map['port']) ?? 443; final uuid = (map['id'] ?? '').toString(); if (uuid.isEmpty) return null; final aid = _toInt(map['aid']) ?? 0; final scy = (map['scy'] ?? map['security'] ?? 'auto').toString(); final net = _normalizeTransportName((map['net'] ?? 'tcp').toString()); final tlsVal = (map['tls'] ?? '').toString().toLowerCase(); final useTls = tlsVal == 'tls' || tlsVal == 'true' || tlsVal == '1'; final host = (map['host'] ?? '').toString(); final path = (map['path'] ?? '').toString(); final sni = (map['sni'] ?? '').toString().isNotEmpty ? map['sni'].toString() : (useTls ? host : ''); final fp = (map['fp'] ?? '').toString(); final extra = { 'security': useTls ? 'tls' : 'none', 'vmess_security': scy.isEmpty ? 'auto' : scy, if (aid > 0) 'alter_id': aid, if (fp.isNotEmpty) 'fp': fp, }; _extractCertPin(extra, map['pcs']?.toString()); final trMap = _buildTransportFromQuery(net, { if (path.isNotEmpty) 'path': path, if (host.isNotEmpty) 'host': host, if (net == 'grpc' && path.isNotEmpty) 'serviceName': path, if (map['mode'] != null) 'mode': map['mode'].toString(), }); if (trMap != null) extra['transport'] = trMap; final ps = (map['ps'] ?? '').toString().trim(); return Profile( id: _id('vmess', '$server:$port:$uuid:$ps'), name: ps.isNotEmpty ? ps : '$server:$port', type: 'vmess', server: server, port: port, uuid: uuid, transport: net, sni: sni, alpn: _splitCsv(map['alpn']?.toString()), allowInsecure: _isTruthy(map['allowInsecure']?.toString() ?? map['insecure']?.toString()) || (useTls && map['verify_cert'] == false), subscriptionId: subscriptionId, extra: extra, ); } catch (_) { return null; } } // --------------------------------------------------------------------------- // Trojan // --------------------------------------------------------------------------- Profile? _parseTrojan(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final password = Uri.decodeComponent(uri.userInfo); if (password.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; final transportType = _normalizeTransportName(query['type'] ?? query['net'] ?? 'tcp'); final rawSec = (query['security'] ?? '').trim().toLowerCase(); final pbk = query['pbk'] ?? query['publicKey'] ?? ''; final sid = query['sid'] ?? query['shortId'] ?? ''; final security = rawSec.isNotEmpty ? rawSec : (pbk.isNotEmpty ? 'reality' : 'tls'); final sni = query['sni'] ?? query['peer'] ?? query['serverName'] ?? ''; final extra = { 'security': security, if ((query['fp'] ?? query['fingerprint'] ?? '').isNotEmpty) 'fp': normalizeUtlsFingerprint(query['fp'] ?? query['fingerprint'] ?? ''), }; _extractCertPin(extra, query['pcs'] ?? query['pinSHA256']); final trMap = _buildTransportFromQuery(transportType, query); if (trMap != null) extra['transport'] = trMap; final resolvedName = _name(query, uri.fragment, '$server:$port'); return Profile( id: _id('trojan', '$server:$port:$password:$resolvedName:${query['path'] ?? ''}'), name: resolvedName, type: 'trojan', server: server, port: port, password: password, transport: transportType, sni: sni, alpn: _splitCsv(query['alpn']), allowInsecure: _isTruthy(query['allowInsecure'] ?? query['insecure']), publicKey: pbk, shortId: sid, subscriptionId: subscriptionId, extra: extra, ); } // --------------------------------------------------------------------------- // Shadowsocks (SIP002, Legacy Base64, SS-2022, плагины, UoT) // --------------------------------------------------------------------------- Profile? _parseShadowsocks(String link, {String subscriptionId = ''}) { final withoutScheme = link.startsWith('shadowsocks://') ? link.substring('shadowsocks://'.length) : link.substring('ss://'.length); final hashIdx = withoutScheme.indexOf('#'); final bodyPart = hashIdx >= 0 ? withoutScheme.substring(0, hashIdx) : withoutScheme; final fragment = hashIdx >= 0 ? withoutScheme.substring(hashIdx + 1) : ''; final qIdx = bodyPart.indexOf('?'); final mainPart = qIdx >= 0 ? bodyPart.substring(0, qIdx) : bodyPart; final queryPart = qIdx >= 0 ? bodyPart.substring(qIdx + 1) : ''; final query = _parseQuerySafe(queryPart); String userInfo; String hostPort; if (mainPart.contains('@')) { final atIdx = mainPart.lastIndexOf('@'); userInfo = mainPart.substring(0, atIdx); hostPort = mainPart.substring(atIdx + 1); final decodedUser = Uri.decodeComponent(userInfo); if (decodedUser.contains(':')) { userInfo = decodedUser; } else { try { userInfo = utf8.decode(base64.decode(_normalizeBase64(decodedUser))); } catch (_) { return null; } } } else { // Legacy ss://base64(method:password@host:port) try { final decoded = utf8.decode(base64.decode(_normalizeBase64(mainPart.replaceAll('/', '')))); final atIdx = decoded.lastIndexOf('@'); if (atIdx <= 0) return null; userInfo = decoded.substring(0, atIdx); hostPort = decoded.substring(atIdx + 1); } catch (_) { return null; } } final sep = userInfo.indexOf(':'); if (sep <= 0) return null; final method = userInfo.substring(0, sep).trim(); final password = userInfo.substring(sep + 1); final hp = _splitHostPort(hostPort.replaceAll('/', ''), 8388); if (hp == null) return null; final extra = {}; final pluginRaw = query['plugin'] ?? ''; if (pluginRaw.isNotEmpty) { final semi = pluginRaw.indexOf(';'); if (semi >= 0) { extra['plugin'] = pluginRaw.substring(0, semi); extra['plugin_opts'] = pluginRaw.substring(semi + 1); } else { extra['plugin'] = pluginRaw; } } if (_isTruthy(query['uot'] ?? query['udp-over-tcp'])) { extra['udp_over_tcp'] = true; } final resolvedName = _name(query, fragment, '${hp.$1}:${hp.$2}'); return Profile( id: _id('ss', '${hp.$1}:${hp.$2}:$method:$password'), name: resolvedName, type: 'shadowsocks', server: hp.$1, port: hp.$2, method: method, password: password, subscriptionId: subscriptionId, extra: extra, ); } // --------------------------------------------------------------------------- // Hysteria 2 & Hysteria v1 // --------------------------------------------------------------------------- Profile? _parseHysteria2(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; final password = Uri.decodeComponent(uri.userInfo.isNotEmpty ? uri.userInfo : (query['auth'] ?? '')); final extra = {}; final rawObfs = (query['obfs'] ?? query['obfs_type'] ?? query['obfs-type'] ?? '').trim(); var isGecko = rawObfs.toLowerCase() == 'gecko'; var obfsPw = query['obfs-password'] ?? query['obfs_password'] ?? query['obfspassword'] ?? ''; int? minPkt = _toInt( query['min_packet_size'] ?? query['min-packet-size'] ?? query['minpacketsize'], ); int? maxPkt = _toInt( query['max_packet_size'] ?? query['max-packet-size'] ?? query['maxpacketsize'], ); final psRaw = query['packetsize'] ?? query['packet-size'] ?? query['packet_size'] ?? ''; if (psRaw.isNotEmpty) { final parsedRange = _parsePacketSizeRange(psRaw); if (parsedRange != null) { minPkt ??= parsedRange.$1; maxPkt ??= parsedRange.$2; } } final fmRaw = query['fm'] ?? query['finalmask'] ?? ''; if (fmRaw.isNotEmpty) { try { final fm = jsonDecode(fmRaw); if (fm is Map && fm['udp'] is List) { for (final item in fm['udp'] as List) { if (item is! Map) continue; final s = item['settings'] is Map ? item['settings'] as Map : const {}; final itemTy = (item['type'] ?? '').toString().toLowerCase(); if (itemTy == 'gecko') isGecko = true; if (obfsPw.isEmpty && s['password'] != null) { obfsPw = s['password'].toString(); } final fmPs = s['packetSize'] ?? s['packet_size']; if (fmPs != null) { final r = _parsePacketSizeRange(fmPs.toString()); if (r != null) { isGecko = true; minPkt ??= r.$1; maxPkt ??= r.$2; } } } } } catch (_) {} } if (isGecko || (minPkt != null && minPkt > 0) || (maxPkt != null && maxPkt > 0)) { extra['obfs'] = { 'type': 'gecko', if (obfsPw.isNotEmpty) 'password': obfsPw, if (minPkt != null && minPkt > 0) 'min_packet_size': minPkt, if (maxPkt != null && maxPkt > 0) 'max_packet_size': maxPkt, }; } else if (rawObfs.isNotEmpty && rawObfs.toLowerCase() != 'none') { extra['obfs'] = { 'type': rawObfs, 'password': obfsPw, }; } final up = _parseMbps(query['upmbps'] ?? query['up_mbps'] ?? query['up']); if (up != null && up > 0) extra['up_mbps'] = up; final down = _parseMbps(query['downmbps'] ?? query['down_mbps'] ?? query['down']); if (down != null && down > 0) extra['down_mbps'] = down; final mport = query['mport'] ?? query['ports'] ?? query['server_ports'] ?? ''; if (mport.isNotEmpty) { final ports = mport .split(',') .map((s) => s.trim().replaceAll('-', ':')) .where((s) => s.isNotEmpty) .toList(); if (ports.isNotEmpty) extra['server_ports'] = ports; } final hopInt = _normalizeDurationString( query['hop_interval'] ?? query['hop-interval'] ?? query['mporthopint'], ); if (hopInt != null) { extra['hop_interval'] = hopInt; } _extractCertPin(extra, query['pinSHA256'] ?? query['pcs']); return Profile( id: _id('hysteria2', '$server:$port:$password'), name: _name(query, uri.fragment, '$server:$port'), type: 'hysteria2', server: server, port: port, password: password, sni: query['sni'] ?? query['peer'] ?? '', alpn: _splitCsv(query['alpn']), allowInsecure: _isTruthy(query['insecure'] ?? query['allowInsecure']), subscriptionId: subscriptionId, extra: extra, ); } Profile? _parseHysteria1(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; final auth = query['auth'] ?? Uri.decodeComponent(uri.userInfo); return Profile( id: _id('hysteria', '$server:$port:$auth'), name: _name(query, uri.fragment, '$server:$port'), type: 'hysteria', server: server, port: port, password: auth, sni: query['peer'] ?? query['sni'] ?? '', alpn: _splitCsv(query['alpn']), allowInsecure: _isTruthy(query['insecure'] ?? query['allowInsecure']), subscriptionId: subscriptionId, extra: { if (auth.isNotEmpty) 'auth_str': auth, if (_toInt(query['upmbps'] ?? query['up']) != null) 'up_mbps': _toInt(query['upmbps'] ?? query['up']), if (_toInt(query['downmbps'] ?? query['down']) != null) 'down_mbps': _toInt(query['downmbps'] ?? query['down']), if ((query['obfs'] ?? '').isNotEmpty) 'obfs': query['obfs'], }, ); } // --------------------------------------------------------------------------- // TUIC v5 // --------------------------------------------------------------------------- Profile? _parseTuic(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; final userInfo = uri.userInfo; final separator = userInfo.indexOf(':'); final uuid = Uri.decodeComponent(separator < 0 ? userInfo : userInfo.substring(0, separator)); final password = Uri.decodeComponent(separator < 0 ? (query['password'] ?? '') : userInfo.substring(separator + 1)); final cc = query['congestion_control'] ?? query['congestion-control'] ?? query['congestion-controller'] ?? 'bbr'; final hbRaw = query['heartbeat'] ?? query['heartbeat-interval'] ?? query['heartbeat_interval']; final hb = _normalizeDurationString(hbRaw, treatLargeIntsAsMillis: query.containsKey('heartbeat-interval')); final extra = { 'congestion_control': cc, if ((query['udp_relay_mode'] ?? query['udp-relay-mode'] ?? '').isNotEmpty) 'udp_relay_mode': query['udp_relay_mode'] ?? query['udp-relay-mode'], if (_isTruthy( query['zero_rtt_handshake'] ?? query['zero-rtt-handshake'] ?? query['reduce_rtt'] ?? query['reduce-rtt'] ?? query['fast-open'] ?? query['fast_open'], )) 'zero_rtt_handshake': true, if (_isTruthy(query['udp_over_stream'] ?? query['udp-over-stream'] ?? query['uos'])) 'udp_over_stream': true, 'heartbeat': ?hb, }; _extractCertPin(extra, query['pcs'] ?? query['pinSHA256']); return Profile( id: _id('tuic', '$server:$port:$uuid:$password'), name: _name(query, uri.fragment, '$server:$port'), type: 'tuic', server: server, port: port, uuid: uuid, password: password, sni: query['sni'] ?? '', alpn: _splitCsv(query['alpn']), allowInsecure: _isTruthy(query['insecure'] ?? query['allowInsecure']), subscriptionId: subscriptionId, extra: extra, ); } // --------------------------------------------------------------------------- // WireGuard & AmneziaWG (URI, amneziawg://#name и .conf INI) // --------------------------------------------------------------------------- /// Проверяет, является ли текст конфигурационным файлом `.conf` WireGuard / AmneziaWG. bool looksLikeWireGuardConf(String s) { return RegExp(r'\[\s*interface\s*\]', caseSensitive: false).hasMatch(s) && RegExp(r'\[\s*peer\s*\]', caseSensitive: false).hasMatch(s); } Profile? _parseWireGuardUri(String link, {String subscriptionId = ''}) { final schemeIdx = link.indexOf('://'); if (schemeIdx > 0) { final afterScheme = link.substring(schemeIdx + 3).trim(); final hashIdx = afterScheme.indexOf('#'); final bodyPart = hashIdx >= 0 ? afterScheme.substring(0, hashIdx).trim() : afterScheme; final fragPart = hashIdx >= 0 ? afterScheme.substring(hashIdx + 1).trim() : ''; // Формат роутера Tachyon: amneziawg://# if (!bodyPart.contains('@') && !bodyPart.contains('?')) { try { final decoded = utf8.decode(base64.decode(_normalizeBase64(bodyPart)), allowMalformed: true); if (looksLikeWireGuardConf(decoded)) { String? nameOverride; if (fragPart.isNotEmpty) { try { nameOverride = Uri.decodeComponent(fragPart); } catch (_) { nameOverride = fragPart; } } return _parseWireGuardConf( decoded, subscriptionId: subscriptionId, nameOverride: nameOverride, ); } } catch (_) {} } } final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final query = {}; for (final entry in _parseQuerySafe(uri.query, plusAsSpace: false).entries) { query[entry.key] = entry.value; query.putIfAbsent(entry.key.toLowerCase(), () => entry.value); } final server = _cleanHost(uri.host); final port = uri.port == 0 ? 51820 : uri.port; final rawPriv = uri.userInfo.isNotEmpty ? uri.userInfo : (query['privatekey'] ?? query['private_key'] ?? query['private-key'] ?? ''); String decodePreservePlus(String rawVal) { try { return Uri.decodeComponent(rawVal); } catch (_) { return rawVal; } } final privKey = decodePreservePlus(rawPriv); final pubKey = decodePreservePlus( query['publickey'] ?? query['public_key'] ?? query['peer_public_key'] ?? query['public-key'] ?? '', ); if (privKey.isEmpty || pubKey.isEmpty) return null; bool isMasqueProto(String? v) { if (v == null) return false; final t = v.trim().toLowerCase(); return t == 'dns' || t == 'stun' || t == 'sip' || t == 'quic'; } final rawQueryIp = query['ip']; final addrsRaw = query['address'] ?? query['local_address'] ?? (!isMasqueProto(rawQueryIp) ? rawQueryIp : null) ?? '10.0.0.2/32'; final localAddress = addrsRaw .split(',') .map((s) => _formatWgCidr(s.trim())) .where((s) => s.isNotEmpty) .toList(); final extra = { 'private_key': privKey, 'peer_public_key': pubKey, 'local_address': localAddress.isEmpty ? ['10.0.0.2/32'] : localAddress, }; final psk = query['psk'] ?? query['presharedkey'] ?? query['pre_shared_key'] ?? ''; if (psk.isNotEmpty) extra['pre_shared_key'] = decodePreservePlus(psk); final mtu = _toInt(query['mtu']); if (mtu != null && mtu > 0) extra['mtu'] = mtu; final rawKeepalive = (query['keepalive'] ?? query['persistent_keepalive'] ?? query['persistent_keepalive_interval'] ?? '') .trim(); if (rawKeepalive.isNotEmpty) { final kaInt = _toInt(rawKeepalive); if (kaInt != null && kaInt > 0) { extra['persistent_keepalive_interval'] = kaInt; } else if (rawKeepalive.contains('-')) { extra['persistent_keepalive_interval'] = rawKeepalive; } } final reserved = _parseWgReserved(query['reserved']); if (reserved != null) extra['reserved'] = reserved; final rawServerPorts = (query['server_ports'] ?? query['ports'] ?? '').trim(); if (rawServerPorts.isNotEmpty) { final ports = rawServerPorts .split(RegExp(r'[,;\s]+')) .map((e) => e.trim()) .where((e) => e.isNotEmpty) .toList(growable: false); if (ports.isNotEmpty) extra['server_ports'] = ports; } if ((query['hop_interval'] ?? '').trim().isNotEmpty) { extra['hop_interval'] = query['hop_interval']!.trim(); } if ((query['hop_interval_max'] ?? '').trim().isNotEmpty) { extra['hop_interval_max'] = query['hop_interval_max']!.trim(); } _extractAwgParams(query, extra, fromUriQuery: true); return Profile( id: _id('wireguard', '$server:$port:$privKey:$pubKey'), name: _name(query, uri.fragment, '$server:$port'), type: 'wireguard', server: server, port: port, password: privKey, publicKey: pubKey, subscriptionId: subscriptionId, extra: extra, ); } Profile? _parseWireGuardConf( String conf, { String subscriptionId = '', String? nameOverride, }) { final list = parseWireGuardConfAll( conf, subscriptionId: subscriptionId, nameOverride: nameOverride, ); return list.isNotEmpty ? list.first : null; } /// Разбирает конфигурационный файл `.conf` WireGuard / AmneziaWG (1.5, 2.0, 3.0, 3.1) /// с поддержкой одной или нескольких секций `[Peer]`. List parseWireGuardConfAll( String conf, { String subscriptionId = '', String? nameOverride, }) { var section = ''; final iface = {}; final peers = >[]; var ifaceCommentName = ''; for (final rawLine in conf.split(RegExp(r'[\r\n]+'))) { final trimmed = rawLine.trim(); if (trimmed.isEmpty) continue; if (trimmed.startsWith('#') || trimmed.startsWith(';')) { final comment = trimmed.substring(1).trim(); if (comment.isNotEmpty) { final eqIdx = comment.indexOf('='); if (eqIdx > 0) { final ck = comment.substring(0, eqIdx).trim().toLowerCase(); final cv = comment.substring(eqIdx + 1).trim(); if (cv.isNotEmpty && (ck == 'name' || ck == 'description' || ck == 'title' || ck == 'tag')) { if (section == 'peer' && peers.isNotEmpty) { peers.last.putIfAbsent('_comment_name', () => cv); } else if (ifaceCommentName.isEmpty) { ifaceCommentName = cv; } } } else if (ifaceCommentName.isEmpty && section.isEmpty && comment.length <= 64 && !comment.toLowerCase().startsWith('generated')) { ifaceCommentName = comment; } } continue; } // Срезаем inline-комментарий `# ...`, если он находится вне угловых скобок CPS `<...>` var line = trimmed; final hashIdx = line.indexOf('#'); if (hashIdx > 0 && !line.substring(hashIdx).contains('>')) { line = line.substring(0, hashIdx).trim(); } if (line.startsWith('[') && line.endsWith(']')) { section = line.substring(1, line.length - 1).trim().toLowerCase(); if (section == 'peer') { peers.add({}); } continue; } final eq = line.indexOf('='); if (eq <= 0) continue; final key = line .substring(0, eq) .trim() .toLowerCase() .replaceAll('-', '_'); var val = line.substring(eq + 1).trim(); if (val.length >= 2 && ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'")))) { val = val.substring(1, val.length - 1).trim(); } if (val.isEmpty) continue; if (section == 'interface') { if (key == 'address' && iface.containsKey('address')) { iface['address'] = '${iface['address']}, $val'; } else { iface[key] = val; } } else if (section == 'peer' && peers.isNotEmpty) { if ((key == 'allowedips' || key == 'allowed_ips') && peers.last.containsKey('allowed_ips')) { peers.last['allowed_ips'] = '${peers.last['allowed_ips']}, $val'; } else { peers.last[key == 'allowedips' ? 'allowed_ips' : key] = val; } } } final privKey = iface['privatekey'] ?? iface['private_key'] ?? ''; if (privKey.isEmpty || peers.isEmpty) return const []; final addrRaw = iface['address'] ?? '10.0.0.2/32'; final localAddress = addrRaw .split(',') .map((s) => _formatWgCidr(s.trim())) .where((s) => s.isNotEmpty) .toList(); final mtu = _toInt(iface['mtu']); final baseName = (nameOverride != null && nameOverride.trim().isNotEmpty) ? nameOverride.trim() : ifaceCommentName; final results = []; final validPeers = peers.where((p) { final pk = p['publickey'] ?? p['public_key'] ?? ''; final ep = p['endpoint'] ?? ''; return pk.isNotEmpty && ep.isNotEmpty; }).toList(); for (var i = 0; i < validPeers.length; i++) { final peer = validPeers[i]; final pubKey = peer['publickey'] ?? peer['public_key'] ?? ''; final endpoint = peer['endpoint'] ?? ''; final hp = _splitHostPort(endpoint, 51820); if (hp == null) continue; final extra = { 'private_key': privKey, 'peer_public_key': pubKey, 'local_address': localAddress.isEmpty ? ['10.0.0.2/32'] : localAddress, }; final psk = peer['presharedkey'] ?? peer['pre_shared_key'] ?? ''; if (psk.isNotEmpty) extra['pre_shared_key'] = psk; if (mtu != null && mtu > 0) extra['mtu'] = mtu; final allowedRaw = peer['allowed_ips'] ?? ''; if (allowedRaw.isNotEmpty) { final allowedList = allowedRaw .split(',') .map((s) => s.trim()) .where((s) => s.isNotEmpty) .toList(); if (allowedList.isNotEmpty) { extra['allowed_ips'] = allowedList; } } final kaRaw = (peer['persistentkeepalive'] ?? peer['persistent_keepalive'] ?? peer['persistent_keepalive_interval'] ?? '') .trim(); if (kaRaw.isNotEmpty) { final kaInt = _toInt(kaRaw); if (kaInt != null && kaInt > 0) { extra['persistent_keepalive_interval'] = kaInt; } else if (kaRaw.contains('-')) { extra['persistent_keepalive_interval'] = kaRaw; } } final reserved = _parseWgReserved(peer['reserved']); if (reserved != null) extra['reserved'] = reserved; _extractAwgParams(iface, extra); final peerComment = peer['_comment_name'] ?? ''; final String resolvedName; if (peerComment.isNotEmpty) { resolvedName = peerComment; } else if (baseName.isNotEmpty) { resolvedName = validPeers.length > 1 ? '$baseName (${i + 1})' : baseName; } else { resolvedName = '${hp.$1}:${hp.$2}'; } results.add(Profile( id: _id('wireguard', '${hp.$1}:${hp.$2}:$privKey:$pubKey'), name: resolvedName, type: 'wireguard', server: hp.$1, port: hp.$2, password: privKey, publicKey: pubKey, subscriptionId: subscriptionId, extra: extra, )); } return results; } Object _parseAwgIntOrRange(String raw) { final s = raw.trim(); final asInt = int.tryParse(s); if (asInt != null) { if (asInt < 0 && asInt >= -2147483648) { return asInt + 4294967296; } return asInt; } return s; } void _extractAwgParams( Map src, Map dst, { bool fromUriQuery = false, }) { String? pickStr(List keys) { for (final k in keys) { final v = src[k] ?? src[k.toUpperCase()] ?? src[k.toLowerCase()]; if (v != null) { final s = v.toString().trim(); if (s.isNotEmpty) return s; } } return null; } for (final k in const ['jc', 'jmin', 'jmax', 's1', 's2', 's3', 's4']) { final raw = pickStr([k]); final v = _toInt(raw); if (v != null && v >= 0) dst[k] = v; } for (final (target, aliases) in const [ ('h1', ['h1']), ('h2', ['h2']), ('h3', ['h3']), ('h4', ['h4']), ( 'content_padding_addition', ['content_padding_addition', 'contentpaddingaddition', 'ContentPaddingAddition'], ), ( 'rekey_after_time', ['rekey_after_time', 'rekeyaftertime', 'RekeyAfterTime'], ), ( 'rekey_timeout', ['rekey_timeout', 'rekeytimeout', 'RekeyTimeout'], ), ( 'reject_after_time', ['reject_after_time', 'rejectaftertime', 'RejectAfterTime'], ), ( 'keepalive_timeout', ['keepalive_timeout', 'keepalivetimeout', 'KeepaliveTimeout'], ), ( 'itime', ['itime', 'ITime'], ), ( 'max_handshake_attempts', ['max_handshake_attempts', 'maxhandshakeattempts', 'MaxHandshakeAttempts'], ), ]) { final raw = pickStr(aliases); if (raw != null) { dst[target] = _parseAwgIntOrRange(raw); } } for (final (target, aliases) in const [ ('i1', ['i1', 'j1']), ('i2', ['i2', 'j2']), ('i3', ['i3', 'j3']), ('i4', ['i4']), ('i5', ['i5']), ]) { final raw = pickStr(aliases); if (raw != null) { final norm = normalizeAwgCpsChain(raw); if (norm.isNotEmpty) { dst[target] = norm; } } } for (final (target, aliases) in const [ ('id', ['id']), ('ib', ['ib']), ( 'header_protection_key', ['header_protection_key', 'headerprotectionkey', 'HeaderProtectionKey', 'hpk'], ), ]) { final raw = pickStr(aliases); if (raw != null) { dst[target] = raw; } } final rawIp = pickStr(fromUriQuery ? ['awg_ip', 'ip'] : ['ip', 'awg_ip']); if (rawIp != null) { final lowerIp = rawIp.toLowerCase(); if (!fromUriQuery || lowerIp == 'dns' || lowerIp == 'stun' || lowerIp == 'sip' || lowerIp == 'quic') { // В Clash YAML `ip` может быть IPv4-адресом клиента (`10.0.0.2`), поэтому проверяем, что это не IP if (!RegExp(r'^\d+\.\d+\.\d+\.\d+').hasMatch(rawIp) && !rawIp.contains(':')) { dst['ip'] = lowerIp; } } } for (final (target, aliases) in const [ ('random_trailers', ['random_trailers', 'randomtrailers', 'RandomTrailers']), ('disable_cookies', ['disable_cookies', 'disablecookies', 'DisableCookies']), ]) { final raw = pickStr(aliases); if (raw != null && (_isTruthy(raw) || raw.toLowerCase() == 'on')) { dst[target] = true; } } // AmneziaWG 3.0+: если задан header_protection_key, s1..s4 должны быть >= 12 (по умолчанию 20) if ((dst['header_protection_key']?.toString().trim() ?? '').isNotEmpty) { for (final sk in const ['s1', 's2', 's3', 's4']) { final cur = _toInt(dst[sk]) ?? 0; if (cur < 12) dst[sk] = 20; } } final ipProto = dst['ip']?.toString().trim().toLowerCase() ?? ''; if ((dst['i1']?.toString().trim() ?? '').isNotEmpty || (ipProto == 'sip' && (dst['i2']?.toString().trim() ?? '').isNotEmpty) || ipProto.isEmpty) { dst ..remove('id') ..remove('ip') ..remove('ib'); } } // --------------------------------------------------------------------------- // AnyTLS, NaiveProxy, Snell, ShadowTLS, FPTN // --------------------------------------------------------------------------- Profile? _parseAnytls(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; final password = Uri.decodeComponent(uri.userInfo.isNotEmpty ? uri.userInfo : (query['password'] ?? '')); final pbk = query['pbk'] ?? query['publicKey'] ?? ''; final sid = query['sid'] ?? query['shortId'] ?? ''; final extra = { if ((query['fp'] ?? query['fingerprint'] ?? '').isNotEmpty) 'fp': query['fp'] ?? query['fingerprint'], if ((query['idle_session_check_interval'] ?? '').isNotEmpty) 'idle_session_check_interval': query['idle_session_check_interval'], if ((query['idle_session_timeout'] ?? '').isNotEmpty) 'idle_session_timeout': query['idle_session_timeout'], if (_toInt(query['min_idle_session']) != null) 'min_idle_session': _toInt(query['min_idle_session']), }; _extractCertPin(extra, query['pcs'] ?? query['pinSHA256']); return Profile( id: _id('anytls', '$server:$port:$password'), name: _name(query, uri.fragment, '$server:$port'), type: 'anytls', server: server, port: port, password: password, sni: query['sni'] ?? query['peer'] ?? '', alpn: _splitCsv(query['alpn']), allowInsecure: _isTruthy(query['insecure'] ?? query['allowInsecure']), publicKey: pbk, shortId: sid, subscriptionId: subscriptionId, extra: extra, ); } Profile? _parseNaive(String link, {String subscriptionId = ''}) { final lower = link.toLowerCase(); final normalized = link.replaceFirst( RegExp(r'^naive(\+https|\+http2|\+h2|\+quic|\+h3)?://', caseSensitive: false), 'https://', ); final uri = Uri.tryParse(normalized); if (uri == null || uri.host.isEmpty) return null; final query = uri.queryParameters; final isQuic = lower.startsWith('naive+quic://') || lower.startsWith('naive+h3://') || _isTruthy(query['quic']); final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; final (username, password) = _splitUserPass(uri.userInfo); final extra = { if (username.isNotEmpty) 'username': username, if (isQuic) 'quic': true, if ((query['quic_congestion_control'] ?? '').isNotEmpty) 'quic_congestion_control': query['quic_congestion_control'], if (_toInt(query['insecure-concurrency'] ?? query['insecure_concurrency']) != null) 'insecure_concurrency': _toInt(query['insecure-concurrency'] ?? query['insecure_concurrency']), if (_isTruthy(query['uot'] ?? query['udp_over_tcp'])) 'udp_over_tcp': true, }; _extractCertPin(extra, query['pcs'] ?? query['pinSHA256']); return Profile( id: _id('naive', '$server:$port:$username:$isQuic'), name: _name(query, uri.fragment, '$server:$port'), type: 'naive', server: server, port: port, password: password, transport: isQuic ? 'quic' : 'tcp', sni: query['sni'] ?? server, alpn: _splitCsv(query['alpn']), allowInsecure: false, subscriptionId: subscriptionId, extra: extra, ); } Profile? _parseSnell(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; final psk = Uri.decodeComponent(uri.userInfo.isNotEmpty ? uri.userInfo : (query['psk'] ?? '')); if (psk.isEmpty) return null; final rawVer = _toInt(query['version'] ?? query['v']) ?? 5; final version = (rawVer == 4 || rawVer == 5 || rawVer == 6) ? rawVer : 5; final obfs = (query['obfs'] ?? query['obfs_mode'] ?? query['plugin'] ?? '').trim().toLowerCase(); final obfsHost = (query['obfs-host'] ?? query['obfs_host'] ?? query['host'] ?? '').trim(); final extra = { 'psk': psk, 'version': version, }; if (version == 4 || version == 5) { if (obfs == 'http' || obfs == 'tls') { extra['obfs'] = { 'mode': obfs, if (obfsHost.isNotEmpty) 'host': obfsHost, }; } if (_isTruthy(query['uot'] ?? query['udp_over_tcp'] ?? query['udp-over-tcp'])) { extra['udp_over_tcp'] = true; } if (_isTruthy(query['reuse'])) extra['reuse'] = true; } else { if ((query['mode'] ?? '').isNotEmpty) extra['mode'] = query['mode']; if (_isTruthy(query['reuse'])) extra['reuse'] = true; } return Profile( id: _id('snell', '$server:$port:$psk:$version'), name: _name(query, uri.fragment, '$server:$port'), type: 'snell', server: server, port: port, password: psk, subscriptionId: subscriptionId, extra: extra, ); } Profile? _parseShadowTls(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; final password = Uri.decodeComponent(uri.userInfo.isNotEmpty ? uri.userInfo : (query['password'] ?? '')); final version = _toInt(query['version']) ?? 3; final extra = { 'version': version, if ((query['fp'] ?? '').isNotEmpty) 'fp': query['fp'], }; _extractCertPin(extra, query['pcs'] ?? query['pinSHA256']); return Profile( id: _id('shadowtls', '$server:$port:$password'), name: _name(query, uri.fragment, '$server:$port'), type: 'shadowtls', server: server, port: port, password: password, sni: query['sni'] ?? query['peer'] ?? server, alpn: _splitCsv(query['alpn']), allowInsecure: _isTruthy(query['insecure'] ?? query['allowInsecure']), subscriptionId: subscriptionId, extra: extra, ); } /// Проверяет, является ли строка токеном или JSON-конфигом FPTN (`fptn:`, `fptnb:` или JSON с `servers` и `username`). bool isFptnPayload(String s) { final trimmed = s.trim(); if (_isFptnToken(trimmed)) return true; if (trimmed.startsWith('{') && trimmed.contains('"servers"') && trimmed.contains('"username"') && trimmed.contains('"password"')) { return true; } return false; } /// Извлекает название сервиса (`service_name` или `#fragment`) из токена FPTN. String extractFptnServiceName(String raw) { final trimmed = raw.trim(); final hashIdx = trimmed.indexOf('#'); if (hashIdx >= 0 && hashIdx + 1 < trimmed.length) { final rawFrag = trimmed.substring(hashIdx + 1).trim(); if (rawFrag.isNotEmpty) { try { return Uri.decodeComponent(rawFrag); } catch (_) { return rawFrag; } } } try { Map? map; if (trimmed.startsWith('{')) { final parsed = jsonDecode(trimmed); if (parsed is Map) map = parsed; } else if (trimmed.toLowerCase().startsWith('fptn:') || trimmed.toLowerCase().startsWith('fptn://')) { final cleanToken = hashIdx >= 0 ? trimmed.substring(0, hashIdx).trim() : trimmed; final body = cleanToken .replaceFirst(RegExp(r'^fptn(://|:)', caseSensitive: false), '') .replaceAll(RegExp(r'\s+'), ''); final decoded = utf8.decode(base64.decode(_normalizeBase64(body))); final parsed = jsonDecode(decoded); if (parsed is Map) map = parsed; } final svc = map?['service_name']?.toString().trim() ?? ''; if (svc.isNotEmpty) return svc; } catch (_) {} return 'FPTN'; } bool _isFptnToken(String s) { final lower = s.toLowerCase(); return lower.startsWith('fptn:') || lower.startsWith('fptn://') || lower.startsWith('fptnb:') || lower.startsWith('fptnb//') || lower.startsWith('fptnb://'); } List _parseFptnToken(String raw, {String subscriptionId = ''}) { final trimmed = raw.trim(); final hashIdx = trimmed.indexOf('#'); final cleanToken = (!trimmed.startsWith('{') && hashIdx >= 0) ? trimmed.substring(0, hashIdx).trim() : trimmed; var fragName = ''; if (!trimmed.startsWith('{') && hashIdx >= 0 && hashIdx + 1 < trimmed.length) { final rawFrag = trimmed.substring(hashIdx + 1).trim(); try { fragName = Uri.decodeComponent(rawFrag); } catch (_) { fragName = rawFrag; } } final lower = cleanToken.toLowerCase(); if (lower.startsWith('fptnb:') || lower.startsWith('fptnb//') || lower.startsWith('fptnb://')) { return [ Profile( id: _id('fptn', cleanToken), name: fragName.isNotEmpty ? fragName : 'FPTN Server', type: 'fptn', server: 'fptn.token', port: 443, subscriptionId: subscriptionId, extra: {'token': cleanToken}, ), ]; } try { final String jsonStr; if (cleanToken.startsWith('{')) { jsonStr = cleanToken; } else { final body = cleanToken .replaceFirst(RegExp(r'^fptn(://|:)', caseSensitive: false), '') .replaceAll(RegExp(r'\s+'), ''); jsonStr = utf8.decode(base64.decode(_normalizeBase64(body))); } final map = jsonDecode(jsonStr); if (map is! Map) return const []; final user = (map['username'] ?? '').toString(); final pass = (map['password'] ?? '').toString(); final service = (map['service_name'] ?? 'FPTN').toString(); final servers = map['servers']; if (servers is! List) return const []; final out = []; for (final srv in servers) { if (srv is! Map) continue; final host = (srv['host'] ?? '').toString(); final port = _toInt(srv['port']) ?? 443; final srvName = (srv['name'] ?? (fragName.isNotEmpty ? fragName : '$service ($host)')).toString(); final md5 = (srv['md5_fingerprint'] ?? '').toString(); if (host.isEmpty) continue; out.add(Profile( id: _id('fptn', '$host:$port:$user'), name: srvName, type: 'fptn', server: host, port: port, password: pass, subscriptionId: subscriptionId, extra: { 'username': user, if (md5.isNotEmpty) 'md5_fingerprint': md5, }, )); } return out; } catch (_) { return const []; } } // --------------------------------------------------------------------------- // SOCKS, HTTP, SSH, MTProto, Mieru, TewFai, MASQUE, TrustTunnel, OpenConnect // --------------------------------------------------------------------------- Profile? _parseSocks(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 1080 : uri.port; final (username, password) = _splitUserPass(uri.userInfo); final isSocks4 = link.toLowerCase().startsWith('socks4'); return Profile( id: _id('socks', '$server:$port:$username:$password'), name: _name(query, uri.fragment, '$server:$port'), type: 'socks', server: server, port: port, password: password, subscriptionId: subscriptionId, extra: { 'version': isSocks4 ? '4' : '5', if (username.isNotEmpty) 'username': username, if (_isTruthy(query['uot'] ?? query['udp-over-tcp'])) 'udp_over_tcp': true, }, ); } /// Разбирает `http://user:pass@host:port#name` только если это именно прокси-сервер /// (есть учётные данные или явный порт без пути подписки). Profile? _parseHttpProxy(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final hasPath = uri.path.isNotEmpty && uri.path != '/'; if (hasPath) return null; if (uri.userInfo.isEmpty && !uri.hasPort) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? (uri.scheme == 'https' ? 443 : 8080) : uri.port; final (username, password) = _splitUserPass(uri.userInfo); final isHttps = uri.scheme.toLowerCase() == 'https'; return Profile( id: _id('http', '$server:$port:$username:$isHttps'), name: _name(query, uri.fragment, '$server:$port'), type: 'http', server: server, port: port, password: password, sni: isHttps ? (query['sni'] ?? server) : '', allowInsecure: _isTruthy(query['insecure'] ?? query['allowInsecure']), subscriptionId: subscriptionId, extra: { 'security': isHttps ? 'tls' : 'none', if (username.isNotEmpty) 'username': username, }, ); } Profile? _parseSsh(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 22 : uri.port; final (user, password) = _splitUserPass(uri.userInfo); return Profile( id: _id('ssh', '$server:$port:$user'), name: _name(query, uri.fragment, '$server:$port'), type: 'ssh', server: server, port: port, password: password, subscriptionId: subscriptionId, extra: { 'user': user.isEmpty ? 'root' : user, if ((query['private_key'] ?? '').isNotEmpty) 'private_key': query['private_key'], }, ); } Profile? _parseMtproto(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null) return null; final query = uri.queryParameters; final scheme = uri.scheme.toLowerCase(); final isTgWeb = scheme == 'tg' || scheme == 'http' || scheme == 'https'; final server = _cleanHost( isTgWeb ? (query['server'] ?? '') : (uri.host.isNotEmpty && uri.host != 'proxy' ? uri.host : (query['server'] ?? '')), ); if (server.isEmpty) return null; final port = _toInt(query['port']) ?? (uri.port == 0 ? 443 : uri.port); var secret = (isTgWeb ? (query['secret'] ?? '') : (uri.userInfo.isNotEmpty ? Uri.decodeComponent(uri.userInfo) : (query['secret'] ?? ''))).trim(); if (secret.isEmpty) return null; final normSecret = secret.toLowerCase(); var paddedPrefix = false; var faketlsDomain = ''; if (normSecret.length >= 34 && RegExp(r'^(ee|dd)[0-9a-f]{32}').hasMatch(normSecret)) { paddedPrefix = normSecret.startsWith('dd'); if (normSecret.startsWith('ee') && normSecret.length > 34 && (normSecret.length - 34).isEven) { final domHex = normSecret.substring(34); final bytes = []; var hexOk = true; for (var i = 0; i < domHex.length; i += 2) { final b = int.tryParse(domHex.substring(i, i + 2), radix: 16); if (b == null) { hexOk = false; break; } bytes.add(b); } if (hexOk) { try { final dom = utf8.decode(bytes).trim(); if (dom.isNotEmpty) faketlsDomain = dom; } catch (_) {} } } secret = normSecret.substring(2, 34); } else if (normSecret.length == 32 && RegExp(r'^[0-9a-f]{32}$').hasMatch(normSecret)) { secret = normSecret; } final extra = {'secret': secret}; final tr = (query['transport'] ?? '').trim().toLowerCase(); if (const {'abridged', 'intermediate', 'padded_intermediate', 'full'}.contains(tr)) { extra['mtproto_transport'] = tr; } else if (paddedPrefix) { extra['mtproto_transport'] = 'padded_intermediate'; } final mode = (query['mode'] ?? '').trim().toLowerCase(); if (mode == 'mtproto' || mode == 'mtproxy') { extra['mode'] = mode; } final rawSni = (query['sni'] ?? query['faketls'] ?? '').trim(); final sni = rawSni.isNotEmpty ? rawSni : faketlsDomain; return Profile( id: _id('mtproto', '$server:$port:$secret'), name: _name(query, uri.fragment, 'MTProto $server:$port'), type: 'mtproto', server: server, port: port, password: secret, sni: sni, subscriptionId: subscriptionId, extra: extra, ); } Profile? _parseMieru(String link, {String subscriptionId = ''}) { final normalizedLink = link.contains('&') ? link.replaceAll('&', '&') : link; final uri = Uri.tryParse(normalizedLink); if (uri == null || uri.host.isEmpty) return null; final query = {}; for (final entry in uri.queryParameters.entries) { query[entry.key] = entry.value; query.putIfAbsent(entry.key.toLowerCase(), () => entry.value); } final server = _cleanHost(uri.host); var (username, password) = _splitUserPass(uri.userInfo); if (password.isEmpty) { password = (query['password'] ?? query['pass'] ?? '').trim(); } if (username.isEmpty || password.isEmpty) return null; final portRanges = []; final seenRanges = {}; var hasExplicitRange = false; var firstParsedPort = 0; void collectPortTokens(String raw, {required bool explicitRange}) { for (final rawTok in raw.split(RegExp(r'[,;\s]+'))) { final tok = rawTok.trim(); if (tok.isEmpty) continue; final norm = tok.replaceAll(':', '-'); final m = RegExp(r'^(\d+)-(\d+)$').firstMatch(norm); if (m != null) { final lo = int.tryParse(m.group(1)!); final hi = int.tryParse(m.group(2)!); if (lo != null && hi != null && lo >= 1 && hi <= 65535 && lo <= hi) { final spec = lo == hi ? '$lo' : '$lo-$hi'; if (lo != hi || explicitRange) hasExplicitRange = true; if (seenRanges.add(spec)) portRanges.add(spec); if (firstParsedPort == 0) firstParsedPort = lo; } } else { final p = int.tryParse(norm); if (p != null && p >= 1 && p <= 65535) { if (explicitRange) hasExplicitRange = true; if (seenRanges.add('$p')) portRanges.add('$p'); if (firstParsedPort == 0) firstParsedPort = p; } } } } for (final key in const ['port', 'ports', 'port-range', 'server_ports']) { final rawVal = (query[key] ?? '').trim(); if (rawVal.isNotEmpty) { collectPortTokens(rawVal, explicitRange: key != 'port'); } } var port = uri.port; final List serverPorts; if (portRanges.isNotEmpty) { if (portRanges.length == 1 && !hasExplicitRange && int.tryParse(portRanges.first) != null) { final qp = int.parse(portRanges.first); if (port == 0 || port == qp || uri.scheme.toLowerCase() == 'mierus') { port = qp; serverPorts = const []; } else { serverPorts = portRanges; } } else { if (port == 0) port = firstParsedPort; serverPorts = portRanges; } } else if (port > 0) { serverPorts = const []; } else { return null; } final rawTr = (query['protocol'] ?? query['transport'] ?? query['network'] ?? 'TCP').trim().toUpperCase(); final transport = rawTr == 'UDP' ? 'UDP' : 'TCP'; final profileName = (query['profile'] ?? '').trim(); final mux = (query['multiplexing'] ?? query['mux'] ?? '').trim(); final trafficPattern = (query['traffic-pattern'] ?? query['traffic_pattern'] ?? '').trim(); final handshakeMode = (query['handshake-mode'] ?? query['handshake_mode'] ?? '').trim(); final mtu = _toInt(query['mtu']); final fallbackName = profileName.isNotEmpty ? profileName : (serverPorts.isNotEmpty ? '$server:${serverPorts.first}' : '$server:$port'); return Profile( id: _id('mieru', '$server:$port:$username:$serverPorts'), name: _name(query, uri.fragment, fallbackName), type: 'mieru', server: server, port: port, password: password, subscriptionId: subscriptionId, extra: { 'username': username, 'mieru_transport': transport, if (serverPorts.isNotEmpty) 'server_ports': serverPorts, if (mux.isNotEmpty) 'multiplexing': mux, if (trafficPattern.isNotEmpty) 'traffic_pattern': trafficPattern, if (handshakeMode.isNotEmpty) 'handshake_mode': handshakeMode, if (mtu != null && mtu >= 576 && mtu <= 9000) 'mtu': mtu, }, ); } Profile? _parseTewfai(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty || uri.userInfo.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; final (uuid, password) = _splitUserPass(uri.userInfo); if (uuid.isEmpty) return null; final trList = []; final rawTr = query['transport'] ?? ''; if (rawTr.isNotEmpty) { for (final item in _splitCsv(rawTr)) { var norm = item.toLowerCase(); if (norm == 'raw_tls' || norm == 'h2' || norm == 'ws') norm = 'tcp'; if (norm == 'h3_stream' || norm == 'h3_datagram') norm = 'quic'; if ((norm == 'tcp' || norm == 'quic') && !trList.contains(norm)) { trList.add(norm); } } } final rotateSecs = int.tryParse(query['rotate_secs'] ?? ''); final rotateMb = int.tryParse(query['rotate_mb'] ?? ''); final mtu = int.tryParse(query['mtu'] ?? ''); final extra = { if (trList.isNotEmpty) 'tewfai_transport': trList, if ((query['inner_cipher'] ?? '').isNotEmpty) 'inner_cipher': query['inner_cipher'], if ((query['morph_profile'] ?? '').isNotEmpty) 'morph_profile': query['morph_profile'], if (_isTruthy(query['pqc'])) 'pqc': true, if (_isTruthy(query['handover'])) 'handover': { 'enabled': true, if (rotateSecs != null && rotateSecs > 0) 'rotate_secs': rotateSecs, if (rotateMb != null && rotateMb > 0) 'rotate_mb': rotateMb, }, if (_isTruthy(query['fec'])) 'fec': true, if (_isTruthy(query['desync'])) 'desync': true, if (_isTruthy(query['shaper'])) 'shaper': { 'mode': const {'fixed', 'off'}.contains(query['shaper_profile']) ? query['shaper_profile'] : 'adaptive', }, if (mtu != null && mtu >= 256) 'mtu': mtu, if ((query['fp'] ?? query['fingerprint'] ?? '').isNotEmpty) 'fp': query['fp'] ?? query['fingerprint'], }; _extractCertPin(extra, query['pcs'] ?? query['pinSHA256']); return Profile( id: _id('tewfai', '$server:$port:$uuid:$password'), name: _name(query, uri.fragment, '$server:$port'), type: 'tewfai', server: server, port: port, uuid: uuid, password: password, transport: trList.contains('quic') ? 'quic' : 'tcp', sni: query['sni'] ?? query['peer'] ?? server, alpn: _splitCsv(query['alpn']), allowInsecure: _isTruthy(query['insecure'] ?? query['allowInsecure']), publicKey: query['pbk'] ?? '', shortId: query['sid'] ?? '', subscriptionId: subscriptionId, extra: extra, ); } Profile? _parseMasque(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; final privKey = Uri.decodeComponent( (query['private_key'] ?? '').isNotEmpty ? query['private_key']! : (!uri.userInfo.contains(':') ? uri.userInfo : ''), ); final pubKey = query['public_key'] ?? query['peer_public_key'] ?? ''; final profileType = (query['profile'] ?? '').isNotEmpty ? query['profile']! : (privKey.isNotEmpty && pubKey.isNotEmpty ? 'cloudflare' : 'standard'); final hostFmt = server.contains(':') ? '[$server]' : server; final masqueUri = (query['uri'] ?? '').isNotEmpty ? query['uri']! : (profileType == 'standard' ? 'https://$hostFmt:$port/.well-known/masque/ip/*/*/' : ''); final extra = { 'profile': profileType, if (privKey.isNotEmpty) 'private_key': privKey, if (pubKey.isNotEmpty) 'public_key': pubKey, if ((query['vhttp'] ?? '').isNotEmpty) 'vhttp': query['vhttp'], if ((query['ip'] ?? query['address'] ?? '').isNotEmpty) 'ip': query['ip'] ?? query['address'], if ((query['ipv6'] ?? '').isNotEmpty) 'ipv6': query['ipv6'], if (masqueUri.isNotEmpty) 'uri': masqueUri, if (_toInt(query['mtu']) != null) 'mtu': _toInt(query['mtu']), }; _extractCertPin(extra, query['pcs'] ?? query['pinSHA256']); return Profile( id: _id('masque', '$server:$port:$privKey:$profileType'), name: _name(query, uri.fragment, 'MASQUE $server:$port'), type: 'masque', server: server, port: port, password: privKey, publicKey: pubKey, sni: query['sni'] ?? server, allowInsecure: _isTruthy(query['insecure'] ?? query['allowInsecure']), subscriptionId: subscriptionId, extra: extra, ); } Profile? _parseTrustTunnel(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; String username = ''; String password = ''; if (uri.userInfo.contains(':')) { final split = _splitUserPass(uri.userInfo); username = split.$1; password = split.$2; } else if (uri.userInfo.isNotEmpty) { password = Uri.decodeComponent(uri.userInfo); } final extra = { if (username.isNotEmpty) 'username': username, if (_isTruthy(query['quic'])) 'quic': true, if ((query['quic_congestion_control'] ?? query['cc'] ?? '').isNotEmpty) 'quic_congestion_control': query['quic_congestion_control'] ?? query['cc'], }; _extractCertPin(extra, query['pcs'] ?? query['pinSHA256']); return Profile( id: _id('trusttunnel', '$server:$port:$username:$password'), name: _name(query, uri.fragment, '$server:$port'), type: 'trusttunnel', server: server, port: port, password: password, sni: query['sni'] ?? server, allowInsecure: _isTruthy(query['insecure'] ?? query['allowInsecure']), subscriptionId: subscriptionId, extra: extra, ); } Profile? _parseOpenConnect(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; var username = ''; var password = ''; var cookie = (query['cookie'] ?? '').trim(); if (uri.userInfo.isNotEmpty) { if (uri.userInfo.contains(':')) { final split = _splitUserPass(uri.userInfo); username = split.$1; password = split.$2; } else if (cookie.isEmpty) { cookie = Uri.decodeComponent(uri.userInfo); } else { username = Uri.decodeComponent(uri.userInfo); } } final extra = { 'flavor': query['flavor'] ?? 'anyconnect', if (username.isNotEmpty) 'username': username, if (cookie.isNotEmpty) 'cookie': cookie, if ((query['urlpath'] ?? '').isNotEmpty) 'urlpath': query['urlpath'], if ((query['realm'] ?? '').isNotEmpty) 'realm': query['realm'], if ((query['region'] ?? '').isNotEmpty) 'region': query['region'], if ((query['user_agent'] ?? '').isNotEmpty) 'user_agent': query['user_agent'], if ((query['local_hostname'] ?? '').isNotEmpty) 'local_hostname': query['local_hostname'], if ((query['server_cert_pin'] ?? '').isNotEmpty) 'server_cert_pin': query['server_cert_pin'], if ((query['address'] ?? '').isNotEmpty) 'address': query['address']!.split('/').first, if ((query['address_v6'] ?? '').isNotEmpty) 'address_v6': query['address_v6']!.split('/').first, if (_isTruthy(query['ip'])) 'ip': true, if (_isTruthy(query['esp'])) 'esp': true, if (_isTruthy(query['dtls'])) 'dtls': true, if (_isTruthy(query['hip_report'])) 'hip_report': true, if ((query['mtu'] ?? '').isNotEmpty) 'mtu': query['mtu'], }; _extractCertPin(extra, query['pcs'] ?? query['pinSHA256']); return Profile( id: _id('openconnect', '$server:$port:$username:$cookie'), name: _name(query, uri.fragment, '$server:$port'), type: 'openconnect', server: server, port: port, password: password, sni: query['sni'] ?? server, allowInsecure: _isTruthy(query['insecure'] ?? query['allowInsecure']), subscriptionId: subscriptionId, extra: extra, ); } Profile? _parseSudoku(String link, {String subscriptionId = ''}) { final uri = Uri.tryParse(link); if (uri == null || uri.host.isEmpty) return null; final query = uri.queryParameters; final server = _cleanHost(uri.host); final port = uri.port == 0 ? 443 : uri.port; final userInfoKey = uri.userInfo.isNotEmpty ? Uri.decodeComponent(uri.userInfo) : ''; final uuid = userInfoKey.isNotEmpty ? userInfoKey : (query['uuid'] ?? query['key'] ?? query['password'] ?? ''); final password = query['password'] ?? ''; final extra = { if ((query['table_type'] ?? query['table-type'] ?? '').isNotEmpty) 'table_type': query['table_type'] ?? query['table-type'], if ((query['custom_table'] ?? query['custom-table'] ?? '').isNotEmpty) 'custom_table': query['custom_table'] ?? query['custom-table'], if ((query['custom_tables'] ?? query['custom-tables'] ?? '').isNotEmpty) 'custom_tables': _splitCsv(query['custom_tables'] ?? query['custom-tables']), if (query.containsKey('enable_pure_downlink')) 'enable_pure_downlink': _isTruthy(query['enable_pure_downlink']), if (query.containsKey('disable_http_mask')) 'disable_http_mask': _isTruthy(query['disable_http_mask']), for (final s in [ 'http_mask_mode', 'http_mask_host', 'http_mask_path_root', 'http_mask_multiplex', ]) if ((query[s] ?? '').isNotEmpty) s: query[s], if ((query['fp'] ?? query['fingerprint'] ?? '').isNotEmpty) 'fp': normalizeUtlsFingerprint(query['fp'] ?? query['fingerprint'] ?? ''), if ((query['security'] ?? '').isNotEmpty) 'security': query['security'], }; _extractCertPin(extra, query['pcs'] ?? query['pinSHA256']); return Profile( id: _id('sudoku', '$server:$port:$uuid:$password'), name: _name(query, uri.fragment, '$server:$port'), type: 'sudoku', server: server, port: port, uuid: uuid, password: password, sni: query['sni'] ?? '', alpn: _splitCsv(query['alpn']), allowInsecure: _isTruthy(query['insecure'] ?? query['allowInsecure']), subscriptionId: subscriptionId, extra: extra, ); } Profile? _parseWdtt(String link, {String subscriptionId = ''}) { final trimmed = link.trim(); final schemeSep = trimmed.indexOf('://'); if (schemeSep <= 0) return null; final scheme = trimmed.substring(0, schemeSep).toLowerCase(); final afterScheme = trimmed.substring(schemeSep + 3); final fragIdx = afterScheme.indexOf('#'); final body = (fragIdx >= 0 ? afterScheme.substring(0, fragIdx) : afterScheme).trim(); var fragTag = ''; if (fragIdx >= 0) { final rawFrag = afterScheme.substring(fragIdx + 1).trim(); try { fragTag = Uri.decodeComponent(rawFrag); } catch (_) { fragTag = rawFrag; } } Profile? buildWdtt({ required String server, required int port, required String pass, required String mode, required String name, int? rawPort, int? wgPort, int? localPort, String? obfs, String? did, List? hashes, int? workers, int? mtu, String? vkToken, String? vkAuthMode, String? vkCookie, List? vkClientIds, String? srtpMode, String? turnTransport, }) { final cleanSrv = _cleanHost(server); if (cleanSrv.isEmpty || pass.isEmpty) return null; final extra = { if (mode != 'wg') 'mode': mode, if (rawPort != null && rawPort > 0) 'raw_port': rawPort, if (wgPort != null && wgPort > 0) 'wg_port': wgPort, if (localPort != null && localPort > 0) 'local_port': localPort, if (obfs != null && const {'audio', 'video', 'unified'}.contains(obfs.toLowerCase())) 'obfs': obfs.toLowerCase(), if (did != null && did.isNotEmpty) 'device_id': did, if (hashes != null && hashes.isNotEmpty) 'hashes': hashes, if (workers != null && workers >= 1 && workers <= 126) 'workers': workers, if (mtu != null && mtu >= 576 && mtu <= 9000) 'mtu': mtu, if (vkToken != null && vkToken.isNotEmpty) 'vk_token': vkToken, if (vkAuthMode != null && vkAuthMode.isNotEmpty) 'vk_auth_mode': vkAuthMode, if (vkCookie != null && vkCookie.isNotEmpty) 'vk_cookie': vkCookie, if (vkClientIds != null && vkClientIds.isNotEmpty) 'vk_client_ids': vkClientIds, if (srtpMode != null && srtpMode.isNotEmpty) 'srtp_mode': srtpMode, if (turnTransport != null && turnTransport.isNotEmpty) 'turn_transport': turnTransport, }; return Profile( id: _id('wdtt', '$cleanSrv:$port:$pass:$mode'), name: name.isNotEmpty ? name : 'wdtt-$cleanSrv:$port', type: 'wdtt', server: cleanSrv, port: port, password: pass, transport: 'udp', subscriptionId: subscriptionId, extra: extra, ); } // 1. csqtt://connect?... if (scheme == 'csqtt' && body.toLowerCase().startsWith('connect?')) { final params = _parseQuerySafe(body.substring(8)); final server = params['host'] ?? params['ip'] ?? params['server'] ?? ''; final pass = params['password'] ?? params['pass'] ?? params['id'] ?? ''; final port = _toInt(params['peer'] ?? params['port'] ?? params['dtls']) ?? 46000; final rawMode = (params['vpn'] ?? params['mode'] ?? '').trim().toLowerCase(); final mode = const {'csqtt', 'csqtt_v1', 'wg', 'raw'}.contains(rawMode) ? rawMode : (params['v'] == '1' ? 'csqtt_v1' : 'csqtt'); final tag = fragTag.isNotEmpty ? fragTag : (params['name'] ?? '').trim(); return buildWdtt( server: server, port: port, pass: pass, mode: mode, name: tag, obfs: params['obfs'], did: params['did'] ?? params['device_id'], hashes: _splitWdttHashes(params['hashes'] ?? params['hash']), workers: _toInt(params['workers'] ?? params['streams']), vkToken: params['token'] ?? params['vk_token'], vkAuthMode: params['vk_auth_mode'], vkCookie: params['vk_cookie'], vkClientIds: _splitCsv(params['vk_client_ids']), srtpMode: params['srtp_mode'] ?? params['srtp'], turnTransport: params['turn_transport'], ); } // 2. qwdtt://config?ip=...&dtls=...&hashes=...&pass= if (scheme == 'qwdtt') { final qIdx = body.indexOf('?'); final qRaw = qIdx >= 0 ? body.substring(qIdx + 1) : body; final passIdx = qRaw.indexOf('&pass='); String headQ; String? rawTailPass; if (passIdx >= 0) { headQ = qRaw.substring(0, passIdx); rawTailPass = qRaw.substring(passIdx + 6); } else if (qRaw.startsWith('pass=')) { headQ = ''; rawTailPass = qRaw.substring(5); } else { headQ = qRaw; } final params = _parseQuerySafe(headQ).map((k, v) => MapEntry(k.toLowerCase(), v)); if (rawTailPass != null) { try { params['pass'] = Uri.decodeComponent(rawTailPass); } catch (_) { params['pass'] = rawTailPass; } } final rawMode = (params['mode'] ?? params['vpn'] ?? '').trim().toLowerCase(); final mode = const {'raw', 'csqtt', 'csqtt_v1'}.contains(rawMode) ? rawMode : 'wg'; final dtlsPort = _toInt(params['dtls'] ?? params['port']) ?? (mode == 'csqtt' || mode == 'csqtt_v1' ? 46000 : 56000); final rawPort = _toInt(params['raw']); final serverPort = mode == 'raw' ? (rawPort ?? dtlsPort) : dtlsPort; return buildWdtt( server: params['ip'] ?? params['server'] ?? params['host'] ?? '', port: serverPort, pass: params['pass'] ?? params['password'] ?? '', mode: mode, name: fragTag.isNotEmpty ? fragTag : (params['name'] ?? '').trim(), rawPort: rawPort, obfs: params['obfs'], hashes: _splitWdttHashes(params['hashes'] ?? params['hash']), workers: _toInt(params['streams'] ?? params['workers']), srtpMode: params['srtp_mode'] ?? params['srtp'], ); } // 3. Base64 JSON format: wdtt://eyJ... / csqtt://eyJ... final b64Candidate = body.replaceAll(RegExp(r'/+$'), ''); if (!b64Candidate.contains(':') && !b64Candidate.contains('?') && !b64Candidate.contains('@')) { final norm = _normalizeBase64(b64Candidate); if (norm.isNotEmpty) { try { final decoded = utf8.decode(base64.decode(norm)); final obj = jsonDecode(decoded); if (obj is Map) { final jmap = Map.from(obj); final rawMode = (jmap['vpn'] ?? jmap['mode'] ?? '').toString().trim().toLowerCase(); final mode = scheme == 'csqtt' ? (rawMode == 'csqtt_v1' ? 'csqtt_v1' : 'csqtt') : (const {'raw', 'csqtt', 'csqtt_v1'}.contains(rawMode) ? rawMode : 'wg'); final dtlsPort = _toInt(jmap['dtls'] ?? jmap['port']) ?? (mode == 'csqtt' || mode == 'csqtt_v1' ? 46000 : 56000); final rawPort = _toInt(jmap['raw']); final serverPort = mode == 'raw' ? (rawPort ?? (dtlsPort + 3)) : dtlsPort; final hRaw = jmap['hash'] ?? jmap['hashes']; final hashes = hRaw is List ? hRaw.expand((e) => _splitWdttHashes(e.toString())).toList() : _splitWdttHashes(hRaw?.toString()); final p = buildWdtt( server: (jmap['ip'] ?? jmap['add'] ?? jmap['server'] ?? '').toString(), port: serverPort, pass: (jmap['pass'] ?? jmap['id'] ?? jmap['password'] ?? '').toString(), mode: mode, name: fragTag.isNotEmpty ? fragTag : (jmap['name'] ?? jmap['ps'] ?? '').toString().trim(), rawPort: rawPort, obfs: jmap['obfs']?.toString(), did: (jmap['did'] ?? jmap['device_id'])?.toString(), hashes: hashes, workers: _toInt(jmap['workers'] ?? jmap['streams']), srtpMode: (jmap['srtp_mode'] ?? jmap['srtp'])?.toString(), ); if (p != null) return p; } } catch (_) {} } } // 4. Colon format: wdtt://host:dtls_port:wg_port:local_port:pass:hash1,hash2#remark if (!body.contains('?') && !body.contains('@')) { String hostPart = body; String? tail; if (body.startsWith('[')) { final close = body.indexOf(']:'); if (close > 1) { hostPart = body.substring(1, close).trim(); tail = body.substring(close + 2); } } else { final colon = body.indexOf(':'); if (colon > 0) { hostPart = body.substring(0, colon).trim(); tail = body.substring(colon + 1); } } if (tail != null) { final parts = _splitFirstN(tail, ':', 5); if (parts.length == 5) { final dtlsPort = _toInt(parts[0]); if (dtlsPort != null && dtlsPort > 0) { final passRaw = parts[3].trim(); final pass = Uri.decodeComponent(passRaw); final hashesRaw = Uri.decodeComponent(parts[4].trim()); return buildWdtt( server: hostPart, port: dtlsPort, pass: pass, mode: scheme == 'csqtt' ? 'csqtt' : 'wg', name: fragTag, wgPort: _toInt(parts[1]), localPort: _toInt(parts[2]), hashes: _splitWdttHashes(hashesRaw), ); } } } } // 5. Standard URI: wdtt://[pass@]host[:port]?pass=...&hashes=...&mode=...#remark final normalized = trimmed.replaceFirst(RegExp(r'^(wdtt|csqtt)://', caseSensitive: false), 'https://'); final uri = Uri.tryParse(normalized); if (uri == null || uri.host.isEmpty) return null; final query = uri.queryParameters; final defaultPort = scheme == 'csqtt' ? 46000 : 56000; final port = uri.port == 0 ? defaultPort : uri.port; final pass = uri.userInfo.isNotEmpty ? Uri.decodeComponent(uri.userInfo) : (query['pass'] ?? query['password'] ?? query['id'] ?? ''); final rawMode = (query['mode'] ?? query['vpn'] ?? '').trim().toLowerCase(); final mode = scheme == 'csqtt' ? (rawMode == 'csqtt_v1' ? 'csqtt_v1' : 'csqtt') : (const {'raw', 'csqtt', 'csqtt_v1'}.contains(rawMode) ? rawMode : 'wg'); return buildWdtt( server: uri.host, port: port, pass: pass, mode: mode, name: _name(query, uri.fragment, ''), rawPort: _toInt(query['raw_port'] ?? query['raw']), obfs: query['obfs'], did: query['did'] ?? query['device_id'], hashes: _splitWdttHashes(query['hashes'] ?? query['hash']), workers: _toInt(query['workers'] ?? query['streams']), mtu: _toInt(query['mtu']), vkToken: query['token'] ?? query['vk_token'], vkAuthMode: query['vk_auth_mode'], vkCookie: query['vk_cookie'], vkClientIds: _splitCsv(query['vk_client_ids']), srtpMode: query['srtp_mode'] ?? query['srtp'], turnTransport: query['turn_transport'], ); } List _parseAmneziaVpnContainer(String raw, {String subscriptionId = ''}) { var clean = raw.trim().replaceFirst( RegExp(r'^(vpn|awg|amneziawg|wg|wireguard)://', caseSensitive: false), '', ); var frag = ''; final hashIdx = clean.indexOf('#'); if (hashIdx >= 0) { try { frag = Uri.decodeComponent(clean.substring(hashIdx + 1).trim()); } catch (_) { frag = clean.substring(hashIdx + 1).trim(); } clean = clean.substring(0, hashIdx).trim(); } final norm = _normalizeBase64(clean); if (norm.isEmpty) return const []; List bytes; try { bytes = base64.decode(norm); } catch (_) { return const []; } if (bytes.isEmpty) return const []; String? decoded; try { final s = utf8.decode(bytes).trim(); if (s.startsWith('{') || s.startsWith('[') || looksLikeWireGuardConf(s)) { decoded = s; } } catch (_) {} String? tryDecodeBytes(List rawBytes, List Function(List) decoder) { try { return utf8.decode(decoder(rawBytes)); } catch (_) { return null; } } // Qt qCompress (4-byte big-endian size header + zlib) -> zlib -> gzip -> raw deflate decoded ??= (bytes.length > 6 ? tryDecodeBytes(bytes.sublist(4), zlib.decode) : null); decoded ??= tryDecodeBytes(bytes, zlib.decode); decoded ??= tryDecodeBytes(bytes, gzip.decode); decoded ??= tryDecodeBytes(bytes, ZLibCodec(raw: true).decode); decoded ??= (bytes.length > 4 ? tryDecodeBytes(bytes.sublist(4), ZLibCodec(raw: true).decode) : null); if (decoded == null || decoded.trim().isEmpty) return const []; final parsed = parseMany(decoded, subscriptionId: subscriptionId); if (parsed.isEmpty) return const []; if (frag.isNotEmpty && parsed.length == 1) { final p = parsed.first; if (p.name == '${p.server}:${p.port}' || p.name.startsWith('amnezia') || p.name == 'proxy') { return [ Profile.fromJson({...p.toJson(), 'name': frag}), ]; } } return parsed; } Profile? _parseSurgeOrQxLine(String line, {String subscriptionId = ''}) { final trimmed = line.trim(); if (trimmed.isEmpty || trimmed.startsWith('#') || trimmed.startsWith(';') || trimmed.startsWith('[') || trimmed.contains('://')) { return null; } final eqIdx = trimmed.indexOf('='); if (eqIdx <= 0) return null; final lhs = trimmed.substring(0, eqIdx).trim(); final rhs = trimmed.substring(eqIdx + 1).trim(); if (lhs.isEmpty || rhs.isEmpty) return null; final parts = rhs.split(',').map((s) => s.trim()).toList(); if (parts.isEmpty) return null; final lhsLower = lhs.toLowerCase(); final isQx = const {'shadowsocks', 'ss', 'vmess', 'trojan', 'vless', 'http', 'socks5'} .contains(lhsLower) && parts[0].contains(':'); final clashMap = {}; late final String protoType; late final List kvSlice; if (isQx) { final hp = _splitHostPort(parts[0], 0); if (hp == null || hp.$1.isEmpty || hp.$2 <= 0) return null; protoType = lhsLower == 'shadowsocks' ? 'ss' : lhsLower; clashMap['type'] = protoType; clashMap['server'] = hp.$1; clashMap['port'] = hp.$2; kvSlice = parts.sublist(1); } else { if (parts.length < 3) return null; final rawProto = parts[0].toLowerCase(); final mappedProto = switch (rawProto) { 'ss' || 'shadowsocks' || 'custom' => 'ss', 'vmess' => 'vmess', 'vless' => 'vless', 'trojan' => 'trojan', 'hysteria2' || 'hy2' => 'hysteria2', 'tuic' || 'tuic-v5' => 'tuic', 'socks5' || 'socks5-tls' => 'socks5', 'http' || 'https' => 'http', 'anytls' => 'anytls', _ => '', }; if (mappedProto.isEmpty) return null; final host = _cleanHost(parts[1]); final port = _toInt(parts[2]); if (host.isEmpty || port == null || port <= 0) return null; protoType = mappedProto; clashMap['name'] = lhs; clashMap['type'] = mappedProto; clashMap['server'] = host; clashMap['port'] = port; if (rawProto == 'https' || rawProto == 'socks5-tls') { clashMap['tls'] = true; } var startIdx = 3; if ((mappedProto == 'socks5' || mappedProto == 'http') && parts.length >= 5 && !parts[3].contains('=') && !parts[4].contains('=')) { clashMap['username'] = parts[3]; clashMap['password'] = parts[4]; startIdx = 5; } kvSlice = parts.sublist(startIdx); } final wsOpts = {}; final wsHeaders = {}; String? ssObfsMode; String? ssObfsHost; String? ssObfsPath; var ssObfsTls = false; for (final item in kvSlice) { final sep = item.indexOf('='); if (sep <= 0) continue; final k = item.substring(0, sep).trim().toLowerCase(); final v = _unquoteYaml(item.substring(sep + 1).trim()); switch (k) { case 'tag': clashMap['name'] = v; case 'encrypt-method': case 'method': case 'cipher': if (protoType == 'ss' || (protoType == 'vmess' && !clashMap.containsKey('cipher'))) { clashMap['cipher'] = v; } case 'password': case 'passwd': if (protoType == 'vmess' || protoType == 'vless') { clashMap['uuid'] = v; } else { clashMap['password'] = v; } case 'username': case 'user': if (protoType == 'vmess' || protoType == 'vless') { clashMap['uuid'] = v; } else { clashMap['username'] = v; } case 'uuid': case 'id': clashMap['uuid'] = v; case 'ws': if (_isTruthy(v)) { if (protoType == 'ss') { ssObfsMode = 'ws'; } else { clashMap['network'] = 'ws'; } } case 'obfs': final vl = v.toLowerCase(); if (protoType == 'ss') { if (const {'http', 'tls', 'ws', 'wss'}.contains(vl)) { if (vl == 'wss') ssObfsTls = true; ssObfsMode = vl; } } else if (vl == 'ws' || vl == 'wss') { clashMap['network'] = 'ws'; if (vl == 'wss') clashMap['tls'] = true; } else if (vl == 'over-tls') { clashMap['tls'] = true; } case 'ws-path': case 'obfs-uri': case 'path': wsOpts['path'] = v; ssObfsPath = v; case 'obfs-host': wsHeaders['Host'] = v; ssObfsHost = v; clashMap.putIfAbsent('sni', () => v); case 'ws-headers': for (final pair in v.split('|')) { final c = pair.indexOf(':'); if (c > 0) { wsHeaders[pair.substring(0, c).trim()] = pair.substring(c + 1).trim(); } } case 'tls': case 'over-tls': final enabled = _isTruthy(v); if (protoType == 'ss' && enabled) ssObfsTls = true; clashMap['tls'] = enabled; case 'sni': case 'tls-host': case 'servername': clashMap['sni'] = v; case 'skip-cert-verify': case 'tls-verification': clashMap['skip-cert-verify'] = k == 'tls-verification' ? (v == 'false' || v == '0' || v == 'no') : _isTruthy(v); case 'alpn': final arr = v .split(RegExp(r'[,|]')) .map((s) => s.trim()) .where((s) => s.isNotEmpty) .toList(); if (arr.isNotEmpty) clashMap['alpn'] = arr; case 'token': clashMap['token'] = v; case 'flow': clashMap['flow'] = v; } } if (protoType == 'ss' && ssObfsMode != null) { if (ssObfsMode == 'http' || ssObfsMode == 'tls') { clashMap['plugin'] = 'obfs-local'; clashMap['plugin-opts'] = { 'mode': ssObfsMode, 'host': ?ssObfsHost, }; } else { clashMap['plugin'] = 'v2ray-plugin'; clashMap['plugin-opts'] = { 'mode': 'websocket', if (ssObfsTls) 'tls': true, 'host': ?ssObfsHost, 'path': ?ssObfsPath, }; } } if (wsHeaders.isNotEmpty) wsOpts['headers'] = wsHeaders; if (wsOpts.isNotEmpty) clashMap['ws-opts'] = wsOpts; clashMap.putIfAbsent('name', () => '${clashMap['server']}:${clashMap['port']}'); final outbound = _convertClashProxy(0, clashMap); if (outbound == null) return null; return _outboundMapToProfile(outbound, subscriptionId: subscriptionId); } // --------------------------------------------------------------------------- // Мультиформатный JSON: sing-box, Xray/V2Ray, SIP008 // --------------------------------------------------------------------------- List _parseJsonPayload(String raw, {String subscriptionId = ''}) { try { final decoded = jsonDecode(raw); final outbounds = _extractOutboundsFromJson(decoded); final profiles = []; for (final item in outbounds) { final p = _outboundMapToProfile(item, subscriptionId: subscriptionId); if (p != null) profiles.add(p); } if (profiles.isNotEmpty) return profiles; if (decoded is Map) { const strKeys = [ 'data', 'result', 'response', 'payload', 'subscription', 'config', 'awg_config', 'wg_config', 'links', 'url', ]; for (final k in strKeys) { final v = decoded[k]; if (v is String && v.trim().isNotEmpty) { final nested = parseSubscription(v, subscriptionId: subscriptionId); if (nested.isNotEmpty) return nested; } else if (v is List) { for (final item in v) { if (item is String && item.trim().isNotEmpty) { profiles.addAll(parseSubscription(item, subscriptionId: subscriptionId)); } } if (profiles.isNotEmpty) return profiles; } } } return profiles; } catch (_) { return const []; } } List> _extractOutboundsFromJson(Object? val, {int depth = 0}) { if (depth > 4) return const []; if (val is List) { final res = >[]; for (var i = 0; i < val.length; i++) { final item = val[i]; if (item is! Map) continue; final map = Map.from(item); if (map.containsKey('protocol')) { res.addAll(_convertXrayOutbound(i, map)); } else if (map['type'] is String && !map.containsKey('server_port') && map.containsKey('port') && map['server'] is String) { final converted = _convertClashProxy(i, map); if (converted != null) res.add(converted); } else if (map['type'] is String) { final ty = (map['type'] as String).toLowerCase(); if (!const {'direct', 'block', 'dns', 'selector', 'urltest'}.contains(ty)) { res.add(map); } } else if (map['outbounds'] is List || map['endpoints'] is List) { res.addAll(_extractFromFullConfigObject(i, map)); } else { final nested = _extractOutboundsFromJson(map, depth: depth + 1); if (nested.isNotEmpty) res.addAll(nested); } } return res; } if (val is Map) { final map = Map.from(val); // SIP008: {"version": 1, "servers": [...]} if (map['servers'] is List) { final servers = map['servers'] as List; final res = >[]; for (var i = 0; i < servers.length; i++) { final s = servers[i]; if (s is! Map) continue; final srv = Map.from(s); if (srv['server'] != null && srv['password'] != null) { res.add({ 'type': 'shadowsocks', 'tag': srv['remarks'] ?? srv['id'] ?? 'ss-${i + 1}', 'server': srv['server'].toString(), 'server_port': _toInt(srv['server_port']) ?? 8388, 'method': (srv['method'] ?? 'aes-256-gcm').toString(), 'password': srv['password'].toString(), if (srv['plugin'] != null && srv['plugin'].toString().isNotEmpty) 'plugin': srv['plugin'].toString(), if (srv['plugin_opts'] != null && srv['plugin_opts'].toString().isNotEmpty) 'plugin_opts': srv['plugin_opts'].toString(), }); } } if (res.isNotEmpty) return res; } if (map['outbounds'] is List || map['endpoints'] is List) { return _extractFromFullConfigObject(0, map); } if (map.containsKey('type') || map.containsKey('protocol')) { return _extractOutboundsFromJson([map], depth: depth + 1); } const wrapperKeys = [ 'data', 'result', 'response', 'payload', 'configs', 'nodes', 'proxies', 'items', 'subscription', 'config', ]; for (final k in wrapperKeys) { final v = map[k]; if (v is Map || v is List) { final res = _extractOutboundsFromJson(v, depth: depth + 1); if (res.isNotEmpty) return res; } } } return const []; } /// Разбирает полный объект конфигурации Xray / sing-box (в т.ч. элемент массива из 40 конфигов), /// сохраняя верхнеуровневое имя (`remarks`/`name`/`ps`) и цепочки прокси (`dialerProxy` / `detour`). List> _extractFromFullConfigObject( int cfgIdx, Map cfgMap, ) { final cfgRemarks = (cfgMap['remarks'] ?? cfgMap['name'] ?? cfgMap['ps'] ?? (cfgMap['meta'] is Map ? (cfgMap['meta'] as Map)['name'] : null) ?? '') .toString() .trim(); final rawList = [ if (cfgMap['outbounds'] is List) ...(cfgMap['outbounds'] as List), if (cfgMap['endpoints'] is List) ...(cfgMap['endpoints'] as List), ]; final nodes = >[]; for (var i = 0; i < rawList.length; i++) { final item = rawList[i]; if (item is! Map) continue; final map = Map.from(item); if (map.containsKey('protocol')) { nodes.addAll(_convertXrayOutbound(i, map)); } else if (map['type'] is String) { final ty = (map['type'] as String).toLowerCase(); if (!const {'direct', 'block', 'dns', 'selector', 'urltest'}.contains(ty)) { nodes.add(map); } } } if (nodes.isEmpty) return const []; final byTag = >{}; for (final n in nodes) { final t = (n['tag'] ?? '').toString().trim(); if (t.isNotEmpty) byTag[t] = n; } final detourTargets = {}; for (final n in nodes) { final dt = (n['detour'] ?? '').toString().trim(); if (dt.isNotEmpty && byTag.containsKey(dt)) { detourTargets.add(dt); } } // Если в конфиге есть цепочка (один прокси ссылается через dialerProxy/detour на другой), // отделяем входные прокси от транзитных узлов цепочки. final entryNodes = detourTargets.isNotEmpty && nodes.length > detourTargets.length ? nodes .where((n) => !detourTargets.contains((n['tag'] ?? '').toString().trim())) .toList() : nodes; final result = >[]; for (var i = 0; i < entryNodes.length; i++) { final mainNode = Map.from(entryNodes[i]); final firstHopTag = (mainNode['detour'] ?? '').toString().trim(); if (firstHopTag.isNotEmpty && byTag.containsKey(firstHopTag)) { final chainOutbounds = >[]; final visited = {}; var currTag = firstHopTag; while (currTag.isNotEmpty && byTag.containsKey(currTag) && visited.add(currTag)) { final hop = Map.from(byTag[currTag]!); final renamedCurr = 'chain-${cfgIdx + 1}-$currTag'; hop['tag'] = renamedCurr; final nextHopTag = (hop['detour'] ?? '').toString().trim(); if (nextHopTag.isNotEmpty && byTag.containsKey(nextHopTag)) { hop['detour'] = 'chain-${cfgIdx + 1}-$nextHopTag'; currTag = nextHopTag; } else { hop.remove('detour'); currTag = ''; } chainOutbounds.add(hop); } if (chainOutbounds.isNotEmpty) { mainNode['detour'] = 'chain-${cfgIdx + 1}-$firstHopTag'; mainNode['chain_outbounds'] = chainOutbounds; } else { mainNode.remove('detour'); } } else if (firstHopTag.isNotEmpty) { // Не оставляем висячий detour на отфильтрованный direct/freedom или отсутствующий узел. mainNode.remove('detour'); } if (cfgRemarks.isNotEmpty) { final origTag = (mainNode['tag'] ?? '').toString().trim(); mainNode['tag'] = entryNodes.length == 1 ? cfgRemarks : (origTag.isEmpty || origTag == 'proxy' || origTag == 'out' || origTag.startsWith('xray-') ? '$cfgRemarks #${i + 1}' : '$cfgRemarks ($origTag)'); } result.add(mainNode); } return result; } /// Конвертирует outbound формата Xray/V2Ray (`protocol` + `settings` + `streamSettings`) /// в канонический outbound `tachyon-core`. List> _convertXrayOutbound(int idx, Map obj) { final proto = (obj['protocol'] ?? '').toString().toLowerCase(); if (const {'freedom', 'blackhole', 'dns', 'loopback', 'dokodemo-door'}.contains(proto)) { return const []; } final baseTag = (obj['tag'] ?? '').toString().trim(); final tag = baseTag.isNotEmpty ? baseTag : 'xray-$proto-${idx + 1}'; final settings = obj['settings'] is Map ? Map.from(obj['settings'] as Map) : {}; final stream = obj['streamSettings'] is Map ? Map.from(obj['streamSettings'] as Map) : null; final sockopt = stream?['sockopt'] is Map ? Map.from(stream!['sockopt'] as Map) : null; final proxySettings = obj['proxySettings'] is Map ? Map.from(obj['proxySettings'] as Map) : null; final dialerProxy = (sockopt?['dialerProxy'] ?? sockopt?['dialer_proxy'] ?? proxySettings?['tag'] ?? proxySettings?['Tag'] ?? obj['detour'] ?? '') .toString() .trim(); final out = >[]; void attachStream(Map node, String server) { if (dialerProxy.isNotEmpty) { node['detour'] = dialerProxy; } if (stream == null) return; final tls = _convertXrayTls(stream, server); if (tls != null) node['tls'] = tls; final tr = _convertXrayTransport(stream); if (tr != null) node['transport'] = tr; } switch (proto) { case 'vless': final vnext = settings['vnext'] is List ? settings['vnext'] as List : const []; for (final v in vnext) { if (v is! Map) continue; final addr = (v['address'] ?? '').toString(); final port = _toInt(v['port']) ?? 443; final users = v['users'] is List ? v['users'] as List : const []; for (final u in users) { if (u is! Map) continue; final uuid = (u['id'] ?? '').toString(); if (addr.isEmpty || uuid.isEmpty) continue; final node = { 'type': 'vless', 'tag': tag, 'server': addr, 'server_port': port, 'uuid': uuid, if ((u['flow'] ?? '').toString().isNotEmpty && (u['flow'] ?? '').toString().toLowerCase() != 'none') 'flow': u['flow'].toString(), if ((u['encryption'] ?? 'none').toString().isNotEmpty && (u['encryption'] ?? 'none').toString().toLowerCase() != 'none') 'encryption': u['encryption'].toString(), }; attachStream(node, addr); out.add(node); } } case 'vmess': final vnext = settings['vnext'] is List ? settings['vnext'] as List : const []; for (final v in vnext) { if (v is! Map) continue; final addr = (v['address'] ?? '').toString(); final port = _toInt(v['port']) ?? 443; final users = v['users'] is List ? v['users'] as List : const []; for (final u in users) { if (u is! Map) continue; final uuid = (u['id'] ?? '').toString(); if (addr.isEmpty || uuid.isEmpty) continue; final node = { 'type': 'vmess', 'tag': tag, 'server': addr, 'server_port': port, 'uuid': uuid, 'security': (u['security'] ?? 'auto').toString(), if ((_toInt(u['alterId']) ?? 0) > 0) 'alter_id': _toInt(u['alterId']), }; attachStream(node, addr); out.add(node); } } case 'trojan': final servers = settings['servers'] is List ? settings['servers'] as List : const []; for (final s in servers) { if (s is! Map) continue; final addr = (s['address'] ?? '').toString(); final port = _toInt(s['port']) ?? 443; final pass = (s['password'] ?? '').toString(); if (addr.isEmpty || pass.isEmpty) continue; final node = { 'type': 'trojan', 'tag': tag, 'server': addr, 'server_port': port, 'password': pass, }; attachStream(node, addr); out.add(node); } case 'shadowsocks': final servers = settings['servers'] is List ? settings['servers'] as List : const []; for (final s in servers) { if (s is! Map) continue; final addr = (s['address'] ?? '').toString(); final port = _toInt(s['port']) ?? 8388; final method = (s['method'] ?? s['cipher'] ?? 'aes-256-gcm').toString(); final pass = (s['password'] ?? '').toString(); if (addr.isEmpty) continue; final node = { 'type': 'shadowsocks', 'tag': tag, 'server': addr, 'server_port': port, 'method': method, 'password': pass, if (s['uot'] == true) 'udp_over_tcp': true, }; if (dialerProxy.isNotEmpty) node['detour'] = dialerProxy; out.add(node); } case 'socks': case 'socks5': final servers = settings['servers'] is List ? settings['servers'] as List : (settings['address'] != null ? [settings] : const []); for (final s in servers) { if (s is! Map) continue; final addr = (s['address'] ?? '').toString(); final port = _toInt(s['port']) ?? 1080; if (addr.isEmpty) continue; final users = s['users'] is List ? s['users'] as List : const []; final u = users.isNotEmpty && users.first is Map ? users.first as Map : null; final user = (u?['user'] ?? s['user'] ?? s['username'] ?? '').toString(); final pass = (u?['pass'] ?? s['pass'] ?? s['password'] ?? '').toString(); final node = { 'type': 'socks', 'tag': tag, 'server': addr, 'server_port': port, if (user.isNotEmpty) 'username': user, if (pass.isNotEmpty) 'password': pass, if (dialerProxy.isNotEmpty) 'detour': dialerProxy, }; out.add(node); } case 'http': final servers = settings['servers'] is List ? settings['servers'] as List : (settings['address'] != null ? [settings] : const []); for (final s in servers) { if (s is! Map) continue; final addr = (s['address'] ?? '').toString(); final port = _toInt(s['port']) ?? 8080; if (addr.isEmpty) continue; final users = s['users'] is List ? s['users'] as List : const []; final u = users.isNotEmpty && users.first is Map ? users.first as Map : null; final user = (u?['user'] ?? s['user'] ?? s['username'] ?? '').toString(); final pass = (u?['pass'] ?? s['pass'] ?? s['password'] ?? '').toString(); final node = { 'type': 'http', 'tag': tag, 'server': addr, 'server_port': port, if (user.isNotEmpty) 'username': user, if (pass.isNotEmpty) 'password': pass, }; attachStream(node, addr); out.add(node); } case 'hysteria2': case 'hy2': final servers = settings['servers'] is List ? settings['servers'] as List : (settings['address'] != null ? [settings] : const []); for (final s in servers) { if (s is! Map) continue; final addr = (s['address'] ?? '').toString(); final port = _toInt(s['port']) ?? 443; final pass = (s['password'] ?? settings['password'] ?? settings['auth'] ?? '').toString(); if (addr.isEmpty) continue; final node = { 'type': 'hysteria2', 'tag': tag, 'server': addr, 'server_port': port, 'password': pass, }; final fm = stream?['finalmask']; if (fm is Map && fm['udp'] is List) { for (final u in fm['udp'] as List) { if (u is! Map) continue; final uTy = (u['type'] ?? '').toString().toLowerCase(); final us = u['settings'] is Map ? u['settings'] as Map : const {}; final obfsPw = (us['password'] ?? '').toString(); final ps = us['packetSize'] ?? us['packet_size']; final r = ps != null ? _parsePacketSizeRange(ps.toString()) : null; if (uTy == 'gecko' || r != null) { node['obfs'] = { 'type': 'gecko', if (obfsPw.isNotEmpty) 'password': obfsPw, if (r != null) ...{ 'min_packet_size': r.$1, 'max_packet_size': r.$2, }, }; } else if (uTy == 'salamander' && obfsPw.isNotEmpty) { node['obfs'] = {'type': 'salamander', 'password': obfsPw}; } } } attachStream(node, addr); out.add(node); } case 'wireguard': final peers = settings['peers'] is List ? settings['peers'] as List : const []; final peer = peers.isNotEmpty && peers.first is Map ? peers.first as Map : null; final privKey = (settings['secretKey'] ?? settings['privateKey'] ?? '').toString(); final pubKey = (peer?['publicKey'] ?? '').toString(); final endpoint = (peer?['endpoint'] ?? '').toString(); final hp = _splitHostPort(endpoint, 51820); if (privKey.isNotEmpty && pubKey.isNotEmpty && hp != null) { final node = { 'type': 'wireguard', 'tag': tag, 'server': hp.$1, 'server_port': hp.$2, 'private_key': privKey, 'peer_public_key': pubKey, 'local_address': settings['address'] is List ? (settings['address'] as List).map((e) => _formatWgCidr(e.toString())).toList() : ['10.0.0.2/32'], if (_toInt(settings['mtu']) != null) 'mtu': _toInt(settings['mtu']), if (dialerProxy.isNotEmpty) 'detour': dialerProxy, }; _extractAwgParams(settings, node); out.add(node); } } return out; } Map? _convertXrayTls(Map stream, String defaultServer) { final sec = (stream['security'] ?? 'none').toString().toLowerCase(); if (sec == 'tls' || sec == 'xtls') { final ts = stream['tlsSettings'] is Map ? stream['tlsSettings'] as Map : const {}; final sni = (ts['serverName'] ?? defaultServer).toString(); final fp = normalizeUtlsFingerprint((ts['fingerprint'] ?? '').toString()); return { 'enabled': true, if (sni.isNotEmpty) 'server_name': sni, if (ts['allowInsecure'] == true) 'insecure': true, if (ts['alpn'] is List) 'alpn': ts['alpn'], if (fp.isNotEmpty) 'utls': {'enabled': true, 'fingerprint': fp}, }; } if (sec == 'reality') { final rs = stream['realitySettings'] is Map ? stream['realitySettings'] as Map : const {}; final sni = (rs['serverName'] ?? defaultServer).toString(); final rawFp = (rs['fingerprint'] ?? '').toString(); final fp = rawFp.isEmpty ? 'chrome' : normalizeUtlsFingerprint(rawFp); final sid = normalizeRealityShortId((rs['shortId'] ?? '').toString()); return { 'enabled': true, if (sni.isNotEmpty) 'server_name': sni, 'utls': {'enabled': true, 'fingerprint': fp}, 'reality': { 'enabled': true, 'public_key': (rs['publicKey'] ?? '').toString().trim(), if (sid.isNotEmpty) 'short_id': sid, }, }; } return null; } Map? _convertXrayTransport(Map stream) { final net = _normalizeTransportName((stream['network'] ?? 'tcp').toString()); switch (net) { case 'ws': final ws = stream['wsSettings'] is Map ? stream['wsSettings'] as Map : const {}; return { 'type': 'ws', if ((ws['path'] ?? '').toString().isNotEmpty) 'path': ws['path'].toString(), if (ws['headers'] is Map) 'headers': ws['headers'], }; case 'grpc': final gs = stream['grpcSettings'] is Map ? stream['grpcSettings'] as Map : const {}; return { 'type': 'grpc', if ((gs['serviceName'] ?? '').toString().isNotEmpty) 'service_name': gs['serviceName'].toString(), }; case 'httpupgrade': final hs = stream['httpupgradeSettings'] is Map ? stream['httpupgradeSettings'] as Map : const {}; return { 'type': 'httpupgrade', if ((hs['path'] ?? '').toString().isNotEmpty) 'path': hs['path'].toString(), if ((hs['host'] ?? '').toString().isNotEmpty) 'host': hs['host'].toString(), }; case 'http': final hs = stream['httpSettings'] is Map ? stream['httpSettings'] as Map : const {}; return { 'type': 'http', if ((hs['path'] ?? '').toString().isNotEmpty) 'path': hs['path'].toString(), if (hs['host'] != null) 'host': hs['host'], }; case 'xhttp': final xs = (stream['xhttpSettings'] ?? stream['splithttpSettings']) is Map ? Map.from((stream['xhttpSettings'] ?? stream['splithttpSettings']) as Map) : {}; xs['type'] = 'xhttp'; if (xs['extra'] is Map) { xs.addAll(Map.from(xs.remove('extra') as Map)); } return normalizeXhttpTransport(xs); default: return null; } } // --------------------------------------------------------------------------- // Clash / Mihomo YAML (proxies:) // --------------------------------------------------------------------------- List _parseClashYaml(String yamlText, {String subscriptionId = ''}) { final entries = >[]; var inProxies = false; var proxiesIndent = 0; List<(int, String)>? currentBlock; var blockIndent = 0; void flushBlock() { if (currentBlock != null && currentBlock!.isNotEmpty) { final map = _parseYamlBlockLines(currentBlock!); if (map.isNotEmpty) entries.add(map); } currentBlock = null; } for (final rawLine in yamlText.split(RegExp(r'[\r\n]+'))) { final lineNoComment = _stripYamlComment(rawLine); final trimmed = lineNoComment.trim(); if (trimmed.isEmpty) continue; final indent = lineNoComment.length - lineNoComment.trimLeft().length; if (!inProxies) { if (trimmed == 'proxies:' || trimmed.startsWith('proxies:')) { inProxies = true; proxiesIndent = indent; } continue; } if (indent <= proxiesIndent && !trimmed.startsWith('-')) { flushBlock(); break; } if (trimmed.startsWith('-')) { final afterDash = trimmed.substring(1).trim(); if (currentBlock != null && indent > blockIndent) { currentBlock!.add((indent, trimmed)); continue; } flushBlock(); if (afterDash.startsWith('{') && afterDash.endsWith('}')) { final flowMap = _parseYamlFlowMap(afterDash); if (flowMap.isNotEmpty) entries.add(flowMap); } else { blockIndent = indent; currentBlock = []; if (afterDash.isNotEmpty) { currentBlock!.add((indent + 2, afterDash)); } } } else if (currentBlock != null) { currentBlock!.add((indent, trimmed)); } } flushBlock(); final convertedNodes = >[]; for (var i = 0; i < entries.length; i++) { final outbound = _convertClashProxy(i, entries[i]); if (outbound != null) convertedNodes.add(outbound); } final byTag = >{ for (final n in convertedNodes) if ((n['tag'] ?? '').toString().trim().isNotEmpty) (n['tag'] as String).trim(): n, }; final out = []; for (var i = 0; i < convertedNodes.length; i++) { final node = Map.from(convertedNodes[i]); final firstHop = (node['detour'] ?? '').toString().trim(); if (firstHop.isNotEmpty) { if (byTag.containsKey(firstHop)) { final chain = >[]; final visited = {}; var curr = firstHop; while (curr.isNotEmpty && byTag.containsKey(curr) && visited.add(curr)) { final hop = Map.from(byTag[curr]!); hop['tag'] = 'clash-chain-${i + 1}-$curr'; final nextHop = (hop['detour'] ?? '').toString().trim(); if (nextHop.isNotEmpty && byTag.containsKey(nextHop)) { hop['detour'] = 'clash-chain-${i + 1}-$nextHop'; curr = nextHop; } else { hop.remove('detour'); curr = ''; } chain.add(hop); } if (chain.isNotEmpty) { node['detour'] = 'clash-chain-${i + 1}-$firstHop'; node['chain_outbounds'] = chain; } else { node.remove('detour'); } } else { node.remove('detour'); } } final profile = _outboundMapToProfile(node, subscriptionId: subscriptionId); if (profile != null) out.add(profile); } return out; } String _stripYamlComment(String line) { var inSingle = false; var inDouble = false; var prevSpace = true; for (var i = 0; i < line.length; i++) { final ch = line[i]; if (ch == '\\' && inDouble) { i++; continue; } if (ch == "'" && !inDouble) inSingle = !inSingle; if (ch == '"' && !inSingle) inDouble = !inDouble; if (ch == '#' && !inSingle && !inDouble && prevSpace) { return line.substring(0, i); } prevSpace = ch == ' ' || ch == '\t'; } return line; } Map _parseYamlFlowMap(String text) { final inner = text.trim(); if (!inner.startsWith('{') || !inner.endsWith('}')) return const {}; final body = inner.substring(1, inner.length - 1); final map = {}; for (final part in _splitYamlTopLevel(body, ',')) { final kv = _splitYamlKeyVal(part); if (kv == null) continue; map[_unquoteYaml(kv.$1)] = _parseYamlValue(kv.$2); } return map; } Object? _parseYamlValue(String raw) { final s = raw.trim(); if (s.isEmpty) return null; if (s.startsWith('{') && s.endsWith('}')) return _parseYamlFlowMap(s); if (s.startsWith('[') && s.endsWith(']')) { final inner = s.substring(1, s.length - 1); return _splitYamlTopLevel(inner, ',') .map((e) => _parseYamlValue(e)) .where((e) => e != null) .toList(); } if ((s.startsWith('"') && s.endsWith('"')) || (s.startsWith("'") && s.endsWith("'"))) { return _unquoteYaml(s); } final lower = s.toLowerCase(); if (lower == 'true' || lower == 'yes') return true; if (lower == 'false' || lower == 'no') return false; final asInt = int.tryParse(s); if (asInt != null) return asInt; return s; } String _unquoteYaml(String raw) { final s = raw.trim(); if (s.length >= 2 && ((s.startsWith('"') && s.endsWith('"')) || (s.startsWith("'") && s.endsWith("'")))) { return s.substring(1, s.length - 1); } return s; } List _splitYamlTopLevel(String s, String sep) { final parts = []; var brace = 0; var bracket = 0; var inSingle = false; var inDouble = false; var start = 0; for (var i = 0; i < s.length; i++) { final ch = s[i]; if (ch == '\\' && inDouble) { i++; continue; } if (ch == "'" && !inDouble) { inSingle = !inSingle; } else if (ch == '"' && !inSingle) { inDouble = !inDouble; } else if (!inSingle && !inDouble) { if (ch == '{') brace++; if (ch == '}') brace--; if (ch == '[') bracket++; if (ch == ']') bracket--; if (ch == sep && brace == 0 && bracket == 0) { parts.add(s.substring(start, i).trim()); start = i + 1; } } } final tail = s.substring(start).trim(); if (tail.isNotEmpty) parts.add(tail); return parts; } (String, String)? _splitYamlKeyVal(String s) { var inSingle = false; var inDouble = false; for (var i = 0; i < s.length; i++) { final ch = s[i]; if (ch == '\\' && inDouble) { i++; continue; } if (ch == "'" && !inDouble) { inSingle = !inSingle; } else if (ch == '"' && !inSingle) { inDouble = !inDouble; } else if (ch == ':' && !inSingle && !inDouble) { if (i + 1 == s.length || s[i + 1] == ' ' || s[i + 1] == '\t') { return (s.substring(0, i).trim(), s.substring(i + 1).trim()); } } } return null; } Map _parseYamlBlockLines(List<(int, String)> lines) { final map = {}; var i = 0; while (i < lines.length) { final (indent, text) = lines[i]; final kv = _splitYamlKeyVal(text); if (kv == null) { i++; continue; } final key = _unquoteYaml(kv.$1); final val = kv.$2; if (val.isNotEmpty) { map[key] = _parseYamlValue(val); i++; } else { final children = <(int, String)>[]; var j = i + 1; while (j < lines.length && lines[j].$1 > indent) { children.add(lines[j]); j++; } if (children.isNotEmpty && children.first.$2.startsWith('-')) { final list = []; for (final (_, itemText) in children) { if (itemText.startsWith('-')) { final v = _parseYamlValue(itemText.substring(1)); if (v != null) list.add(v); } } map[key] = list; } else if (children.isNotEmpty) { map[key] = _parseYamlBlockLines(children); } i = j; } } return map; } Map? _convertClashProxy(int idx, Map p) { final ty = (p['type'] ?? '').toString().toLowerCase(); final server = _cleanHost((p['server'] ?? '').toString()); final port = _toInt(p['port']); if (ty.isEmpty || server.isEmpty || port == null) return null; final tag = (p['name'] ?? 'clash-$ty-${idx + 1}').toString(); Map? buildTls(bool force) { final tlsOn = force || p['tls'] == true || p['reality-opts'] is Map; if (!tlsOn) return null; final sni = (p['servername'] ?? p['sni'] ?? server).toString(); final fp = normalizeUtlsFingerprint( (p['client-fingerprint'] ?? p['fingerprint'] ?? '').toString(), ); final tls = { 'enabled': true, if (sni.isNotEmpty) 'server_name': sni, if (p['skip-cert-verify'] == true) 'insecure': true, if (p['alpn'] is List) 'alpn': p['alpn'], if (fp.isNotEmpty) 'utls': {'enabled': true, 'fingerprint': fp}, }; if (p['reality-opts'] is Map) { final ro = p['reality-opts'] as Map; final sid = normalizeRealityShortId((ro['short-id'] ?? '').toString()); tls['reality'] = { 'enabled': true, if (ro['public-key'] != null) 'public_key': ro['public-key'].toString().trim(), if (sid.isNotEmpty) 'short_id': sid, }; tls.putIfAbsent('utls', () => {'enabled': true, 'fingerprint': 'chrome'}); } return tls; } Map? buildTransport() { final net = _normalizeTransportName((p['network'] ?? 'tcp').toString()); switch (net) { case 'ws': final wso = p['ws-opts'] is Map ? p['ws-opts'] as Map : const {}; return { 'type': 'ws', if (wso['path'] != null) 'path': wso['path'].toString(), if (wso['headers'] is Map) 'headers': wso['headers'], }; case 'grpc': final go = p['grpc-opts'] is Map ? p['grpc-opts'] as Map : const {}; return { 'type': 'grpc', if (go['grpc-service-name'] != null) 'service_name': go['grpc-service-name'].toString(), }; case 'http': final ho = (p['h2-opts'] ?? p['http-opts']) is Map ? (p['h2-opts'] ?? p['http-opts']) as Map : const {}; return { 'type': 'http', if (ho['path'] != null) 'path': ho['path'] is List ? (ho['path'] as List).first.toString() : ho['path'].toString(), if (ho['host'] != null) 'host': ho['host'], }; case 'xhttp': final xo = (p['xhttp-opts'] ?? p['splithttp-opts']) is Map ? Map.from((p['xhttp-opts'] ?? p['splithttp-opts']) as Map) : {}; xo['type'] = 'xhttp'; return normalizeXhttpTransport(xo); default: return null; } } final tr = buildTransport(); Map? out; switch (ty) { case 'vless': final tls = buildTls(false); out = { 'type': 'vless', 'tag': tag, 'server': server, 'server_port': port, 'uuid': (p['uuid'] ?? '').toString(), if ((p['flow'] ?? '').toString().isNotEmpty) 'flow': p['flow'].toString(), 'tls': ?tls, 'transport': ?tr, }; case 'vmess': final tls = buildTls(false); out = { 'type': 'vmess', 'tag': tag, 'server': server, 'server_port': port, 'uuid': (p['uuid'] ?? '').toString(), 'security': (p['cipher'] ?? 'auto').toString(), 'tls': ?tls, 'transport': ?tr, }; case 'trojan': final tls = buildTls(true); out = { 'type': 'trojan', 'tag': tag, 'server': server, 'server_port': port, 'password': (p['password'] ?? '').toString(), 'tls': ?tls, 'transport': ?tr, }; case 'ss': case 'shadowsocks': String? ssPlugin = p['plugin']?.toString().trim(); String? ssPluginOpts; final rawPluginOpts = p['plugin-opts'] ?? p['plugin_opts']; if (ssPlugin == 'obfs') ssPlugin = 'obfs-local'; if (rawPluginOpts is Map) { final optsMap = Map.from(rawPluginOpts); if (ssPlugin == 'obfs-local' || ssPlugin == 'simple-obfs') { final parts = [ if (optsMap['mode'] != null) 'obfs=${optsMap['mode']}', if (optsMap['host'] != null) 'obfs-host=${optsMap['host']}', ]; ssPluginOpts = parts.join(';'); } else { final parts = [ if (optsMap['mode'] != null) 'mode=${optsMap['mode']}', if (optsMap['tls'] == true) 'tls', if (optsMap['host'] != null) 'host=${optsMap['host']}', if (optsMap['path'] != null) 'path=${optsMap['path']}', ]; ssPluginOpts = parts.join(';'); } } else if (rawPluginOpts is String && rawPluginOpts.isNotEmpty) { ssPluginOpts = rawPluginOpts; } out = { 'type': 'shadowsocks', 'tag': tag, 'server': server, 'server_port': port, 'method': (p['cipher'] ?? p['method'] ?? 'aes-256-gcm').toString(), 'password': (p['password'] ?? '').toString(), if (ssPlugin != null && ssPlugin.isNotEmpty) 'plugin': ssPlugin, if (ssPluginOpts != null && ssPluginOpts.isNotEmpty) 'plugin_opts': ssPluginOpts, if (p['udp-over-tcp'] == true || p['uot'] == true) 'udp_over_tcp': true, }; case 'hysteria2': case 'hy2': final tls = buildTls(true); final rawHy2Obfs = (p['obfs'] ?? '').toString().trim().toLowerCase(); final hy2ObfsPw = (p['obfs-password'] ?? p['obfs_password'] ?? '').toString(); var minPkt = _toInt(p['min-packet-size'] ?? p['min_packet_size']); var maxPkt = _toInt(p['max-packet-size'] ?? p['max_packet-size']); final psVal = p['packet-size'] ?? p['packet_size']; if (psVal != null) { final r = _parsePacketSizeRange(psVal.toString()); if (r != null) { minPkt ??= r.$1; maxPkt ??= r.$2; } } Map? hy2Obfs; if (rawHy2Obfs == 'gecko' || minPkt != null || maxPkt != null) { hy2Obfs = { 'type': 'gecko', if (hy2ObfsPw.isNotEmpty) 'password': hy2ObfsPw, if (minPkt != null && minPkt > 0) 'min_packet_size': minPkt, if (maxPkt != null && maxPkt > 0) 'max_packet_size': maxPkt, }; } else if (rawHy2Obfs == 'salamander') { hy2Obfs = {'type': 'salamander', 'password': hy2ObfsPw}; } final hy2PortsRaw = (p['ports'] ?? p['mport'] ?? '').toString().trim(); final hy2Ports = hy2PortsRaw.isEmpty ? const [] : hy2PortsRaw .split(',') .map((s) => s.trim().replaceAll('-', ':')) .where((s) => s.isNotEmpty) .toList(); final hy2Hop = _normalizeDurationString(p['hop-interval'] ?? p['hop_interval']); final hy2Up = _parseMbps(p['up']); final hy2Down = _parseMbps(p['down']); out = { 'type': 'hysteria2', 'tag': tag, 'server': server, 'server_port': port, 'password': (p['password'] ?? '').toString(), 'obfs': ?hy2Obfs, if (hy2Ports.isNotEmpty) 'server_ports': hy2Ports, 'hop_interval': ?hy2Hop, if (hy2Up != null && hy2Up > 0) 'up_mbps': hy2Up, if (hy2Down != null && hy2Down > 0) 'down_mbps': hy2Down, 'tls': ?tls, }; case 'hysteria': case 'hy': final tls = buildTls(true); final hyUp = _parseMbps(p['up']); final hyDown = _parseMbps(p['down']); out = { 'type': 'hysteria', 'tag': tag, 'server': server, 'server_port': port, if ((p['auth-str'] ?? p['auth_str'] ?? p['auth'] ?? '').toString().isNotEmpty) 'auth_str': (p['auth-str'] ?? p['auth_str'] ?? p['auth']).toString(), if ((p['obfs'] ?? '').toString().isNotEmpty) 'obfs': p['obfs'].toString(), if (hyUp != null && hyUp > 0) 'up_mbps': hyUp, if (hyDown != null && hyDown > 0) 'down_mbps': hyDown, 'tls': ?tls, }; case 'tuic': final tls = buildTls(true); final token = p['token']?.toString(); final uuid = (p['uuid'] ?? token ?? '').toString(); final pw = (p['password'] ?? token ?? '').toString(); final tuicHb = _normalizeDurationString( p['heartbeat-interval'] ?? p['heartbeat_interval'] ?? p['heartbeat'], treatLargeIntsAsMillis: p.containsKey('heartbeat-interval'), ); out = { 'type': 'tuic', 'tag': tag, 'server': server, 'server_port': port, 'uuid': uuid, 'password': pw, if ((p['congestion-controller'] ?? p['congestion_control'] ?? '').toString().isNotEmpty) 'congestion_control': (p['congestion-controller'] ?? p['congestion_control']).toString(), if (p['udp-relay-mode'] != null) 'udp_relay_mode': p['udp-relay-mode'].toString(), if (p['reduce-rtt'] == true || p['fast-open'] == true || p['zero-rtt-handshake'] == true) 'zero_rtt_handshake': true, if (p['udp-over-stream'] == true || p['udp_over_stream'] == true) 'udp_over_stream': true, 'heartbeat': ?tuicHb, 'tls': ?tls, }; case 'wireguard': case 'amneziawg': case 'awg': final addrs = []; if ((p['ip'] ?? '').toString().isNotEmpty) { addrs.add(_formatWgCidr(p['ip'].toString())); } if ((p['ipv6'] ?? '').toString().isNotEmpty) { addrs.add(_formatWgCidr(p['ipv6'].toString())); } final node = { 'type': 'wireguard', 'tag': tag, 'server': server, 'server_port': port, 'private_key': (p['private-key'] ?? p['private_key'] ?? '').toString(), 'peer_public_key': (p['public-key'] ?? p['public_key'] ?? '').toString(), 'local_address': addrs.isEmpty ? ['10.0.0.2/32'] : addrs, if (_toInt(p['mtu']) != null) 'mtu': _toInt(p['mtu']), if ((p['pre-shared-key'] ?? p['preshared-key'] ?? p['pre_shared_key'] ?? '') .toString() .isNotEmpty) 'pre_shared_key': (p['pre-shared-key'] ?? p['preshared-key'] ?? p['pre_shared_key']).toString(), if (p['reserved'] != null) 'reserved': p['reserved'], }; if (p['amnezia-wg-option'] is Map) { _extractAwgParams(Map.from(p['amnezia-wg-option'] as Map), node); } _extractAwgParams(p, node); out = node; case 'anytls': final tls = buildTls(true); out = { 'type': 'anytls', 'tag': tag, 'server': server, 'server_port': port, 'password': (p['password'] ?? '').toString(), 'tls': ?tls, }; case 'socks5': case 'socks': final tls = buildTls(false); out = { 'type': 'socks', 'tag': tag, 'server': server, 'server_port': port, if (p['username'] != null) 'username': p['username'].toString(), if (p['password'] != null) 'password': p['password'].toString(), 'tls': ?tls, }; case 'http': case 'https': final tls = buildTls(ty == 'https'); out = { 'type': 'http', 'tag': tag, 'server': server, 'server_port': port, if (p['username'] != null) 'username': p['username'].toString(), if (p['password'] != null) 'password': p['password'].toString(), 'tls': ?tls, }; case 'shadow-tls': case 'shadowtls': final tls = buildTls(true); out = { 'type': 'shadowtls', 'tag': tag, 'server': server, 'server_port': port, 'version': _toInt(p['version']) ?? 3, if (p['password'] != null) 'password': p['password'].toString(), 'tls': ?tls, }; case 'mieru': final mTr = (p['transport'] ?? 'TCP').toString().toUpperCase(); final pr = (p['port-range'] ?? p['ports'] ?? '').toString().trim(); final mieruMtu = _toInt(p['mtu']); out = { 'type': 'mieru', 'tag': tag, 'server': server, if (pr.isEmpty || _toInt(p['port']) != null) 'server_port': port, 'username': (p['username'] ?? '').toString(), 'password': (p['password'] ?? '').toString(), 'transport': mTr == 'UDP' ? 'UDP' : 'TCP', if (pr.isNotEmpty) 'server_ports': pr .split(RegExp(r'[,;\s]+')) .map((e) => e.trim().replaceAll(':', '-')) .where((e) => e.isNotEmpty) .toList(), if ((p['multiplexing'] ?? '').toString().trim().isNotEmpty) 'multiplexing': p['multiplexing'].toString().trim(), if ((p['traffic-pattern'] ?? p['traffic_pattern'] ?? '').toString().trim().isNotEmpty) 'traffic_pattern': (p['traffic-pattern'] ?? p['traffic_pattern']).toString().trim(), if ((p['handshake-mode'] ?? p['handshake_mode'] ?? '').toString().trim().isNotEmpty) 'handshake_mode': (p['handshake-mode'] ?? p['handshake_mode']).toString().trim(), if (mieruMtu != null && mieruMtu >= 576 && mieruMtu <= 9000) 'mtu': mieruMtu, }; case 'tg': case 'mtproto': case 'telegram': out = { 'type': 'mtproto', 'tag': tag, 'server': server, 'server_port': port, 'secret': (p['secret'] ?? '').toString(), }; case 'tewfai': final tls = buildTls(true); out = { 'type': 'tewfai', 'tag': tag, 'server': server, 'server_port': port, 'uuid': (p['uuid'] ?? '').toString(), if (p['password'] != null) 'password': p['password'].toString(), if (p['pqc'] == true) 'pqc': true, if (p['fec'] == true) 'fec': true, if (p['desync'] == true) 'desync': true, 'tls': ?tls, }; case 'masque': final tls = buildTls(true); out = { 'type': 'masque', 'tag': tag, 'server': server, 'server_port': port, if (p['profile'] != null) 'profile': p['profile'].toString(), if (p['vhttp'] != null) 'vhttp': p['vhttp'].toString(), if ((p['private-key'] ?? p['private_key']) != null) 'private_key': (p['private-key'] ?? p['private_key']).toString(), if ((p['public-key'] ?? p['public_key']) != null) 'public_key': (p['public-key'] ?? p['public_key']).toString(), if (p['ip'] != null) 'ip': p['ip'].toString(), if (p['ipv6'] != null) 'ipv6': p['ipv6'].toString(), 'tls': ?tls, }; case 'trusttunnel': final tls = buildTls(true); out = { 'type': 'trusttunnel', 'tag': tag, 'server': server, 'server_port': port, if (p['username'] != null) 'username': p['username'].toString(), if (p['password'] != null) 'password': p['password'].toString(), 'tls': ?tls, }; case 'snell': final ver = _toInt(p['version']) ?? 5; final obfsOpts = p['obfs-opts'] is Map ? p['obfs-opts'] as Map : const {}; final obfsMode = (obfsOpts['mode'] ?? p['obfs-opts.mode'] ?? p['obfs'] ?? '') .toString() .trim() .toLowerCase(); final obfsHost = (obfsOpts['host'] ?? p['obfs-opts.host'] ?? p['obfs-host'] ?? '').toString().trim(); out = { 'type': 'snell', 'tag': tag, 'server': server, 'server_port': port, 'psk': (p['psk'] ?? p['password'] ?? '').toString(), 'version': (ver >= 4 && ver <= 6) ? ver : 5, if ((obfsMode == 'http' || obfsMode == 'tls') && ver != 6) 'obfs': { 'mode': obfsMode, if (obfsHost.isNotEmpty) 'host': obfsHost, }, if (p['uot'] == true || p['udp-over-tcp'] == true || p['udp_over_tcp'] == true) 'udp_over_tcp': true, if (p['reuse'] == true) 'reuse': true, }; case 'sudoku': final sKey = (p['key'] ?? p['password'] ?? '').toString(); final hMode = (p['http-mask-mode'] ?? p['http_mask_mode'])?.toString(); final hHost = (p['http-mask-host'] ?? p['http_mask_host'])?.toString(); final hPath = (p['http-mask-path'] ?? p['http_mask_path'] ?? p['path'])?.toString(); final hmBool = p['http-mask'] ?? p['http_mask']; out = { 'type': 'sudoku', 'tag': tag, 'server': server, 'server_port': port, if (sKey.isNotEmpty) 'key': sKey, if ((p['aead-method'] ?? p['aead_method'] ?? p['aead']) != null) 'aead': (p['aead-method'] ?? p['aead_method'] ?? p['aead']).toString(), if (_toInt(p['padding-min'] ?? p['padding_min']) != null) 'padding_min': _toInt(p['padding-min'] ?? p['padding_min']), if (_toInt(p['padding-max'] ?? p['padding_max']) != null) 'padding_max': _toInt(p['padding-max'] ?? p['padding_max']), if ((p['table-type'] ?? p['table_type'] ?? p['ascii']) != null) 'table_type': (p['table-type'] ?? p['table_type'] ?? p['ascii']).toString(), if (hmBool is bool) 'disable_http_mask': !hmBool, if (hMode != null || hHost != null || hPath != null) 'http_mask': { 'mode': ?hMode, 'host': ?hHost, 'path_root': ?hPath, }, }; case 'ssh': out = { 'type': 'ssh', 'tag': tag, 'server': server, 'server_port': port, 'user': (p['username'] ?? p['user'] ?? 'root').toString(), if (p['password'] != null) 'password': p['password'].toString(), if ((p['private-key'] ?? p['private_key']) != null) 'private_key': (p['private-key'] ?? p['private_key']).toString(), if ((p['client-version'] ?? p['client_version']) != null) 'client_version': (p['client-version'] ?? p['client_version']).toString(), }; case 'openconnect': case 'anyconnect': final tls = buildTls(true); out = { 'type': 'openconnect', 'tag': tag, 'server': server, 'server_port': port, 'flavor': (p['flavor'] ?? 'anyconnect').toString(), if (p['username'] != null) 'username': p['username'].toString(), if (p['password'] != null) 'password': p['password'].toString(), if (p['cookie'] != null) 'cookie': p['cookie'].toString(), 'tls': ?tls, }; case 'naive': final tls = buildTls(true); tls?.remove('insecure'); tls?.remove('disable_sni'); final isQuic = p['quic'] == true || _isTruthy(p['quic']?.toString()) || (p['network'] ?? '').toString().toLowerCase() == 'quic' || (p['network'] ?? '').toString().toLowerCase() == 'h3' || (p['network'] ?? '').toString().toLowerCase() == 'udp'; out = { 'type': 'naive', 'tag': tag, 'server': server, 'server_port': port, if (p['username'] != null) 'username': p['username'].toString(), if (p['password'] != null) 'password': p['password'].toString(), if (isQuic) 'quic': true, if (p['uot'] == true || p['udp-over-tcp'] == true || p['udp_over_tcp'] == true) 'udp_over_tcp': true, 'tls': ?tls, }; default: return null; } final detour = (p['dialer-proxy'] ?? p['detour'] ?? '').toString().trim(); if (detour.isNotEmpty && !const {'direct', 'reject', 'block'}.contains(detour.toLowerCase())) { out['detour'] = detour; } return out; } /// Превращает канонический JSON outbound `tachyon-core` в модель `Profile`. Profile? _outboundMapToProfile( Map item, { String subscriptionId = '', }) { final type = (item['type'] as String?)?.toLowerCase(); if (type == null || type.isEmpty) return null; var server = (item['server'] as String?) ?? ''; var port = (item['server_port'] as num?)?.toInt() ?? (type == 'openvpn-client' ? 1194 : 443); if (type == 'tailscale' && server.isEmpty) { final ctrl = (item['control_url'] ?? '').toString().trim(); final uri = ctrl.isNotEmpty ? Uri.tryParse(ctrl) : null; server = (uri != null && uri.host.isNotEmpty) ? uri.host : 'controlplane.tailscale.com'; } // Для WireGuard-эндпоинтов с массивом peers[] String peerPubFromPeers = ''; String pskFromPeers = ''; Object? keepaliveFromPeers; Object? reservedFromPeers; List? allowedIpsFromPeers; if (item['peers'] is List && (item['peers'] as List).isNotEmpty) { final peer = (item['peers'] as List).first; if (peer is Map) { if (server.isEmpty) { server = (peer['address'] ?? peer['server'] ?? '').toString(); } port = _toInt(peer['port'] ?? peer['server_port']) ?? 51820; peerPubFromPeers = (peer['public_key'] ?? '').toString(); pskFromPeers = (peer['pre_shared_key'] ?? '').toString(); keepaliveFromPeers = peer['persistent_keepalive_interval']; reservedFromPeers = peer['reserved']; if (peer['allowed_ips'] is List) { allowedIpsFromPeers = (peer['allowed_ips'] as List) .map((e) => e.toString().trim()) .where((s) => s.isNotEmpty) .toList(); } } } if (server.isEmpty && item['token'] == null && type != 'tailscale') return null; final rest = Map.from(item) ..remove('type') ..remove('tag') ..remove('server') ..remove('server_port') ..remove('peers'); if (type == 'openvpn-client') { if (rest['address'] is List && !rest.containsKey('local_address')) { rest['local_address'] = rest.remove('address'); } else if (rest['address'] is String && !rest.containsKey('local_address')) { rest['local_address'] = [rest.remove('address').toString()]; } } if (type == 'wireguard') { if (rest['address'] is List && !rest.containsKey('local_address')) { rest['local_address'] = rest.remove('address'); } else { rest.remove('address'); } if (peerPubFromPeers.isNotEmpty && !rest.containsKey('peer_public_key')) { rest['peer_public_key'] = peerPubFromPeers; } if (pskFromPeers.isNotEmpty && !rest.containsKey('pre_shared_key')) { rest['pre_shared_key'] = pskFromPeers; } if (keepaliveFromPeers != null && !rest.containsKey('persistent_keepalive_interval')) { rest['persistent_keepalive_interval'] = keepaliveFromPeers; } if (reservedFromPeers != null && !rest.containsKey('reserved')) { rest['reserved'] = reservedFromPeers; } final normReserved = normalizeWgReserved(rest['reserved']); if (normReserved != null) { rest['reserved'] = normReserved; } else { rest.remove('reserved'); } for (final cpsKey in const ['i1', 'i2', 'i3', 'i4', 'i5']) { if (rest[cpsKey] != null) { final norm = normalizeAwgCpsChain(rest[cpsKey].toString()); if (norm.isEmpty) { rest.remove(cpsKey); } else { rest[cpsKey] = norm; } } } if (allowedIpsFromPeers != null && allowedIpsFromPeers.isNotEmpty && !rest.containsKey('allowed_ips')) { rest['allowed_ips'] = allowedIpsFromPeers; } if (item['peers'] is List && (item['peers'] as List).isNotEmpty) { final peer = (item['peers'] as List).first; if (peer is Map) { if (peer['server_ports'] is List && !rest.containsKey('server_ports')) { rest['server_ports'] = peer['server_ports']; } if (peer['hop_interval'] != null && !rest.containsKey('hop_interval')) { rest['hop_interval'] = peer['hop_interval']; } if (peer['hop_interval_max'] != null && !rest.containsKey('hop_interval_max')) { rest['hop_interval_max'] = peer['hop_interval_max']; } } } } final rawTls = rest.remove('tls'); final tls = rawTls is Map ? Map.from(rawTls) : null; final reality = tls?['reality'] is Map ? Map.from(tls!['reality'] as Map) : null; final utls = tls?['utls'] is Map ? Map.from(tls!['utls'] as Map) : null; if (tls?['certificate_sha256'] is List) { rest['certificate_sha256'] = tls!['certificate_sha256']; } Map? transportMap; var transportType = 'tcp'; if (type == 'tewfai') { final rawTr = rest.remove('transport'); if (rawTr is List) { rest['tewfai_transport'] = rawTr.map((e) => e.toString()).toList(); if (rawTr.contains('quic')) transportType = 'quic'; } else if (rawTr is String && rawTr.isNotEmpty) { rest['tewfai_transport'] = [rawTr]; if (rawTr == 'quic') transportType = 'quic'; } } else if (type == 'naive') { final isQuic = rest['quic'] == true || rest['network']?.toString().toLowerCase() == 'udp'; if (isQuic) { rest['quic'] = true; transportType = 'quic'; } } else if (type == 'hysteria2' || type == 'hysteria' || type == 'tuic' || type == 'masque') { transportType = 'quic'; } else if (type == 'wireguard' || type == 'tailscale' || type == 'wdtt') { transportType = 'udp'; } else if (type == 'openvpn-client') { transportType = (rest['network'] ?? 'udp').toString().toLowerCase().contains('tcp') ? 'tcp' : 'udp'; } else if (type == 'mieru') { final rawTr = rest.remove('transport'); if (rawTr is String && rawTr.isNotEmpty) { rest['mieru_transport'] = rawTr.toUpperCase(); } } else if (type == 'mtproto') { final rawTr = rest.remove('transport'); if (rawTr is String && rawTr.isNotEmpty) { rest['mtproto_transport'] = rawTr.toLowerCase(); } } else if (rest['transport'] is Map) { transportMap = Map.from(rest['transport'] as Map); transportType = _normalizeTransportName((transportMap['type'] ?? 'tcp').toString()); if (transportType == 'xhttp') { rest['transport'] = normalizeXhttpTransport(transportMap); } } if (tls == null || tls['enabled'] == false) { rest['security'] = 'none'; } else if (reality?['enabled'] == true) { rest['security'] = 'reality'; } else { rest['security'] = 'tls'; } if (utls?['fingerprint'] != null) { final normFp = normalizeUtlsFingerprint(utls!['fingerprint'].toString()); if (normFp.isNotEmpty) { rest['fp'] = normFp; } } if (rest['chain_outbounds'] is List) { rest['chain_outbounds'] = (rest['chain_outbounds'] as List) .whereType() .map((h) => normalizeOutboundNode(Map.from(h))) .toList(growable: false); } final uuid = (rest.remove('uuid') ?? '').toString(); final password = (rest.remove('password') ?? rest['private_key'] ?? rest['psk'] ?? rest['secret'] ?? rest['static_key'] ?? rest['auth_key'] ?? '') .toString(); final method = (rest.remove('method') ?? '').toString(); final flow = normalizeVlessFlow((rest.remove('flow') ?? '').toString()); final pubKey = (reality?['public_key'] ?? rest['peer_public_key'] ?? rest['public_key'] ?? '') .toString() .trim(); final shortId = normalizeRealityShortId((reality?['short_id'] ?? '').toString()); final tag = (item['tag'] ?? '').toString().trim(); return Profile( id: _id(type, '$server:$port:$uuid:$password:$tag'), name: tag.isNotEmpty ? tag : '$server:$port', type: type, server: server, port: port, uuid: uuid, password: password, method: method, transport: transportType, sni: (tls?['server_name'] ?? '').toString(), alpn: ((tls?['alpn'] as List?) ?? const []) .map((e) => e.toString()) .toList(growable: false), allowInsecure: tls?['insecure'] == true, flow: flow, publicKey: pubKey, shortId: shortId, subscriptionId: subscriptionId, extra: rest, ); } // --------------------------------------------------------------------------- // Транспорты и вспомогательные функции // --------------------------------------------------------------------------- String _normalizeTransportName(String raw) { return switch (raw.trim().toLowerCase()) { 'ws' || 'websocket' => 'ws', 'grpc' || 'gun' => 'grpc', 'httpupgrade' || 'http-upgrade' => 'httpupgrade', 'h2' || 'http' || 'http2' => 'http', 'xhttp' || 'splithttp' || 'xh' => 'xhttp', 'quic' => 'quic', _ => 'tcp', }; } Map? _buildTransportFromQuery( String transportType, Map query, ) { if (transportType == 'tcp') return null; final tMap = {'type': transportType}; var path = query['path'] ?? ''; final host = query['host'] ?? ''; if (transportType == 'ws') { // Обработка ?ed=2048 внутри пути WebSocket (Early Data) if (path.contains('?ed=')) { final idx = path.indexOf('?ed='); final edStr = path.substring(idx + 4).split('&').first; path = path.substring(0, idx); final ed = int.tryParse(edStr); if (ed != null && ed > 0) { tMap['max_early_data'] = ed; tMap['early_data_header_name'] = 'Sec-WebSocket-Protocol'; } } final edParam = int.tryParse(query['ed'] ?? query['max_early_data'] ?? ''); if (edParam != null && edParam > 0) { tMap['max_early_data'] = edParam; tMap['early_data_header_name'] = query['early_data_header_name'] ?? 'Sec-WebSocket-Protocol'; } if (path.isNotEmpty) tMap['path'] = path; if (host.isNotEmpty) tMap['headers'] = {'Host': host}; return tMap; } if (transportType == 'grpc') { final sn = query['serviceName'] ?? query['service_name'] ?? path; if (sn.isNotEmpty) tMap['service_name'] = sn; return tMap; } if (transportType == 'http') { if (path.isNotEmpty) tMap['path'] = path; if (host.isNotEmpty) { tMap['host'] = host.split(',').map((s) => s.trim()).where((s) => s.isNotEmpty).toList(); } return tMap; } if (path.isNotEmpty) tMap['path'] = path; if (host.isNotEmpty) tMap['host'] = host; if ((query['mode'] ?? '').isNotEmpty) tMap['mode'] = query['mode']; if ((query['xPaddingBytes'] ?? query['x_padding_bytes'] ?? '').isNotEmpty) { tMap['x_padding_bytes'] = query['xPaddingBytes'] ?? query['x_padding_bytes']; } if (query.containsKey('extra')) { final rawExtra = query['extra']!; Map? decodedExtra; try { final parsed = jsonDecode(rawExtra); if (parsed is Map) decodedExtra = parsed; } catch (_) { try { final parsed = jsonDecode(Uri.decodeQueryComponent(rawExtra)); if (parsed is Map) decodedExtra = parsed; } catch (_) {} } if (decodedExtra != null) tMap.addAll(decodedExtra); } return transportType == 'xhttp' ? normalizeXhttpTransport(tMap) : tMap; } String _cleanHost(String host) { var h = host.trim(); if (h.startsWith('[') && h.endsWith(']')) { h = h.substring(1, h.length - 1); } return h; } (String, int)? _splitHostPort(String raw, int defaultPort) { final s = raw.trim(); if (s.isEmpty) return null; if (s.startsWith('[')) { final close = s.indexOf(']'); if (close <= 1) return null; final host = s.substring(1, close); final rest = s.substring(close + 1); final port = rest.startsWith(':') ? (int.tryParse(rest.substring(1)) ?? defaultPort) : defaultPort; return (host, port); } final colon = s.lastIndexOf(':'); if (colon > 0 && !s.substring(0, colon).contains(':')) { final host = s.substring(0, colon); final port = int.tryParse(s.substring(colon + 1)) ?? defaultPort; return (host, port); } return (s, defaultPort); } (String, String) _splitUserPass(String userInfo) { if (userInfo.isEmpty) return ('', ''); final sep = userInfo.indexOf(':'); if (sep < 0) return (Uri.decodeComponent(userInfo), ''); return ( Uri.decodeComponent(userInfo.substring(0, sep)), Uri.decodeComponent(userInfo.substring(sep + 1)), ); } Map _parseQuerySafe(String query, {bool plusAsSpace = true}) { if (query.isEmpty) return const {}; final out = {}; for (final pair in query.split('&')) { if (pair.isEmpty) continue; final eq = pair.indexOf('='); if (eq <= 0) continue; final kRaw = pair.substring(0, eq); final vRaw = pair.substring(eq + 1); String decodeVal(String s) { if (plusAsSpace) return Uri.decodeQueryComponent(s); try { return Uri.decodeComponent(s); } catch (_) { return s; } } out[decodeVal(kRaw)] = decodeVal(vRaw); } return out; } String _formatWgCidr(String addr) { final s = _cleanHost(addr); if (s.isEmpty) return ''; if (s.contains('/')) return s; return s.contains(':') ? '$s/128' : '$s/32'; } Object? _parseWgReserved(String? raw) => normalizeWgReserved(raw); List _splitCsv(String? raw) { if (raw == null || raw.trim().isEmpty) return const []; return raw.split(',').map((s) => s.trim()).where((s) => s.isNotEmpty).toList(growable: false); } List _splitWdttHashes(String? raw) { if (raw == null || raw.trim().isEmpty) return const []; return raw .split(RegExp(r'[,;\s+]+')) .map((s) => s.trim()) .where((s) => s.isNotEmpty) .toList(growable: false); } List _splitFirstN(String s, String sep, int n) { final res = []; var rest = s; for (var i = 0; i < n - 1; i++) { final idx = rest.indexOf(sep); if (idx < 0) return [s]; res.add(rest.substring(0, idx)); rest = rest.substring(idx + sep.length); } res.add(rest); return res; } (int, int)? _parsePacketSizeRange(String raw) { final s = raw.trim(); if (s.isEmpty) return null; final sep = s.indexOf('-'); if (sep >= 0) { final lo = int.tryParse(s.substring(0, sep).trim()); final hi = int.tryParse(s.substring(sep + 1).trim()); if (lo != null && hi != null && lo > 0 && hi >= lo) return (lo, hi); return null; } final single = int.tryParse(s); if (single != null && single > 0) return (single, single); return null; } String? _normalizeDurationString(Object? raw, {bool treatLargeIntsAsMillis = false}) { if (raw == null) return null; if (raw is num) { final v = raw.toInt(); if (v <= 0) return null; if (treatLargeIntsAsMillis) { final secs = v >= 1000 ? v ~/ 1000 : 1; return '${secs}s'; } return '${v}s'; } final s = raw.toString().trim(); if (s.isEmpty) return null; final asInt = int.tryParse(s); if (asInt != null) { if (asInt <= 0) return null; if (treatLargeIntsAsMillis) { final secs = asInt >= 1000 ? asInt ~/ 1000 : 1; return '${secs}s'; } return '${asInt}s'; } return s; } int? _parseMbps(Object? raw) { if (raw == null) return null; if (raw is num) return raw.toInt(); final m = RegExp(r'(\d+)').firstMatch(raw.toString()); return m == null ? null : int.tryParse(m.group(1)!); } bool _isTruthy(String? val) { if (val == null) return false; final lower = val.trim().toLowerCase(); return lower == '1' || lower == 'true' || lower == 'yes'; } int? _toInt(Object? val) { if (val is int) return val; if (val is num) return val.toInt(); if (val is String) return int.tryParse(val.trim()); return null; } String _name(Map query, String fragment, String fallback) { final remark = query['remarks'] ?? query['remark'] ?? query['name'] ?? query['description']; if (remark != null && remark.isNotEmpty) return remark; if (fragment.isNotEmpty) { try { return Uri.decodeComponent(fragment); } catch (_) { return fragment; } } return fallback; } String _id(String type, String seed) { var hash = 0; for (final unit in seed.codeUnits) { hash = (hash * 31 + unit) & 0x7fffffff; } return '$type-$hash'; } String _normalizeBase64(String value) { var cleaned = value .replaceAll(RegExp(r'[^A-Za-z0-9+/=_-]'), '') .replaceAll('-', '+') .replaceAll('_', '/'); final padding = cleaned.length % 4; if (padding == 1) return ''; if (padding != 0) { cleaned = cleaned.padRight(cleaned.length + (4 - padding), '='); } return cleaned; } void _extractCertPin(Map extra, String? rawPin) { if (rawPin == null || rawPin.trim().isEmpty) return; var pin = rawPin.trim().replaceFirst(RegExp(r'^sha256/', caseSensitive: false), ''); final hexCandidate = pin.replaceAll(':', ''); if (hexCandidate.length == 64 && RegExp(r'^[0-9a-fA-F]{64}$').hasMatch(hexCandidate)) { final bytes = []; for (var i = 0; i < 64; i += 2) { bytes.add(int.parse(hexCandidate.substring(i, i + 2), radix: 16)); } final b64 = base64.encode(bytes); extra['certificate_sha256'] = [b64]; extra['pcs'] = hexCandidate.toLowerCase(); return; } if (pin.isNotEmpty) { extra['certificate_sha256'] = [pin]; } } /// Проверяет, похож ли текст на конфигурацию OpenVPN (`.ovpn`). bool looksLikeOpenVpnConf(String text) { final hasRemote = RegExp(r'(^|\n)\s*remote\s+\S+', multiLine: true).hasMatch(text); final hasOvpnMarker = RegExp( r'(^|\n)\s*(client|dev\s+tun|proto\s+(udp|tcp)|||)', multiLine: true, caseSensitive: false, ).hasMatch(text); return hasRemote && hasOvpnMarker; } /// Разбирает конфигурацию OpenVPN статического ключа (`.ovpn`) в профиль `openvpn-client`. Profile? parseOpenVpnConf( String content, { String subscriptionId = '', String? nameOverride, }) { var server = ''; var port = 1194; var network = 'udp'; var cipher = 'AES-256-CBC'; var auth = 'SHA256'; var keyDirection = ''; var peerAddress = ''; int? mtu; final addresses = []; final secretMatch = RegExp( r'\s*([\s\S]*?)\s*', caseSensitive: false, ).firstMatch(content); final staticKey = secretMatch?.group(1)?.trim() ?? ''; for (final rawLine in content.split(RegExp(r'[\r\n]+'))) { final line = rawLine.trim(); if (line.isEmpty || line.startsWith('#') || line.startsWith(';') || line.startsWith('<')) { continue; } final tokens = line.split(RegExp(r'\s+')); if (tokens.isEmpty) continue; final cmd = tokens.first.toLowerCase(); switch (cmd) { case 'remote': if (server.isEmpty && tokens.length >= 2) { server = _cleanHost(tokens[1]); if (tokens.length >= 3) { final p = int.tryParse(tokens[2]); if (p != null && p > 0 && p <= 65535) port = p; } if (tokens.length >= 4) { final proto = tokens[3].toLowerCase(); if (proto.startsWith('tcp') || proto.startsWith('udp')) { network = proto; } } } case 'proto': if (tokens.length >= 2) { final proto = tokens[1].toLowerCase(); if (proto.startsWith('tcp') || proto.startsWith('udp')) { network = proto; } } case 'port': if (tokens.length >= 2) { final p = int.tryParse(tokens[1]); if (p != null && p > 0 && p <= 65535) port = p; } case 'cipher': if (tokens.length >= 2) cipher = tokens[1].toUpperCase(); case 'auth': if (tokens.length >= 2) auth = tokens[1].toUpperCase(); case 'tun-mtu': case 'mtu': if (tokens.length >= 2) mtu = int.tryParse(tokens[1]); case 'key-direction': if (tokens.length >= 2) { final v = tokens[1].toLowerCase(); keyDirection = switch (v) { '0' || 'server' => 'server', '1' || 'client' => 'client', _ => '', }; } case 'ifconfig': if (tokens.length >= 3) { final ip = tokens[1].trim(); final maskOrPeer = tokens[2].trim(); final maskPrefix = _netmaskToPrefix(maskOrPeer); final prefix = maskPrefix ?? 30; if (maskPrefix == null && peerAddress.isEmpty && RegExp(r'^\d+\.\d+\.\d+\.\d+$').hasMatch(maskOrPeer)) { peerAddress = maskOrPeer; } if (ip.isNotEmpty) addresses.add('$ip/$prefix'); } } } if (server.isEmpty || staticKey.isEmpty) return null; final resolvedAddrs = addresses.isEmpty ? ['10.8.0.2/24'] : addresses; final label = (nameOverride != null && nameOverride.trim().isNotEmpty) ? nameOverride.trim() : 'OpenVPN $server:$port'; return Profile( id: _id('openvpn-client', '$server:$port:$cipher:$label'), name: label, type: 'openvpn-client', server: server, port: port, password: staticKey, transport: network.contains('tcp') ? 'tcp' : 'udp', subscriptionId: subscriptionId, extra: { 'mode': 'static_key', 'network': network, 'local_address': resolvedAddrs, if (peerAddress.isNotEmpty) 'peer_address': peerAddress, 'static_key': staticKey, if (keyDirection.isNotEmpty) 'key_direction': keyDirection, 'cipher': cipher, 'auth': auth, if (mtu != null && mtu >= 576 && mtu <= 65535) 'mtu': mtu, }, ); } int? _netmaskToPrefix(String mask) { final octets = mask.split('.'); if (octets.length != 4) return null; if (octets.first != '255' && octets.first != '0') return null; var bits = 0; for (final raw in octets) { final n = int.tryParse(raw); if (n == null || n < 0 || n > 255) return null; bits += switch (n) { 255 => 8, 254 => 7, 252 => 6, 248 => 5, 240 => 4, 224 => 3, 192 => 2, 128 => 1, 0 => 0, _ => -1, }; if (bits < 0) return null; } return bits > 0 ? bits : null; } }