diff --git a/lib/g1/g1_helper.dart b/lib/g1/g1_helper.dart index 595c09a6306f48a5ac6e2a9739e1c5344ac1db55..5c9ee3ed71b200d55134ef9e953cc6225573704b 100644 --- a/lib/g1/g1_helper.dart +++ b/lib/g1/g1_helper.dart @@ -4,6 +4,8 @@ import 'dart:typed_data'; import 'package:durt/durt.dart'; +import '../main.dart'; + Random createRandom() { try { return Random.secure(); @@ -42,38 +44,43 @@ String generateSalt(int length) { length, (int index) => charset[random.nextInt(charset.length)]).join(); } -String parseHost(String endpointUnParsed) { - final List<String> parts = endpointUnParsed.split(' '); - // FIXME (vjrj): figure out if exists a way to detect http or https - const String protocol = 'https'; - final String lastPart = parts.removeLast(); - String path = - RegExp(r'^\/[a-zA-Z0-9\-\/]+$').hasMatch(lastPart) ? lastPart : ''; +String? parseHost(String endpointUnParsed) { + try { + final List<String> parts = endpointUnParsed.split(' '); + // FIXME (vjrj): figure out if exists a way to detect http or https + const String protocol = 'https'; + final String lastPart = parts.removeLast(); + String path = + RegExp(r'^\/[a-zA-Z0-9\-\/]+$').hasMatch(lastPart) ? lastPart : ''; - final String nextToLast = parts[parts.length - 1]; - final String port = lastPart == '' - ? (RegExp(r'^\/[0-9]$').hasMatch(lastPart) ? lastPart : '443') - : RegExp(r'^\/[0-9]$').hasMatch(nextToLast) - ? nextToLast - : '443'; - final List<String> hostSplited = parts[1].split('/'); - // Process hosts like monnaie-libre.ortie.org/bma/ - final String host = hostSplited[0]; - path = path.isEmpty - ? ((hostSplited.length > 1 && hostSplited[1].isNotEmpty - ? hostSplited[1] - : '') - .isNotEmpty - ? hostSplited.length > 1 && hostSplited[1].isNotEmpty - ? '/${hostSplited[1]}' - : '' - : path) - : path; - //final bool hasIp = - // RegExp(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}').hasMatch(parts[2]); + final String nextToLast = parts[parts.length - 1]; + final String port = lastPart == '' + ? (RegExp(r'^\/[0-9]$').hasMatch(lastPart) ? lastPart : '443') + : RegExp(r'^\/[0-9]$').hasMatch(nextToLast) + ? nextToLast + : '443'; + final List<String> hostSplited = parts[1].split('/'); + // Process hosts like monnaie-libre.ortie.org/bma/ + final String host = hostSplited[0]; + path = path.isEmpty + ? ((hostSplited.length > 1 && hostSplited[1].isNotEmpty + ? hostSplited[1] + : '') + .isNotEmpty + ? hostSplited.length > 1 && hostSplited[1].isNotEmpty + ? '/${hostSplited[1]}' + : '' + : path) + : path; + //final bool hasIp = + // RegExp(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}').hasMatch(parts[2]); // final String port = hasIp ? parts[3] : parts[2]; - //final int pathIndex = hasIp ? 4 : 3; - //final String path = parts.length > pathIndex ? parts[pathIndex] : ''; - final String endpoint = '$protocol://$host:$port$path'.trim(); - return endpoint; + //final int pathIndex = hasIp ? 4 : 3; + //final String path = parts.length > pathIndex ? parts[pathIndex] : ''; + final String endpoint = '$protocol://$host:$port$path'.trim(); + return endpoint; + } catch (e) { + logger('Cannot parse endpoint $endpointUnParsed'); + return null; + } } diff --git a/lib/g1/node_bloc.dart b/lib/g1/node_bloc.dart index 72870342d10d781b3ab8d60af9357cf92fe6fa0c..a855fae68f7474064fe8ecc6d06c91dac7922c53 100644 --- a/lib/g1/node_bloc.dart +++ b/lib/g1/node_bloc.dart @@ -120,19 +120,21 @@ class NodeBloc extends HydratedBloc<NodeEvent, NodeState> { for (int j = 0; j < endpoints.length; j++) { if (endpoints[j].startsWith('BMAS')) { final String endpointUnParsed = endpoints[j]; - final String endpoint = parseHost(endpointUnParsed); - final Duration latency = await _pingNode(endpoint); - if (fastestNode == null || latency < fastestLatency) { - fastestNode = endpoint; - fastestLatency = latency; - if (!kReleaseMode) { - logger('Node bloc: Current faster node $fastestNode'); + final String? endpoint = parseHost(endpointUnParsed); + if (endpoint != null) { + final Duration latency = await _pingNode(endpoint); + if (fastestNode == null || latency < fastestLatency) { + fastestNode = endpoint; + fastestLatency = latency; + if (!kReleaseMode) { + logger('Node bloc: Current faster node $fastestNode'); + } } + final Node node = + Node(url: endpoint, latency: latency.inSeconds); + add(InsertNode(node: node)); + nodes.insert(0, node); } - final Node node = - Node(url: endpoint, latency: latency.inSeconds); - add(InsertNode(node: node)); - nodes.insert(0, node); } } }