status method

Future<VpnStatus> status()

Get latest connection status

Implementation

Future<VpnStatus> status() {
  //Have to check if user already connected to get real data
  return stage().then((value) async {
    var status = VpnStatus.empty();
    if (value == VPNStage.connected) {
      status = await _channelControl.invokeMethod("status").then((value) {
        if (value == null) return VpnStatus.empty();

        if (Platform.isIOS) {
          var splitted = value.split("_");
          var connectedOn = DateTime.tryParse(splitted[0]);
          if (connectedOn == null) return VpnStatus.empty();
          return VpnStatus(
            connectedOn: connectedOn,
            duration: _duration(DateTime.now().difference(connectedOn).abs()),
            packetsIn: splitted[1],
            packetsOut: splitted[2],
            byteIn: splitted[3],
            byteOut: splitted[4],
          );
        } else if (Platform.isAndroid) {
          var data = jsonDecode(value);
          var connectedOn =
              DateTime.tryParse(data["connected_on"].toString()) ??
                  _tempDateTime;
          String byteIn =
              data["byte_in"] != null ? data["byte_in"].toString() : "0";
          String byteOut =
              data["byte_out"] != null ? data["byte_out"].toString() : "0";
          if (byteIn.trim().isEmpty) byteIn = "0";
          if (byteOut.trim().isEmpty) byteOut = "0";
          return VpnStatus(
            connectedOn: connectedOn,
            duration:
                _duration(DateTime.now().difference(connectedOn!).abs()),
            byteIn: byteIn,
            byteOut: byteOut,
            packetsIn: byteIn,
            packetsOut: byteOut,
          );
        } else {
          throw Exception("Openvpn not supported on this platform");
        }
      });
    }
    return status;
  });
}