Search in sources :

Example 1 with HttpRequestForm

use of org.pdown.rest.form.HttpRequestForm 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);
}
Also used : Undefined(jdk.nashorn.internal.runtime.Undefined) ExtensionInfo(org.pdown.gui.extension.ExtensionInfo) HttpRequestForm(org.pdown.rest.form.HttpRequestForm) TaskForm(org.pdown.rest.form.TaskForm) Event(org.pdown.gui.extension.HookScript.Event) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with HttpRequestForm

use of org.pdown.rest.form.HttpRequestForm in project proxyee-down by monkeyWie.

the class JavascriptEngine method main.

public static void main(String[] args) throws ScriptException, NoSuchMethodException, JsonProcessingException, InterruptedException {
    ScriptEngine engine = buildEngine();
    Invocable invocable = (Invocable) engine;
    engine.eval("load('E:/study/extensions/bilibili-helper/dist/hook.js')");
    HttpRequestForm requestForm = new HttpRequestForm();
    requestForm.setUrl("https://www.bilibili.com/video/av34765642");
    Object result = invocable.invokeFunction("error");
    ScriptContext ctx = new SimpleScriptContext();
    ctx.setAttribute("result", result, ScriptContext.ENGINE_SCOPE);
    System.out.println(engine.eval("!!result&&typeof result=='object'&&typeof result.then=='function'", ctx));
}
Also used : Invocable(javax.script.Invocable) SimpleScriptContext(javax.script.SimpleScriptContext) HttpRequestForm(org.pdown.rest.form.HttpRequestForm) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) ScriptEngine(javax.script.ScriptEngine)

Example 3 with HttpRequestForm

use of org.pdown.rest.form.HttpRequestForm in project proxyee-down by monkeyWie.

the class SniffIntercept method afterResponse.

@Override
public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) throws Exception {
    if (!matchFlag) {
        super.afterResponse(clientChannel, proxyChannel, httpResponse, pipeline);
        return;
    }
    if ((httpResponse.status().code() + "").indexOf("20") == 0) {
        // 响应码为20x
        HttpHeaders httpResHeaders = httpResponse.headers();
        String accept = pipeline.getHttpRequest().headers().get(HttpHeaderNames.ACCEPT);
        String contentType = httpResHeaders.get(HttpHeaderNames.CONTENT_TYPE);
        // 有两种情况进行下载 1.url后缀为.xxx  2.带有CONTENT_DISPOSITION:ATTACHMENT响应头
        String disposition = httpResHeaders.get(HttpHeaderNames.CONTENT_DISPOSITION);
        if (accept != null && accept.matches("^.*text/html.*$") && ((disposition != null && disposition.contains(HttpHeaderValues.ATTACHMENT) && disposition.contains(HttpHeaderValues.FILENAME)) || isDownContentType(contentType))) {
            downFlag = true;
        }
        HttpRequestInfo httpRequestInfo = (HttpRequestInfo) pipeline.getHttpRequest();
        if (downFlag) {
            // 如果是下载
            LOGGER.debug("=====================下载===========================\n" + pipeline.getHttpRequest().toString() + "\n" + "------------------------------------------------" + httpResponse.toString() + "\n" + "================================================");
            // 关闭嗅探下载连接
            proxyChannel.close();
            httpRequestInfo.setRequestProto(new RequestProto(pipeline.getRequestProto().getHost(), pipeline.getRequestProto().getPort(), pipeline.getRequestProto().getSsl()));
            HttpRequestForm requestForm = HttpRequestForm.parse(httpRequestInfo);
            HttpResponseInfo responseInfo = HttpDownUtil.getHttpResponseInfo(httpRequestInfo, null, null, (NioEventLoopGroup) clientChannel.eventLoop().parent());
            httpResponse.headers().clear();
            httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html");
            httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
            String js = "<script type=\"text/javascript\">window.history.go(-1)</script>";
            HttpContent httpContent = new DefaultLastHttpContent();
            httpContent.content().writeBytes(js.getBytes());
            httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, httpContent.content().readableBytes());
            clientChannel.writeAndFlush(httpResponse);
            clientChannel.writeAndFlush(httpContent);
            clientChannel.close();
            ObjectMapper objectMapper = new ObjectMapper();
            String requestParam = URLEncoder.encode(objectMapper.writeValueAsString(requestForm), "utf-8");
            String responseParam = URLEncoder.encode(objectMapper.writeValueAsString(responseInfo), "utf-8");
            String uri = "/#/tasks?request=" + requestParam + "&response=" + responseParam;
            DownApplication.INSTANCE.loadUri(uri, false);
            return;
        } else {
            if (httpRequestInfo.content() != null) {
                httpRequestInfo.setContent(null);
            }
        }
    }
    super.afterResponse(clientChannel, proxyChannel, httpResponse, pipeline);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) HttpResponseInfo(org.pdown.core.entity.HttpResponseInfo) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) HttpRequestForm(org.pdown.rest.form.HttpRequestForm) HttpRequestInfo(org.pdown.core.entity.HttpRequestInfo) RequestProto(org.pdown.core.util.ProtoUtil.RequestProto) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

HttpRequestForm (org.pdown.rest.form.HttpRequestForm)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)1 HttpContent (io.netty.handler.codec.http.HttpContent)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)1 IOException (java.io.IOException)1 Invocable (javax.script.Invocable)1 ScriptContext (javax.script.ScriptContext)1 ScriptEngine (javax.script.ScriptEngine)1 SimpleScriptContext (javax.script.SimpleScriptContext)1 Undefined (jdk.nashorn.internal.runtime.Undefined)1 HttpRequestInfo (org.pdown.core.entity.HttpRequestInfo)1 HttpResponseInfo (org.pdown.core.entity.HttpResponseInfo)1 RequestProto (org.pdown.core.util.ProtoUtil.RequestProto)1 ExtensionInfo (org.pdown.gui.extension.ExtensionInfo)1 Event (org.pdown.gui.extension.HookScript.Event)1 TaskForm (org.pdown.rest.form.TaskForm)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1