blob: fed01296e5ac994426508e3a7b8486add835a424 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
/*
* Copyright (c) 2012-2016 Arne Schwabe
* Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
*/
package de.blinkt.openvpn.core;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import de.blinkt.openvpn.R;
import de.blinkt.openvpn.VpnProfile;
public class ProxyDetection {
static SocketAddress detectProxy(VpnProfile vp) {
// Construct a new url with https as protocol
try {
URL url = new URL(String.format("https://%s:%s",vp.mServerName,vp.mServerPort));
Proxy proxy = getFirstProxy(url);
if(proxy==null)
return null;
SocketAddress addr = proxy.address();
if (addr instanceof InetSocketAddress) {
return addr;
}
} catch (MalformedURLException e) {
VpnStatus.logError(R.string.getproxy_error, e.getLocalizedMessage());
} catch (URISyntaxException e) {
VpnStatus.logError(R.string.getproxy_error, e.getLocalizedMessage());
}
return null;
}
static Proxy getFirstProxy(URL url) throws URISyntaxException {
System.setProperty("java.net.useSystemProxies", "true");
List<Proxy> proxylist = ProxySelector.getDefault().select(url.toURI());
if (proxylist != null) {
for (Proxy proxy: proxylist) {
SocketAddress addr = proxy.address();
if (addr != null) {
return proxy;
}
}
}
return null;
}
}
|