Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix solution for launching the service on pre-8.0 devices. #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

w3rkzeug
Copy link

Fix for the following error that occurs when launching the service from background (onBoot f.e.):
E/WireGuard/TunnelManager: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.tun.android.debug/com.wireguard.android.backend.GoBackend$VpnService }: app is in background

@w3rkzeug w3rkzeug changed the title Fix solution for launching the service pre-8.0 devices. Fix solution for launching the service on pre-8.0 devices. Jun 18, 2021
@msfjarvis
Copy link
Member

When are you encountering this? We already ship a trampoline activity that is launched by the QS tile to bring the app to the foreground before the GoBackend code path is hit.

@msfjarvis msfjarvis self-assigned this Jun 18, 2021
@w3rkzeug
Copy link
Author

w3rkzeug commented Jun 18, 2021

Oh , I embedded the library implementation 'com.wireguard.android:tunnel:1.0.20210211' version, applied the fix & it worked tho.

@w3rkzeug
Copy link
Author

btw this may occur when calling from BootShutdownReceiver -> tunnelManager.restoreState(true),followed by this https://github.com/WireGuard/wireguard-android/blob/master/ui/src/main/java/com/wireguard/android/BootShutdownReceiver.kt

@msfjarvis
Copy link
Member

Oh , I embedded the library implementation 'com.wireguard.android:tunnel:$wireguardTunnelVersion' version, applied the fix & it worked tho.

Ah, I see. The problem with launching a foreground service is that it requires a persistent notification as well as an additional permission. My personal recommendation would be to replicate the trampoline workaround that we use in the WireGuard app for user-initiated triggers and relying on the Android platform's always-on VPN option for starting up right after boot.

@msfjarvis
Copy link
Member

btw this may occur when calling from BootShutdownReceiver -> tunnelManager.restoreState(true),followed by this https://github.com/WireGuard/wireguard-android/blob/master/ui/src/main/java/com/wireguard/android/BootShutdownReceiver.kt

Yeah I guessed as much. I will look into fixing this separately. In the meantime can you please force push your commit with your real name as author and a Signed-off-by line? That'll make my life easier in case I end up merging your patch.

@w3rkzeug
Copy link
Author

Oh , I embedded the library implementation 'com.wireguard.android:tunnel:$wireguardTunnelVersion' version, applied the fix & it worked tho.

Ah, I see. The problem with launching a foreground service is that it requires a persistent notification as well as an additional permission. My personal recommendation would be to replicate the trampoline workaround that we use in the WireGuard app for user-initiated triggers and relying on the Android platform's always-on VPN option for starting up right after boot.

Well at this point the best option (tested) would be to create a trampoline foreground service to trigger from BootShutdownReceiver on ACTION_BOOT_COMPLETED :)
Anyway Thanks

@zzb-zzb
Copy link

zzb-zzb commented Dec 6, 2022

implementation 'com.wireguard.android:tunnel:1.0.20220516' not on mavent
有没有谁知道,解决方案是什么

@zzb-zzb
Copy link

zzb-zzb commented Dec 6, 2022

Failed to resolve: com.wireguard.android:tunnel:1.0.20220516

@zzb-zzb
Copy link

zzb-zzb commented Dec 6, 2022

btw this may occur when calling from BootShutdownReceiver -> tunnelManager.restoreState(true),followed by this https://github.com/WireGuard/wireguard-android/blob/master/ui/src/main/java/com/wireguard/android/BootShutdownReceiver.kt

Yeah I guessed as much. I will look into fixing this separately. In the meantime can you please force push your commit with your real name as author and a Signed-off-by line? That'll make my life easier in case I end up merging your patch.

Failed to resolve: com.wireguard.android:tunnel:1.0.20220516

@@ -219,7 +219,11 @@ private void setStateInternal(final Tunnel tunnel, @Nullable final Config config
final VpnService service;
if (!vpnService.isDone()) {
Log.d(TAG, "Requesting to start VpnService");
context.startService(new Intent(context, VpnService.class));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, VpnService.class));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are starting the service as a foreground service, you should accompany it with a notification.
Also, according to the guidelines, it is okay to start the VPN service as a background service, but you must promote it to foreground by showing the notification using the Service#setForeground method.

https://developer.android.com/guide/topics/connectivity/vpn#starting_a_service

Thoughts?

@zx2c4-bot zx2c4-bot force-pushed the master branch 4 times, most recently from 581ab92 to ec126a9 Compare March 28, 2023 14:04
@zx2c4-bot zx2c4-bot force-pushed the master branch 5 times, most recently from b2568c4 to b1324e2 Compare April 4, 2023 13:32
@zx2c4-bot zx2c4-bot force-pushed the master branch 4 times, most recently from ab02c51 to 44a2603 Compare April 5, 2023 10:36
@zx2c4-bot zx2c4-bot force-pushed the master branch 3 times, most recently from a1670c8 to dc1860c Compare April 5, 2023 11:54
@zx2c4-bot zx2c4-bot force-pushed the master branch 5 times, most recently from baa1257 to 79b8c34 Compare April 27, 2023 13:52
@zx2c4-bot zx2c4-bot force-pushed the master branch 13 times, most recently from c046706 to 40eaa54 Compare May 5, 2023 11:18
aperfilyev added a commit to aperfilyev/wireguard-android that referenced this pull request Oct 16, 2023
In my opinion, it would be better to start a VPN Service as a foreground one, but I've seen(WireGuard#29) that you don't want additional permissions that come with it.
aperfilyev added a commit to aperfilyev/wireguard-android that referenced this pull request Oct 16, 2023
In my opinion, it would be better to start a VPN Service as a foreground one, but I've seen(WireGuard#29) that you don't want additional permissions that come with it.

Signed-off-by: Alexander Perfilyev <[email protected]>
@zx2c4-bot zx2c4-bot force-pushed the master branch 4 times, most recently from 827495b to 4ba8794 Compare October 22, 2023 00:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants