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() {
        if (tunnel != null) {
            tunnel.close();
            tunnel = null;
        }
    }
}

@@ -304,16 +304,23 @@
 an first
+ = true
 ;%0A%0A    p
@@ -513,16 +513,50 @@
 ption %7B%0A
+        if (tunnel == null) %7B%0A    
         
@@ -581,16 +581,20 @@
         
+  
+  
 // add d
@@ -615,16 +615,20 @@
 ttempts%0A
+    
         
@@ -663,29 +663,24 @@
         
-first = true;
+%7D else %7B
 %0A       
@@ -680,26 +680,24 @@
         
-%7D%0A
         
 if (tunn
@@ -688,37 +688,44 @@
         
-if (tunnel == null) %7B
+first = false;%0A            %7D
 %0A       

Fix tunnel provider connection temporization