use of lee.study.down.model.HttpRequestInfo in project proxyee-down by monkeyWie.
the class GithubUpdateService method update.
@Override
public AbstractHttpDownBootstrap update(UpdateInfo updateInfo, HttpDownCallback callback) throws Exception {
HttpRequestInfo requestInfo = HttpDownUtil.buildGetRequest(updateInfo.getUrl());
TaskInfo taskInfo = HttpDownUtil.getTaskInfo(requestInfo, null, null, HttpDownConstant.clientSslContext, HttpDownConstant.clientLoopGroup).setConnections(64).setFileName("proxyee-down-jar.zip").setFilePath(HttpDownConstant.HOME_PATH.substring(0, HttpDownConstant.HOME_PATH.length() - 1));
HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, requestInfo, null);
AbstractHttpDownBootstrap bootstrap = HttpDownBootstrapFactory.create(httpDownInfo, 5, HttpDownConstant.clientSslContext, HttpDownConstant.clientLoopGroup, callback);
FileUtil.deleteIfExists(bootstrap.getHttpDownInfo().getTaskInfo().buildTaskFilePath());
bootstrap.startDown();
return bootstrap;
}
use of lee.study.down.model.HttpRequestInfo in project proxyee-down by monkeyWie.
the class HttpDownUtil method getResponse.
/**
* 取请求响应
*/
public static HttpResponse getResponse(HttpRequest httpRequest, ProxyConfig proxyConfig, SslContext clientSslCtx, NioEventLoopGroup loopGroup) throws Exception {
final HttpResponse[] httpResponses = new HttpResponse[1];
CountDownLatch cdl = new CountDownLatch(1);
HttpRequestInfo requestInfo = (HttpRequestInfo) httpRequest;
RequestProto requestProto = requestInfo.requestProto();
Bootstrap bootstrap = new Bootstrap();
// 注册线程池
bootstrap.group(loopGroup).channel(// 使用NioSocketChannel来作为连接用的channel类
NioSocketChannel.class).handler(new ChannelInitializer() {
@Override
protected void initChannel(Channel ch) throws Exception {
if (proxyConfig != null) {
ch.pipeline().addLast(ProxyHandleFactory.build(proxyConfig));
}
if (requestProto.getSsl()) {
ch.pipeline().addLast(clientSslCtx.newHandler(ch.alloc()));
}
ch.pipeline().addLast("httpCodec", new HttpClientCodec());
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx0, Object msg0) throws Exception {
if (msg0 instanceof HttpResponse) {
HttpResponse httpResponse = (HttpResponse) msg0;
httpResponses[0] = httpResponse;
ctx0.channel().close();
cdl.countDown();
}
}
});
}
});
if (proxyConfig != null) {
// 代理服务器解析DNS和连接
bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
}
ChannelFuture cf = bootstrap.connect(requestProto.getHost(), requestProto.getPort());
cf.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
httpRequest.headers().set(HttpHeaderNames.RANGE, "bytes=0-0");
cf.channel().writeAndFlush(httpRequest);
if (requestInfo.content() != null) {
HttpContent content = new DefaultLastHttpContent();
content.content().writeBytes(requestInfo.content());
cf.channel().writeAndFlush(content);
}
} else {
cdl.countDown();
}
});
cdl.await(30, TimeUnit.SECONDS);
if (httpResponses[0] == null) {
throw new TimeoutException("getResponse timeout");
}
return httpResponses[0];
}
use of lee.study.down.model.HttpRequestInfo in project proxyee-down by monkeyWie.
the class HttpDownUtil method buildGetRequest.
public static HttpRequestInfo buildGetRequest(String url, Map<String, String> heads, String body) throws MalformedURLException {
URL u = new URL(url);
HttpHeadsInfo headsInfo = new HttpHeadsInfo();
headsInfo.add("Host", u.getHost());
headsInfo.add("Connection", "keep-alive");
headsInfo.add("Upgrade-Insecure-Requests", "1");
headsInfo.add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36");
headsInfo.add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
headsInfo.add("Referer", u.getHost());
headsInfo.add("Accept-Encoding", "gzip, deflate, br");
headsInfo.add("Accept-Language", "zh-CN,zh;q=0.9");
if (heads != null) {
for (Entry<String, String> entry : heads.entrySet()) {
headsInfo.set(entry.getKey(), entry.getValue());
}
}
byte[] content = null;
if (body != null && body.length() > 0) {
content = body.getBytes();
headsInfo.add("Content-Length", content.length);
}
HttpRequestInfo requestInfo = new HttpRequestInfo(HttpVer.HTTP_1_1, HttpMethod.GET.toString(), url, headsInfo, content);
requestInfo.setRequestProto(ProtoUtil.getRequestProto(requestInfo));
return requestInfo;
}
use of lee.study.down.model.HttpRequestInfo in project proxyee-down by monkeyWie.
the class HttpDownSniffIntercept method afterResponse.
@Override
public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) throws Exception {
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)) || (isChrome(pipeline.getHttpRequest().headers().get(HttpHeaderNames.USER_AGENT)) && pipeline.getHttpRequest().uri().matches("^.*\\.[^./]{1,5}(\\?[^?]*)?$") && contentType != null && !contentType.matches("^.*text/.*$")))) {
downFlag = true;
}
HttpRequestInfo httpRequestInfo = (HttpRequestInfo) pipeline.getHttpRequest();
if (downFlag) {
// 如果是下载
// 关闭嗅探下载连接
proxyChannel.close();
LOGGER.debug("=====================下载===========================\n" + pipeline.getHttpRequest().toString() + "\n" + "------------------------------------------------" + httpResponse.toString() + "\n" + "================================================");
// 原始的请求协议
httpRequestInfo.setRequestProto(pipeline.getRequestProto());
pipeline.afterResponse(clientChannel, proxyChannel, httpResponse);
} else {
if (httpRequestInfo.content() != null) {
httpRequestInfo.setContent(null);
}
}
}
pipeline.getDefault().afterResponse(clientChannel, proxyChannel, httpResponse, pipeline);
}
Aggregations