From 25aa6bd9f427f27363b2b542919c70e2112c2cb6 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Tue, 2 May 2023 12:29:41 -0500 Subject: [PATCH] add connectivity_plus listener to restart ACN/tor when network comes back up --- lib/main.dart | 21 ++++++++ macos/Flutter/GeneratedPluginRegistrant.swift | 2 + pubspec.lock | 53 +++++++++++++------ pubspec.yaml | 5 ++ .../flutter/generated_plugin_registrant.cc | 3 ++ windows/flutter/generated_plugins.cmake | 1 + 6 files changed, 70 insertions(+), 15 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 449b4026..9515f33a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:convert'; import 'package:cwtch/config.dart'; import 'package:cwtch/notification_manager.dart'; @@ -29,6 +30,8 @@ import 'views/splashView.dart'; import 'dart:io' show Platform, exit; import 'themes/opaque.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:connectivity_plus/connectivity_plus.dart'; + import 'package:intl/intl.dart' as intl; @@ -53,6 +56,8 @@ class Flwtch extends StatefulWidget { FlwtchState createState() => FlwtchState(); } +enum ConnectivityState { assumed_online, confirmed_offline, confirmed_online } + class FlwtchState extends State with WindowListener { final TextStyle biggerFont = const TextStyle(fontSize: 18); late Cwtch cwtch; @@ -60,6 +65,8 @@ class FlwtchState extends State with WindowListener { final MethodChannel notificationClickChannel = MethodChannel('im.cwtch.flwtch/notificationClickHandler'); final MethodChannel shutdownMethodChannel = MethodChannel('im.cwtch.flwtch/shutdownClickHandler'); final MethodChannel shutdownLinuxMethodChannel = MethodChannel('im.cwtch.linux.shutdown'); + late StreamSubscription connectivityStream; + ConnectivityState connectivityState = ConnectivityState.assumed_online; final GlobalKey navKey = GlobalKey(); @@ -99,6 +106,19 @@ class FlwtchState extends State with WindowListener { new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, newDesktopNotificationsManager(_notificationSelectConvo), globalAppState, globalServersList, this); cwtch = CwtchFfi(cwtchNotifier); } + connectivityStream = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) { + // Got a new connectivity status! + if (result == ConnectivityResult.none) { + connectivityState = ConnectivityState.confirmed_offline; + } else { + // were we offline? + if (connectivityState == ConnectivityState.confirmed_offline) { + EnvironmentConfig.debugLog("Network appears to have come back online, restarting Tor"); + cwtch.ResetTor(); + } + connectivityState = ConnectivityState.confirmed_online; + } + }); print("initState: invoking cwtch.Start()"); cwtch.Start(); print("initState: done!"); @@ -275,6 +295,7 @@ class FlwtchState extends State with WindowListener { cwtch.Shutdown(); windowManager.removeListener(this); cwtch.dispose(); + connectivityStream.cancel(); super.dispose(); } } diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index dcf1815c..3dbcbd23 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,6 +5,7 @@ import FlutterMacOS import Foundation +import connectivity_plus import flutter_local_notifications import package_info_plus_macos import path_provider_foundation @@ -13,6 +14,7 @@ import url_launcher_macos import window_manager func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin")) FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin")) FLTPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlusPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) diff --git a/pubspec.lock b/pubspec.lock index 2dd65711..7eb708ce 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -5,18 +5,18 @@ packages: dependency: transitive description: name: _fe_analyzer_shared - sha256: "4897882604d919befd350648c7f91926a9d5de99e67b455bf0917cc2362f4bb8" + sha256: "4826f97faae3af9761f26c52e56b2aa5ffd18d2c1721d984ad85137721c25f43" url: "https://pub.dev" source: hosted - version: "47.0.0" + version: "31.0.0" analyzer: dependency: transitive description: name: analyzer - sha256: "690e335554a8385bc9d787117d9eb52c0c03ee207a607e593de3c9d71b1cfe80" + sha256: "7337610c3f9cd13e6b7c6bb0f410644091cf63c9a1436e73352a70f3286abb03" url: "https://pub.dev" source: hosted - version: "4.7.0" + version: "2.8.0" archive: dependency: transitive description: @@ -77,18 +77,18 @@ packages: dependency: transitive description: name: build_resolvers - sha256: "687cf90a3951affac1bd5f9ecb5e3e90b60487f3d9cdc359bb310f8876bb02a6" + sha256: "4666aef1d045c5ca15ebba63e400bd4e4fbd9f0dd06e791b51ab210da78a27f7" url: "https://pub.dev" source: hosted - version: "2.0.10" + version: "2.0.6" build_runner: dependency: "direct dev" description: name: build_runner - sha256: b0a8a7b8a76c493e85f1b84bffa0588859a06197863dba8c9036b15581fd9727 + sha256: "56942f8114731d1e79942cd981cfef29501937ff1bccf4dbdce0273f31f13640" url: "https://pub.dev" source: hosted - version: "2.3.3" + version: "2.2.0" build_runner_core: dependency: transitive description: @@ -161,6 +161,21 @@ packages: url: "https://pub.dev" source: hosted version: "1.17.0" + connectivity_plus: + dependency: "direct main" + description: + path: "lib/third_party/connectivity_plus/connectivity_plus" + relative: true + source: path + version: "3.0.6" + connectivity_plus_platform_interface: + dependency: transitive + description: + name: connectivity_plus_platform_interface + sha256: cf1d1c28f4416f8c654d7dc3cd638ec586076255d407cef3ddbdaf178272a71a + url: "https://pub.dev" + source: hosted + version: "1.2.4" console: dependency: transitive description: @@ -197,12 +212,12 @@ packages: dependency: transitive description: name: dart_style - sha256: "7a03456c3490394c8e7665890333e91ae8a49be43542b616e414449ac358acd4" + sha256: "6e8086e1d3c2f6bc15056ee248c4ddc48c2bc71287c0961bf801a08633ed4333" url: "https://pub.dev" source: hosted - version: "2.2.4" + version: "2.2.1" dbus: - dependency: transitive + dependency: "direct main" description: name: dbus sha256: "253bfaa3d340778d8bc755e89c3af38e85ef95e65fd5d5670aa3167f8d4f6577" @@ -335,10 +350,10 @@ packages: dependency: transitive description: name: frontend_server_client - sha256: "408e3ca148b31c20282ad6f37ebfa6f4bdc8fede5b74bc2f08d9d92b55db3612" + sha256: "4f4a162323c86ffc1245765cfe138872b8f069deb42f7dbb36115fa27f31469b" url: "https://pub.dev" source: hosted - version: "3.2.0" + version: "2.1.3" fuchsia_remote_debug_protocol: dependency: transitive description: flutter @@ -501,6 +516,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.0" + nm: + dependency: transitive + description: + name: nm + sha256: "2c9aae4127bdc8993206464fcc063611e0e36e72018696cd9631023a31b24254" + url: "https://pub.dev" + source: hosted + version: "0.5.0" package_config: dependency: transitive description: @@ -734,10 +757,10 @@ packages: dependency: transitive description: name: source_gen - sha256: "2d79738b6bbf38a43920e2b8d189e9a3ce6cc201f4b8fc76be5e4fe377b1c38d" + sha256: "00f8b6b586f724a8c769c96f1d517511a41661c0aede644544d8d86a1ab11142" url: "https://pub.dev" source: hosted - version: "1.2.6" + version: "1.2.2" source_span: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 6a230d00..71cca820 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -46,6 +46,11 @@ dependencies: win_toast: ^0.0.2 flutter_local_notifications: ^9.6.1 desktop_notifications: ^0.6.3 + # network management plugins + dbus: ^0.7.0 + connectivity_plus: + path: lib/third_party/connectivity_plus/connectivity_plus + # misc plugins qr_flutter: ^4.0.0 dev_dependencies: diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index c253d427..b4b9a696 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -6,12 +6,15 @@ #include "generated_plugin_registrant.h" +#include #include #include #include #include void RegisterPlugins(flutter::PluginRegistry* registry) { + ConnectivityPlusWindowsPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("ConnectivityPlusWindowsPlugin")); ScreenRetrieverPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("ScreenRetrieverPlugin")); UrlLauncherWindowsRegisterWithRegistrar( diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index 22680c99..427d1281 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + connectivity_plus screen_retriever url_launcher_windows win_toast