From a7c32f5bd4acb2e36d17bba8d611d745080507ba Mon Sep 17 00:00:00 2001 From: Nate Brown Date: Thu, 29 Apr 2021 10:17:43 -0500 Subject: [PATCH] Kotlin active site race on boot fix (#29) --- .../net/defined/mobile_nebula/MainActivity.kt | 14 ++++++++++---- .../main/kotlin/net/defined/mobile_nebula/Sites.kt | 1 + lib/screens/MainScreen.dart | 5 +++++ nebula/go.mod | 8 ++++---- nebula/go.sum | 9 +++++++++ 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/android/app/src/main/kotlin/net/defined/mobile_nebula/MainActivity.kt b/android/app/src/main/kotlin/net/defined/mobile_nebula/MainActivity.kt index 93ffb38..b41c3c3 100644 --- a/android/app/src/main/kotlin/net/defined/mobile_nebula/MainActivity.kt +++ b/android/app/src/main/kotlin/net/defined/mobile_nebula/MainActivity.kt @@ -39,15 +39,12 @@ class MainActivity: FlutterActivity() { //TODO: Initializing in the constructor leads to a context lacking info we need, figure out the right way to do this sites = Sites(flutterEngine) - // Bind against our service to detect which site is running on app boot - val intent = Intent(this, NebulaVpnService::class.java) - bindService(intent, connection, 0) - GeneratedPluginRegistrant.registerWith(flutterEngine); MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result -> when(call.method) { "android.requestPermissions" -> androidPermissions(result) + "android.registerActiveSite" -> registerActiveSite(result) "nebula.parseCerts" -> nebulaParseCerts(call, result) "nebula.generateKeyPair" -> nebulaGenerateKeyPair(result) @@ -73,6 +70,15 @@ class MainActivity: FlutterActivity() { } } + // This is called by the UI _after_ it has finished rendering the site list to avoid a race condition with detecting + // the current active site and attaching site specific event channels in the event the UI app was quit + private fun registerActiveSite(result: MethodChannel.Result) { + // Bind against our service to detect which site is running on app boot + val intent = Intent(this, NebulaVpnService::class.java) + bindService(intent, connection, 0) + result.success(null) + } + private fun nebulaParseCerts(call: MethodCall, result: MethodChannel.Result) { val certs = call.argument("certs") if (certs == "") { diff --git a/android/app/src/main/kotlin/net/defined/mobile_nebula/Sites.kt b/android/app/src/main/kotlin/net/defined/mobile_nebula/Sites.kt index 5046744..2405aee 100644 --- a/android/app/src/main/kotlin/net/defined/mobile_nebula/Sites.kt +++ b/android/app/src/main/kotlin/net/defined/mobile_nebula/Sites.kt @@ -25,6 +25,7 @@ class Sites(private var engine: FlutterEngine) { fun refreshSites(activeSite: String? = null) { val context = MainActivity.getContext()!! val sitesDir = context.filesDir.resolve("sites") + if (!sitesDir.isDirectory) { sitesDir.delete() sitesDir.mkdir() diff --git a/lib/screens/MainScreen.dart b/lib/screens/MainScreen.dart index 94de84b..1298757 100644 --- a/lib/screens/MainScreen.dart +++ b/lib/screens/MainScreen.dart @@ -233,6 +233,11 @@ rmXnR1yvDZi1VPVmnNVY8NMsQpEpbbYlq7rul+ByQvg= } }); + if (Platform.isAndroid) { + // Android suffers from a race to discover the active site and attach site specific event listeners + platform.invokeMethod("android.registerActiveSite"); + } + if (hasErrors) { Utils.popError(context, "Site Error(s)", "1 or more sites have errors and need your attention, problem sites have a red border."); } diff --git a/nebula/go.mod b/nebula/go.mod index aaed68e..11fd84b 100644 --- a/nebula/go.mod +++ b/nebula/go.mod @@ -12,17 +12,17 @@ require ( github.com/miekg/dns v1.1.41 // indirect github.com/nbrownus/go-metrics-prometheus v0.0.0-20210317214817-64743030013d // indirect github.com/prometheus/client_golang v1.10.0 // indirect - github.com/prometheus/common v0.21.0 // indirect + github.com/prometheus/common v0.23.0 // indirect github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect github.com/sirupsen/logrus v1.8.1 - github.com/slackhq/nebula v1.3.1-0.20210422202340-1deb5d98e836 + github.com/slackhq/nebula v1.3.1-0.20210428022318-44cb6975523e github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8 // indirect github.com/vishvananda/netlink v1.1.0 // indirect github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f // indirect golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b golang.org/x/mobile v0.0.0-20210220033013-bdb1ca9a1e08 // indirect - golang.org/x/net v0.0.0-20210421230115-4e50805a0758 // indirect - golang.org/x/sys v0.0.0-20210421221651-33663a62ff08 // indirect + golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 // indirect + golang.org/x/sys v0.0.0-20210426230700-d19ff857e887 // indirect golang.org/x/term v0.0.0-20210422114643-f5beecf764ed // indirect gopkg.in/yaml.v2 v2.4.0 ) diff --git a/nebula/go.sum b/nebula/go.sum index 1e238a6..02f3c12 100644 --- a/nebula/go.sum +++ b/nebula/go.sum @@ -261,6 +261,8 @@ github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB8 github.com/prometheus/common v0.18.0/go.mod h1:U+gB1OBLb1lF3O42bTCL+FK18tX9Oar16Clt/msog/s= github.com/prometheus/common v0.21.0 h1:SMvI2JVldvfUvRVlP64jkIJEC6WiGHJcN2e5tB+ztF8= github.com/prometheus/common v0.21.0/go.mod h1:U+gB1OBLb1lF3O42bTCL+FK18tX9Oar16Clt/msog/s= +github.com/prometheus/common v0.23.0 h1:GXWvPYuTUenIa+BhOq/x+L/QZzCqASkVRny5KTlPDGM= +github.com/prometheus/common v0.23.0/go.mod h1:H6QK/N6XVT42whUeIdI3dp36w49c+/iMDk7UAI2qm7Q= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= @@ -288,6 +290,8 @@ github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/slackhq/nebula v1.3.1-0.20210422202340-1deb5d98e836 h1:kXBj2jeI6HaewEyhYOJtSFRrbigFbQaEV3gOtTd7Yls= github.com/slackhq/nebula v1.3.1-0.20210422202340-1deb5d98e836/go.mod h1:N4OtbI4997CFRdZZiJSOwuQdvslvef5CkWR6Nd+tUB4= +github.com/slackhq/nebula v1.3.1-0.20210428022318-44cb6975523e h1:OStZ9ed/HCuFDa2LOU5izfcFl3Ythk/rc5bFF7I36Vs= +github.com/slackhq/nebula v1.3.1-0.20210428022318-44cb6975523e/go.mod h1:N4OtbI4997CFRdZZiJSOwuQdvslvef5CkWR6Nd+tUB4= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= @@ -384,6 +388,8 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210421230115-4e50805a0758 h1:aEpZnXcAmXkd6AvLb2OPt+EN1Zu/8Ne3pCqPjja5PXY= golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM= +golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0= +golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -430,6 +436,9 @@ golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210421221651-33663a62ff08 h1:qyN5bV+96OX8pL78eXDuz6YlDPzCYgdW74H5yE9BoSU= golang.org/x/sys v0.0.0-20210421221651-33663a62ff08/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210426230700-d19ff857e887 h1:dXfMednGJh/SUUFjTLsWJz3P+TQt9qnR11GgeI3vWKs= +golang.org/x/sys v0.0.0-20210426230700-d19ff857e887/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210422114643-f5beecf764ed h1:Ei4bQjjpYUsS4efOUz+5Nz++IVkHk87n2zBA0NxBWc0= golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=