Skip to content
Snippets Groups Projects
Commit c57f7092 authored by vjrj's avatar vjrj
Browse files

Better parsing of network/peers

parent f8b74005
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,8 @@ import 'dart:typed_data'; ...@@ -4,6 +4,8 @@ import 'dart:typed_data';
import 'package:durt/durt.dart'; import 'package:durt/durt.dart';
import '../main.dart';
Random createRandom() { Random createRandom() {
try { try {
return Random.secure(); return Random.secure();
...@@ -42,38 +44,43 @@ String generateSalt(int length) { ...@@ -42,38 +44,43 @@ String generateSalt(int length) {
length, (int index) => charset[random.nextInt(charset.length)]).join(); length, (int index) => charset[random.nextInt(charset.length)]).join();
} }
String parseHost(String endpointUnParsed) { String? parseHost(String endpointUnParsed) {
final List<String> parts = endpointUnParsed.split(' '); try {
// FIXME (vjrj): figure out if exists a way to detect http or https final List<String> parts = endpointUnParsed.split(' ');
const String protocol = 'https'; // FIXME (vjrj): figure out if exists a way to detect http or https
final String lastPart = parts.removeLast(); const String protocol = 'https';
String path = final String lastPart = parts.removeLast();
RegExp(r'^\/[a-zA-Z0-9\-\/]+$').hasMatch(lastPart) ? lastPart : ''; String path =
RegExp(r'^\/[a-zA-Z0-9\-\/]+$').hasMatch(lastPart) ? lastPart : '';
final String nextToLast = parts[parts.length - 1]; final String nextToLast = parts[parts.length - 1];
final String port = lastPart == '' final String port = lastPart == ''
? (RegExp(r'^\/[0-9]$').hasMatch(lastPart) ? lastPart : '443') ? (RegExp(r'^\/[0-9]$').hasMatch(lastPart) ? lastPart : '443')
: RegExp(r'^\/[0-9]$').hasMatch(nextToLast) : RegExp(r'^\/[0-9]$').hasMatch(nextToLast)
? nextToLast ? nextToLast
: '443'; : '443';
final List<String> hostSplited = parts[1].split('/'); final List<String> hostSplited = parts[1].split('/');
// Process hosts like monnaie-libre.ortie.org/bma/ // Process hosts like monnaie-libre.ortie.org/bma/
final String host = hostSplited[0]; final String host = hostSplited[0];
path = path.isEmpty path = path.isEmpty
? ((hostSplited.length > 1 && hostSplited[1].isNotEmpty ? ((hostSplited.length > 1 && hostSplited[1].isNotEmpty
? hostSplited[1] ? hostSplited[1]
: '') : '')
.isNotEmpty .isNotEmpty
? hostSplited.length > 1 && hostSplited[1].isNotEmpty ? hostSplited.length > 1 && hostSplited[1].isNotEmpty
? '/${hostSplited[1]}' ? '/${hostSplited[1]}'
: '' : ''
: path) : path)
: path; : path;
//final bool hasIp = //final bool hasIp =
// RegExp(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}').hasMatch(parts[2]); // 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 String port = hasIp ? parts[3] : parts[2];
//final int pathIndex = hasIp ? 4 : 3; //final int pathIndex = hasIp ? 4 : 3;
//final String path = parts.length > pathIndex ? parts[pathIndex] : ''; //final String path = parts.length > pathIndex ? parts[pathIndex] : '';
final String endpoint = '$protocol://$host:$port$path'.trim(); final String endpoint = '$protocol://$host:$port$path'.trim();
return endpoint; return endpoint;
} catch (e) {
logger('Cannot parse endpoint $endpointUnParsed');
return null;
}
} }
...@@ -120,19 +120,21 @@ class NodeBloc extends HydratedBloc<NodeEvent, NodeState> { ...@@ -120,19 +120,21 @@ class NodeBloc extends HydratedBloc<NodeEvent, NodeState> {
for (int j = 0; j < endpoints.length; j++) { for (int j = 0; j < endpoints.length; j++) {
if (endpoints[j].startsWith('BMAS')) { if (endpoints[j].startsWith('BMAS')) {
final String endpointUnParsed = endpoints[j]; final String endpointUnParsed = endpoints[j];
final String endpoint = parseHost(endpointUnParsed); final String? endpoint = parseHost(endpointUnParsed);
final Duration latency = await _pingNode(endpoint); if (endpoint != null) {
if (fastestNode == null || latency < fastestLatency) { final Duration latency = await _pingNode(endpoint);
fastestNode = endpoint; if (fastestNode == null || latency < fastestLatency) {
fastestLatency = latency; fastestNode = endpoint;
if (!kReleaseMode) { fastestLatency = latency;
logger('Node bloc: Current faster node $fastestNode'); 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);
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment