Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/selector/pac/PacProxySelector.java

raw symbols SCIP
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 * ProxySelector that will use a PAC script to find an proxy for a given URI.
18 *
19 * @author Markus Bernhardt, Copyright 2016
20 * @author Bernd Rosstauscher, Copyright 2009
21 ****************************************************************************/
22 public class PacProxySelector extends ProxySelector {
23
24 // private static final String PAC_PROXY = "PROXY";
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 * Constructor
34 *
35 * @param pacSource
36 * the source for the PAC file.
37 ************************************************************************/
38
39 public PacProxySelector(PacScriptSource pacSource) {
40 super();
41 selectEngine(pacSource);
42 }
43
44 /*************************************************************************
45 * Can be used to enable / disable the proxy selector. If disabled it will
46 * return DIRECT for all urls.
47 *
48 * @param enable
49 * the new status to set.
50 ************************************************************************/
51
52 public static void setEnabled(boolean enable) {
53 enabled = enable;
54 }
55
56 /*************************************************************************
57 * Checks if the selector is currently enabled.
58 *
59 * @return true if enabled else false.
60 ************************************************************************/
61
62 public static boolean isEnabled() {
63 return enabled;
64 }
65
66 /*************************************************************************
67 * Selects one of the available PAC parser engines.
68 *
69 * @param pacSource
70 * to use as input.
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 * connectFailed
84 *
85 * @see java.net.ProxySelector#connectFailed(java.net.URI,
86 * java.net.SocketAddress, java.io.IOException)
87 ************************************************************************/
88 @Override
89 public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
90 // Not used.
91 }
92
93 /*************************************************************************
94 * select
95 *
96 * @see java.net.ProxySelector#select(java.net.URI)
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 // Fix for Java 1.6.16+ where we get a infinite loop because
105 // URL.connect(Proxy.NO_PROXY) does not work as expected.
106 if (!enabled) {
107 return ProxyUtil.noProxyList();
108 }
109
110 return findProxy(uri);
111 }
112
113 /*************************************************************************
114 * Evaluation of the given URL with the PAC-file.
115 *
116 * Two cases can be handled here: DIRECT Fetch the object directly from the
117 * content HTTP server denoted by its URL PROXY name:port Fetch the object
118 * via the proxy HTTP server at the given location (name and port)
119 *
120 * @param uri
121 * <code>URI</code> to be evaluated.
122 * @return <code>Proxy</code>-object list as result of the evaluation.
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 * The proxy evaluator will return a proxy string. This method will take
147 * this string and build a matching <code>Proxy</code> for it.
148 *
149 * @param pacResult
150 * the result from the PAC parser.
151 * @return a Proxy
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 // Check proxy type.
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 // Split port from host
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 }