1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.googlecode.android_scripting.facade; 18 19 import android.app.Service; 20 import android.net.VpnManager; 21 import android.os.RemoteException; 22 23 import com.android.internal.net.LegacyVpnInfo; 24 import com.android.internal.net.VpnConfig; 25 import com.android.internal.net.VpnProfile; 26 27 import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 28 import com.googlecode.android_scripting.rpc.Rpc; 29 import com.googlecode.android_scripting.rpc.RpcParameter; 30 31 import org.json.JSONObject; 32 33 /** 34 * Access VPN functions. 35 */ 36 public class VpnFacade extends RpcReceiver { 37 38 private final Service mService; 39 private final VpnManager mVpnManager; 40 private CertInstallerHelper mCertHelper; 41 VpnFacade(FacadeManager manager)42 public VpnFacade(FacadeManager manager) { 43 super(manager); 44 mService = manager.getService(); 45 mCertHelper = new CertInstallerHelper(); 46 mVpnManager = mService.getSystemService(VpnManager.class); 47 } 48 genLegacyVpnProfile(JSONObject vpnProfileJson)49 private VpnProfile genLegacyVpnProfile(JSONObject vpnProfileJson) { 50 VpnProfile vp = new VpnProfile(vpnProfileJson.optString("key", "")); 51 vp.name = vpnProfileJson.optString("name", ""); 52 vp.type = vpnProfileJson.optInt("type", VpnProfile.TYPE_PPTP); 53 vp.server = vpnProfileJson.optString("server", ""); 54 vp.username = vpnProfileJson.optString("username", ""); 55 vp.password = vpnProfileJson.optString("password", ""); 56 vp.dnsServers = vpnProfileJson.optString("dnsServers", ""); 57 vp.searchDomains = vpnProfileJson.optString("searchDomains", ""); 58 vp.routes = vpnProfileJson.optString("routes", ""); 59 vp.mppe = vpnProfileJson.optBoolean("mppe", true); 60 vp.l2tpSecret = vpnProfileJson.optString("l2tpSecret", ""); 61 vp.ipsecIdentifier = vpnProfileJson.optString("ipsecIdentifier", ""); 62 vp.ipsecSecret = vpnProfileJson.optString("ipsecSecret", ""); 63 vp.ipsecUserCert = vpnProfileJson.optString("ipsecUserCert", ""); 64 vp.ipsecCaCert = vpnProfileJson.optString("ipsecCaCert", ""); 65 vp.ipsecServerCert = vpnProfileJson.optString("ipsecServerCert", ""); 66 vp.saveLogin = vpnProfileJson.optBoolean("saveLogin", false); 67 return vp; 68 } 69 70 @Rpc(description = "Start legacy VPN with a profile.") vpnStartLegacyVpn(@pcParametername = "vpnProfile") JSONObject vpnProfile)71 public void vpnStartLegacyVpn(@RpcParameter(name = "vpnProfile") JSONObject vpnProfile) 72 throws RemoteException { 73 VpnProfile profile = genLegacyVpnProfile(vpnProfile); 74 mVpnManager.startLegacyVpn(profile); 75 } 76 77 @Rpc(description = "Stop the current legacy VPN connection.") vpnStopLegacyVpn()78 public void vpnStopLegacyVpn() throws RemoteException { 79 mVpnManager.prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN, mService.getUserId()); 80 } 81 82 @Rpc(description = "Get the info object of the currently active legacy VPN connection.") vpnGetLegacyVpnInfo()83 public LegacyVpnInfo vpnGetLegacyVpnInfo() throws RemoteException { 84 return mVpnManager.getLegacyVpnInfo(mService.getUserId()); 85 } 86 87 @Override shutdown()88 public void shutdown() { 89 } 90 91 @Rpc(description = "Install certificate for RSA VPNs.") installCertificate(@pcParametername = "vpnProfile") JSONObject vpnProfile, @RpcParameter(name = "certFile") String certFile, @RpcParameter(name = "password") String password)92 public void installCertificate(@RpcParameter(name = "vpnProfile") JSONObject vpnProfile, 93 @RpcParameter(name = "certFile") String certFile, 94 @RpcParameter(name = "password") String password) 95 throws RemoteException { 96 VpnProfile profile = genLegacyVpnProfile(vpnProfile); 97 mCertHelper.installCertificate(profile, certFile, password); 98 } 99 } 100