0
0
mirror of https://github.com/schwabe/ics-openvpn.git synced 2024-09-19 19:42:29 +02:00

Implement also Connect for the Remote API

This commit is contained in:
Arne Schwabe 2018-03-22 13:51:07 +01:00
parent b6180c14ff
commit 3eca5f7e42
9 changed files with 90 additions and 32 deletions

View File

@ -73,10 +73,10 @@ A: public class StartOpenVPNActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String EXTRA_NAME = "de.blinkt.openvpn.shortcutProfileName";
final String EXTRA_NAME = "de.blinkt.openvpn.api.profileName";
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName("de.blinkt.openvpn", "de.blinkt.openvpn.LaunchVPN");
shortcutIntent.setClassName("de.blinkt.openvpn", "de.blinkt.openvpn.api.ConnectVPN");
shortcutIntent.putExtra(EXTRA_NAME,"upb ssl");
startActivity(shortcutIntent);
}

View File

@ -230,6 +230,12 @@
android:exported="true"
android:name=".api.DisconnectVPN"
android:targetActivity=".api.RemoteAction" />
<activity-alias
android:exported="true"
android:name=".api.ConnectVPN"
android:targetActivity=".api.RemoteAction" />
</application>
</manifest>

View File

@ -22,4 +22,6 @@ interface IOpenVPNServiceInternal {
boolean stopVPN(boolean replaceConnection);
void addAllowedExternalApp(String packagename);
boolean isAllowedExternalApp(String packagename);
}

View File

