startVpn method

  1. @override
Future<void> startVpn({
  1. required String serverAddress,
  2. required String wgQuickConfig,
  3. required String providerBundleIdentifier,
})
override

Implementation

@override
Future<void> startVpn({
  required String serverAddress,
  required String wgQuickConfig,
  required String providerBundleIdentifier,
}) async {
  final isAlreadyConnected = await isConnected();
  if (!isAlreadyConnected) {
    _setStage(VpnStage.preparing);
  } else {
    debugPrint('Already connected');
  }

  try {
    configFile = await File(await filePath).create();
    await configFile!.writeAsString(wgQuickConfig);
  } on PathAccessException {
    debugPrint('Denied to write file. Trying to start interface');
    if (isAlreadyConnected) {
      return _setStage(VpnStage.connected);
    }

    try {
      await shell.run('sudo wg-quick up $name');
    } catch (_) {
    } finally {
      _setStage(VpnStage.denied);
    }
  }

  if (!isAlreadyConnected) {
    _setStage(VpnStage.connecting);
    await shell.run('sudo wg-quick up ${configFile?.path ?? await filePath}');
    _setStage(VpnStage.connected);
  }
}