Caqil / wireguard_flutter

wireguard_flutter
https://pub.dev/packages/wireguard_flutter
MIT License
20 stars 20 forks source link

Context.getPackageName()' on a null object reference - Possibility to run from WorkManager? #15

Open bbijan opened 5 months ago

bbijan commented 5 months ago

Hi

Thanks for a great package :)

Would it be possible to create a variable which could be set so that the vpn could be triggered from the background. This is useful for "snoozing" the vpn for a user defined time. Right now it seems that the package is using Context to get package name which is not accessible from an isolate.

Heres the error log:

E/MainActivity( 7910): com.wireguard.android.util.RootShell$RootShellException
E/MainActivity( 7910):  at com.wireguard.android.util.RootShell.start(RootShell.java:135)
E/MainActivity( 7910):  at app.wachu.wireguard_vpn.WireguardVpnPlugin.createBackend(WireguardVpnPlugin.kt:145)
E/MainActivity( 7910):  at app.wachu.wireguard_vpn.WireguardVpnPlugin.access$createBackend(WireguardVpnPlugin.kt:39)
E/MainActivity( 7910):  at app.wachu.wireguard_vpn.WireguardVpnPlugin$onAttachedToEngine$1.invokeSuspend(WireguardVpnPlugin.kt:104)
E/MainActivity( 7910):  at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/MainActivity( 7910):  at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
E/MainActivity( 7910):  at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
E/MainActivity( 7910):  at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
E/MainActivity( 7910):  at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
E/MainActivity( 7910):  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
E/MainActivity( 7910):  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
E/MainActivity( 7910):  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
E/MainActivity( 7910): Entre 1
E/MainActivity( 7910): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
E/MainActivity( 7910):  at android.net.VpnService.prepare(VpnService.java:208)
E/MainActivity( 7910):  at app.wachu.wireguard_vpn.WireguardVpnPlugin.checkPermission(WireguardVpnPlugin.kt:131)
E/MainActivity( 7910):  at app.wachu.wireguard_vpn.WireguardVpnPlugin.access$checkPermission(WireguardVpnPlugin.kt:39)
E/MainActivity( 7910):  at app.wachu.wireguard_vpn.WireguardVpnPlugin$onAttachedToEngine$1.invokeSuspend(WireguardVpnPlugin.kt:107)
E/MainActivity( 7910):  at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/MainActivity( 7910):  at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
E/MainActivity( 7910):  at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
E/MainActivity( 7910):  at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
E/MainActivity( 7910):  at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
E/MainActivity( 7910):  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
E/MainActivity( 7910):  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
E/MainActivity( 7910):  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
talkbiz commented 4 months ago

facing same issue

talkbiz commented 4 months ago

I have been able to fix it changing the native code to this

override fun onAttachedToActivity(binding: ActivityPluginBinding) { this.activity = binding.activity; }

bbijan commented 4 months ago

override fun onAttachedToActivity(binding: ActivityPluginBinding) { this.activity = binding.activity; }

Hi

Could you provide an example? I've extended ActivityAware in my app's MainActivity.kt and override onAttachedToActivty with your suggestion. However, I'm still getting same error.

However, if I edit the plugins WireguardFlutterPlugin.kt checkPermission() method and manually setting havePermission = true and commenting the rest out, it works.

Thanks.