User0987
User0987

Reputation: 199

How to use wireguard tunnel library?

I want to create a third-party VPN app by using the library com.wireguard.android:tunnel (1.0.20211029) Can anyone help me to provide me any good resources or guide me?

In my app I am importing .config file from file manager now how to connect that file to server using tunnel library . I am using java .

I did followed https://github.com/WireGuard/wireguard-android but The app wraps a lot of the tunnel library code. I am looking for a simple example of how to use the tunnel library directly without all the wrapping with TunnelManager and ObservableTunnel.

Please guide me

Upvotes: 10

Views: 7049

Answers (2)

kimono
kimono

Reputation: 41

in my case the dns address needed to be added and I added it to anton2319's code and the vpn worked. Like this. Thank you anton2319.

backend.setState(tunnel, UP, new Config.Builder()
                  .setInterface(interfaceBuilder.addDnsServer(InetAddress.getByName("your dns adress")).addAddress(InetNetwork.parse("10.0.0.2/32")).parsePrivateKey("privatekeybase64").build())

Upvotes: 2

Anton2319
Anton2319

Reputation: 160

UPD: added request intent, because it is necessary on first connection

UPD 2: I have a made a step-by-step guide: https://github.com/Anton2319/Anton2319/blob/master/articles/wireguard-guide/article.md

I think this code will work for you:

In your main activity, use this (replace IP, pubkey and privatekey):

Tunnel tunnel = new WgTunnel();
Intent intentPrepare = GoBackend.VpnService.prepare(this);
if(intentPrepare != null) {
    startActivityForResult(intentPrepare, 0);
}
Interface.Builder interfaceBuilder = new Interface.Builder();
Peer.Builder peerBuilder = new Peer.Builder();
Backend backend = new GoBackend(this);

AsyncTask.execute(new Runnable() {
    @Override
    public void run() {
        try {
            backend.setState(tunnel, UP, new Config.Builder()
                      .setInterface(interfaceBuilder.addAddress(InetNetwork.parse("10.0.0.2/32")).parsePrivateKey("privatekeybase64").build())
                        .addPeer(peerBuilder.addAllowedIp(InetNetwork.parse("0.0.0.0/0")).setEndpoint(InetEndpoint.parse("yourhost:51820")).parsePublicKey("pubkeybase64").build())
                        .build());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

In the same package as your main activity, create this class:

package package.name.here;

import com.wireguard.android.backend.Tunnel;

public class WgTunnel implements Tunnel {
    @Override
    public String getName() {
        return "wgpreconf";
    }

    @Override
    public void onStateChange(State newState) {
    }
}

In AndroidManifest.xml:

<application ...>
    <service android:name="com.wireguard.android.backend.GoBackend$VpnService" android:permission="android.permission.BIND_VPN_SERVICE" android:exported="true">
                <intent-filter>
                    <action android:name="android.net.VpnService"/>
                </intent-filter>
    </service>
</application>
<uses-permission android:name="android.permission.INTERNET" />

Upvotes: 12

Related Questions