mobile_nebula/lib/models/HostInfo.dart

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

84 lines
1.9 KiB
Dart
Raw Normal View History

2020-07-27 20:43:58 +00:00
import 'package:mobile_nebula/models/Certificate.dart';
class HostInfo {
String vpnIp;
int localIndex;
int remoteIndex;
List<UDPAddress> remoteAddresses;
Certificate? cert;
UDPAddress? currentRemote;
2020-07-27 20:43:58 +00:00
int messageCounter;
HostInfo({
required this.vpnIp,
required this.localIndex,
required this.remoteIndex,
required this.remoteAddresses,
required this.messageCounter,
this.cert,
this.currentRemote,
});
2020-07-27 20:43:58 +00:00
factory HostInfo.fromJson(Map<String, dynamic> json) {
UDPAddress? currentRemote;
if (json['currentRemote'] != "") {
2020-07-27 20:43:58 +00:00
currentRemote = UDPAddress.fromJson(json['currentRemote']);
}
Certificate? cert;
2020-07-27 20:43:58 +00:00
if (json['cert'] != null) {
cert = Certificate.fromJson(json['cert']);
}
List<dynamic>? addrs = json['remoteAddrs'];
List<UDPAddress> remoteAddresses = [];
addrs?.forEach((val) {
2020-07-27 20:43:58 +00:00
remoteAddresses.add(UDPAddress.fromJson(val));
});
return HostInfo(
vpnIp: json['vpnIp'],
localIndex: json['localIndex'],
remoteIndex: json['remoteIndex'],
remoteAddresses: remoteAddresses,
messageCounter: json['messageCounter'],
cert: cert,
currentRemote: currentRemote,
);
2020-07-27 20:43:58 +00:00
}
}
class UDPAddress {
String ip;
int port;
UDPAddress({
required this.ip,
required this.port,
});
2020-07-27 20:43:58 +00:00
@override
String toString() {
2021-04-23 21:23:06 +00:00
// Simple check on if nebula told us about a v4 or v6 ip address
if (ip.contains(':')) {
return '[$ip]:$port';
}
2020-07-27 20:43:58 +00:00
return '$ip:$port';
}
factory UDPAddress.fromJson(String json) {
// IPv4 Address
if (json.contains('.')) {
var ip = json.split(':')[0];
var port = int.parse(json.split(':')[1]);
return UDPAddress(ip: ip, port: port);
}
// IPv6 Address
var ip = json.split(']')[0].substring(1);
var port = int.parse(json.split(']')[1].split(':')[1]);
return UDPAddress(ip: ip, port: port);
}
2020-07-27 20:43:58 +00:00
}