stack_wallet/lib/utilities/test_epic_box_connection.dart

80 lines
2.1 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
2022-08-26 08:11:35 +00:00
import 'dart:convert';
import '../networking/http.dart';
import '../pages/settings_views/global_settings_view/manage_nodes_views/add_edit_node_view.dart';
import '../services/tor_service.dart';
import 'logger.dart';
import 'prefs.dart';
2022-08-26 08:11:35 +00:00
2022-12-12 21:59:06 +00:00
Future<bool> _testEpicBoxNodeConnection(Uri uri) async {
2023-09-11 20:20:40 +00:00
HTTP client = HTTP();
2022-08-26 08:11:35 +00:00
try {
2023-09-11 20:20:40 +00:00
final response = await client
.get(
url: uri,
headers: {'Content-Type': 'application/json'},
proxyInfo: Prefs.instance.useTor
2023-09-15 19:51:20 +00:00
? TorService.sharedInstance.getProxyInfo()
2023-09-11 20:20:40 +00:00
: null,
)
.timeout(const Duration(milliseconds: 2000),
onTimeout: () async => Response(utf8.encode('Error'), 408));
2022-08-26 08:11:35 +00:00
final json = jsonDecode(response.body);
2023-09-11 20:20:40 +00:00
if (response.code == 200 && json["node_version"] != null) {
2022-08-26 08:11:35 +00:00
return true;
} else {
return false;
}
} catch (e, s) {
Logging.instance.log("$e\n$s", level: LogLevel.Warning);
return false;
}
}
2022-12-12 21:59:06 +00:00
// returns node data with properly formatted host/url if successful, otherwise null
Future<NodeFormData?> testEpicNodeConnection(NodeFormData data) async {
if (data.host == null || data.port == null || data.useSSL == null) {
return null;
}
const String path_postfix = "/v1/version";
if (data.host!.startsWith("https://")) {
data.useSSL = true;
} else if (data.host!.startsWith("http://")) {
data.useSSL = false;
} else {
if (data.useSSL!) {
data.host = "https://${data.host!}";
} else {
data.host = "http://${data.host!}";
}
}
Uri uri = Uri.parse(data.host! + path_postfix);
uri = uri.replace(port: data.port);
try {
if (await _testEpicBoxNodeConnection(uri)) {
return data;
} else {
return null;
}
} catch (e, s) {
Logging.instance.log("$e\n$s", level: LogLevel.Warning);
return null;
}
}