| 1 |
package com.github.markusbernhardt.proxy.selector.pac; |
| 2 |
|
| 3 |
import java.lang.reflect.Method; |
| 4 |
|
| 5 |
import javax.script.ScriptEngine; |
| 6 |
import javax.script.ScriptEngineManager; |
| 7 |
import javax.script.ScriptException; |
| 8 |
|
| 9 |
import com.github.markusbernhardt.proxy.util.Logger; |
| 10 |
import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class JavaxPacScriptParser implements PacScriptParser { |
| 26 |
static final String SCRIPT_METHODS_OBJECT = "__pacutil"; |
| 27 |
|
| 28 |
private final PacScriptSource source; |
| 29 |
private final ScriptEngine engine; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public JavaxPacScriptParser(PacScriptSource source) throws ProxyEvaluationException { |
| 40 |
this.source = source; |
| 41 |
this.engine = setupEngine(); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private ScriptEngine setupEngine() throws ProxyEvaluationException { |
| 52 |
ScriptEngineManager mng = new ScriptEngineManager(); |
| 53 |
ScriptEngine engine = mng.getEngineByMimeType("text/javascript"); |
| 54 |
engine.put(SCRIPT_METHODS_OBJECT, new PacScriptMethods()); |
| 55 |
|
| 56 |
Class<?> scriptMethodsClazz = ScriptMethods.class; |
| 57 |
Method[] scriptMethods = scriptMethodsClazz.getMethods(); |
| 58 |
|
| 59 |
for (Method method : scriptMethods) { |
| 60 |
String name = method.getName(); |
| 61 |
int args = method.getParameterTypes().length; |
| 62 |
StringBuilder toEval = new StringBuilder(name).append(" = function("); |
| 63 |
for (int i = 0; i < args; i++) { |
| 64 |
if (i > 0) { |
| 65 |
toEval.append(","); |
| 66 |
} |
| 67 |
toEval.append("arg").append(i); |
| 68 |
} |
| 69 |
toEval.append(") {return "); |
| 70 |
|
| 71 |
String functionCall = buildFunctionCallCode(name, args); |
| 72 |
|
| 73 |
|
| 74 |
if (String.class.isAssignableFrom(method.getReturnType())) { |
| 75 |
functionCall = "String(" + functionCall + ")"; |
| 76 |
} |
| 77 |
toEval.append(functionCall).append("; }"); |
| 78 |
try { |
| 79 |
engine.eval(toEval.toString()); |
| 80 |
} catch (ScriptException e) { |
| 81 |
Logger.log(getClass(), LogLevel.ERROR, "JS evaluation error when creating alias for " + name + ".", e); |
| 82 |
throw new ProxyEvaluationException("Error setting up script engine", e); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return engine; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
private String buildFunctionCallCode(String functionName, int args) { |
| 100 |
StringBuilder functionCall = new StringBuilder(); |
| 101 |
functionCall.append(SCRIPT_METHODS_OBJECT).append(".").append(functionName).append("("); |
| 102 |
for (int i = 0; i < args; i++) { |
| 103 |
if (i > 0) { |
| 104 |
functionCall.append(","); |
| 105 |
} |
| 106 |
functionCall.append("arg").append(i); |
| 107 |
} |
| 108 |
functionCall.append(")"); |
| 109 |
return functionCall.toString(); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public PacScriptSource getScriptSource() { |
| 118 |
return this.source; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public String evaluate(String url, String host) throws ProxyEvaluationException { |
| 133 |
try { |
| 134 |
StringBuilder script = new StringBuilder(this.source.getScriptContent()); |
| 135 |
String evalMethod = " ;FindProxyForURL (\"" + url + "\",\"" + host + "\")"; |
| 136 |
script.append(evalMethod); |
| 137 |
Object result = this.engine.eval(script.toString()); |
| 138 |
return (String) result; |
| 139 |
} catch (Exception e) { |
| 140 |
Logger.log(getClass(), LogLevel.ERROR, "JS evaluation error.", e); |
| 141 |
throw new ProxyEvaluationException("Error while executing PAC script: " + e.getMessage(), e); |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
} |