2022-08-26 08:11:35 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:http/http.dart' as http;
|
2022-12-12 21:59:06 +00:00
|
|
|
import 'package:stackwallet/pages/settings_views/global_settings_view/manage_nodes_views/add_edit_node_view.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
|
|
|
|
2022-12-12 21:59:06 +00:00
|
|
|
Future<bool> _testEpicBoxNodeConnection(Uri uri) async {
|
2022-08-26 08:11:35 +00:00
|
|
|
try {
|
|
|
|
final client = http.Client();
|
|
|
|
final response = await client.get(
|
|
|
|
uri,
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
2022-12-12 21:59:06 +00:00
|
|
|
).timeout(const Duration(milliseconds: 2000),
|
2022-08-26 08:11:35 +00:00
|
|
|
onTimeout: () async => http.Response('Error', 408));
|
|
|
|
|
|
|
|
final json = jsonDecode(response.body);
|
|
|
|
|
|
|
|
if (response.statusCode == 200 && json["node_version"] != null) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|