From 5bdc1c881baee78a5a66e6b43ba1e97a734f0a2d Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Fri, 13 Jul 2018 23:40:51 +0800 Subject: [PATCH] =?UTF-8?q?ftps=20=E4=B8=8A=E4=BC=A0/=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=8C=E4=B8=8A=E4=BC=A0/=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E9=80=9F=E5=BA=A6=E9=99=90=E5=88=B6=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/arialyy/aria/core/ConfigHelper.java | 3 + .../com/arialyy/aria/core/FtpUrlEntity.java | 9 +- .../arialyy/aria/core/common/AbsFileer.java | 38 ++++--- .../aria/core/common/AbsThreadTask.java | 39 +++++-- .../aria/core/common/BandwidthLimiter.java | 104 ++++++++++++++++++ .../com/arialyy/aria/core/common/IUtil.java | 6 +- .../aria/core/common/ProtocolType.java | 2 +- .../core/common/ftp/AbsFtpInfoThread.java | 12 +- .../core/common/ftp/AbsFtpThreadTask.java | 7 +- .../{FTPSSLConfig.java => FTPSConfig.java} | 16 +-- .../aria/core/common/ftp/FtpDelegate.java | 6 + .../aria/core/download/DownloadReceiver.java | 4 +- .../aria/core/download/DownloadTask.java | 10 -- .../core/download/FtpDirDownloadTarget.java | 36 +++++- .../aria/core/download/FtpDownloadTarget.java | 14 ++- .../download/downloader/AbsGroupUtil.java | 10 +- .../download/downloader/ConnectionHelp.java | 5 +- .../core/download/downloader/Downloader.java | 9 ++ .../download/downloader/FtpDirInfoThread.java | 2 +- .../download/downloader/FtpThreadTask.java | 34 +++--- .../download/downloader/HttpThreadTask.java | 29 +++-- .../downloader/SimpleDownloadUtil.java | 6 +- .../arialyy/aria/core/inf/AbsGroupTask.java | 8 +- .../com/arialyy/aria/core/inf/AbsTask.java | 13 +++ .../com/arialyy/aria/core/inf/IFtpTarget.java | 9 ++ .../arialyy/aria/core/queue/AbsTaskQueue.java | 14 +++ .../aria/core/queue/DownloadTaskQueue.java | 12 -- .../aria/core/upload/FtpUploadTarget.java | 33 +++++- .../aria/core/upload/UploadReceiver.java | 10 ++ .../core/upload/uploader/FtpThreadTask.java | 29 +++-- .../core/upload/uploader/HttpThreadTask.java | 17 ++- .../upload/uploader/SimpleUploadUtil.java | 4 +- .../com/arialyy/aria/util/CommonUtil.java | 18 +++ .../com/arialyy/aria/util/SSLContextUtil.java | 45 ++++++-- DEV_LOG.md | 1 + app/src/main/AndroidManifest.xml | 10 +- app/src/main/assets/aria_config.xml | 7 +- ...TestActivity.java => TestFTPActivity.java} | 16 ++- ...pActivity.java => TestFTPDirActivity.java} | 8 +- .../com/arialyy/aria/core/Configuration.java | 45 +++++--- .../arialyy/aria/core/upload/UploadTask.java | 1 - 41 files changed, 518 insertions(+), 183 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/common/BandwidthLimiter.java rename Aria/src/main/java/com/arialyy/aria/core/common/ftp/{FTPSSLConfig.java => FTPSConfig.java} (83%) rename app/src/main/java/com/arialyy/simple/test/{TestActivity.java => TestFTPActivity.java} (84%) rename app/src/main/java/com/arialyy/simple/test/{TestGroupActivity.java => TestFTPDirActivity.java} (90%) diff --git a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java index 0d26d978..e8dda5b6 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java +++ b/Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java @@ -184,6 +184,9 @@ class ConfigHelper extends DefaultHandler { if (mType == ConfigType.DOWNLOAD) { mDownloadConfig.maxSpeed = maxSpeed; } + if (mType == ConfigType.UPLOAD) { + mUploadConfig.maxSpeed = maxSpeed; + } } private void loadConvertSpeed(String value) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java b/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java index eaa7173c..8d4df9e5 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/FtpUrlEntity.java @@ -17,6 +17,7 @@ */ package com.arialyy.aria.core; +import com.arialyy.aria.core.common.ProtocolType; import com.arialyy.aria.orm.annotation.Ignore; import java.net.InetAddress; @@ -47,7 +48,7 @@ public class FtpUrlEntity implements Cloneable { /** * ftp协议:ftp */ - public String protocol; + public String scheme; /** * 登录的用户名 @@ -91,9 +92,11 @@ public class FtpUrlEntity implements Cloneable { public String storePass; /** - * SSL协议 + * 连接协议 + * {@link ProtocolType} */ - public String SSLProtocol; + @ProtocolType + public String protocol; /** * 私钥别名 diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java index 23250a4b..91fbc955 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java @@ -21,7 +21,6 @@ import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.AbsNormalEntity; import com.arialyy.aria.core.inf.AbsTaskEntity; -import com.arialyy.aria.core.inf.IDownloadListener; import com.arialyy.aria.core.inf.IEventListener; import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.ALog; @@ -45,7 +44,7 @@ import java.util.concurrent.Executors; * 任务处理器 */ public abstract class AbsFileer> - implements Runnable, IUtil { + implements Runnable { private static final String STATE = "_state_"; private static final String RECORD = "_record_"; /** @@ -93,7 +92,12 @@ public abstract class AbsFileer tr.endLocation) { + } else if (realLocation > tr.endLocation) { ALog.i(TAG, String.format("分块【%s】错误,将重新开始该分块", temp.getPath())); temp.delete(); tr.startLocation = i * blockLen; diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java index edf100a5..e8be8cf4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java @@ -26,7 +26,6 @@ import com.arialyy.aria.util.ErrorHelp; import com.arialyy.aria.util.FileUtil; import com.arialyy.aria.util.NetUtils; import java.io.File; -import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.Timer; @@ -49,7 +48,7 @@ public abstract class AbsThreadTask 0) { + mSpeedBandUtil = new BandwidthLimiter(getMaxSpeed(), mThreadNum); } } @@ -115,6 +115,23 @@ public abstract class AbsThreadTask 1) { + maxRate = maxRate / threadNum; + } + this.setMaxRate(maxRate); + } + + /** + * Set the max upload or download rate in KB/s. maxRate must be grater than + * 0. If maxRate is zero, it means there is no bandwidth limit. + * + * @param maxRate If maxRate is zero, it means there is no bandwidth limit. + * @throws IllegalArgumentException + */ + public synchronized void setMaxRate(int maxRate) + throws IllegalArgumentException { + if (maxRate < 0) { + throw new IllegalArgumentException("maxRate can not less than 0"); + } + this.maxRate = maxRate; + if (maxRate == 0) { + this.timeCostPerChunk = 0; + } else { + this.timeCostPerChunk = (1000000000L * CHUNK_LENGTH) + / (this.maxRate * KB); + } + } + + /** + * Next 1 byte should do bandwidth limit. + */ + public synchronized void limitNextBytes() { + this.limitNextBytes(1); + } + + /** + * Next len bytes should do bandwidth limit + */ + public synchronized void limitNextBytes(int len) { + this.bytesWillBeSentOrReceive += len; + + /* We have sent CHUNK_LENGTH bytes */ + while (this.bytesWillBeSentOrReceive > CHUNK_LENGTH) { + long nowTick = System.nanoTime(); + long missedTime = this.timeCostPerChunk + - (nowTick - this.lastPieceSentOrReceiveTick); + if (missedTime > 0) { + try { + Thread.currentThread().sleep(missedTime / 1000000, + (int) (missedTime % 1000000)); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + this.bytesWillBeSentOrReceive -= CHUNK_LENGTH; + this.lastPieceSentOrReceiveTick = nowTick + + (missedTime > 0 ? missedTime : 0); + } + } +} \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/IUtil.java b/Aria/src/main/java/com/arialyy/aria/core/common/IUtil.java index 17698d8f..f7354574 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/IUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/IUtil.java @@ -60,7 +60,9 @@ public interface IUtil { void resume(); /** - * 设置最大速度 + * 设置最大下载/上传速度 + * + * @param speed 单位:kb */ - void setMaxSpeed(double maxSpeed); + void setMaxSpeed(int speed); } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ProtocolType.java b/Aria/src/main/java/com/arialyy/aria/core/common/ProtocolType.java index 740e85ee..8a80e123 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ProtocolType.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/ProtocolType.java @@ -29,7 +29,7 @@ import java.lang.annotation.RetentionPolicy; ProtocolType.TLSv1_2 }) @Retention(RetentionPolicy.SOURCE) public @interface ProtocolType { - String Default = "Default"; + String Default = "TLS"; String SSL = "SSL"; String SSLv3 = "SSLv3"; String TLS = "TLS"; diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpInfoThread.java index 799bdb87..2379d7f4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/AbsFtpInfoThread.java @@ -85,7 +85,7 @@ public abstract class AbsFtpInfoThread implements ITarget { - private final String TAG = "FTPSSLConfig"; +public class FTPSConfig implements ITarget { + private final String TAG = "FTPSConfig"; private TARGET mTarget; private FtpUrlEntity mUrlEntity; - public FTPSSLConfig(TARGET target) { + public FTPSConfig(TARGET target) { mTarget = target; mUrlEntity = mTarget.getTaskEntity().getUrlEntity(); } @@ -39,11 +39,11 @@ public class FTPSSLConfig implements ITarget { * * @param protocol {@link ProtocolType} */ - public FTPSSLConfig setProtocol(@ProtocolType String protocol) { + public FTPSConfig setProtocol(@ProtocolType String protocol) { if (TextUtils.isEmpty(protocol)) { throw new NullPointerException("协议为空"); } - mUrlEntity.SSLProtocol = protocol; + mUrlEntity.protocol = protocol; return this; } @@ -52,7 +52,7 @@ public class FTPSSLConfig implements ITarget { * * @param keyAlias 别名 */ - public FTPSSLConfig setAlias(String keyAlias) { + public FTPSConfig setAlias(String keyAlias) { if (TextUtils.isEmpty(keyAlias)) { throw new NullPointerException("别名为空"); } @@ -65,7 +65,7 @@ public class FTPSSLConfig implements ITarget { * * @param storePass 私钥密码 */ - public FTPSSLConfig setStorePass(String storePass) { + public FTPSConfig setStorePass(String storePass) { if (TextUtils.isEmpty(storePass)) { throw new NullPointerException("证书密码为空"); } @@ -78,7 +78,7 @@ public class FTPSSLConfig implements ITarget { * * @param storePath 证书路径 */ - public FTPSSLConfig setStorePath(String storePath) { + public FTPSConfig setStorePath(String storePath) { if (TextUtils.isEmpty(storePath)) { throw new NullPointerException("证书路径为空"); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpDelegate.java index 80e1c284..04f2792d 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpDelegate.java @@ -20,6 +20,7 @@ import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.inf.IFtpTarget; import com.arialyy.aria.util.ALog; +import java.net.Proxy; /** * Created by laoyuyu on 2018/3/9. @@ -61,4 +62,9 @@ public class FtpDelegate implements IFtpTarget mUrlEntity.account = account; return mTarget; } + + @Override public TARGET setProxy(Proxy proxy) { + mTarget.getTaskEntity().setProxy(proxy); + return mTarget; + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java index a151a1d0..143c0a69 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java @@ -47,12 +47,12 @@ public class DownloadReceiver extends AbsReceiver { /** * 设置最大下载速度,单位:kb - * 该方法为实验性功能,清不要轻易在生产环境中使用。 * * @param maxSpeed 为0表示不限速 */ - @Deprecated public void setMaxSpeed(int maxSpeed) { + public DownloadReceiver setMaxSpeed(int maxSpeed) { AriaManager.getInstance(AriaManager.APP).getDownloadConfig().setMaxSpeed(maxSpeed); + return this; } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java index 56181f2f..f86f9721 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java @@ -35,7 +35,6 @@ public class DownloadTask extends AbsNormalTask { private DownloadListener mListener; private DownloadEntity mEntity; - private IUtil mUtil; private DownloadTask(DownloadTaskEntity taskEntity, Handler outHandler) { mTaskEntity = taskEntity; @@ -96,15 +95,6 @@ public class DownloadTask extends AbsNormalTask { stop(true); } - /** - * 设置最大下载速度,单位:kb - * - * @param maxSpeed 为0表示不限速 - */ - public void setMaxSpeed(double maxSpeed) { - mUtil.setMaxSpeed(maxSpeed); - } - /** * 开始下载 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java index 758d8cb5..d18b90dd 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java @@ -17,11 +17,14 @@ package com.arialyy.aria.core.download; import android.support.annotation.CheckResult; import android.text.TextUtils; +import com.arialyy.aria.core.FtpUrlEntity; +import com.arialyy.aria.core.common.ftp.FTPSConfig; import com.arialyy.aria.core.common.ftp.FtpDelegate; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IFtpTarget; import com.arialyy.aria.core.manager.TEManager; import com.arialyy.aria.util.ALog; +import java.net.Proxy; /** * Created by Aria.Lao on 2017/7/26. @@ -58,11 +61,21 @@ public class FtpDirDownloadTarget extends BaseGroupTarget mTaskEntity.save(); if (mTaskEntity.getSubTaskEntities() != null) { //初始化子项的登录信息 + FtpUrlEntity tUrlEntity = mTaskEntity.getUrlEntity(); for (DownloadTaskEntity entity : mTaskEntity.getSubTaskEntities()) { - entity.getUrlEntity().needLogin = mTaskEntity.getUrlEntity().needLogin; - entity.getUrlEntity().account = mTaskEntity.getUrlEntity().account; - entity.getUrlEntity().user = mTaskEntity.getUrlEntity().user; - entity.getUrlEntity().password = mTaskEntity.getUrlEntity().password; + FtpUrlEntity urlEntity = entity.getUrlEntity(); + urlEntity.needLogin = tUrlEntity.needLogin; + urlEntity.account = tUrlEntity.account; + urlEntity.user = tUrlEntity.user; + urlEntity.password = tUrlEntity.password; + // 处理ftps详细 + if (tUrlEntity.isFtps) { + urlEntity.isFtps = true; + urlEntity.protocol = tUrlEntity.protocol; + urlEntity.storePath = tUrlEntity.storePath; + urlEntity.storePass = tUrlEntity.storePass; + urlEntity.keyAlias = tUrlEntity.keyAlias; + } } } } @@ -91,6 +104,17 @@ public class FtpDirDownloadTarget extends BaseGroupTarget return true; } + /** + * 是否是FTPS协议 + * 如果是FTPS协议,需要使用{@link FTPSConfig#setStorePath(String)} 、{@link FTPSConfig#setAlias(String)} + * 设置证书信息 + */ + @CheckResult + public FTPSConfig asFtps() { + mTaskEntity.getUrlEntity().isFtps = true; + return new FTPSConfig<>(this); + } + @CheckResult @Override public FtpDirDownloadTarget charSet(String charSet) { return mDelegate.charSet(charSet); @@ -105,4 +129,8 @@ public class FtpDirDownloadTarget extends BaseGroupTarget @Override public FtpDirDownloadTarget login(String userName, String password, String account) { return mDelegate.login(userName, password, account); } + + @Override public FtpDirDownloadTarget setProxy(Proxy proxy) { + return mDelegate.setProxy(proxy); + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java index 3637f347..8494ec20 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java @@ -17,11 +17,12 @@ package com.arialyy.aria.core.download; import android.support.annotation.CheckResult; import android.support.annotation.NonNull; -import com.arialyy.aria.core.common.ftp.FTPSSLConfig; +import com.arialyy.aria.core.common.ftp.FTPSConfig; import com.arialyy.aria.core.common.ftp.FtpDelegate; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IFtpTarget; import com.arialyy.aria.util.CommonUtil; +import java.net.Proxy; /** * Created by lyy on 2016/12/5. @@ -51,13 +52,13 @@ public class FtpDownloadTarget extends BaseNormalTarget /** * 是否是FTPS协议 - * 如果是FTPS协议,需要使用{@link FTPSSLConfig#setPrivateKeyPath(String)}、{@link FTPSSLConfig#setCertPath(String)} + * 如果是FTPS协议,需要使用{@link FTPSConfig#setStorePath(String)} 、{@link FTPSConfig#setAlias(String)} * 设置证书信息 */ @CheckResult - public FTPSSLConfig asFtps() { + public FTPSConfig asFtps() { mTaskEntity.getUrlEntity().isFtps = true; - return new FTPSSLConfig<>(this); + return new FTPSConfig<>(this); } /** @@ -102,4 +103,9 @@ public class FtpDownloadTarget extends BaseNormalTarget @Override public FtpDownloadTarget login(String userName, String password, String account) { return mDelegate.login(userName, password, account); } + + @CheckResult + @Override public FtpDownloadTarget setProxy(Proxy proxy) { + return mDelegate.setProxy(proxy); + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java index 845aff7e..03a1b21b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java @@ -312,8 +312,14 @@ public abstract class AbsGroupUtil implements IUtil { start(); } - @Override public void setMaxSpeed(double maxSpeed) { - + @Override public void setMaxSpeed(int speed) { + Set keys = mDownloaderMap.keySet(); + for (String key : keys) { + Downloader dt = mDownloaderMap.get(key); + if (dt != null) { + dt.setMaxSpeed(speed); + } + } } private void clearState() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java index 6b9250f1..a7b985a7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java @@ -17,6 +17,7 @@ package com.arialyy.aria.core.download.downloader; import android.text.TextUtils; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.common.ProtocolType; import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity; @@ -78,9 +79,9 @@ class ConnectionHelp { conn = (HttpsURLConnection) urlConn; SSLContext sslContext = SSLContextUtil.getSSLContextFromAssets(manager.getDownloadConfig().getCaName(), - manager.getDownloadConfig().getCaPath()); + manager.getDownloadConfig().getCaPath(), ProtocolType.Default); if (sslContext == null) { - sslContext = SSLContextUtil.getDefaultSLLContext(); + sslContext = SSLContextUtil.getDefaultSLLContext(ProtocolType.Default); } SSLSocketFactory ssf = sslContext.getSocketFactory(); ((HttpsURLConnection) conn).setSSLSocketFactory(ssf); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java index 2335093d..caa53482 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/Downloader.java @@ -83,6 +83,15 @@ class Downloader extends AbsFileer { return false; } + @Override protected void onPostPre() { + super.onPostPre(); + ((IDownloadListener) mListener).onPostPre(mEntity.getFileSize()); + File file = new File(mEntity.getDownloadPath()); + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } + } + @Override protected AbsThreadTask selectThreadTask(SubThreadConfig config) { switch (mTaskEntity.getRequestType()) { case AbsTaskEntity.D_FTP: diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java index 65797d49..269f4b36 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpDirInfoThread.java @@ -61,7 +61,7 @@ class FtpDirInfoThread extends AbsFtpInfoThread FtpThreadTask(StateConstance constance, IDownloadListener listener, SubThreadConfig downloadInfo) { super(constance, listener, downloadInfo); - AriaManager manager = AriaManager.getInstance(AriaManager.APP); - mConnectTimeOut = manager.getDownloadConfig().getConnectTimeOut(); - mReadTimeOut = manager.getDownloadConfig().getIOTimeOut(); - mBufSize = manager.getDownloadConfig().getBuffSize(); - isNotNetRetry = manager.getDownloadConfig().isNotNetRetry(); + mConnectTimeOut = mAridManager.getDownloadConfig().getConnectTimeOut(); + mReadTimeOut = mAridManager.getDownloadConfig().getIOTimeOut(); + mBufSize = mAridManager.getDownloadConfig().getBuffSize(); + isNotNetRetry = mAridManager.getDownloadConfig().isNotNetRetry(); isOpenDynamicFile = STATE.TASK_RECORD.isOpenDynamicFile; isBlock = STATE.TASK_RECORD.isBlock; - setMaxSpeed(manager.getDownloadConfig().getMaxSpeed()); } @Override public void run() { @@ -70,7 +67,10 @@ class FtpThreadTask extends AbsFtpThreadTask String.format("任务【%s】线程__%s__开始下载【开始位置 : %s,结束位置:%s】", mConfig.TEMP_FILE.getName(), mConfig.THREAD_ID, mConfig.START_LOCATION, mConfig.END_LOCATION)); client = createClient(); - if (client == null) return; + if (client == null) { + fail(mChildCurrentLocation, "ftp client 创建失败", null); + return; + } if (mConfig.START_LOCATION > 0) { client.setRestartOffset(mConfig.START_LOCATION); } @@ -168,8 +168,8 @@ class FtpThreadTask extends AbsFtpThreadTask if (isBreak()) { break; } - if (mSleepTime > 0) { - Thread.sleep(mSleepTime); + if (mSpeedBandUtil != null) { + mSpeedBandUtil.limitNextBytes(len); } if (mChildCurrentLocation + len >= mConfig.END_LOCATION) { len = (int) (mConfig.END_LOCATION - mChildCurrentLocation); @@ -186,8 +186,6 @@ class FtpThreadTask extends AbsFtpThreadTask } } handleComplete(); - } catch (InterruptedException e) { - e.printStackTrace(); } catch (IOException e) { fail(mChildCurrentLocation, String.format("下载失败【%s】", mConfig.URL), e); } finally { @@ -221,7 +219,9 @@ class FtpThreadTask extends AbsFtpThreadTask if (isBreak()) { break; } - if (mSleepTime > 0) Thread.sleep(mSleepTime); + if (mSpeedBandUtil != null) { + mSpeedBandUtil.limitNextBytes(len); + } if (mChildCurrentLocation + len >= mConfig.END_LOCATION) { len = (int) (mConfig.END_LOCATION - mChildCurrentLocation); file.write(buffer, 0, len); @@ -234,8 +234,6 @@ class FtpThreadTask extends AbsFtpThreadTask } } catch (IOException e) { fail(mChildCurrentLocation, String.format("下载失败【%s】", mConfig.URL), e); - } catch (InterruptedException e) { - e.printStackTrace(); } finally { try { if (file != null) { @@ -246,4 +244,8 @@ class FtpThreadTask extends AbsFtpThreadTask } } } + + @Override public int getMaxSpeed() { + return mAridManager.getDownloadConfig().getMaxSpeed(); + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java index 1424fd6f..9e75a3f4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java @@ -15,7 +15,6 @@ */ package com.arialyy.aria.core.download.downloader; -import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.common.AbsThreadTask; import com.arialyy.aria.core.common.StateConstance; import com.arialyy.aria.core.common.SubThreadConfig; @@ -49,14 +48,12 @@ final class HttpThreadTask extends AbsThreadTask downloadInfo) { super(constance, listener, downloadInfo); - AriaManager manager = AriaManager.getInstance(AriaManager.APP); - mConnectTimeOut = manager.getDownloadConfig().getConnectTimeOut(); - mReadTimeOut = manager.getDownloadConfig().getIOTimeOut(); - mBufSize = manager.getDownloadConfig().getBuffSize(); - isNotNetRetry = manager.getDownloadConfig().isNotNetRetry(); + mConnectTimeOut = mAridManager.getDownloadConfig().getConnectTimeOut(); + mReadTimeOut = mAridManager.getDownloadConfig().getIOTimeOut(); + mBufSize = mAridManager.getDownloadConfig().getBuffSize(); + isNotNetRetry = mAridManager.getDownloadConfig().isNotNetRetry(); isOpenDynamicFile = STATE.TASK_RECORD.isOpenDynamicFile; isBlock = STATE.TASK_RECORD.isBlock; - setMaxSpeed(manager.getDownloadConfig().getMaxSpeed()); } @Override public void run() { @@ -140,8 +137,8 @@ final class HttpThreadTask extends AbsThreadTask 0) { - Thread.sleep(mSleepTime); + if (mSpeedBandUtil != null) { + mSpeedBandUtil.limitNextBytes(len); } if (mChildCurrentLocation + len >= mConfig.END_LOCATION) { len = (int) (mConfig.END_LOCATION - mChildCurrentLocation); @@ -158,8 +155,6 @@ final class HttpThreadTask extends AbsThreadTask 0) { - Thread.sleep(mSleepTime); + if (mSpeedBandUtil != null) { + mSpeedBandUtil.limitNextBytes(len); } file.write(buffer, 0, len); progress(len); @@ -251,4 +246,8 @@ final class HttpThreadTask extends AbsThreadTask extends AbsTask { - protected AbsGroupUtil mUtil; - @Override public String getKey() { return mTaskEntity.getEntity().getKey(); } @@ -37,7 +35,7 @@ public abstract class AbsGroupTask */ public void startSubTask(String url) { if (mUtil != null) { - mUtil.startSubTask(url); + ((AbsGroupUtil) mUtil).startSubTask(url); } } @@ -48,7 +46,7 @@ public abstract class AbsGroupTask */ public void stopSubTask(String url) { if (mUtil != null) { - mUtil.stopSubTask(url); + ((AbsGroupUtil) mUtil).stopSubTask(url); } } @@ -59,7 +57,7 @@ public abstract class AbsGroupTask */ public void cancelSubTask(String url) { if (mUtil != null) { - mUtil.cancelSubTask(url); + ((AbsGroupUtil) mUtil).cancelSubTask(url); } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java index bb5a1ba6..c8eeec44 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java @@ -17,6 +17,7 @@ package com.arialyy.aria.core.inf; import android.content.Context; import android.os.Handler; +import com.arialyy.aria.core.common.IUtil; import com.arialyy.aria.util.CommonUtil; /** @@ -38,11 +39,23 @@ public abstract class AbsTask implements ITas protected Context mContext; protected boolean isHeighestTask = false; private boolean isCancel = false, isStop = false; + protected IUtil mUtil; public Handler getOutHandler() { return mOutHandler; } + /** + * 设置最大下载/上传速度 + * + * @param speed 单位为:kb + */ + public void setMaxSpeed(int speed) { + if (mUtil != null) { + mUtil.setMaxSpeed(speed); + } + } + /** * 任务是否完成 * diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IFtpTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IFtpTarget.java index 129c70d3..2fc81a17 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/IFtpTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IFtpTarget.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.inf; import android.support.annotation.CheckResult; +import java.net.Proxy; /** * Created by laoyuyu on 2018/3/9. @@ -45,4 +46,12 @@ public interface IFtpTarget { */ @CheckResult TARGET login(String userName, String password, String account); + + /** + * 设置代理 + * + * @param proxy {@link Proxy} + */ + @CheckResult + TARGET setProxy(Proxy proxy); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java index 5a171948..7bf73715 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java @@ -22,6 +22,8 @@ import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.queue.pool.BaseCachePool; import com.arialyy.aria.core.queue.pool.BaseExecutePool; import com.arialyy.aria.util.ALog; +import java.util.Map; +import java.util.Set; /** * Created by lyy on 2017/2/23. @@ -80,6 +82,18 @@ abstract class AbsTaskQueue tasks = mExecutePool.getAllTask(); + Set keys = tasks.keySet(); + for (String key : keys) { + TASK task = tasks.get(key); + task.setMaxSpeed(maxSpeed); + } + } + /** * 获取配置文件配置的最大可执行任务数 */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java index 8245d30a..60b9561e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java @@ -104,18 +104,6 @@ public class DownloadTaskQueue extends AbsTaskQueue tasks = mExecutePool.getAllTask(); - Set keys = tasks.keySet(); - for (String key : keys) { - DownloadTask task = tasks.get(key); - task.setMaxSpeed(maxSpeed); - } - } - @Override public DownloadTask createTask(String target, DownloadTaskEntity entity) { DownloadTask task = null; if (!TextUtils.isEmpty(target)) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java index 06793fd7..fe60aae0 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java @@ -17,11 +17,14 @@ package com.arialyy.aria.core.upload; import android.support.annotation.CheckResult; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.command.normal.NormalCmdFactory; +import com.arialyy.aria.core.common.ftp.FTPSConfig; import com.arialyy.aria.core.common.ftp.FtpDelegate; import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.IFtpTarget; import com.arialyy.aria.util.CommonUtil; +import java.net.Proxy; /** * Created by Aria.Lao on 2017/7/27. @@ -62,7 +65,16 @@ public class FtpUploadTarget extends BaseNormalTarget if (!b) { return false; } - mTaskEntity.setUrlEntity(CommonUtil.getFtpUrlInfo(mTempUrl)); + FtpUrlEntity temp = mTaskEntity.getUrlEntity(); + FtpUrlEntity newEntity = CommonUtil.getFtpUrlInfo(mTempUrl); + if (temp != null) { //处理FTPS的信息 + newEntity.isFtps = temp.isFtps; + newEntity.storePass = temp.storePass; + newEntity.keyAlias = temp.keyAlias; + newEntity.protocol = temp.protocol; + newEntity.storePath = temp.storePath; + } + mTaskEntity.setUrlEntity(newEntity); mTaskEntity.getUrlEntity().account = mAccount; mTaskEntity.getUrlEntity().user = mUser; mTaskEntity.getUrlEntity().password = mPw; @@ -70,6 +82,21 @@ public class FtpUploadTarget extends BaseNormalTarget return true; } + /** + * 是否是FTPS协议 + * 如果是FTPS协议,需要使用{@link FTPSConfig#setStorePath(String)} 、{@link FTPSConfig#setAlias(String)} + * 设置证书信息 + */ + @CheckResult + public FTPSConfig asFtps() { + if (mTaskEntity.getUrlEntity() == null) { + FtpUrlEntity urlEntity = new FtpUrlEntity(); + urlEntity.isFtps = true; + mTaskEntity.setUrlEntity(urlEntity); + } + return new FTPSConfig<>(this); + } + @CheckResult @Override public FtpUploadTarget charSet(String charSet) { return mDelegate.charSet(charSet); @@ -89,4 +116,8 @@ public class FtpUploadTarget extends BaseNormalTarget mAccount = account; return this; } + + @Override public FtpUploadTarget setProxy(Proxy proxy) { + return mDelegate.setProxy(proxy); + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java index 6f25ce8c..dd42bb1b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java @@ -40,6 +40,16 @@ import java.util.Set; public class UploadReceiver extends AbsReceiver { private static final String TAG = "UploadReceiver"; + /** + * 设置最大上传速度,单位:kb + * + * @param maxSpeed 为0表示不限速 + */ + public UploadReceiver setMaxSpeed(int maxSpeed) { + AriaManager.getInstance(AriaManager.APP).getUploadConfig().setMaxSpeed(maxSpeed); + return this; + } + /** * 加载HTTP单文件上传任务 * diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java index 511d6f7d..bfb59627 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpThreadTask.java @@ -15,10 +15,9 @@ */ package com.arialyy.aria.core.upload.uploader; -import com.arialyy.aria.core.AriaManager; -import com.arialyy.aria.core.common.ftp.AbsFtpThreadTask; import com.arialyy.aria.core.common.StateConstance; import com.arialyy.aria.core.common.SubThreadConfig; +import com.arialyy.aria.core.common.ftp.AbsFtpThreadTask; import com.arialyy.aria.core.inf.IEventListener; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadTaskEntity; @@ -41,11 +40,14 @@ class FtpThreadTask extends AbsFtpThreadTask { FtpThreadTask(StateConstance constance, IEventListener listener, SubThreadConfig info) { super(constance, listener, info); - AriaManager manager = AriaManager.getInstance(AriaManager.APP); - mConnectTimeOut = manager.getUploadConfig().getConnectTimeOut(); - mReadTimeOut = manager.getUploadConfig().getIOTimeOut(); - mBufSize = manager.getUploadConfig().getBuffSize(); - isNotNetRetry = manager.getUploadConfig().isNotNetRetry(); + mConnectTimeOut = mAridManager.getUploadConfig().getConnectTimeOut(); + mReadTimeOut = mAridManager.getUploadConfig().getIOTimeOut(); + mBufSize = mAridManager.getUploadConfig().getBuffSize(); + isNotNetRetry = mAridManager.getUploadConfig().isNotNetRetry(); + } + + @Override public int getMaxSpeed() { + return mAridManager.getUploadConfig().getMaxSpeed(); } @Override public void run() { @@ -128,15 +130,18 @@ class FtpThreadTask extends AbsFtpThreadTask { @Override public void onFtpInputStream(FTPClient client, long totalBytesTransferred, int bytesTransferred, long streamSize) { - if (isBreak() && !isStoped) { - try { + try { + if (isBreak() && !isStoped) { isStoped = true; client.abor(); - } catch (IOException e) { - e.printStackTrace(); } + if (mSpeedBandUtil != null) { + mSpeedBandUtil.limitNextBytes(bytesTransferred); + } + progress(bytesTransferred); + } catch (IOException e) { + e.printStackTrace(); } - progress(bytesTransferred); } }); } catch (IOException e) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java index ac14c2a7..822ba74e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/HttpThreadTask.java @@ -15,7 +15,6 @@ */ package com.arialyy.aria.core.upload.uploader; -import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.common.AbsThreadTask; import com.arialyy.aria.core.common.StateConstance; import com.arialyy.aria.core.common.SubThreadConfig; @@ -52,11 +51,10 @@ class HttpThreadTask extends AbsThreadTask { HttpThreadTask(StateConstance constance, IUploadListener listener, SubThreadConfig uploadInfo) { super(constance, listener, uploadInfo); - AriaManager manager = AriaManager.getInstance(AriaManager.APP); - mConnectTimeOut = manager.getUploadConfig().getConnectTimeOut(); - mReadTimeOut = manager.getUploadConfig().getIOTimeOut(); - mBufSize = manager.getUploadConfig().getBuffSize(); - isNotNetRetry = manager.getUploadConfig().isNotNetRetry(); + mConnectTimeOut = mAridManager.getUploadConfig().getConnectTimeOut(); + mReadTimeOut = mAridManager.getUploadConfig().getIOTimeOut(); + mBufSize = mAridManager.getUploadConfig().getBuffSize(); + isNotNetRetry = mAridManager.getUploadConfig().isNotNetRetry(); } @Override public void run() { @@ -167,6 +165,9 @@ class HttpThreadTask extends AbsThreadTask { if (STATE.isCancel) { break; } + if (mSpeedBandUtil != null) { + mSpeedBandUtil.limitNextBytes(bytesRead); + } } mOutputStream.flush(); @@ -212,4 +213,8 @@ class HttpThreadTask extends AbsThreadTask { mOutputStream.close(); return response.toString(); } + + @Override public int getMaxSpeed() { + return mAridManager.getUploadConfig().getMaxSpeed(); + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java index 9f4d1c1c..591876c3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java @@ -99,7 +99,7 @@ public class SimpleUploadUtil implements IUtil, Runnable { mUploader.cancel(); } - @Override public void setMaxSpeed(double maxSpeed) { - mUploader.setMaxSpeed(maxSpeed); + @Override public void setMaxSpeed(int speed) { + mUploader.setMaxSpeed(speed); } } diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java index 1763f596..dc89d6b6 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -289,6 +289,7 @@ public class CommonUtil { entity.user = userInfo; } } + entity.scheme = uri.getScheme(); entity.remotePath = TextUtils.isEmpty(remotePath) ? "/" : remotePath; return entity; } @@ -395,6 +396,23 @@ public class CommonUtil { return md5; } + /** + * 获取字符串的md5 + * + * @return 字符串为空或获取md5失败,则返回"" + */ + public static String getStrMd5(String str) { + if (TextUtils.isEmpty(str)) return ""; + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update(str.getBytes()); + return new BigInteger(1, md.digest()).toString(16); + } catch (NoSuchAlgorithmException e) { + ALog.e(TAG, e.getMessage()); + } + return ""; + } + /** * 删除任务组记录 * diff --git a/Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java b/Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java index 9a2e465c..0e5f7b48 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java @@ -17,6 +17,7 @@ package com.arialyy.aria.util; import android.text.TextUtils; import com.arialyy.aria.core.AriaManager; +import com.arialyy.aria.core.common.ProtocolType; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -30,6 +31,8 @@ import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; +import java.util.Map; +import java.util.WeakHashMap; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; @@ -44,18 +47,26 @@ import javax.net.ssl.X509TrustManager; */ public class SSLContextUtil { private static final String TAG = "SSLContextUtil"; + private static Map SSL_CACHE = new WeakHashMap<>(); /** * 从assets目录下加载证书 * * @param caAlias CA证书别名 * @param caPath 保存在assets目录下的CA证书完整路径 + * @param protocol 连接协议 */ - public static SSLContext getSSLContextFromAssets(String caAlias, String caPath) { + public static SSLContext getSSLContextFromAssets(String caAlias, String caPath, + @ProtocolType String protocol) { try { + String cacheKey = getCacheKey(caAlias, caPath); + SSLContext sslContext = SSL_CACHE.get(cacheKey); + if (sslContext != null) { + return sslContext; + } InputStream caInput = AriaManager.APP.getAssets().open(caPath); Certificate ca = loadCert(caInput); - return createContext(caAlias, ca); + return createContext(caAlias, ca, protocol, cacheKey); } catch (IOException | CertificateException e) { e.printStackTrace(); } @@ -67,21 +78,32 @@ public class SSLContextUtil { * * @param caAlias CA证书别名 * @param caPath CA证书路径 + * @param protocol 连接协议 */ - public static SSLContext getSSLContext(String caAlias, String caPath) { + public static SSLContext getSSLContext(String caAlias, String caPath, + @ProtocolType String protocol) { if (TextUtils.isEmpty(caAlias) || TextUtils.isEmpty(caPath)) { return null; } try { + String cacheKey = getCacheKey(caAlias, caPath); + SSLContext sslContext = SSL_CACHE.get(cacheKey); + if (sslContext != null) { + return sslContext; + } Certificate ca = loadCert(new FileInputStream(caPath)); - return createContext(caAlias, ca); + return createContext(caAlias, ca, protocol, cacheKey); } catch (CertificateException | IOException e) { e.printStackTrace(); } return null; } - private static SSLContext createContext(String caAlias, Certificate ca) { + /** + * @param cacheKey 别名 + 证书路径,然后取md5 + */ + private static SSLContext createContext(String caAlias, Certificate ca, + @ProtocolType String protocol, String cacheKey) { try { String keyStoreType = KeyStore.getDefaultType(); KeyStore keyStore = KeyStore.getInstance(keyStoreType); @@ -97,8 +119,10 @@ public class SSLContextUtil { kmf.init(keyStore, null); // Create an SSLContext that uses our TrustManager - SSLContext context = SSLContext.getInstance("TLS"); + SSLContext context = + SSLContext.getInstance(TextUtils.isEmpty(protocol) ? ProtocolType.Default : protocol); context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom()); + SSL_CACHE.put(cacheKey, context); return context; } catch (CertificateException | NoSuchAlgorithmException | IOException | KeyStoreException | KeyManagementException | UnrecoverableKeyException e) { e.printStackTrace(); @@ -106,6 +130,10 @@ public class SSLContextUtil { return null; } + private static String getCacheKey(String alias, String path) { + return CommonUtil.getStrMd5(String.format("%s_%s", alias, path)); + } + /** * 加载CA证书 * @@ -123,10 +151,11 @@ public class SSLContextUtil { /** * 服务器证书不是由 CA 签署的,而是自签署时,获取默认的SSL */ - public static SSLContext getDefaultSLLContext() { + public static SSLContext getDefaultSLLContext(@ProtocolType String protocol) { SSLContext sslContext = null; try { - sslContext = SSLContext.getInstance("TLS"); + sslContext = + SSLContext.getInstance(TextUtils.isEmpty(protocol) ? ProtocolType.Default : protocol); sslContext.init(null, new TrustManager[] { trustManagers }, new SecureRandom()); } catch (Exception e) { e.printStackTrace(); diff --git a/DEV_LOG.md b/DEV_LOG.md index bf17d87b..2f0f0626 100644 --- a/DEV_LOG.md +++ b/DEV_LOG.md @@ -3,6 +3,7 @@ - 组合任务新增`updateUrls(List)`用于修改组合子任务的url,[see](https://aria.laoyuyu.me/aria_doc/api/task_handle.html#%E6%9B%B4%E6%96%B0%E4%BB%BB%E5%8A%A1url) - 出于安全考虑,FTP数据库去掉密码的保存 - 增加FTPS支持 [see]() + - 增加速度限制支持[see]() + v_3.4.7 - 修复分块任务异常操作导致的问题 + v_3.4.6 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a79260ea..36686969 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,13 +13,13 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme.NoActionBar"> - - - + + + @@ -29,7 +29,7 @@ - + diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index 01b6db9a..aacfdc1b 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -11,6 +11,10 @@ + + + + + diff --git a/app/src/main/java/com/arialyy/simple/test/TestActivity.java b/app/src/main/java/com/arialyy/simple/test/TestFTPActivity.java similarity index 84% rename from app/src/main/java/com/arialyy/simple/test/TestActivity.java rename to app/src/main/java/com/arialyy/simple/test/TestFTPActivity.java index 4c680de5..7be6bf1b 100644 --- a/app/src/main/java/com/arialyy/simple/test/TestActivity.java +++ b/app/src/main/java/com/arialyy/simple/test/TestFTPActivity.java @@ -19,12 +19,12 @@ import java.io.File; * Created by Administrator on 2018/4/12. */ -public class TestActivity extends BaseActivity { - String TAG = "TestActivity"; +public class TestFTPActivity extends BaseActivity { + String TAG = "TestFTPActivity"; //String URL = "http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 //private final String URL = "ftp://192.168.1.3:21/download//AriaPrj.rar"; - private final String FILE_PATH = "/mnt/sdcard/AriaPrj.rar"; - private final String URL = "ftp://192.168.1.2:21/aa//你好"; + private final String FILE_PATH = "/mnt/sdcard/mmm.mp4"; + private final String URL = "ftps://192.168.29.140:990/aa/你好"; @Override protected int setLayoutId() { @@ -35,7 +35,7 @@ public class TestActivity extends BaseActivity { super.init(savedInstanceState); mBar.setVisibility(View.GONE); Aria.upload(this).register(); - + Aria.upload(this).setMaxSpeed(128); } @Upload.onWait void onWait(UploadTask task) { @@ -51,7 +51,7 @@ public class TestActivity extends BaseActivity { } @Upload.onTaskRunning protected void running(UploadTask task) { - Log.d(TAG, "running"); + Log.d(TAG, "running,speed=" + task.getConvertSpeed()); } @Upload.onTaskResume void taskResume(UploadTask task) { @@ -82,6 +82,10 @@ public class TestActivity extends BaseActivity { .login("lao", "123456") .setUploadUrl(URL) .setExtendField("韩寒哈大双") + .asFtps() + .setStorePath("/mnt/sdcard/Download/server.crt") + .setAlias("www.laoyuyu.me") + .setStorePass("123456") .start(); //Uri uri = Uri.parse("ftp://z:z@dygod18.com:21211/[电影天堂www.dy2018.com]猩球崛起3:终极之战BD国英双语中英双字.mkv"); //ALog.d(TAG, "sh = " + uri.getScheme() + ", user = " + uri.getUserInfo() + ", host = " + uri.getHost() + ", port = " + uri.getPort() + " remotePath = " + uri.getPath()); diff --git a/app/src/main/java/com/arialyy/simple/test/TestGroupActivity.java b/app/src/main/java/com/arialyy/simple/test/TestFTPDirActivity.java similarity index 90% rename from app/src/main/java/com/arialyy/simple/test/TestGroupActivity.java rename to app/src/main/java/com/arialyy/simple/test/TestFTPDirActivity.java index d8856485..74831170 100644 --- a/app/src/main/java/com/arialyy/simple/test/TestGroupActivity.java +++ b/app/src/main/java/com/arialyy/simple/test/TestFTPDirActivity.java @@ -17,9 +17,9 @@ import java.util.List; * Created by Administrator on 2018/4/12. */ -public class TestGroupActivity extends BaseActivity { +public class TestFTPDirActivity extends BaseActivity { List mUrls; - private static final String dir = "ftp://192.168.1.8:21/upload/测试"; + private static final String dir = "ftps://192.168.29.140:990/upload/测试"; @Override protected int setLayoutId() { return R.layout.activity_test; @@ -87,6 +87,10 @@ public class TestGroupActivity extends BaseActivity { .setGroupAlias("ftp文件夹下载") //.setSubTaskFileName(getModule(GroupModule.class).getSubName()) .login("lao", "123456") + .asFtps() + .setStorePath("/mnt/sdcard/Download/server.crt") + .setAlias("www.laoyuyu.me") + .setStorePass("123456") .start(); break; case R.id.stop: diff --git a/aria/src/main/java/com/arialyy/aria/core/Configuration.java b/aria/src/main/java/com/arialyy/aria/core/Configuration.java index 99ffc7a3..09cc56c8 100644 --- a/aria/src/main/java/com/arialyy/aria/core/Configuration.java +++ b/aria/src/main/java/com/arialyy/aria/core/Configuration.java @@ -17,6 +17,7 @@ package com.arialyy.aria.core; import android.text.TextUtils; import com.arialyy.aria.core.common.QueueMod; +import com.arialyy.aria.core.queue.DownloadGroupTaskQueue; import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.queue.UploadTaskQueue; import com.arialyy.aria.util.ALog; @@ -247,6 +248,21 @@ class Configuration { */ int iOTimeOut = 20 * 1000; + /** + * 设置最大下载/上传速度,单位:kb, 为0表示不限速 + */ + int maxSpeed = 0; + + public int getMaxSpeed() { + return maxSpeed; + } + + public BaseTaskConfig setMaxSpeed(int maxSpeed) { + this.maxSpeed = maxSpeed; + saveKey("maxSpeed", String.valueOf(maxSpeed)); + return this; + } + public long getUpdateInterval() { return updateInterval; } @@ -374,11 +390,6 @@ class Configuration { */ int threadNum = 3; - /** - * 设置最大下载速度,单位:kb, 为0表示不限速 - */ - int maxSpeed = 0; - /** * 多线程下载是否使用块下载模式,{@code true}使用,{@code false}不使用 * 注意: @@ -393,6 +404,13 @@ class Configuration { return useBlock; } + @Override public DownloadConfig setMaxSpeed(int maxSpeed) { + super.setMaxSpeed(maxSpeed); + DownloadTaskQueue.getInstance().setMaxSpeed(maxSpeed); + DownloadGroupTaskQueue.getInstance().setMaxSpeed(maxSpeed); + return this; + } + public DownloadConfig setUseBlock(boolean useBlock) { this.useBlock = useBlock; saveKey("useBlock", String.valueOf(useBlock)); @@ -407,17 +425,6 @@ class Configuration { return this; } - public int getMaxSpeed() { - return maxSpeed; - } - - public DownloadConfig setMaxSpeed(int maxSpeed) { - this.maxSpeed = maxSpeed; - saveKey("maxSpeed", String.valueOf(maxSpeed)); - DownloadTaskQueue.getInstance().setMaxSpeed(maxSpeed); - return this; - } - public DownloadConfig setThreadNum(int threadNum) { this.threadNum = threadNum; saveKey("threadNum", String.valueOf(threadNum)); @@ -478,6 +485,12 @@ class Configuration { loadConfig(); } + @Override public UploadConfig setMaxSpeed(int maxSpeed) { + super.setMaxSpeed(maxSpeed); + UploadTaskQueue.getInstance().setMaxSpeed(maxSpeed); + return this; + } + public UploadConfig setMaxTaskNum(int maxTaskNum) { oldMaxTaskNum = this.maxTaskNum; this.maxTaskNum = maxTaskNum; diff --git a/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java b/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java index f2c91750..af00d525 100644 --- a/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java +++ b/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java @@ -29,7 +29,6 @@ import com.arialyy.aria.util.ALog; public class UploadTask extends AbsNormalTask { private static final String TAG = "UploadTask"; - private SimpleUploadUtil mUtil; private BaseUListener mListener; private UploadTask(UploadTaskEntity taskEntity, Handler outHandler) {