• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.android.internal.net;
18 
19 import android.app.PendingIntent;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.pm.PackageManager;
25 import android.content.pm.PackageManager.NameNotFoundException;
26 import android.content.pm.ResolveInfo;
27 import android.content.res.Resources;
28 import android.net.IpPrefix;
29 import android.net.LinkAddress;
30 import android.net.Network;
31 import android.net.ProxyInfo;
32 import android.net.RouteInfo;
33 import android.os.Parcel;
34 import android.os.Parcelable;
35 import android.os.UserHandle;
36 
37 import java.net.Inet4Address;
38 import java.net.InetAddress;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.List;
42 
43 /**
44  * A simple container used to carry information in VpnBuilder, VpnDialogs,
45  * and com.android.server.connectivity.Vpn. Internal use only.
46  *
47  * @hide
48  */
49 public class VpnConfig implements Parcelable {
50 
51     public static final String SERVICE_INTERFACE = "android.net.VpnService";
52 
53     public static final String DIALOGS_PACKAGE = "com.android.vpndialogs";
54 
55     // TODO: Rename this to something that encompasses Settings-based Platform VPNs as well.
56     public static final String LEGACY_VPN = "[Legacy VPN]";
57 
getIntentForConfirmation()58     public static Intent getIntentForConfirmation() {
59         Intent intent = new Intent();
60         ComponentName componentName = ComponentName.unflattenFromString(
61                 Resources.getSystem().getString(
62                         com.android.internal.R.string.config_customVpnConfirmDialogComponent));
63         intent.setClassName(componentName.getPackageName(), componentName.getClassName());
64         return intent;
65     }
66 
67     /** NOTE: This should only be used for legacy VPN. */
getIntentForStatusPanel(Context context)68     public static PendingIntent getIntentForStatusPanel(Context context) {
69         Intent intent = new Intent();
70         intent.setClassName(DIALOGS_PACKAGE, DIALOGS_PACKAGE + ".ManageDialog");
71         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY |
72                 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
73         return PendingIntent.getActivityAsUser(context, 0 /* requestCode */, intent,
74                 PendingIntent.FLAG_IMMUTABLE, null /* options */, UserHandle.CURRENT);
75     }
76 
getVpnLabel(Context context, String packageName)77     public static CharSequence getVpnLabel(Context context, String packageName)
78             throws NameNotFoundException {
79         PackageManager pm = context.getPackageManager();
80         Intent intent = new Intent(SERVICE_INTERFACE);
81         intent.setPackage(packageName);
82         List<ResolveInfo> services = pm.queryIntentServices(intent, 0 /* flags */);
83         if (services != null && services.size() == 1) {
84             // This app contains exactly one VPN service. Call loadLabel, which will attempt to
85             // load the service's label, and fall back to the app label if none is present.
86             return services.get(0).loadLabel(pm);
87         } else {
88             return pm.getApplicationInfo(packageName, 0).loadLabel(pm);
89         }
90     }
91 
92     public String user;
93     public String interfaze;
94     public String session;
95     public int mtu = -1;
96     public List<LinkAddress> addresses = new ArrayList<LinkAddress>();
97     public List<RouteInfo> routes = new ArrayList<RouteInfo>();
98     public List<String> dnsServers;
99     public List<String> searchDomains;
100     public List<String> allowedApplications;
101     public List<String> disallowedApplications;
102     public PendingIntent configureIntent;
103     public long startTime = -1;
104     public boolean legacy;
105     public boolean blocking;
106     public boolean allowBypass;
107     public boolean allowIPv4;
108     public boolean allowIPv6;
109     public boolean isMetered = true;
110     public Network[] underlyingNetworks;
111     public ProxyInfo proxyInfo;
112 
113     @UnsupportedAppUsage
VpnConfig()114     public VpnConfig() {
115     }
116 
updateAllowedFamilies(InetAddress address)117     public void updateAllowedFamilies(InetAddress address) {
118         if (address instanceof Inet4Address) {
119             allowIPv4 = true;
120         } else {
121             allowIPv6 = true;
122         }
123     }
124 
addLegacyRoutes(String routesStr)125     public void addLegacyRoutes(String routesStr) {
126         if (routesStr.trim().equals("")) {
127             return;
128         }
129         String[] routes = routesStr.trim().split(" ");
130         for (String route : routes) {
131             //each route is ip/prefix
132             RouteInfo info = new RouteInfo(new IpPrefix(route), null, null, RouteInfo.RTN_UNICAST);
133             this.routes.add(info);
134             updateAllowedFamilies(info.getDestination().getAddress());
135         }
136     }
137 
addLegacyAddresses(String addressesStr)138     public void addLegacyAddresses(String addressesStr) {
139         if (addressesStr.trim().equals("")) {
140             return;
141         }
142         String[] addresses = addressesStr.trim().split(" ");
143         for (String address : addresses) {
144             //each address is ip/prefix
145             LinkAddress addr = new LinkAddress(address);
146             this.addresses.add(addr);
147             updateAllowedFamilies(addr.getAddress());
148         }
149     }
150 
151     @Override
describeContents()152     public int describeContents() {
153         return 0;
154     }
155 
156     @Override
writeToParcel(Parcel out, int flags)157     public void writeToParcel(Parcel out, int flags) {
158         out.writeString(user);
159         out.writeString(interfaze);
160         out.writeString(session);
161         out.writeInt(mtu);
162         out.writeTypedList(addresses);
163         out.writeTypedList(routes);
164         out.writeStringList(dnsServers);
165         out.writeStringList(searchDomains);
166         out.writeStringList(allowedApplications);
167         out.writeStringList(disallowedApplications);
168         out.writeParcelable(configureIntent, flags);
169         out.writeLong(startTime);
170         out.writeInt(legacy ? 1 : 0);
171         out.writeInt(blocking ? 1 : 0);
172         out.writeInt(allowBypass ? 1 : 0);
173         out.writeInt(allowIPv4 ? 1 : 0);
174         out.writeInt(allowIPv6 ? 1 : 0);
175         out.writeInt(isMetered ? 1 : 0);
176         out.writeTypedArray(underlyingNetworks, flags);
177         out.writeParcelable(proxyInfo, flags);
178     }
179 
180     public static final Parcelable.Creator<VpnConfig> CREATOR =
181             new Parcelable.Creator<VpnConfig>() {
182         @Override
183         public VpnConfig createFromParcel(Parcel in) {
184             VpnConfig config = new VpnConfig();
185             config.user = in.readString();
186             config.interfaze = in.readString();
187             config.session = in.readString();
188             config.mtu = in.readInt();
189             in.readTypedList(config.addresses, LinkAddress.CREATOR);
190             in.readTypedList(config.routes, RouteInfo.CREATOR);
191             config.dnsServers = in.createStringArrayList();
192             config.searchDomains = in.createStringArrayList();
193             config.allowedApplications = in.createStringArrayList();
194             config.disallowedApplications = in.createStringArrayList();
195             config.configureIntent = in.readParcelable(null);
196             config.startTime = in.readLong();
197             config.legacy = in.readInt() != 0;
198             config.blocking = in.readInt() != 0;
199             config.allowBypass = in.readInt() != 0;
200             config.allowIPv4 = in.readInt() != 0;
201             config.allowIPv6 = in.readInt() != 0;
202             config.isMetered = in.readInt() != 0;
203             config.underlyingNetworks = in.createTypedArray(Network.CREATOR);
204             config.proxyInfo = in.readParcelable(null);
205             return config;
206         }
207 
208         @Override
209         public VpnConfig[] newArray(int size) {
210             return new VpnConfig[size];
211         }
212     };
213 
214     @Override
toString()215     public String toString() {
216         return new StringBuilder()
217                 .append("VpnConfig")
218                 .append("{ user=").append(user)
219                 .append(", interface=").append(interfaze)
220                 .append(", session=").append(session)
221                 .append(", mtu=").append(mtu)
222                 .append(", addresses=").append(toString(addresses))
223                 .append(", routes=").append(toString(routes))
224                 .append(", dns=").append(toString(dnsServers))
225                 .append(", searchDomains=").append(toString(searchDomains))
226                 .append(", allowedApps=").append(toString(allowedApplications))
227                 .append(", disallowedApps=").append(toString(disallowedApplications))
228                 .append(", configureIntent=").append(configureIntent)
229                 .append(", startTime=").append(startTime)
230                 .append(", legacy=").append(legacy)
231                 .append(", blocking=").append(blocking)
232                 .append(", allowBypass=").append(allowBypass)
233                 .append(", allowIPv4=").append(allowIPv4)
234                 .append(", allowIPv6=").append(allowIPv6)
235                 .append(", underlyingNetworks=").append(Arrays.toString(underlyingNetworks))
236                 .append(", proxyInfo=").append(proxyInfo)
237                 .append("}")
238                 .toString();
239     }
240 
toString(List<T> ls)241     static <T> String toString(List<T> ls) {
242         if (ls == null) {
243             return "null";
244         }
245         return Arrays.toString(ls.toArray());
246     }
247 }
248