@ -142,7 +142,7 @@ public class LaunchVPN extends Activity {
VpnProfile profileToConnect = ProfileManager.get(this, shortcutUUID);
if (shortcutName != null && profileToConnect == null) {
profileToConnect = ProfileManager.getInstance(this).getProfileByName(shortcutName);
if (!(new ExternalAppDatabase(this).checkRemoteActionPermission(this))) {
if (!(new ExternalAppDatabase(this).checkRemoteActionPermission(this, getCallingPackage()))) {
finish();
return;
}

View File

@ -19,6 +19,8 @@ import java.util.Set;
import de.blinkt.openvpn.core.Preferences;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
public class ExternalAppDatabase {
Context mContext;
@ -89,19 +91,17 @@ public class ExternalAppDatabase {
}
public boolean checkRemoteActionPermission(Activity a) {
String callingPackage = a.getCallingPackage();
public boolean checkRemoteActionPermission(Context c, String callingPackage) {
if (callingPackage == null)
callingPackage = ConfirmDialog.ANONYMOUS_PACKAGE;
if (isAllowed(callingPackage)) {
return true;
} else {
Intent confirmDialog = new Intent(a, ConfirmDialog.class);
Intent confirmDialog = new Intent(c, ConfirmDialog.class);
confirmDialog.addFlags(FLAG_ACTIVITY_NEW_TASK);
confirmDialog.putExtra(ConfirmDialog.EXTRA_PACKAGE_NAME, callingPackage);
a.startActivity(confirmDialog);
c.startActivity(confirmDialog);
return false;
}
}

View File

@ -13,29 +13,28 @@ import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;
import de.blinkt.openvpn.LaunchVPN;
import de.blinkt.openvpn.VpnProfile;
import de.blinkt.openvpn.core.IOpenVPNServiceInternal;
import de.blinkt.openvpn.core.OpenVPNService;
import de.blinkt.openvpn.core.ProfileManager;
public class RemoteAction extends Activity {
private ExternalAppDatabase mExtAppDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mExtAppDb = new ExternalAppDatabase(this);
}
public static final String EXTRA_NAME = "de.blinkt.openvpn.api.profileName";
private ExternalAppDatabase mExtAppDb;
private boolean mDoDisconnect;
private IOpenVPNServiceInternal mService;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
IOpenVPNServiceInternal myservice = IOpenVPNServiceInternal.Stub.asInterface(service);
mService = IOpenVPNServiceInternal.Stub.asInterface(service);
try {
myservice.stopVPN(false);
performAction();
} catch (RemoteException e) {
e.printStackTrace();
}
@ -48,27 +47,59 @@ public class RemoteAction extends Activity {
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mExtAppDb = new ExternalAppDatabase(this);
}
@Override
protected void onResume() {
super.onResume();
if (mExtAppDb.checkRemoteActionPermission(this))
performAction();
finish();
}
private void performAction() {
Intent intent = new Intent(this, OpenVPNService.class);
intent.setAction(OpenVPNService.START_SERVICE);
ComponentName component = getIntent().getComponent();
if (component.getShortClassName().equals(".api.DisconnectVPN")) {
boolean mDoDisconnect = true;
}
getApplicationContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
private void performAction() throws RemoteException {
if (!mService.isAllowedExternalApp(getCallingPackage())) {
finish();
return;
}
Intent intent = getIntent();
setIntent(null);
ComponentName component = intent.getComponent();
if (component.getShortClassName().equals(".api.DisconnectVPN")) {
mService.stopVPN(false);
} else if (component.getShortClassName().equals(".api.ConnectVPN")) {
String vpnName = intent.getStringExtra(EXTRA_NAME);
VpnProfile profile = ProfileManager.getInstance(this).getProfileByName(vpnName);
if (profile == null) {
Toast.makeText(this, String.format("Vpn profile %s from API call not found", vpnName), Toast.LENGTH_LONG).show();
} else {
Intent startVPN = new Intent(this, LaunchVPN.class);
startVPN.putExtra(LaunchVPN.EXTRA_KEY, profile.getUUID().toString());
startVPN.setAction(Intent.ACTION_MAIN);
startActivity(startVPN);
}
}
finish();
}
@Override
public void finish() {
if(mService!=null) {
mService = null;
getApplicationContext().unbindService(mConnection);
}
super.finish();
}
}

View File

@ -113,6 +113,12 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac
OpenVPNService.this.addAllowedExternalApp(packagename);
}
@Override
public boolean isAllowedExternalApp(String packagename) throws RemoteException {
return OpenVPNService.this.isAllowedExternalApp(packagename);
}
};
private String mLastTunCfg;
@ -174,6 +180,13 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac
extapps.addApp(packagename);
}
@Override
public boolean isAllowedExternalApp(String packagename) throws RemoteException {
ExternalAppDatabase extapps = new ExternalAppDatabase(OpenVPNService.this);
return extapps.checkRemoteActionPermission(this, packagename);
}
@Override
public IBinder onBind(Intent intent) {
String action = intent.getAction();

View File

@ -112,6 +112,8 @@ public class FaqFragment extends Fragment {
new FAQEntry(Build.VERSION_CODES.ICE_CREAM_SANDWICH, -1, R.string.faq_howto_title, R.string.faq_howto),
new FAQEntry(Build.VERSION_CODES.ICE_CREAM_SANDWICH, -1, R.string.faq_remote_api_title, R.string.faq_remote_api),
new FAQEntry(Build.VERSION_CODES.ICE_CREAM_SANDWICH, -1, R.string.weakmd_title, R.string.weakmd),
new FAQEntry(Build.VERSION_CODES.LOLLIPOP, -1, R.string.samsung_broken_title, R.string.samsung_broken),

View File

@ -469,5 +469,9 @@
<string name="proxy">Proxy</string>
<string name="Use_no_proxy">None</string>
<string name="tor_orbot">Tor (Orbot)</string>
<string name="openvpn3_socksproxy">OpenVPN 3 C++ implementation does not support connecting via Socks proxy</string>
<string name="no_orbotfound">Orbot application cannot be found. Please install Orbot or use manual Socks v5 integration.</string>
<string name="faq_remote_api_title">Remote API</string>
<string name="faq_remote_api">OpenVPN for Android supports two remote APIs, a sophisticated API using AIDL (remoteEXample in the git repository) and a simple one using Intents. &lt;p>Examples using adb shell and the intents. Replace profilname with your profile name&lt;p>&lt;p> adb shell am start-activity -a android.intent.action.MAIN de.blinkt.openvpn/.api.DisconnectVPN&lt;p> adb shell am start-activity -a android.intent.action.MAIN -e de.blinkt.openvpn.api.profileName Blinkt de.blinkt.openvpn/.api.ConnectVPN</string>
</resources>