2021-03-22 02:41:03 +01:00
|
|
|
import 'dart:async';
|
2021-05-30 23:18:08 +02:00
|
|
|
import 'dart:convert';
|
2021-03-22 02:41:03 +01:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
|
|
|
|
class LokinetLib {
|
2022-03-08 05:25:36 +01:00
|
|
|
static const MethodChannel _methodChannel =
|
|
|
|
const MethodChannel('lokinet_lib_method_channel');
|
|
|
|
|
2022-03-09 16:27:05 +01:00
|
|
|
static const EventChannel _isConnectedEventChannel =
|
|
|
|
const EventChannel('lokinet_lib_is_connected_event_channel');
|
2021-03-22 02:41:03 +01:00
|
|
|
|
2022-03-09 16:27:05 +01:00
|
|
|
static bool _isConnected = false;
|
2022-03-09 00:11:16 +01:00
|
|
|
|
2022-03-09 16:27:05 +01:00
|
|
|
static bool get isConnected => _isConnected;
|
2022-03-09 00:11:16 +01:00
|
|
|
|
2022-03-09 16:27:05 +01:00
|
|
|
static Stream<bool> _isConnectedEventStream = _isConnectedEventChannel
|
2022-03-09 00:11:16 +01:00
|
|
|
.receiveBroadcastStream()
|
|
|
|
.cast<bool>()
|
2022-03-09 16:27:05 +01:00
|
|
|
..listen((dynamic newIsConnected) => _isConnected = newIsConnected);
|
2022-03-09 00:11:16 +01:00
|
|
|
|
2022-03-09 16:27:05 +01:00
|
|
|
static Stream<bool> get isConnectedEventStream => _isConnectedEventStream;
|
2022-03-09 00:11:16 +01:00
|
|
|
|
2021-03-22 02:41:03 +01:00
|
|
|
static Future bootstrapLokinet() async {
|
2021-05-16 19:19:23 +02:00
|
|
|
final request = await HttpClient()
|
|
|
|
.getUrl(Uri.parse('https://seed.lokinet.org/lokinet.signed'));
|
2021-03-22 02:41:03 +01:00
|
|
|
final response = await request.close();
|
|
|
|
var path = await getApplicationDocumentsDirectory();
|
2021-05-16 19:19:23 +02:00
|
|
|
await response
|
|
|
|
.pipe(File('${path.parent.path}/files/bootstrap.signed').openWrite());
|
|
|
|
if (await isBootstrapped) {
|
|
|
|
print("Successfully bootstrapped!");
|
|
|
|
} else {
|
|
|
|
print("Bootstrapping went wrong!");
|
|
|
|
print(Directory('${path.parent.path}/files/').listSync().toString());
|
|
|
|
}
|
2021-03-22 02:41:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Future<bool> prepareConnection() async {
|
2021-05-16 19:19:23 +02:00
|
|
|
if (!(await isBootstrapped)) await bootstrapLokinet();
|
2022-03-08 05:25:36 +01:00
|
|
|
final bool prepare = await _methodChannel.invokeMethod('prepare');
|
2021-03-22 02:41:03 +01:00
|
|
|
return prepare;
|
|
|
|
}
|
|
|
|
|
2021-06-22 22:53:28 +02:00
|
|
|
static Future<bool> connectToLokinet(
|
|
|
|
{String exitNode = "exit.loki", String upstreamDNS = "9.9.9.9"}) async {
|
2022-03-08 05:25:36 +01:00
|
|
|
final bool connect = await _methodChannel.invokeMethod(
|
2021-06-22 22:53:28 +02:00
|
|
|
'connect', {"exit_node": exitNode, "upstream_dns": upstreamDNS});
|
2021-03-22 02:41:03 +01:00
|
|
|
return connect;
|
|
|
|
}
|
2021-03-22 23:33:01 +01:00
|
|
|
|
|
|
|
static Future<bool> disconnectFromLokinet() async {
|
2022-03-08 05:25:36 +01:00
|
|
|
final bool disconnect = await _methodChannel.invokeMethod('disconnect');
|
2021-03-22 23:33:01 +01:00
|
|
|
return disconnect;
|
|
|
|
}
|
2021-04-10 22:24:21 +02:00
|
|
|
|
2021-05-16 19:19:23 +02:00
|
|
|
static Future<bool> get isPrepared async {
|
2022-03-08 05:25:36 +01:00
|
|
|
final bool prepared = await _methodChannel.invokeMethod('isPrepared');
|
2021-05-16 19:19:23 +02:00
|
|
|
return prepared;
|
|
|
|
}
|
|
|
|
|
2021-04-10 22:24:21 +02:00
|
|
|
static Future<bool> get isRunning async {
|
2022-03-08 05:25:36 +01:00
|
|
|
final bool isRunning = await _methodChannel.invokeMethod('isRunning');
|
2021-04-10 22:24:21 +02:00
|
|
|
return isRunning;
|
|
|
|
}
|
2021-05-16 19:19:23 +02:00
|
|
|
|
|
|
|
static Future<bool> get isBootstrapped async {
|
|
|
|
var path = await getApplicationDocumentsDirectory();
|
|
|
|
return File('${path.parent.path}/files/bootstrap.signed').existsSync();
|
|
|
|
}
|
2021-05-30 23:18:08 +02:00
|
|
|
|
2022-03-09 21:34:30 +01:00
|
|
|
static Future<dynamic> get status async {
|
2022-03-09 16:27:05 +01:00
|
|
|
var status = await _methodChannel.invokeMethod('getStatus') as String;
|
2021-05-30 23:18:08 +02:00
|
|
|
if (status.isNotEmpty) return jsonDecode(status);
|
|
|
|
return null;
|
|
|
|
}
|
2021-03-22 02:41:03 +01:00
|
|
|
}
|