Search in sources :

Example 1 with ProxyConfig

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);
        }
    };
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) TaskInfo(lee.study.down.model.TaskInfo) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Channel(io.netty.channel.Channel) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpProxyIntercept(lee.study.proxyee.intercept.HttpProxyIntercept) ProxyConfig(lee.study.proxyee.proxy.ProxyConfig) HttpDownInfo(lee.study.down.model.HttpDownInfo) HttpProxyInterceptPipeline(lee.study.proxyee.intercept.HttpProxyInterceptPipeline) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 2 with ProxyConfig

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;
}
Also used : ProxyConfig(lee.study.proxyee.proxy.ProxyConfig) ResultInfo(lee.study.down.model.ResultInfo) ConfigInfo(lee.study.down.model.ConfigInfo) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with ProxyConfig

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];
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ProxyConfig(lee.study.proxyee.proxy.ProxyConfig) ProxyHandleFactory(lee.study.proxyee.proxy.ProxyHandleFactory) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) URLDecoder(java.net.URLDecoder) URL(java.net.URL) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) TimeoutException(java.util.concurrent.TimeoutException) RequestProto(lee.study.proxyee.util.ProtoUtil.RequestProto) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) NoopAddressResolverGroup(io.netty.resolver.NoopAddressResolverGroup) TaskInfo(lee.study.down.model.TaskInfo) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Matcher(java.util.regex.Matcher) HttpHeadsInfo(lee.study.down.model.HttpHeadsInfo) HttpVer(lee.study.down.model.HttpRequestInfo.HttpVer) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Map(java.util.Map) HttpContent(io.netty.handler.codec.http.HttpContent) HttpRequest(io.netty.handler.codec.http.HttpRequest) ChannelInitializer(io.netty.channel.ChannelInitializer) SslContext(io.netty.handler.ssl.SslContext) MalformedURLException(java.net.MalformedURLException) HttpRequestInfo(lee.study.down.model.HttpRequestInfo) HttpMethod(io.netty.handler.codec.http.HttpMethod) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) IOException(java.io.IOException) UUID(java.util.UUID) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Closeable(java.io.Closeable) Entry(java.util.Map.Entry) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Pattern(java.util.regex.Pattern) ProtoUtil(lee.study.proxyee.util.ProtoUtil) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) HttpResponse(io.netty.handler.codec.http.HttpResponse) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpRequestInfo(lee.study.down.model.HttpRequestInfo) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) TimeoutException(java.util.concurrent.TimeoutException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) RequestProto(lee.study.proxyee.util.ProtoUtil.RequestProto) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ProxyConfig (lee.study.proxyee.proxy.ProxyConfig)3 Channel (io.netty.channel.Channel)2 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)2 HttpContent (io.netty.handler.codec.http.HttpContent)2 HttpRequest (io.netty.handler.codec.http.HttpRequest)2 HttpResponse (io.netty.handler.codec.http.HttpResponse)2 TaskInfo (lee.study.down.model.TaskInfo)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)1 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1