From 5cd9b7fdb0e78fbe30aca02ad75306a3a5208d3c Mon Sep 17 00:00:00 2001 From: cyBerta Date: Sat, 19 Oct 2019 02:10:30 +0200 Subject: fix error handling in Shapeshifter class --- .../de/blinkt/openvpn/core/OpenVPNService.java | 6 +-- .../pluggableTransports/Shapeshifter.java | 61 ++++++++++++++++------ 2 files changed, 46 insertions(+), 21 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java b/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java index e446021f..2c4462e0 100644 --- a/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java +++ b/app/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java @@ -387,11 +387,7 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac if (mProfile.mUsePluggableTransports) { Obfs4Connection obfs4Connection = (Obfs4Connection) connection; shapeshifter = new Shapeshifter(obfs4Connection.getDispatcherOptions()); - if (!shapeshifter.start()) { - //TODO: implement useful error handling - Log.e(TAG, "Cannot initialize shapeshifter dispatcher for obfs4 connection. Shutting down."); - VpnStatus.logError("Cannot initialize shapeshifter dispatcher for obfs4 connection. Shutting down."); - } + shapeshifter.start(); } VpnStatus.logInfo(R.string.building_configration); diff --git a/app/src/main/java/se/leap/bitmaskclient/pluggableTransports/Shapeshifter.java b/app/src/main/java/se/leap/bitmaskclient/pluggableTransports/Shapeshifter.java index f7b18083..6d3b1db2 100644 --- a/app/src/main/java/se/leap/bitmaskclient/pluggableTransports/Shapeshifter.java +++ b/app/src/main/java/se/leap/bitmaskclient/pluggableTransports/Shapeshifter.java @@ -18,12 +18,16 @@ package se.leap.bitmaskclient.pluggableTransports; import android.os.AsyncTask; +import android.os.Handler; +import android.os.Looper; import android.util.Log; +import java.lang.ref.WeakReference; + import de.blinkt.openvpn.core.VpnStatus; import shapeshifter.ShapeShifter; -public class Shapeshifter { +public class Shapeshifter { public static final String DISPATCHER_PORT = "4430"; public static final String DISPATCHER_IP = "127.0.0.1"; @@ -32,31 +36,48 @@ public class Shapeshifter { ShapeShifter shapeShifter; ShapeshifterErrorListner shapeshifterErrorListner; + public interface ShapeshifterErrorListenerCallback { + void onStarted(); + } + public Shapeshifter(Obfs4Options options) { shapeShifter = new ShapeShifter(); + setup(options); + Log.d(TAG, "shapeshifter initialized with: \n" + shapeShifter.toString()); + } + + private void setup(Obfs4Options options) { shapeShifter.setSocksAddr(DISPATCHER_IP+":"+DISPATCHER_PORT); shapeShifter.setTarget(options.remoteIP+":"+options.remotePort); shapeShifter.setCert(options.cert); - Log.d(TAG, "shapeshifter initialized with: \n" + shapeShifter.toString()); } - public boolean start() { + public void start() { try { - shapeshifterErrorListner = new ShapeshifterErrorListner(); + ShapeshifterErrorListenerCallback errorListenerCallback = () -> { + ; + Handler handler = new Handler(Looper.getMainLooper()); + handler.postDelayed(() -> { + try { + Log.d(TAG, "shapeshifter open"); + shapeShifter.open(); + } catch (Exception e) { + e.printStackTrace(); + } + }, 200); + }; + shapeshifterErrorListner = new ShapeshifterErrorListner(errorListenerCallback); shapeshifterErrorListner.execute(shapeShifter); - shapeShifter.open(); - Log.d(TAG, "shapeshifter opened"); - return true; + } catch (Exception e) { - VpnStatus.logError("SHAPESHIFTER ERROR " + e.getLocalizedMessage()); e.printStackTrace(); } - return false; } public boolean stop() { try { shapeShifter.close(); + shapeshifterErrorListner.cancel(true); Log.d(TAG, "shapeshifter closed"); return true; } catch (Exception e) { @@ -67,28 +88,36 @@ public class Shapeshifter { } static class ShapeshifterErrorListner extends AsyncTask { + + WeakReference callbackWeakReference; + + public ShapeshifterErrorListner(ShapeshifterErrorListenerCallback callback) { + callbackWeakReference = new WeakReference<>(callback); + } + @Override protected Void doInBackground(ShapeShifter... shapeShifters) { ShapeShifter shapeshifter = shapeShifters[0]; + Log.d(TAG, "Shapeshifter error listener started"); + ShapeshifterErrorListenerCallback callback = callbackWeakReference.get(); + if (callback != null) { + callback.onStarted(); + } try { shapeshifter.getLastError(); } catch (Exception e) { e.printStackTrace(); - VpnStatus.logError("SHAPESHIFTER ERROR " + e.getLocalizedMessage()); + Log.e(TAG, "SHAPESHIFTER ERROR: " + e.getLocalizedMessage()); + VpnStatus.logError(VpnStatus.ErrorType.SHAPESHIFTER); + VpnStatus.logError(e.getLocalizedMessage()); try { shapeshifter.close(); } catch (Exception ex) { ex.printStackTrace(); - VpnStatus.logError("SHAPESHIFTER ERROR " + e.getLocalizedMessage()); } } return null; } - - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - } } } -- cgit v1.2.3