Fix download validation, TLS security and task coordination
This commit is contained in:
1 parent
e9d9281333
commit
94e19b4fe4
11 files changed
+163
-80
No files matched your search
@@ -18,7 +18,6 @@ package com.arialyy.aria.core.event;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -27,18 +26,16 @@ import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 消息发送工具
|
||||
*/
|
||||
public class EventMsgUtil {
|
||||
private static final String TAG = "EventUtil";
|
||||
private static EventMsgUtil defaultInstance;
|
||||
private static volatile EventMsgUtil defaultInstance;
|
||||
private Map<Object, List<EventMethodInfo>> mEventMethods =
|
||||
new ConcurrentHashMap<>();
|
||||
private ArrayBlockingQueue<Object> mEventQueue = new ArrayBlockingQueue<>(10);
|
||||
private ExecutorService mPool = Executors.newFixedThreadPool(5);
|
||||
|
||||
private EventMsgUtil() {
|
||||
ExecutorService pool = Executors.newSingleThreadExecutor();
|
||||
@@ -48,8 +45,11 @@ public class EventMsgUtil {
|
||||
try {
|
||||
Object info = mEventQueue.take();
|
||||
sendEvent(info);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ALog.e(TAG, "事件处理失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,8 +57,6 @@ public class EventMsgUtil {
|
||||
}
|
||||
|
||||
private void sendEvent(final Object param) {
|
||||
mPool.submit(new Runnable() {
|
||||
@Override public void run() {
|
||||
Set<Object> keys = mEventMethods.keySet();
|
||||
for (Object key : keys) {
|
||||
List<EventMethodInfo> list = mEventMethods.get(key);
|
||||
@@ -76,8 +74,6 @@ public class EventMsgUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static EventMsgUtil getDefault() {
|
||||
@@ -119,7 +115,7 @@ public class EventMsgUtil {
|
||||
methodInfo.param = clazz[0];
|
||||
List<EventMethodInfo> list = mEventMethods.get(obj);
|
||||
if (list == null) {
|
||||
list = new ArrayList<>();
|
||||
list = new java.util.concurrent.CopyOnWriteArrayList<>();
|
||||
mEventMethods.put(obj, list);
|
||||
}
|
||||
list.add(methodInfo);
|
||||
@@ -142,12 +138,11 @@ public class EventMsgUtil {
|
||||
*/
|
||||
public void post(Object param) {
|
||||
|
||||
synchronized (EventMsgUtil.class) {
|
||||
try {
|
||||
mEventQueue.offer(param, 2, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (param == null) {
|
||||
throw new IllegalArgumentException("事件不能为空");
|
||||
}
|
||||
if (!mEventQueue.offer(param)) {
|
||||
throw new java.util.concurrent.RejectedExecutionException("事件队列已满,请降低发送频率");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -608,16 +608,18 @@ class DelegateFind extends AbsDelegate {
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("SELECT rowid FROM ").append(CommonUtil.getClassName(clazz)).append(" WHERE ");
|
||||
int i = 0;
|
||||
for (Object where : wheres) {
|
||||
sb.append(where).append("=").append("'").append(values[i]).append("'");
|
||||
sb.append(i >= wheres.length - 1 ? "" : ",");
|
||||
i++;
|
||||
String[] args = new String[values.length];
|
||||
for (int i = 0; i < wheres.length; i++) {
|
||||
if (i > 0) sb.append(" AND ");
|
||||
sb.append(wheres[i]).append("=?");
|
||||
args[i] = String.valueOf(values[i]);
|
||||
}
|
||||
Cursor c = db.rawQuery(sb.toString(), args);
|
||||
try {
|
||||
return c.moveToFirst() ? c.getInt(c.getColumnIndexOrThrow("rowid")) : -1;
|
||||
} finally {
|
||||
c.close();
|
||||
}
|
||||
Cursor c = db.rawQuery(sb.toString(), null);
|
||||
int id = c.getColumnIndex("rowid");
|
||||
c.close();
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -150,42 +150,20 @@ public class SSLContextUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务器证书不是由 CA 签署的,而是自签署时,获取默认的SSL
|
||||
* 使用系统信任链;自签名服务必须显式配置 CA 证书
|
||||
*/
|
||||
public static SSLContext getDefaultSLLContext(String protocol) {
|
||||
SSLContext sslContext = null;
|
||||
try {
|
||||
sslContext =
|
||||
SSLContext.getInstance(TextUtils.isEmpty(protocol) ? ProtocolType.Default : protocol);
|
||||
sslContext.init(null, new TrustManager[] { trustManagers }, new SecureRandom());
|
||||
sslContext.init(null, null, new SecureRandom());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return sslContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建自己的 TrustManager,这次直接信任服务器证书。这种方法具有前面所述的将应用与证书直接关联的所有弊端,但可以安全地操作。
|
||||
*/
|
||||
private static TrustManager trustManagers = new X509TrustManager() {
|
||||
|
||||
@Override public void checkClientTrusted(X509Certificate[] chain, String authType)
|
||||
throws CertificateException {
|
||||
}
|
||||
|
||||
@Override public void checkServerTrusted(X509Certificate[] chain, String authType)
|
||||
throws CertificateException {
|
||||
|
||||
}
|
||||
|
||||
@Override public X509Certificate[] getAcceptedIssuers() {
|
||||
return new X509Certificate[0];
|
||||
}
|
||||
};
|
||||
|
||||
public static final HostnameVerifier HOSTNAME_VERIFIER = new HostnameVerifier() {
|
||||
public boolean verify(String hostname, SSLSession session) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
public static final HostnameVerifier HOSTNAME_VERIFIER =
|
||||
javax.net.ssl.HttpsURLConnection.getDefaultHostnameVerifier();
|
||||
}
|
||||
@@ -162,7 +162,7 @@ public class UriFileUtil {
|
||||
String scheme = uri.getScheme();
|
||||
String authority = uri.getAuthority();
|
||||
if (!"content".equalsIgnoreCase(scheme) || authority == null
|
||||
|| !authority.startsWith("media")) {
|
||||
|| !"media".equals(authority)) {
|
||||
return null;
|
||||
}
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
||||
@@ -183,12 +183,6 @@ public class UriFileUtil {
|
||||
// 3) 旧版 Uri 任务没有持久化原始路径(filePath 为空):按文件名在 Download 目录兜底查找/重建,
|
||||
// 让历史死记录不再无限重试失败
|
||||
if (!TextUtils.isEmpty(displayName)) {
|
||||
Uri exist = queryBySelection(context, MediaStore.Downloads.EXTERNAL_CONTENT_URI,
|
||||
MediaStore.MediaColumns.DISPLAY_NAME + "=?",
|
||||
new String[]{displayName});
|
||||
if (exist != null && canWrite(context, exist)) {
|
||||
return exist;
|
||||
}
|
||||
Uri created = createDownloadUri(context, displayName);
|
||||
if (created != null && canWrite(context, created)) {
|
||||
return created;
|
||||
@@ -249,10 +243,7 @@ public class UriFileUtil {
|
||||
new String[]{displayName, relativePath});
|
||||
if (uri != null) return uri;
|
||||
|
||||
// 3) 兜底仅按 DISPLAY_NAME 匹配(目录不再精确匹配)
|
||||
return queryBySelection(context, contentUri,
|
||||
MediaStore.MediaColumns.DISPLAY_NAME + "=?",
|
||||
new String[]{displayName});
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Uri queryBySelection(Context context, Uri contentUri, String selection,
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.arialyy.aria.core.event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class EventMsgUtilTest {
|
||||
static class Receiver {
|
||||
final List<Integer> values = new ArrayList<>();
|
||||
final CountDownLatch done = new CountDownLatch(8);
|
||||
@Event public void receive(Integer value) {
|
||||
values.add(value);
|
||||
done.countDown();
|
||||
}
|
||||
}
|
||||
@Test public void deliversEventsInPostingOrder() throws Exception {
|
||||
Receiver receiver = new Receiver();
|
||||
EventMsgUtil bus = EventMsgUtil.getDefault();
|
||||
bus.register(receiver);
|
||||
try {
|
||||
for (int i = 0; i < 8; i++) bus.post(i);
|
||||
assertTrue(receiver.done.await(5, TimeUnit.SECONDS));
|
||||
for (int i = 0; i < 8; i++) assertEquals(Integer.valueOf(i), receiver.values.get(i));
|
||||
} finally {
|
||||
bus.unRegister(receiver);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user