use of org.pdown.core.util.ProtoUtil.RequestProto 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);
}
Aggregations