| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.net; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.annotation.SdkConstant; |
| 21 |
import android.annotation.SdkConstant.SdkConstantType; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.compat.annotation.UnsupportedAppUsage; |
| 24 |
import android.content.Context; |
| 25 |
import android.os.Build; |
| 26 |
import android.text.TextUtils; |
| 27 |
import android.util.Log; |
| 28 |
|
| 29 |
import com.android.net.module.util.ProxyUtils; |
| 30 |
|
| 31 |
import java.net.InetSocketAddress; |
| 32 |
import java.net.ProxySelector; |
| 33 |
import java.net.URI; |
| 34 |
import java.util.List; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public final class Proxy { |
| 41 |
|
| 42 |
private static final String TAG = "Proxy"; |
| 43 |
|
| 44 |
private static final ProxySelector sDefaultProxySelector; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 54 |
public static final String PROXY_CHANGE_ACTION = "android.intent.action.PROXY_CHANGE"; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
@Deprecated |
| 66 |
public static final String EXTRA_PROXY_INFO = "android.intent.extra.PROXY_INFO"; |
| 67 |
|
| 68 |
private static ConnectivityManager sConnectivityManager = null; |
| 69 |
|
| 70 |
static { |
| 71 |
sDefaultProxySelector = ProxySelector.getDefault(); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
@UnsupportedAppUsage |
| 84 |
public static final java.net.Proxy getProxy(Context ctx, String url) { |
| 85 |
String host = ""; |
| 86 |
if ((url != null) && !isLocalHost(host)) { |
| 87 |
URI uri = URI.create(url); |
| 88 |
ProxySelector proxySelector = ProxySelector.getDefault(); |
| 89 |
|
| 90 |
List<java.net.Proxy> proxyList = proxySelector.select(uri); |
| 91 |
|
| 92 |
if (proxyList.size() > 0) { |
| 93 |
return proxyList.get(0); |
| 94 |
} |
| 95 |
} |
| 96 |
return java.net.Proxy.NO_PROXY; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
@Deprecated |
| 110 |
public static final String getHost(Context ctx) { |
| 111 |
java.net.Proxy proxy = getProxy(ctx, null); |
| 112 |
if (proxy == java.net.Proxy.NO_PROXY) return null; |
| 113 |
try { |
| 114 |
return ((InetSocketAddress)(proxy.address())).getHostName(); |
| 115 |
} catch (Exception e) { |
| 116 |
return null; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
@Deprecated |
| 128 |
public static final int getPort(Context ctx) { |
| 129 |
java.net.Proxy proxy = getProxy(ctx, null); |
| 130 |
if (proxy == java.net.Proxy.NO_PROXY) return -1; |
| 131 |
try { |
| 132 |
return ((InetSocketAddress)(proxy.address())).getPort(); |
| 133 |
} catch (Exception e) { |
| 134 |
return -1; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
@Deprecated |
| 147 |
public static final String getDefaultHost() { |
| 148 |
String host = System.getProperty("http.proxyHost"); |
| 149 |
if (TextUtils.isEmpty(host)) return null; |
| 150 |
return host; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
@Deprecated |
| 162 |
public static final int getDefaultPort() { |
| 163 |
if (getDefaultHost() == null) return -1; |
| 164 |
try { |
| 165 |
return Integer.parseInt(System.getProperty("http.proxyPort")); |
| 166 |
} catch (NumberFormatException e) { |
| 167 |
return -1; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
private static final boolean isLocalHost(String host) { |
| 172 |
if (host == null) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
try { |
| 176 |
if (host != null) { |
| 177 |
if (host.equalsIgnoreCase("localhost")) { |
| 178 |
return true; |
| 179 |
} |
| 180 |
if (InetAddresses.parseNumericAddress(host).isLoopbackAddress()) { |
| 181 |
return true; |
| 182 |
} |
| 183 |
} |
| 184 |
} catch (IllegalArgumentException iex) { |
| 185 |
} |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 191 |
@Deprecated |
| 192 |
public static void setHttpProxySystemProperty(ProxyInfo p) { |
| 193 |
setHttpProxyConfiguration(p); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 203 |
public static void setHttpProxyConfiguration(@Nullable ProxyInfo p) { |
| 204 |
String host = null; |
| 205 |
String port = null; |
| 206 |
String exclList = null; |
| 207 |
Uri pacFileUrl = Uri.EMPTY; |
| 208 |
if (p != null) { |
| 209 |
host = p.getHost(); |
| 210 |
port = Integer.toString(p.getPort()); |
| 211 |
exclList = ProxyUtils.exclusionListAsString(p.getExclusionList()); |
| 212 |
pacFileUrl = p.getPacFileUrl(); |
| 213 |
} |
| 214 |
setHttpProxyConfiguration(host, port, exclList, pacFileUrl); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
public static void setHttpProxyConfiguration(String host, String port, String exclList, |
| 219 |
Uri pacFileUrl) { |
| 220 |
if (exclList != null) exclList = exclList.replace(",", "|"); |
| 221 |
if (false) Log.d(TAG, "setHttpProxySystemProperty :"+host+":"+port+" - "+exclList); |
| 222 |
if (host != null) { |
| 223 |
System.setProperty("http.proxyHost", host); |
| 224 |
System.setProperty("https.proxyHost", host); |
| 225 |
} else { |
| 226 |
System.clearProperty("http.proxyHost"); |
| 227 |
System.clearProperty("https.proxyHost"); |
| 228 |
} |
| 229 |
if (port != null) { |
| 230 |
System.setProperty("http.proxyPort", port); |
| 231 |
System.setProperty("https.proxyPort", port); |
| 232 |
} else { |
| 233 |
System.clearProperty("http.proxyPort"); |
| 234 |
System.clearProperty("https.proxyPort"); |
| 235 |
} |
| 236 |
if (exclList != null) { |
| 237 |
System.setProperty("http.nonProxyHosts", exclList); |
| 238 |
System.setProperty("https.nonProxyHosts", exclList); |
| 239 |
} else { |
| 240 |
System.clearProperty("http.nonProxyHosts"); |
| 241 |
System.clearProperty("https.nonProxyHosts"); |
| 242 |
} |
| 243 |
if (!Uri.EMPTY.equals(pacFileUrl)) { |
| 244 |
ProxySelector.setDefault(new PacProxySelector()); |
| 245 |
} else { |
| 246 |
ProxySelector.setDefault(sDefaultProxySelector); |
| 247 |
} |
| 248 |
} |
| 249 |
} |