2020-07-27 20:43:58 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:mobile_nebula/models/HostInfo.dart';
|
|
|
|
import 'package:mobile_nebula/models/UnsafeRoute.dart';
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
import 'Certificate.dart';
|
|
|
|
import 'StaticHosts.dart';
|
|
|
|
|
|
|
|
var uuid = Uuid();
|
|
|
|
|
|
|
|
class Site {
|
|
|
|
static const platform = MethodChannel('net.defined.mobileNebula/NebulaVpnService');
|
2022-09-21 20:27:35 +00:00
|
|
|
late EventChannel _updates;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
/// Signals that something about this site has changed. onError is called with an error string if there was an error
|
|
|
|
StreamController _change = StreamController.broadcast();
|
|
|
|
|
|
|
|
// Identifiers
|
2022-09-21 20:27:35 +00:00
|
|
|
late String name;
|
|
|
|
late String id;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
// static_host_map
|
2022-09-21 20:27:35 +00:00
|
|
|
late Map<String, StaticHost> staticHostmap;
|
|
|
|
late List<UnsafeRoute> unsafeRoutes;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
// pki fields
|
2022-09-21 20:27:35 +00:00
|
|
|
late List<CertificateInfo> ca;
|
|
|
|
String? key;
|
|
|
|
late CertificateInfo? certInfo;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
// lighthouse options
|
2022-09-21 20:27:35 +00:00
|
|
|
late int lhDuration; // in seconds
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
// listen settings
|
2022-09-21 20:27:35 +00:00
|
|
|
late int port;
|
|
|
|
late int mtu;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
late String cipher;
|
|
|
|
late int sortKey;
|
|
|
|
late bool connected;
|
|
|
|
late String status;
|
|
|
|
late String logFile;
|
|
|
|
late String logVerbosity;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
2022-11-17 21:43:16 +00:00
|
|
|
late bool managed;
|
|
|
|
// The following fields are present when managed = true
|
|
|
|
late String? rawConfig;
|
|
|
|
late DateTime? lastManagedUpdate;
|
|
|
|
|
2020-07-27 20:43:58 +00:00
|
|
|
// A list of errors encountered while loading the site
|
2022-09-21 20:27:35 +00:00
|
|
|
late List<String> errors;
|
|
|
|
|
|
|
|
Site({
|
|
|
|
String name = '',
|
|
|
|
String? id,
|
|
|
|
Map<String, StaticHost>? staticHostmap,
|
|
|
|
List<CertificateInfo>? ca,
|
|
|
|
CertificateInfo? certInfo,
|
|
|
|
int lhDuration = 0,
|
|
|
|
int port = 0,
|
|
|
|
String cipher = "aes",
|
|
|
|
int sortKey = 0,
|
|
|
|
int mtu = 1300,
|
|
|
|
bool connected = false,
|
|
|
|
String status = '',
|
|
|
|
String logFile = '',
|
|
|
|
String logVerbosity = 'info',
|
|
|
|
List<String>? errors,
|
|
|
|
List<UnsafeRoute>? unsafeRoutes,
|
2022-11-17 21:43:16 +00:00
|
|
|
bool managed = false,
|
|
|
|
String? rawConfig,
|
|
|
|
DateTime? lastManagedUpdate,
|
2022-09-21 20:27:35 +00:00
|
|
|
}) {
|
|
|
|
this.name = name;
|
|
|
|
this.id = id ?? uuid.v4();
|
|
|
|
this.staticHostmap = staticHostmap ?? {};
|
|
|
|
this.ca = ca ?? [];
|
|
|
|
this.certInfo = certInfo;
|
|
|
|
this.lhDuration = lhDuration;
|
|
|
|
this.port = port;
|
|
|
|
this.cipher = cipher;
|
|
|
|
this.sortKey = sortKey;
|
|
|
|
this.mtu = mtu;
|
|
|
|
this.connected = connected;
|
|
|
|
this.status = status;
|
|
|
|
this.logFile = logFile;
|
|
|
|
this.logVerbosity = logVerbosity;
|
|
|
|
this.errors = errors ?? [];
|
|
|
|
this.unsafeRoutes = unsafeRoutes ?? [];
|
2022-11-17 21:43:16 +00:00
|
|
|
this.managed = managed;
|
|
|
|
this.rawConfig = rawConfig;
|
|
|
|
this.lastManagedUpdate = lastManagedUpdate;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
2024-10-30 12:05:32 +00:00
|
|
|
_updates = EventChannel('net.defined.nebula/${this.id}');
|
2022-09-21 20:27:35 +00:00
|
|
|
_updates.receiveBroadcastStream().listen((d) {
|
|
|
|
try {
|
2022-11-17 21:43:16 +00:00
|
|
|
_updateFromJson(d);
|
2022-09-21 20:27:35 +00:00
|
|
|
_change.add(null);
|
|
|
|
} catch (err) {
|
|
|
|
//TODO: handle the error
|
|
|
|
print(err);
|
|
|
|
}
|
|
|
|
}, onError: (err) {
|
2022-11-17 21:43:16 +00:00
|
|
|
_updateFromJson(err.details);
|
2022-09-21 20:27:35 +00:00
|
|
|
var error = err as PlatformException;
|
|
|
|
_change.addError(error.message ?? 'An unexpected error occurred');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
factory Site.fromJson(Map<String, dynamic> json) {
|
2022-11-17 21:43:16 +00:00
|
|
|
var decoded = Site._fromJson(json);
|
|
|
|
return Site(
|
|
|
|
name: decoded["name"],
|
|
|
|
id: decoded['id'],
|
|
|
|
staticHostmap: decoded['staticHostmap'],
|
|
|
|
ca: decoded['ca'],
|
|
|
|
certInfo: decoded['certInfo'],
|
|
|
|
lhDuration: decoded['lhDuration'],
|
|
|
|
port: decoded['port'],
|
|
|
|
cipher: decoded['cipher'],
|
|
|
|
sortKey: decoded['sortKey'],
|
|
|
|
mtu: decoded['mtu'],
|
|
|
|
connected: decoded['connected'],
|
|
|
|
status: decoded['status'],
|
|
|
|
logFile: decoded['logFile'],
|
|
|
|
logVerbosity: decoded['logVerbosity'],
|
|
|
|
errors: decoded['errors'],
|
|
|
|
unsafeRoutes: decoded['unsafeRoutes'],
|
|
|
|
managed: decoded['managed'],
|
|
|
|
rawConfig: decoded['rawConfig'],
|
|
|
|
lastManagedUpdate: decoded['lastManagedUpdate'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateFromJson(String json) {
|
|
|
|
var decoded = Site._fromJson(jsonDecode(json));
|
|
|
|
this.name = decoded["name"];
|
|
|
|
this.id = decoded['id']; // TODO update EventChannel
|
|
|
|
this.staticHostmap = decoded['staticHostmap'];
|
|
|
|
this.ca = decoded['ca'];
|
|
|
|
this.certInfo = decoded['certInfo'];
|
|
|
|
this.lhDuration = decoded['lhDuration'];
|
|
|
|
this.port = decoded['port'];
|
|
|
|
this.cipher = decoded['cipher'];
|
|
|
|
this.sortKey = decoded['sortKey'];
|
|
|
|
this.mtu = decoded['mtu'];
|
|
|
|
this.connected = decoded['connected'];
|
|
|
|
this.status = decoded['status'];
|
|
|
|
this.logFile = decoded['logFile'];
|
|
|
|
this.logVerbosity = decoded['logVerbosity'];
|
|
|
|
this.errors = decoded['errors'];
|
|
|
|
this.unsafeRoutes = decoded['unsafeRoutes'];
|
|
|
|
this.managed = decoded['managed'];
|
|
|
|
this.rawConfig = decoded['rawConfig'];
|
|
|
|
this.lastManagedUpdate = decoded['lastManagedUpdate'];
|
|
|
|
}
|
|
|
|
|
|
|
|
static _fromJson(Map<String, dynamic> json) {
|
2020-07-27 20:43:58 +00:00
|
|
|
Map<String, dynamic> rawHostmap = json['staticHostmap'];
|
2022-09-21 20:27:35 +00:00
|
|
|
Map<String, StaticHost> staticHostmap = {};
|
2020-07-27 20:43:58 +00:00
|
|
|
rawHostmap.forEach((key, val) {
|
|
|
|
staticHostmap[key] = StaticHost.fromJson(val);
|
|
|
|
});
|
|
|
|
|
|
|
|
List<dynamic> rawUnsafeRoutes = json['unsafeRoutes'];
|
2022-09-21 20:27:35 +00:00
|
|
|
List<UnsafeRoute> unsafeRoutes = [];
|
|
|
|
rawUnsafeRoutes.forEach((val) {
|
|
|
|
unsafeRoutes.add(UnsafeRoute.fromJson(val));
|
|
|
|
});
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
List<dynamic> rawCA = json['ca'];
|
2022-09-21 20:27:35 +00:00
|
|
|
List<CertificateInfo> ca = [];
|
2020-07-27 20:43:58 +00:00
|
|
|
rawCA.forEach((val) {
|
|
|
|
ca.add(CertificateInfo.fromJson(val));
|
|
|
|
});
|
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
CertificateInfo? certInfo;
|
2020-07-27 20:43:58 +00:00
|
|
|
if (json['cert'] != null) {
|
2021-05-03 20:16:00 +00:00
|
|
|
certInfo = CertificateInfo.fromJson(json['cert']);
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
List<dynamic> rawErrors = json["errors"];
|
2022-09-21 20:27:35 +00:00
|
|
|
List<String> errors = [];
|
2020-07-27 20:43:58 +00:00
|
|
|
rawErrors.forEach((error) {
|
|
|
|
errors.add(error);
|
|
|
|
});
|
|
|
|
|
2022-11-17 21:43:16 +00:00
|
|
|
return {
|
|
|
|
"name": json["name"],
|
|
|
|
"id": json['id'],
|
|
|
|
"staticHostmap": staticHostmap,
|
|
|
|
"ca": ca,
|
|
|
|
"certInfo": certInfo,
|
|
|
|
"lhDuration": json['lhDuration'],
|
|
|
|
"port": json['port'],
|
|
|
|
"cipher": json['cipher'],
|
|
|
|
"sortKey": json['sortKey'],
|
|
|
|
"mtu": json['mtu'],
|
|
|
|
"connected": json['connected'] ?? false,
|
|
|
|
"status": json['status'] ?? "",
|
|
|
|
"logFile": json['logFile'],
|
|
|
|
"logVerbosity": json['logVerbosity'],
|
|
|
|
"errors": errors,
|
|
|
|
"unsafeRoutes": unsafeRoutes,
|
|
|
|
"managed": json['managed'] ?? false,
|
|
|
|
"rawConfig": json['rawConfig'],
|
2024-09-20 18:19:23 +00:00
|
|
|
"lastManagedUpdate": json["lastManagedUpdate"] == null ? null : DateTime.parse(json["lastManagedUpdate"]),
|
2022-11-17 21:43:16 +00:00
|
|
|
};
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Stream onChange() {
|
|
|
|
return _change.stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() {
|
|
|
|
return {
|
|
|
|
'name': name,
|
|
|
|
'id': id,
|
|
|
|
'staticHostmap': staticHostmap,
|
|
|
|
'unsafeRoutes': unsafeRoutes,
|
2022-09-21 20:27:35 +00:00
|
|
|
'ca': ca.map((cert) {
|
|
|
|
return cert.rawCert;
|
|
|
|
}).join('\n'),
|
2021-05-03 20:16:00 +00:00
|
|
|
'cert': certInfo?.rawCert,
|
2020-07-27 20:43:58 +00:00
|
|
|
'key': key,
|
|
|
|
'lhDuration': lhDuration,
|
|
|
|
'port': port,
|
|
|
|
'mtu': mtu,
|
|
|
|
'cipher': cipher,
|
|
|
|
'sortKey': sortKey,
|
|
|
|
'logVerbosity': logVerbosity,
|
2022-11-17 21:43:16 +00:00
|
|
|
'managed': managed,
|
|
|
|
'rawConfig': rawConfig,
|
2020-07-27 20:43:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
save() async {
|
|
|
|
try {
|
|
|
|
var raw = jsonEncode(this);
|
|
|
|
await platform.invokeMethod("saveSite", raw);
|
|
|
|
} on PlatformException catch (err) {
|
|
|
|
//TODO: fix this message
|
|
|
|
throw err.details ?? err.message ?? err.toString();
|
|
|
|
} catch (err) {
|
|
|
|
throw err.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> renderConfig() async {
|
|
|
|
try {
|
|
|
|
var raw = jsonEncode(this);
|
|
|
|
return await platform.invokeMethod("nebula.renderConfig", raw);
|
|
|
|
} on PlatformException catch (err) {
|
|
|
|
//TODO: fix this message
|
|
|
|
throw err.details ?? err.message ?? err.toString();
|
|
|
|
} catch (err) {
|
|
|
|
throw err.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
start() async {
|
|
|
|
try {
|
|
|
|
await platform.invokeMethod("startSite", <String, String>{"id": id});
|
|
|
|
} on PlatformException catch (err) {
|
2021-04-27 15:29:28 +00:00
|
|
|
throw err.message ?? err.toString();
|
2020-07-27 20:43:58 +00:00
|
|
|
} catch (err) {
|
|
|
|
throw err.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() async {
|
|
|
|
try {
|
|
|
|
await platform.invokeMethod("stopSite", <String, String>{"id": id});
|
|
|
|
} on PlatformException catch (err) {
|
|
|
|
//TODO: fix this message
|
|
|
|
throw err.details ?? err.message ?? err.toString();
|
|
|
|
} catch (err) {
|
|
|
|
throw err.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<HostInfo>> listHostmap() async {
|
|
|
|
try {
|
|
|
|
var ret = await platform.invokeMethod("active.listHostmap", <String, String>{"id": id});
|
2022-11-18 19:37:15 +00:00
|
|
|
if (ret == null || ret == "null") {
|
2020-08-31 21:59:38 +00:00
|
|
|
return [];
|
|
|
|
}
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
List<dynamic> f = jsonDecode(ret);
|
|
|
|
List<HostInfo> hosts = [];
|
|
|
|
f.forEach((v) {
|
|
|
|
hosts.add(HostInfo.fromJson(v));
|
|
|
|
});
|
|
|
|
|
|
|
|
return hosts;
|
|
|
|
} on PlatformException catch (err) {
|
|
|
|
//TODO: fix this message
|
|
|
|
throw err.details ?? err.message ?? err.toString();
|
|
|
|
} catch (err) {
|
|
|
|
throw err.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<HostInfo>> listPendingHostmap() async {
|
|
|
|
try {
|
|
|
|
var ret = await platform.invokeMethod("active.listPendingHostmap", <String, String>{"id": id});
|
2022-11-18 19:37:15 +00:00
|
|
|
if (ret == null || ret == "null") {
|
2020-08-31 21:59:38 +00:00
|
|
|
return [];
|
|
|
|
}
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
List<dynamic> f = jsonDecode(ret);
|
|
|
|
List<HostInfo> hosts = [];
|
|
|
|
f.forEach((v) {
|
|
|
|
hosts.add(HostInfo.fromJson(v));
|
|
|
|
});
|
|
|
|
|
|
|
|
return hosts;
|
|
|
|
} on PlatformException catch (err) {
|
|
|
|
throw err.details ?? err.message ?? err.toString();
|
|
|
|
} catch (err) {
|
|
|
|
throw err.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Map<String, List<HostInfo>>> listAllHostmaps() async {
|
|
|
|
try {
|
|
|
|
var res = await Future.wait([this.listHostmap(), this.listPendingHostmap()]);
|
|
|
|
return {"active": res[0], "pending": res[1]};
|
|
|
|
} on PlatformException catch (err) {
|
|
|
|
throw err.details ?? err.message ?? err.toString();
|
|
|
|
} catch (err) {
|
|
|
|
throw err.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dispose() {
|
|
|
|
_change.close();
|
|
|
|
}
|
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
Future<HostInfo?> getHostInfo(String vpnIp, bool pending) async {
|
2020-07-27 20:43:58 +00:00
|
|
|
try {
|
|
|
|
var ret = await platform
|
|
|
|
.invokeMethod("active.getHostInfo", <String, dynamic>{"id": id, "vpnIp": vpnIp, "pending": pending});
|
|
|
|
final h = jsonDecode(ret);
|
|
|
|
if (h == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HostInfo.fromJson(h);
|
|
|
|
} on PlatformException catch (err) {
|
|
|
|
throw err.details ?? err.message ?? err.toString();
|
|
|
|
} catch (err) {
|
|
|
|
throw err.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
Future<HostInfo?> setRemoteForTunnel(String vpnIp, String addr) async {
|
2020-07-27 20:43:58 +00:00
|
|
|
try {
|
|
|
|
var ret = await platform
|
|
|
|
.invokeMethod("active.setRemoteForTunnel", <String, dynamic>{"id": id, "vpnIp": vpnIp, "addr": addr});
|
|
|
|
final h = jsonDecode(ret);
|
|
|
|
if (h == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HostInfo.fromJson(h);
|
|
|
|
} on PlatformException catch (err) {
|
|
|
|
throw err.details ?? err.message ?? err.toString();
|
|
|
|
} catch (err) {
|
|
|
|
throw err.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> closeTunnel(String vpnIp) async {
|
|
|
|
try {
|
|
|
|
return await platform.invokeMethod("active.closeTunnel", <String, dynamic>{"id": id, "vpnIp": vpnIp});
|
|
|
|
} on PlatformException catch (err) {
|
|
|
|
throw err.details ?? err.message ?? err.toString();
|
|
|
|
} catch (err) {
|
|
|
|
throw err.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|