|
| 1 | +import '../models/vpn_profile.dart'; |
| 2 | + |
| 3 | +class VlessValidationResult { |
| 4 | + const VlessValidationResult({ |
| 5 | + required this.ok, |
| 6 | + this.message, |
| 7 | + this.details = const {}, |
| 8 | + }); |
| 9 | + |
| 10 | + final bool ok; |
| 11 | + final String? message; |
| 12 | + final Map<String, String> details; |
| 13 | +} |
| 14 | + |
| 15 | +class VlessProfileValidator { |
| 16 | + const VlessProfileValidator._(); |
| 17 | + |
| 18 | + static bool get isSupportedRealityTcpOnly => true; |
| 19 | + |
| 20 | + static VlessValidationResult validate(VpnProfile profile) { |
| 21 | + if (profile.kind != VpnProfileKind.vlessReality) { |
| 22 | + return const VlessValidationResult(ok: true); |
| 23 | + } |
| 24 | + final outbound = profile.outbound; |
| 25 | + if (outbound == null) { |
| 26 | + return const VlessValidationResult( |
| 27 | + ok: false, |
| 28 | + message: 'VLESS Reality профиль без outbound-конфига.', |
| 29 | + ); |
| 30 | + } |
| 31 | + return validateOutbound(outbound); |
| 32 | + } |
| 33 | + |
| 34 | + static VlessValidationResult validateOutbound(Map<String, dynamic> outbound) { |
| 35 | + final type = outbound['type']?.toString().toLowerCase(); |
| 36 | + if (type != 'vless') { |
| 37 | + return const VlessValidationResult( |
| 38 | + ok: false, |
| 39 | + message: 'VLESS Reality профиль содержит не VLESS outbound.', |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + final server = outbound['server']?.toString().trim() ?? ''; |
| 44 | + final uuid = outbound['uuid']?.toString().trim() ?? ''; |
| 45 | + final tls = _map(outbound['tls']); |
| 46 | + final reality = _map(tls?['reality']); |
| 47 | + final transport = _transport(outbound); |
| 48 | + final serverName = tls?['server_name']?.toString().trim() ?? ''; |
| 49 | + final publicKey = reality?['public_key']?.toString().trim() ?? ''; |
| 50 | + final shortId = reality?['short_id']?.toString().trim() ?? ''; |
| 51 | + final flow = outbound['flow']?.toString().trim() ?? ''; |
| 52 | + final packetEncoding = outbound['packet_encoding']?.toString().trim() ?? ''; |
| 53 | + final fingerprint = |
| 54 | + _map(tls?['utls'])?['fingerprint']?.toString().trim() ?? ''; |
| 55 | + |
| 56 | + final errors = <String>[]; |
| 57 | + if (server.isEmpty) { |
| 58 | + errors.add('host'); |
| 59 | + } |
| 60 | + if (uuid.isEmpty) { |
| 61 | + errors.add('uuid'); |
| 62 | + } |
| 63 | + if (tls?['enabled'] != true) { |
| 64 | + errors.add('tls.enabled'); |
| 65 | + } |
| 66 | + if (reality?['enabled'] != true) { |
| 67 | + errors.add('reality.enabled'); |
| 68 | + } |
| 69 | + if (serverName.isEmpty) { |
| 70 | + errors.add('sni/server_name'); |
| 71 | + } |
| 72 | + if (publicKey.isEmpty) { |
| 73 | + errors.add('pbk/public_key'); |
| 74 | + } |
| 75 | + if (transport != 'tcp') { |
| 76 | + errors.add('transport=$transport'); |
| 77 | + } |
| 78 | + if (flow.isNotEmpty && flow != 'xtls-rprx-vision') { |
| 79 | + errors.add('flow=$flow'); |
| 80 | + } |
| 81 | + if (packetEncoding.isNotEmpty && packetEncoding != 'xudp') { |
| 82 | + errors.add('packet_encoding=$packetEncoding'); |
| 83 | + } |
| 84 | + |
| 85 | + if (errors.isNotEmpty) { |
| 86 | + return VlessValidationResult( |
| 87 | + ok: false, |
| 88 | + message: |
| 89 | + 'VLESS Reality профиль неполный или несовместимый: ${errors.join(', ')}.', |
| 90 | + details: { |
| 91 | + 'server': server, |
| 92 | + 'transport': transport, |
| 93 | + 'flow': flow.isEmpty ? 'default' : flow, |
| 94 | + 'sni': serverName, |
| 95 | + 'fingerprint': fingerprint, |
| 96 | + 'short_id': shortId.isEmpty ? 'none' : shortId, |
| 97 | + }, |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + return VlessValidationResult( |
| 102 | + ok: true, |
| 103 | + details: { |
| 104 | + 'server': server, |
| 105 | + 'transport': transport, |
| 106 | + 'flow': flow.isEmpty ? 'xtls-rprx-vision' : flow, |
| 107 | + 'sni': serverName, |
| 108 | + 'fingerprint': fingerprint.isEmpty ? 'chrome' : fingerprint, |
| 109 | + 'short_id': shortId.isEmpty ? 'none' : shortId, |
| 110 | + }, |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + static Map<String, dynamic> normalizeRealityTcpOutbound( |
| 115 | + Map<String, dynamic> outbound, |
| 116 | + ) { |
| 117 | + final normalized = Map<String, dynamic>.from(outbound); |
| 118 | + normalized['type'] = 'vless'; |
| 119 | + normalized.remove('network'); |
| 120 | + normalized['packet_encoding'] = 'xudp'; |
| 121 | + normalized.putIfAbsent('flow', () => 'xtls-rprx-vision'); |
| 122 | + |
| 123 | + final tls = _deepMap(normalized['tls']); |
| 124 | + tls['enabled'] = true; |
| 125 | + tls.putIfAbsent('server_name', () => normalized['server']); |
| 126 | + final utls = _deepMap(tls['utls']); |
| 127 | + utls['enabled'] = true; |
| 128 | + utls.putIfAbsent('fingerprint', () => 'chrome'); |
| 129 | + tls['utls'] = utls; |
| 130 | + |
| 131 | + final reality = _deepMap(tls['reality']); |
| 132 | + reality['enabled'] = true; |
| 133 | + tls['reality'] = reality; |
| 134 | + normalized['tls'] = tls; |
| 135 | + |
| 136 | + final result = validateOutbound(normalized); |
| 137 | + if (!result.ok) { |
| 138 | + throw StateError(result.message ?? 'Некорректный VLESS Reality профиль.'); |
| 139 | + } |
| 140 | + return normalized; |
| 141 | + } |
| 142 | + |
| 143 | + static String transportLabel(Map<String, dynamic>? outbound) { |
| 144 | + if (outbound == null) { |
| 145 | + return 'unknown'; |
| 146 | + } |
| 147 | + return _transport(outbound); |
| 148 | + } |
| 149 | + |
| 150 | + static String _transport(Map<String, dynamic> outbound) { |
| 151 | + final unsupported = outbound['unsupported_transport']?.toString().trim(); |
| 152 | + if (unsupported != null && unsupported.isNotEmpty) { |
| 153 | + return unsupported.toLowerCase(); |
| 154 | + } |
| 155 | + final transport = _map(outbound['transport']); |
| 156 | + if (transport != null) { |
| 157 | + return (transport['type']?.toString().trim().toLowerCase() ?? 'tcp'); |
| 158 | + } |
| 159 | + return (outbound['network']?.toString().trim().toLowerCase() ?? 'tcp'); |
| 160 | + } |
| 161 | + |
| 162 | + static Map<String, dynamic>? _map(Object? value) => |
| 163 | + value is Map ? value.cast<String, dynamic>() : null; |
| 164 | + |
| 165 | + static Map<String, dynamic> _deepMap(Object? value) { |
| 166 | + if (value is! Map) { |
| 167 | + return <String, dynamic>{}; |
| 168 | + } |
| 169 | + return Map<String, dynamic>.from(value.cast<String, dynamic>()); |
| 170 | + } |
| 171 | +} |
0 commit comments