| 1 |
package com.github.markusbernhardt.proxy.selector.pac; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.net.InetSocketAddress; |
| 5 |
import java.net.Proxy; |
| 6 |
import java.net.ProxySelector; |
| 7 |
import java.net.SocketAddress; |
| 8 |
import java.net.URI; |
| 9 |
import java.util.ArrayList; |
| 10 |
import java.util.List; |
| 11 |
|
| 12 |
import com.github.markusbernhardt.proxy.util.Logger; |
| 13 |
import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 14 |
import com.github.markusbernhardt.proxy.util.ProxyUtil; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class PacProxySelector extends ProxySelector { |
| 23 |
|
| 24 |
|
| 25 |
private static final String PAC_SOCKS = "SOCKS"; |
| 26 |
private static final String PAC_DIRECT = "DIRECT"; |
| 27 |
|
| 28 |
private PacScriptParser pacScriptParser; |
| 29 |
|
| 30 |
private static volatile boolean enabled = true; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public PacProxySelector(PacScriptSource pacSource) { |
| 40 |
super(); |
| 41 |
selectEngine(pacSource); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static void setEnabled(boolean enable) { |
| 53 |
enabled = enable; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public static boolean isEnabled() { |
| 63 |
return enabled; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
private void selectEngine(PacScriptSource pacSource) { |
| 74 |
try { |
| 75 |
Logger.log(getClass(), LogLevel.INFO, "Using javax.script JavaScript engine."); |
| 76 |
this.pacScriptParser = new JavaxPacScriptParser(pacSource); |
| 77 |
} catch (Exception e) { |
| 78 |
Logger.log(getClass(), LogLevel.ERROR, "PAC parser error.", e); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
@Override |
| 89 |
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
@Override |
| 99 |
public List<Proxy> select(URI uri) { |
| 100 |
if (uri == null) { |
| 101 |
throw new IllegalArgumentException("URI must not be null."); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if (!enabled) { |
| 107 |
return ProxyUtil.noProxyList(); |
| 108 |
} |
| 109 |
|
| 110 |
return findProxy(uri); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private List<Proxy> findProxy(URI uri) { |
| 126 |
try { |
| 127 |
List<Proxy> proxies = new ArrayList<Proxy>(); |
| 128 |
String parseResult = this.pacScriptParser.evaluate(uri.toString(), uri.getHost()); |
| 129 |
if (parseResult == null) { |
| 130 |
parseResult = PAC_DIRECT; |
| 131 |
} |
| 132 |
String[] proxyDefinitions = parseResult.split("[;]"); |
| 133 |
for (String proxyDef : proxyDefinitions) { |
| 134 |
if (proxyDef.trim().length() > 0) { |
| 135 |
proxies.add(buildProxyFromPacResult(proxyDef)); |
| 136 |
} |
| 137 |
} |
| 138 |
return proxies; |
| 139 |
} catch (ProxyEvaluationException e) { |
| 140 |
Logger.log(getClass(), LogLevel.ERROR, "PAC resolving error.", e); |
| 141 |
return ProxyUtil.noProxyList(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
private Proxy buildProxyFromPacResult(String pacResult) { |
| 155 |
if (pacResult.trim().length() < 6) { |
| 156 |
return Proxy.NO_PROXY; |
| 157 |
} |
| 158 |
String proxyDef = pacResult.trim(); |
| 159 |
if (proxyDef.toUpperCase().startsWith(PAC_DIRECT)) { |
| 160 |
return Proxy.NO_PROXY; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
Proxy.Type type = Proxy.Type.HTTP; |
| 165 |
if (proxyDef.toUpperCase().startsWith(PAC_SOCKS)) { |
| 166 |
type = Proxy.Type.SOCKS; |
| 167 |
} |
| 168 |
|
| 169 |
String host = proxyDef.substring(6); |
| 170 |
Integer port = ProxyUtil.DEFAULT_PROXY_PORT; |
| 171 |
|
| 172 |
|
| 173 |
int indexOfPort = host.indexOf(':'); |
| 174 |
int index2 = host.lastIndexOf(']'); |
| 175 |
if (indexOfPort != -1 && index2 < indexOfPort) { |
| 176 |
port = Integer.parseInt(host.substring(indexOfPort + 1).trim()); |
| 177 |
host = host.substring(0, indexOfPort).trim(); |
| 178 |
} |
| 179 |
|
| 180 |
SocketAddress adr = InetSocketAddress.createUnresolved(host, port); |
| 181 |
return new Proxy(type, adr); |
| 182 |
} |
| 183 |
|
| 184 |
} |