use of lee.study.proxyee.proxy.ProxyConfig in project proxyee-down by monkeyWie.
the class HttpDownHandleInterceptFactory method create.
@Override
public HttpProxyIntercept create() {
return new HttpProxyIntercept() {
@Override
public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse, HttpProxyInterceptPipeline pipeline) throws Exception {
HttpRequest httpRequest = pipeline.getHttpRequest();
ProxyConfig proxyConfig = ContentManager.CONFIG.get().getSecProxyConfig();
TaskInfo taskInfo = HttpDownUtil.getTaskInfo(httpRequest, httpResponse.headers(), proxyConfig, HttpDownConstant.clientSslContext, HttpDownConstant.clientLoopGroup);
HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, httpRequest, proxyConfig);
ContentManager.DOWN.putBoot(httpDownInfo);
httpResponse.setStatus(HttpResponseStatus.OK);
httpResponse.headers().clear();
httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html");
byte[] content;
if (HttpUtil.checkHead(httpRequest, HttpHeaderNames.HOST, "^.*\\.baidupcs\\.com.*$")) {
content = "<script>window.close();</script>".getBytes();
} else {
content = "<script>window.history.go(-1);</script>".getBytes();
}
httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.length);
clientChannel.writeAndFlush(httpResponse);
HttpContent httpContent = new DefaultLastHttpContent();
httpContent.content().writeBytes(content);
clientChannel.writeAndFlush(httpContent);
clientChannel.close();
httpDownDispatch.dispatch(httpDownInfo);
}
};
}
use of lee.study.proxyee.proxy.ProxyConfig in project proxyee-down by monkeyWie.
the class HttpDownController method setConfigInfo.
@RequestMapping("/setConfigInfo")
public ResultInfo setConfigInfo(@RequestBody ConfigForm configForm) {
ResultInfo resultInfo = new ResultInfo();
int beforePort = ContentManager.CONFIG.get().getProxyPort();
boolean beforeSecProxyEnable = ContentManager.CONFIG.get().isSecProxyEnable();
ProxyConfig beforeSecProxyConfig = ContentManager.CONFIG.get().getSecProxyConfig();
if (!configForm.isSecProxyEnable()) {
configForm.setSecProxyConfig(null);
}
ConfigInfo configInfo = configForm.convert();
ContentManager.CONFIG.set(configInfo);
ContentManager.CONFIG.save();
// 代理服务器需要重新启动
if (beforePort != configInfo.getProxyPort() || beforeSecProxyEnable != configInfo.isSecProxyEnable() || (configInfo.isSecProxyEnable() && !configInfo.getSecProxyConfig().equals(beforeSecProxyConfig))) {
new Thread(() -> {
HttpDownApplication.getProxyServer().close();
HttpDownApplication.getProxyServer().setProxyConfig(configInfo.getSecProxyConfig());
HttpDownApplication.getProxyServer().start(configInfo.getProxyPort());
}).start();
}
return resultInfo;
}
use of lee.study.proxyee.proxy.ProxyConfig 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];
}
Aggregations