| 1 |
package com.github.markusbernhardt.proxy.util; |
| 2 |
|
| 3 |
import java.net.MalformedURLException; |
| 4 |
import java.net.Proxy; |
| 5 |
import java.net.URL; |
| 6 |
import java.util.ArrayList; |
| 7 |
import java.util.Collections; |
| 8 |
import java.util.List; |
| 9 |
|
| 10 |
import com.github.markusbernhardt.proxy.selector.fixed.FixedProxySelector; |
| 11 |
import com.github.markusbernhardt.proxy.selector.pac.PacProxySelector; |
| 12 |
import com.github.markusbernhardt.proxy.selector.pac.PacScriptSource; |
| 13 |
import com.github.markusbernhardt.proxy.selector.pac.UrlPacScriptSource; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class ProxyUtil { |
| 23 |
|
| 24 |
public static final int DEFAULT_PROXY_PORT = 80; |
| 25 |
|
| 26 |
private static List<Proxy> noProxyList; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public static FixedProxySelector parseProxySettings(String proxyVar) { |
| 37 |
if (proxyVar == null || proxyVar.trim().length() == 0) { |
| 38 |
return null; |
| 39 |
} |
| 40 |
|
| 41 |
try { |
| 42 |
|
| 43 |
if (proxyVar.indexOf(":/") == -1) { |
| 44 |
proxyVar = "http://" + proxyVar; |
| 45 |
} |
| 46 |
|
| 47 |
URL url = new URL(proxyVar); |
| 48 |
String host = cleanIPv6(url.getHost()); |
| 49 |
|
| 50 |
int port = url.getPort(); |
| 51 |
if (port == -1) { |
| 52 |
port = DEFAULT_PROXY_PORT; |
| 53 |
} |
| 54 |
return new FixedProxySelector(host.trim(), port); |
| 55 |
} catch (MalformedURLException e) { |
| 56 |
Logger.log(ProxyUtil.class, Logger.LogLevel.WARNING, "Cannot parse Proxy Settings {0}", proxyVar); |
| 57 |
return null; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static synchronized List<Proxy> noProxyList() { |
| 69 |
if (noProxyList == null) { |
| 70 |
ArrayList<Proxy> list = new ArrayList<Proxy>(1); |
| 71 |
list.add(Proxy.NO_PROXY); |
| 72 |
noProxyList = Collections.unmodifiableList(list); |
| 73 |
} |
| 74 |
return noProxyList; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public static PacProxySelector buildPacSelectorForUrl(String url) { |
| 87 |
PacProxySelector result = null; |
| 88 |
PacScriptSource pacSource = new UrlPacScriptSource(url); |
| 89 |
if (pacSource.isScriptValid()) { |
| 90 |
result = new PacProxySelector(pacSource); |
| 91 |
} |
| 92 |
return result; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public static String cleanIPv6(String hostOrIP) { |
| 106 |
if (hostOrIP == null) { |
| 107 |
return null; |
| 108 |
} |
| 109 |
hostOrIP = hostOrIP.trim(); |
| 110 |
if (hostOrIP.startsWith("[")) { |
| 111 |
hostOrIP = hostOrIP.substring(1); |
| 112 |
} |
| 113 |
if (hostOrIP.endsWith("]")) { |
| 114 |
hostOrIP = hostOrIP.substring(0, hostOrIP.length() - 1); |
| 115 |
} |
| 116 |
return hostOrIP; |
| 117 |
} |
| 118 |
|
| 119 |
} |