package com.genymobile.gnirehtet;

import android.net.VpnService;

import java.io.IOException;

/**
 * Provide a valid {@link RelayTunnel}, creating a new one if necessary.
 */
public class RelayTunnelProvider {

    private final VpnService vpnService;
    private RelayTunnel tunnel;
    private boolean first;

    public RelayTunnelProvider(VpnService vpnService) {
        this.vpnService = vpnService;
    }

    public synchronized RelayTunnel getCurrentTunnel() throws IOException, InterruptedException {
        if (!first) {
            // add delay between attempts
            Thread.sleep(5000);
            first = true;
        }
        if (tunnel == null) {
            tunnel = RelayTunnel.open(vpnService);
        }
        return tunnel;
    }

    public synchronized void invalidateTunnel() {
        tunnel.close();
        tunnel = null;
    }
}

@@ -805,24 +805,58 @@
 eTunnel() %7B%0A
+        if (tunnel != null) %7B%0A    
         tunn
@@ -867,32 +867,36 @@
 lose();%0A        
+  
+  
 tunnel = null;%0A 
@@ -894,16 +894,26 @@
 = null;%0A
+        %7D%0A
     %7D%0A%7D%0A

Fix NPE on invalidateTunnel()