From c57f7092ead9ac9fbb549508df884e371fcc998f Mon Sep 17 00:00:00 2001 From: vjrj <vjrj@comunes.org> Date: Sun, 12 Mar 2023 14:01:45 +0100 Subject: [PATCH] Better parsing of network/peers --- lib/g1/g1_helper.dart | 71 ++++++++++++++++++++++++------------------- lib/g1/node_bloc.dart | 24 ++++++++------- 2 files changed, 52 insertions(+), 43 deletions(-) diff --git a/lib/g1/g1_helper.dart b/lib/g1/g1_helper.dart index 595c09a6..5c9ee3ed 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 72870342..a855fae6 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); } } } -- GitLab