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;
        }
    }
}

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 = true;

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

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

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

Fix tunnel provider connection temporization
Fix tunnel provider connection temporization

The mechanism to wait several seconds between connection attempts to
provide a tunnel to PersistentRelayTunnel was totally broken.

Fix it to wait 5 seconds only before creating a new tunnel, except the
very first time.
