Search in sources :

Example 6 with ExtensionInfo

use of org.pdown.gui.extension.ExtensionInfo in project proxyee-down by monkeyWie.

the class NativeController method updateExtensionSetting.

@RequestMapping("updateExtensionSetting")
public FullHttpResponse updateExtensionSetting(Channel channel, FullHttpRequest request) throws Exception {
    Map<String, Object> map = getJSONParams(request);
    String path = (String) map.get("path");
    Map<String, Object> setting = (Map<String, Object>) map.get("setting");
    ExtensionInfo extensionInfo = ExtensionContent.get().stream().filter(e -> e.getMeta().getPath().equals(path)).findFirst().get();
    extensionInfo.getMeta().setSettings(setting).save();
    return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
}
Also used : ExtensionInfo(org.pdown.gui.extension.ExtensionInfo) HashMap(java.util.HashMap) Map(java.util.Map) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with ExtensionInfo

use of org.pdown.gui.extension.ExtensionInfo in project proxyee-down by monkeyWie.

the class ScriptIntercept method handelResponse.

@Override
public void handelResponse(HttpRequest httpRequest, FullHttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) {
    List<ExtensionInfo> extensionInfoList = ExtensionContent.get();
    if (isEmpty(extensionInfoList)) {
        return;
    }
    for (ExtensionInfo extensionInfo : extensionInfoList) {
        if (isEmpty(extensionInfo.getContentScripts())) {
            continue;
        }
        for (ContentScript contentScript : extensionInfo.getContentScripts()) {
            // 扩展注入正则表达式与当前访问的url匹配则注入脚本
            String url = HttpDownUtil.getUrl(httpRequest);
            if (contentScript.isMatch(url)) {
                String apiTemplate = readInsertTemplate(extensionInfo);
                StringBuilder scriptsBuilder = new StringBuilder();
                for (String script : contentScript.getScripts()) {
                    File scriptFile = new File(extensionInfo.getMeta().getFullPath() + File.separator + script);
                    if (scriptFile.exists() && scriptFile.isFile()) {
                        try {
                            scriptsBuilder.append(new String(Files.readAllBytes(scriptFile.toPath()), "UTF-8"));
                        } catch (IOException e) {
                        }
                    }
                }
                apiTemplate = apiTemplate.replace("${content}", scriptsBuilder.toString());
                int index = ByteUtil.findText(httpResponse.content(), INSERT_TOKEN);
                ByteUtil.insertText(httpResponse.content(), index == -1 ? 0 : index - INSERT_TOKEN.length(), apiTemplate, Charset.forName("UTF-8"));
            }
        }
    }
}
Also used : ExtensionInfo(org.pdown.gui.extension.ExtensionInfo) ContentScript(org.pdown.gui.extension.ContentScript) IOException(java.io.IOException) File(java.io.File)

Aggregations

ExtensionInfo (org.pdown.gui.extension.ExtensionInfo)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 IOException (java.io.IOException)3 Event (org.pdown.gui.extension.HookScript.Event)3 TaskForm (org.pdown.rest.form.TaskForm)3 File (java.io.File)2 HashMap (java.util.HashMap)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1