2

I'm trying to enable and disable Wireguard tunnels through the ADB shell.

I can see the followings actions and receivers are available :

adb:# dumpsys package com.wireguard.android
...
Receiver Resolver Table:
  Non-Data Actions:
      com.wireguard.android.action.SET_TUNNEL_UP:
        4166aa1 com.wireguard.android/.model.TunnelManager$IntentReceiver filter 631a6bf
          Action: "com.wireguard.android.action.REFRESH_TUNNEL_STATES"
          Action: "com.wireguard.android.action.SET_TUNNEL_UP"
          Action: "com.wireguard.android.action.SET_TUNNEL_DOWN"
      com.wireguard.android.action.REFRESH_TUNNEL_STATES:
        4166aa1 com.wireguard.android/.model.TunnelManager$IntentReceiver filter 631a6bf
          Action: "com.wireguard.android.action.REFRESH_TUNNEL_STATES"
          Action: "com.wireguard.android.action.SET_TUNNEL_UP"
          Action: "com.wireguard.android.action.SET_TUNNEL_DOWN"
      com.wireguard.android.action.SET_TUNNEL_DOWN:
        4166aa1 com.wireguard.android/.model.TunnelManager$IntentReceiver filter 631a6bf
          Action: "com.wireguard.android.action.REFRESH_TUNNEL_STATES"
          Action: "com.wireguard.android.action.SET_TUNNEL_UP"
          Action: "com.wireguard.android.action.SET_TUNNEL_DOWN"
      android.intent.action.BOOT_COMPLETED:
        4f4cb74 com.wireguard.android/.BootShutdownReceiver filter 426c4de
          Action: "android.intent.action.ACTION_SHUTDOWN"
          Action: "android.intent.action.BOOT_COMPLETED"
      android.intent.action.ACTION_SHUTDOWN:
        4f4cb74 com.wireguard.android/.BootShutdownReceiver filter 426c4de
          Action: "android.intent.action.ACTION_SHUTDOWN"
          Action: "android.intent.action.BOOT_COMPLETED"
...

Looking at the package's sourcecode, I see that com.wireguard.android/.model.TunnelManager$IntentReceiver asks for the name of the tunnel to enable/disable to be passed as an extra string :

 val tunnelName = intent.getStringExtra("tunnel") ?: return@launch
 val tunnels = manager.getTunnels()
 val tunnel = tunnels[tunnelName] ?: return@launch

So I tried two different versions of the same command to enable a tunnel named my-tunnel :


adb:# am broadcast -a com.wireguard.android.action.SET_TUNNEL_UP -e tunnel my-tunnel
Broadcasting: Intent { act=com.wireguard.android.action.SET_TUNNEL_UP flg=0x400000 (has extras) }
Broadcast completed: result=0
# am broadcast -a com.wireguard.android.action.SET_TUNNEL_UP -e tunnel my-tunnel -n com.wireguard.android/.model.TunnelManager$IntentReceiver                                                    
Broadcasting: Intent { act=com.wireguard.android.action.SET_TUNNEL_UP flg=0x400000 cmp=com.wireguard.android/.model.TunnelManager (has extras) }
Broadcast completed: result=0

But after checking on my phone my-tunnel is still disabled.

What am I missing ? Note that I'm pretty new to using ADB commands so it might be something obvious.

2
  • When calling a Intent/Broadcast via adb and am then actual code is executed asynchronously, therefore you never get an result. Effectively this means the result code you see is meaningless.
    – Robert
    Dec 16, 2020 at 11:46
  • Good to know ! I edited my question in accordance.
    – Quentin
    Dec 16, 2020 at 15:51

1 Answer 1

2

This is the most reliable way that I have found:

am broadcast -a com.wireguard.android.action.SET_TUNNEL_UP -n 'com.wireguard.android/.model.TunnelManager$IntentReceiver' -e tunnel <tunnel_name>
am broadcast -a com.wireguard.android.action.SET_TUNNEL_DOWN -n 'com.wireguard.android/.model.TunnelManager$IntentReceiver' -e tunnel <tunnel_name>

Notes:

  • This only works if adb has root rights.
  • You need to enable "Allow remote control apps" under "Advanced" in the settings of the WireGuard app.
  • If you also want this to work when the WireGuard app is not running, you need to disable background battery optimizations for the app.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .