Anton2319 / WireGuardExamples

Examples for https://github.com/Anton2319/Anton2319/blob/master/articles/wireguard-guide/article.md
https://github.com/Anton2319/Anton2319/blob/master/articles/wireguard-guide/article.md
11 stars 4 forks source link

Stop exist backend and tunnel which running. #2

Closed dovanvu1792 closed 1 year ago

dovanvu1792 commented 1 year ago

I have just connect to VPN and force close app, and then reopen app, i don't know how can found backend and tunnel exist before to stop it.

Anton2319 commented 1 year ago

When you are force-stopping your app it's your system duty to kill the VPN tunnel. If you are experiencing incorrect behavior I recommend you to try to run the code on the emulator or original google pixel device. Comment out if you have difficulties or if I understood you incorrectly

dovanvu1792 commented 1 year ago

Step 1: Click Connect VPN -> (vpn connected) Step 2: Close app (click recent list app and force close app) Step 3: ReOpen app, state of vpn should be "Connected". But state is "not connected" because this app was close and recreate instance of new GoBackend(this), new WgTunnel().

So, how to check vpn running and close this old session?

Anton2319 commented 1 year ago

Does that help?

bhargavmagic commented 5 months ago

Hello It seems that it works But i can't have access on internet. I have put in android manifest permission for internet but when i press connect it can't explore the internet in order to see my ip. Do you have any idea what is going on ?

using this library

Anton2319 commented 5 months ago

Hello, please share your client code and server configuration, otherwise I am going to be unable to help

bhargavmagic commented 5 months ago

this is a my server configuration

[Interface] Address = 10.7.0.2/24 DNS = 8.8.8.8, 8.8.4.4 PrivateKey = II1EglhgBoTGElVeErKF9/f1BPZrU1LERn4IWW2JQ0E=

[Peer] PublicKey = a+OPJmelJn/kbSigVt0w0eiI75+r8US/OW7BG6c58DY= PresharedKey = ghbOVYZF0oSfmB6S0yY0sUwy7h8k8YVIHyoV092Z5tY= AllowedIPs = 0.0.0.0/0, ::/0 Endpoint = 13.57.258.60:51820 PersistentKeepalive = 25

code connect to vpn

private void connectToVpn() { Interface.Builder interfaceBuilder = new Interface.Builder(); Peer.Builder peerBuilder = new Peer.Builder();

    AsyncTask.execute(() -> {

        try {
            Config.Builder configBuilder = new Config.Builder()
                    .setInterface(interfaceBuilder
                            .addAddress(InetNetwork.parse(tunnelModel.getIP()))
                            .parsePrivateKey(tunnelModel.getPrivateKey())
                            .build())
                    .addPeer(peerBuilder
                            .addAllowedIps(tunnelModel.getAllowedIPs())
                            .setEndpoint(InetEndpoint.parse(tunnelModel.getEndpoint()))
                            .setPersistentKeepalive(tunnelModel.getPersistentKeepalive())
                            .parsePublicKey(tunnelModel.getPublicKey())
                            .build());

            try {
                String[] dnsServers = tunnelModel.getDns().split(", ");
                for (String dnsServer : dnsServers) {
                    interfaceBuilder.addDnsServer(InetAddress.getByName(dnsServer));
                }
                Log.d("VPNConfig", "DNS servers added: " + Arrays.toString(dnsServers));
            } catch (Exception e) {
                Log.e("VPNConfig", "Failed to add DNS servers", e);
            }

            if (tunnelModel.getPresharedKey() != null && !tunnelModel.getPresharedKey().isEmpty()) {
                peerBuilder.parsePreSharedKey(tunnelModel.getPresharedKey());
                Log.d("VPNConfig", "Pre-shared key added");
            }

            backend.setState(tunnel, UP, configBuilder.build());
            vpnConnected = true;
            runOnUiThread(() -> Toast.makeText(this, "VPN Connected", Toast.LENGTH_SHORT).show());
        } catch (Exception e) {
            e.printStackTrace();
            runOnUiThread(() -> Toast.makeText(this, "Failed to connect VPN", Toast.LENGTH_SHORT).show());
        }
    });
}
bhargavmagic commented 5 months ago

how to add connect internet start to vpn ?

Anton2319 commented 5 months ago

You haven't added a preshared key to your client configuration. You can either do it like this: https://javadoc.io/static/com.wireguard.android/tunnel/1.0.20230706/com/wireguard/config/Peer.Builder.html#parsePreSharedKey(String) or just remove preSharedKey field from the server since it is not necessary for most users

bhargavmagic commented 5 months ago

Ohhh its my mistake, Now it works fine. Thank you so much!!