Search in sources :

Example 1 with HttpDownInfo

use of lee.study.down.model.HttpDownInfo in project proxyee-down by monkeyWie.

the class AbstractHttpDownBootstrap method startChunkDown.

protected void startChunkDown(ChunkInfo chunkInfo, int updateStatus) throws Exception {
    HttpRequestInfo requestInfo = (HttpRequestInfo) httpDownInfo.getRequest();
    RequestProto requestProto = requestInfo.requestProto();
    LOGGER.debug("开始下载:" + chunkInfo);
    Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(clientLoopGroup).handler(new HttpDownInitializer(requestProto.getSsl(), this, chunkInfo));
    if (httpDownInfo.getProxyConfig() != null) {
        // 代理服务器解析DNS和连接
        bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
    }
    if (callback != null) {
        callback.onChunkConnecting(httpDownInfo, chunkInfo);
    }
    ChannelFuture cf = bootstrap.connect(requestProto.getHost(), requestProto.getPort());
    chunkInfo.setStatus(updateStatus);
    // 重置最后下载时间
    chunkInfo.setLastDownTime(System.currentTimeMillis());
    cf.addListener((ChannelFutureListener) future -> {
        if (future.isSuccess()) {
            synchronized (chunkInfo) {
                setChannel(chunkInfo, future.channel());
            }
            synchronized (requestInfo) {
                LOGGER.debug("下载连接成功:channelId[" + future.channel().id() + "]\t" + chunkInfo);
                if (httpDownInfo.getTaskInfo().isSupportRange()) {
                    requestInfo.headers().set(HttpHeaderNames.RANGE, "bytes=" + chunkInfo.getNowStartPosition() + "-" + chunkInfo.getEndPosition());
                } else {
                    requestInfo.headers().remove(HttpHeaderNames.RANGE);
                }
                future.channel().writeAndFlush(httpDownInfo.getRequest());
            }
            if (requestInfo.content() != null) {
                HttpContent content = new DefaultLastHttpContent();
                content.content().writeBytes(requestInfo.content());
                future.channel().writeAndFlush(content);
            }
        } else {
            LOGGER.debug("下载连接失败:" + chunkInfo);
            chunkInfo.setStatus(HttpDownStatus.FAIL);
            future.channel().close();
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) RandomAccessFile(java.io.RandomAccessFile) HttpDownStatus(lee.study.down.constant.HttpDownStatus) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) RequestProto(lee.study.proxyee.util.ProtoUtil.RequestProto) ByteBuffer(java.nio.ByteBuffer) FileUtil(lee.study.down.util.FileUtil) NoopAddressResolverGroup(io.netty.resolver.NoopAddressResolverGroup) TaskInfo(lee.study.down.model.TaskInfo) HttpDownInitializer(lee.study.down.handle.HttpDownInitializer) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Map(java.util.Map) ChunkInfo(lee.study.down.model.ChunkInfo) HttpContent(io.netty.handler.codec.http.HttpContent) Logger(org.slf4j.Logger) SslContext(io.netty.handler.ssl.SslContext) HttpRequestInfo(lee.study.down.model.HttpRequestInfo) IOException(java.io.IOException) HttpDownCallback(lee.study.down.dispatch.HttpDownCallback) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) File(java.io.File) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) HttpDownInfo(lee.study.down.model.HttpDownInfo) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Closeable(java.io.Closeable) Data(lombok.Data) BootstrapException(lee.study.down.exception.BootstrapException) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) AllArgsConstructor(lombok.AllArgsConstructor) HttpDownUtil(lee.study.down.util.HttpDownUtil) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Bootstrap(io.netty.bootstrap.Bootstrap) HttpRequestInfo(lee.study.down.model.HttpRequestInfo) RequestProto(lee.study.proxyee.util.ProtoUtil.RequestProto) HttpDownInitializer(lee.study.down.handle.HttpDownInitializer) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 2 with HttpDownInfo

use of lee.study.down.model.HttpDownInfo in project proxyee-down by monkeyWie.

the class DownContent method getDownInfos.

public List<HttpDownInfo> getDownInfos() {
    List<HttpDownInfo> httpDownInfoList = new ArrayList<>();
    for (String id : downContent.keySet()) {
        HttpDownInfo httpDownInfo = getDownInfo(id);
        httpDownInfoList.add(httpDownInfo);
    }
    return httpDownInfoList;
}
Also used : ArrayList(java.util.ArrayList) HttpDownInfo(lee.study.down.model.HttpDownInfo)

Example 3 with HttpDownInfo

use of lee.study.down.model.HttpDownInfo in project proxyee-down by monkeyWie.

the class DownContent method init.

/**
 * 从配置文件中加载信息
 */
public void init() {
    downContent = new ConcurrentHashMap<>();
    if (FileUtil.exists(HttpDownConstant.TASK_RECORD_PATH)) {
        try {
            List<HttpDownInfo> records = (List<HttpDownInfo>) ByteUtil.deserialize(HttpDownConstant.TASK_RECORD_PATH);
            for (HttpDownInfo httpDownInfo : records) {
                AbstractHttpDownBootstrap bootstrap = HttpDownBootstrapFactory.create(httpDownInfo, ContentManager.CONFIG.get().getRetryCount(), HttpDownConstant.clientSslContext, HttpDownConstant.clientLoopGroup, HttpDownConstant.httpDownCallback);
                TaskInfo taskInfo = httpDownInfo.getTaskInfo();
                if (taskInfo.getStatus() == HttpDownStatus.WAIT) {
                    continue;
                }
                // 下载未完成
                if (taskInfo.getStatus() != HttpDownStatus.DONE) {
                    String taskDetailPath = taskInfo.buildTaskRecordFilePath();
                    String taskDetailBakPath = taskInfo.buildTaskRecordBakFilePath();
                    // 存在下载进度信息则更新,否则重新下载
                    if (FileUtil.existsAny(taskDetailPath, taskDetailBakPath)) {
                        try {
                            taskInfo = (TaskInfo) ByteUtil.deserialize(taskDetailPath, taskDetailBakPath);
                            httpDownInfo.setTaskInfo(taskInfo);
                        } catch (IOException | ClassNotFoundException e) {
                            taskInfo.reset();
                        }
                    } else {
                        taskInfo.reset();
                    }
                    if (taskInfo.getStatus() != HttpDownStatus.FAIL) {
                        // 设置为暂停状态
                        taskInfo.setStatus(HttpDownStatus.PAUSE);
                        taskInfo.getChunkInfoList().forEach((chunk) -> {
                            if (chunk.getStatus() != HttpDownStatus.DONE) {
                                chunk.setStatus(HttpDownStatus.PAUSE);
                            }
                        });
                    }
                }
                putBoot(bootstrap);
            }
        } catch (Exception e) {
            LOGGER.warn("加载配置文件失败:", e);
        }
    }
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) AbstractHttpDownBootstrap(lee.study.down.boot.AbstractHttpDownBootstrap) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) HttpDownInfo(lee.study.down.model.HttpDownInfo) IOException(java.io.IOException)

Example 4 with HttpDownInfo

use of lee.study.down.model.HttpDownInfo 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 5 with HttpDownInfo

use of lee.study.down.model.HttpDownInfo in project proxyee-down by monkeyWie.

the class HttpDownController method commonBuildTask.

public static ResultInfo commonBuildTask(BuildTaskForm form) throws Exception {
    ResultInfo resultInfo = new ResultInfo();
    Map<String, String> heads = new LinkedHashMap<>();
    if (form.getHeads() != null) {
        for (Map<String, String> head : form.getHeads()) {
            String key = head.get("key");
            String value = head.get("value");
            if (!StringUtils.isEmpty(head.get("key")) && !StringUtils.isEmpty(head.get("value"))) {
                heads.put(key, value);
            }
        }
    }
    try {
        HttpRequestInfo requestInfo = HttpDownUtil.buildGetRequest(form.getUrl(), heads, form.getBody());
        TaskInfo taskInfo = HttpDownUtil.getTaskInfo(requestInfo, null, ContentManager.CONFIG.get().getSecProxyConfig(), HttpDownConstant.clientSslContext, HttpDownConstant.clientLoopGroup);
        HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, requestInfo, ContentManager.CONFIG.get().getSecProxyConfig());
        ContentManager.DOWN.putBoot(httpDownInfo);
        resultInfo.setData(taskInfo.getId());
    } catch (MalformedURLException e) {
        resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("链接格式不正确");
    } catch (TimeoutException e) {
        resultInfo.setStatus(ResultStatus.BAD.getCode()).setMsg("连接超时,请重试");
    } catch (Exception e) {
        throw new RuntimeException("buildTask error:" + form.toString(), e);
    }
    return resultInfo;
}
Also used : TaskInfo(lee.study.down.model.TaskInfo) MalformedURLException(java.net.MalformedURLException) HttpRequestInfo(lee.study.down.model.HttpRequestInfo) ResultInfo(lee.study.down.model.ResultInfo) HttpDownInfo(lee.study.down.model.HttpDownInfo) TimeoutException(java.util.concurrent.TimeoutException) BootstrapException(lee.study.down.exception.BootstrapException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

HttpDownInfo (lee.study.down.model.HttpDownInfo)9 TaskInfo (lee.study.down.model.TaskInfo)7 IOException (java.io.IOException)4 AbstractHttpDownBootstrap (lee.study.down.boot.AbstractHttpDownBootstrap)4 BootstrapException (lee.study.down.exception.BootstrapException)4 HttpRequestInfo (lee.study.down.model.HttpRequestInfo)4 ResultInfo (lee.study.down.model.ResultInfo)4 TimeoutException (java.util.concurrent.TimeoutException)3 HttpDownCallback (lee.study.down.dispatch.HttpDownCallback)3 Channel (io.netty.channel.Channel)2 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)2 HttpContent (io.netty.handler.codec.http.HttpContent)2 File (java.io.File)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 HttpDownStatus (lee.study.down.constant.HttpDownStatus)2