WireGuard Android

repository·master·Indexed 23 days ago

https://github.com/wireguard/wireguard-android

An Android GUI for the WireGuard VPN protocol that manages tunnels using kernel or userspace implementations. It includes a tunnel library available via Maven Central, a TunnelManager for lifecycle and state control, and a FileConfigStore for persisting configurations in wg-quick format. The project provides APIs for initializing tunnels (wgTurnOn), shutting them down (wgTurnOff), and controlling tunnel states via Android Broadcast Intents.

Tokens
1.6K
Snippets
3
Records
15
Agent score
82%

What's inside wireguard-android

  1. Build the WireGuard Android app

    master

    To build the WireGuard Android application from source, clone the repository including all submodules and use the Gradle wrapper to assemble a release build. macOS users may require flock(1) to complete the build process.

    $ git clone --recurse-submodules https://git.zx2c4.com/wireguard-android
    $ cd wireguard-android
    $ ./gradlew assembleRelease
  2. Embed the WireGuard tunnel library

    master

    You can embed the WireGuard tunnel library into your own Android project via Maven Central. The library requires Java 8+ support, so you must enable library desugaring in your Gradle configuration.

    implementation 'com.wireguard.android:tunnel:$wireguardTunnelVersion'
  3. Configure Java 8 desugaring for the tunnel library

    master

    Because the WireGuard tunnel library uses Java 8 features, you must configure your build.gradle to support desugaring. Set the sourceCompatibility and targetCompatibility to JavaVersion.VERSION_17 (or your required version) and enable coreLibraryDesugaring.

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
        coreLibraryDesugaringEnabled = true
    }
    
    dependencies {
        coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:2.0.3"
    }
  4. Manage WireGuard tunnels with TunnelManager

    master

    The TunnelManager class is the primary interface for managing the lifecycle and state of WireGuard tunnels. It mediates between the ConfigStore (persistent storage) and the Backend (the actual VPN engine).

    Key capabilities include:

    • Creating and Deleting Tunnels: Adding new configurations or removing existing ones.
    • State Control: Bringing tunnels UP or DOWN.
    • Configuration Management: Loading, saving, and renaming tunnel configurations.
    • Observability: Providing access to an observable list of tunnels and their real-time statistics.
  5. Get current configuration with wgGetConfig

    master

    Use wgGetConfig to retrieve the current WireGuard configuration settings from an active tunnel.

    Parameters:

    • tunnelHandle (int32): The unique handle returned by wgTurnOn.

    Returns:

    • A pointer to a C-style string (*C.char) containing the current settings. Note: The caller is responsible for managing the memory of the returned string according to C conventions.
  6. Start a WireGuard tunnel with wgTurnOn

    master

    Use wgTurnOn to initialize a WireGuard device using a provided TUN file descriptor. This function sets up the device, applies initial settings, starts the UAPI listener for management, and brings the interface up.

    Parameters:

    • interfaceName (string): The name of the network interface.
    • tunFd (int32): The file descriptor of the existing TUN device.
    • settings (string): The initial WireGuard configuration settings (UAPI format).

    Returns:

    • An int32 representing a unique tunnelHandle if successful, or -1 if an error occurs.
  7. Control tunnel state and configuration

    master

    You can programmatically control whether a tunnel is active or modify its settings.

    • setTunnelState(tunnel, state): Transitions a tunnel to the requested Tunnel.State (e.g., UP or DOWN).
    • setTunnelConfig(tunnel, config): Updates the configuration for an existing tunnel.
    • setTunnelName(tunnel, name): Renames a tunnel. This handles the necessary backend and storage updates to ensure consistency.
    • getTunnelStatistics(tunnel): Retrieves real-time Statistics for the specified tunnel.
  8. Create and delete tunnels in TunnelManager

    master

    Use create to add a new tunnel and delete to remove one.

    • create(name, config): Creates a new tunnel with the specified name and config. Throws IllegalArgumentException if the name is invalid or already exists.
    • delete(tunnel): Removes the specified ObservableTunnel. If the tunnel is currently UP, it is first brought DOWN via the backend before deletion.
  9. Use FileConfigStore for tunnel configuration persistence

    master

    The FileConfigStore is an implementation of the ConfigStore interface that persists WireGuard tunnel configurations as files in the application's internal files directory. It uses a wg-quick-style format, where each tunnel is stored in a .conf file named after the tunnel.

    Key behaviors:

    • File Naming: Tunnels are identified by a name, which corresponds to a file named {name}.conf.
    • Format: Configurations are serialized using Config.toWgQuickString() and stored as UTF-8 encoded text.
    • Enumeration: Calling enumerate() returns a set of all tunnel names currently stored (by stripping the .conf suffix from existing files).