|
| 1 | +package link.infra.sslsocks.gui; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.ComponentName; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.Intent; |
| 7 | +import android.content.ServiceConnection; |
| 8 | +import android.os.IBinder; |
| 9 | +import android.os.RemoteException; |
| 10 | +import android.util.Log; |
| 11 | + |
| 12 | +import java.lang.ref.WeakReference; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Objects; |
| 15 | + |
| 16 | +import de.blinkt.openvpn.api.APIVpnProfile; |
| 17 | +import de.blinkt.openvpn.api.IOpenVPNAPIService; |
| 18 | + |
| 19 | +public class OpenVPNIntegrationHandler { |
| 20 | + private IOpenVPNAPIService srv = null; |
| 21 | + public static final int PERMISSION_REQUEST = 100; |
| 22 | + public static final int VPN_PERMISSION_REQUEST = 101; |
| 23 | + |
| 24 | + private static final String TAG = OpenVPNIntegrationHandler.class.getSimpleName(); |
| 25 | + private WeakReference<Context> ctxRef; |
| 26 | + private boolean isActivity = true; |
| 27 | + private final Runnable doneCallback; |
| 28 | + private final String profileName; |
| 29 | + private final boolean shouldDisconnect; |
| 30 | + |
| 31 | + public OpenVPNIntegrationHandler(Activity ctx, Runnable doneCallback, String profile, boolean shouldDisconnect) { |
| 32 | + this.ctxRef = new WeakReference<Context>(ctx); |
| 33 | + this.doneCallback = doneCallback; |
| 34 | + this.profileName = profile; |
| 35 | + this.shouldDisconnect = shouldDisconnect; |
| 36 | + Log.d(TAG, "created"); |
| 37 | + } |
| 38 | + |
| 39 | + public OpenVPNIntegrationHandler(Context ctx, Runnable doneCallback, String profile, boolean shouldDisconnect) { |
| 40 | + isActivity = false; |
| 41 | + this.ctxRef = new WeakReference<>(ctx); |
| 42 | + this.doneCallback = doneCallback; |
| 43 | + this.profileName = profile; |
| 44 | + this.shouldDisconnect = shouldDisconnect; |
| 45 | + Log.d(TAG, "created"); |
| 46 | + } |
| 47 | + |
| 48 | + private final ServiceConnection mConnection = new ServiceConnection() { |
| 49 | + public void onServiceConnected(ComponentName className, IBinder service) { |
| 50 | + srv = IOpenVPNAPIService.Stub.asInterface(service); |
| 51 | + |
| 52 | + try { |
| 53 | + Intent intent = srv.prepare(ctxRef.get().getPackageName()); |
| 54 | + if (intent != null && isActivity) { |
| 55 | + Log.d(TAG, "requesting permission"); |
| 56 | + ((Activity)ctxRef.get()).startActivityForResult(intent, PERMISSION_REQUEST); |
| 57 | + } else { |
| 58 | + doVpnPermissionRequest(); |
| 59 | + } |
| 60 | + } catch (RemoteException e) { |
| 61 | + Log.e(TAG, "Failed to connect to OpenVPN", e); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public void onServiceDisconnected(ComponentName className) { |
| 66 | + srv = null; |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + public void bind() { |
| 71 | + if (srv != null || ctxRef.get() == null) return; |
| 72 | + Intent intent = new Intent(IOpenVPNAPIService.class.getName()); |
| 73 | + intent.setPackage("de.blinkt.openvpn"); |
| 74 | + ctxRef.get().bindService(intent, mConnection, Context.BIND_AUTO_CREATE); |
| 75 | + Log.d(TAG, "bound"); |
| 76 | + } |
| 77 | + |
| 78 | + public void unbind() { |
| 79 | + if (ctxRef.get() == null) return; |
| 80 | + ctxRef.get().unbindService(mConnection); |
| 81 | + } |
| 82 | + |
| 83 | + public void doVpnPermissionRequest() { |
| 84 | + try { |
| 85 | + Intent intent = srv.prepareVPNService(); |
| 86 | + if (intent != null && isActivity) { |
| 87 | + Log.d(TAG, "requesting vpn perms"); |
| 88 | + ((Activity)ctxRef.get()).startActivityForResult(intent, VPN_PERMISSION_REQUEST); |
| 89 | + } else { |
| 90 | + if (shouldDisconnect) { |
| 91 | + disconnect(); |
| 92 | + } else { |
| 93 | + connectProfile(); |
| 94 | + } |
| 95 | + } |
| 96 | + } catch (RemoteException e) { |
| 97 | + Log.e(TAG, "Failed to connect to OpenVPN", e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public void connectProfile() { |
| 102 | + try { |
| 103 | + List<APIVpnProfile> profiles = srv.getProfiles(); |
| 104 | + APIVpnProfile foundProfile = null; |
| 105 | + for (APIVpnProfile profile : profiles) { |
| 106 | + if (Objects.equals(profile.mName, profileName)) { |
| 107 | + foundProfile = profile; |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + if (foundProfile == null) { |
| 112 | + Log.e(TAG, "Failed to find profile"); |
| 113 | + return; |
| 114 | + } |
| 115 | + Log.d(TAG, "starting profile"); |
| 116 | + srv.startProfile(foundProfile.mUUID); |
| 117 | + } catch (RemoteException e) { |
| 118 | + Log.e(TAG, "Failed to connect to OpenVPN", e); |
| 119 | + } |
| 120 | + doneCallback.run(); |
| 121 | + } |
| 122 | + |
| 123 | + public void disconnect() { |
| 124 | + try { |
| 125 | + srv.disconnect(); |
| 126 | + } catch (RemoteException e) { |
| 127 | + Log.e(TAG, "Failed to connect to OpenVPN", e); |
| 128 | + } |
| 129 | + if (shouldDisconnect) { |
| 130 | + doneCallback.run(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments