use of org.pdown.gui.extension.ExtensionInfo in project proxyee-down by monkeyWie.
the class HttpDownAppCallback method commonHook.
private void commonHook(HttpDownBootstrap httpDownBootstrap, String event, boolean async) {
DownInfo downInfo = findDownInfo(httpDownBootstrap);
Map<String, Object> taskInfo = buildTaskInfo(downInfo);
if (taskInfo != null) {
// 遍历扩展模块是否有对应的处理
List<ExtensionInfo> extensionInfos = ExtensionContent.get();
for (ExtensionInfo extensionInfo : extensionInfos) {
if (extensionInfo.getMeta().isEnabled()) {
if (extensionInfo.getHookScript() != null && !StringUtils.isEmpty(extensionInfo.getHookScript().getScript())) {
Event e = extensionInfo.getHookScript().hasEvent(event, HttpDownUtil.getUrl(httpDownBootstrap.getRequest()));
if (e != null) {
try {
// 执行钩子函数
Object result = ExtensionUtil.invoke(extensionInfo, e, taskInfo, async);
if (result != null) {
ObjectMapper objectMapper = new ObjectMapper();
String temp = objectMapper.writeValueAsString(result);
TaskForm taskForm = objectMapper.readValue(temp, TaskForm.class);
if (taskForm.getRequest() != null) {
httpDownBootstrap.setRequest(HttpDownUtil.buildRequest(taskForm.getRequest().getMethod(), taskForm.getRequest().getUrl(), taskForm.getRequest().getHeads(), taskForm.getRequest().getBody()));
}
if (taskForm.getResponse() != null) {
httpDownBootstrap.setResponse(taskForm.getResponse());
}
if (taskForm.getData() != null) {
downInfo.setData(taskForm.getData());
}
HttpDownContent.getInstance().save();
}
} catch (Exception ex) {
LOGGER.error("An hook exception occurred while " + event + "()", ex);
}
}
}
}
}
}
}
use of org.pdown.gui.extension.ExtensionInfo in project proxyee-down by monkeyWie.
the class ExtensionUtil method invoke.
/**
* 运行一个js方法
*/
public static Object invoke(ExtensionInfo extensionInfo, Event event, Object param, boolean async) throws NoSuchMethodException, ScriptException, FileNotFoundException, InterruptedException {
// 初始化js引擎
ScriptEngine engine = ExtensionUtil.buildExtensionRuntimeEngine(extensionInfo);
Invocable invocable = (Invocable) engine;
// 执行resolve方法
Object result = invocable.invokeFunction(StringUtils.isEmpty(event.getMethod()) ? event.getOn() : event.getMethod(), param);
// 结果为null或者异步调用直接返回
if (result == null || async) {
return result;
}
final Object[] ret = { null };
// 判断是不是返回Promise对象
ScriptContext ctx = new SimpleScriptContext();
ctx.setAttribute("result", result, ScriptContext.ENGINE_SCOPE);
boolean isPromise = (boolean) engine.eval("!!result&&typeof result=='object'&&typeof result.then=='function'", ctx);
if (isPromise) {
// 如果是返回的Promise则等待执行完成
CountDownLatch countDownLatch = new CountDownLatch(1);
invocable.invokeMethod(result, "then", (Function) o -> {
try {
ret[0] = o;
} catch (Exception e) {
LOGGER.error("An exception occurred while resolve()", e);
} finally {
countDownLatch.countDown();
}
return null;
});
invocable.invokeMethod(result, "catch", (Function) o -> {
countDownLatch.countDown();
return null;
});
// 等待解析完成
countDownLatch.await();
} else {
ret[0] = result;
}
return ret[0];
}
use of org.pdown.gui.extension.ExtensionInfo in project proxyee-down by monkeyWie.
the class NativeController method onResolve.
@RequestMapping("onResolve")
public FullHttpResponse onResolve(Channel channel, FullHttpRequest request) throws Exception {
HttpRequestForm taskRequest = getJSONParams(request, HttpRequestForm.class);
// 遍历扩展模块是否有对应的处理
List<ExtensionInfo> extensionInfos = ExtensionContent.get();
for (ExtensionInfo extensionInfo : extensionInfos) {
if (extensionInfo.getMeta().isEnabled()) {
if (extensionInfo.getHookScript() != null && !StringUtils.isEmpty(extensionInfo.getHookScript().getScript())) {
Event event = extensionInfo.getHookScript().hasEvent(HookScript.EVENT_RESOLVE, taskRequest.getUrl());
if (event != null) {
try {
// 执行resolve方法
Object result = ExtensionUtil.invoke(extensionInfo, event, taskRequest, false);
if (result != null && !(result instanceof Undefined)) {
ObjectMapper objectMapper = new ObjectMapper();
String temp = objectMapper.writeValueAsString(result);
TaskForm taskForm = objectMapper.readValue(temp, TaskForm.class);
// 有一个扩展解析成功的话直接返回
return HttpHandlerUtil.buildJson(taskForm, Include.NON_DEFAULT);
}
} catch (Exception e) {
LOGGER.error("An exception occurred while resolve()", e);
}
}
}
}
}
return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
}
use of org.pdown.gui.extension.ExtensionInfo in project proxyee-down by monkeyWie.
the class NativeController method installLocalExtension.
/**
* 加载本地扩展
*/
@RequestMapping("installLocalExtension")
public FullHttpResponse installLocalExtension(Channel channel, FullHttpRequest request) throws Exception {
Map<String, Object> data = new HashMap<>();
Map<String, Object> map = getJSONParams(request);
String path = (String) map.get("path");
// 刷新扩展content
ExtensionInfo loadExt = ExtensionContent.refresh(path, true);
if (loadExt == null) {
return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
}
data.put("data", loadExt);
// 刷新系统pac代理
AppUtil.refreshPAC();
return HttpHandlerUtil.buildJson(data);
}
use of org.pdown.gui.extension.ExtensionInfo in project proxyee-down by monkeyWie.
the class NativeController method toggleExtension.
/**
* 启用或禁用插件
*/
@RequestMapping("toggleExtension")
public FullHttpResponse toggleExtension(Channel channel, FullHttpRequest request) throws Exception {
Map<String, Object> map = getJSONParams(request);
String path = (String) map.get("path");
boolean enabled = (boolean) map.get("enabled");
boolean local = map.get("local") != null ? (boolean) map.get("local") : false;
ExtensionInfo extensionInfo = ExtensionContent.get().stream().filter(e -> e.getMeta().getPath().equals(path)).findFirst().get();
extensionInfo.getMeta().setEnabled(enabled).save();
// 刷新pac
ExtensionContent.refresh(extensionInfo.getMeta().getFullPath(), local);
AppUtil.refreshPAC();
return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
}
Aggregations