|
|
|
|
@ -3,10 +3,15 @@ package com.github.catvod.crawler; |
|
|
|
|
import android.content.Context; |
|
|
|
|
|
|
|
|
|
import com.github.tvbox.osc.base.App; |
|
|
|
|
import com.github.tvbox.osc.util.MD5; |
|
|
|
|
import com.lzy.okgo.OkGo; |
|
|
|
|
|
|
|
|
|
import org.json.JSONObject; |
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.FileOutputStream; |
|
|
|
|
import java.io.InputStream; |
|
|
|
|
import java.io.OutputStream; |
|
|
|
|
import java.lang.reflect.Method; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.LinkedHashMap; |
|
|
|
|
@ -14,10 +19,14 @@ import java.util.Map; |
|
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
|
|
|
|
|
|
import dalvik.system.DexClassLoader; |
|
|
|
|
import okhttp3.Response; |
|
|
|
|
|
|
|
|
|
public class JarLoader { |
|
|
|
|
private DexClassLoader classLoader = null; |
|
|
|
|
private ConcurrentHashMap<String, DexClassLoader> classLoaders = new ConcurrentHashMap<>(); |
|
|
|
|
private ConcurrentHashMap<String, Spider> spiders = new ConcurrentHashMap<>(); |
|
|
|
|
/** |
|
|
|
|
* always from main jar. |
|
|
|
|
*/ |
|
|
|
|
private Method proxyFun = null; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -28,12 +37,17 @@ public class JarLoader { |
|
|
|
|
public boolean load(String cache) { |
|
|
|
|
spiders.clear(); |
|
|
|
|
proxyFun = null; |
|
|
|
|
classLoaders.clear(); |
|
|
|
|
return loadClassLoader(cache, "main"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean loadClassLoader(String jar, String key) { |
|
|
|
|
boolean success = false; |
|
|
|
|
try { |
|
|
|
|
File cacheDir = new File(App.getInstance().getCacheDir().getAbsolutePath() + "/catvod_csp"); |
|
|
|
|
if (!cacheDir.exists()) |
|
|
|
|
cacheDir.mkdirs(); |
|
|
|
|
classLoader = new DexClassLoader(cache, cacheDir.getAbsolutePath(), null, App.getInstance().getClassLoader()); |
|
|
|
|
DexClassLoader classLoader = new DexClassLoader(jar, cacheDir.getAbsolutePath(), null, App.getInstance().getClassLoader()); |
|
|
|
|
// make force wait here, some device async dex load
|
|
|
|
|
int count = 0; |
|
|
|
|
do { |
|
|
|
|
@ -59,16 +73,66 @@ public class JarLoader { |
|
|
|
|
} |
|
|
|
|
count++; |
|
|
|
|
} while (count < 5); |
|
|
|
|
|
|
|
|
|
if (success) { |
|
|
|
|
classLoaders.put(key, classLoader); |
|
|
|
|
} |
|
|
|
|
} catch (Throwable th) { |
|
|
|
|
th.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
return success; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Spider getSpider(String key, String cls, String ext) { |
|
|
|
|
private DexClassLoader loadJarInternal(String jar) { |
|
|
|
|
String[] urls = jar.split(";md5;"); |
|
|
|
|
String jarUrl = urls[0]; |
|
|
|
|
String urlMd5 = MD5.string2MD5(jarUrl); |
|
|
|
|
if (classLoaders.contains(urlMd5)) |
|
|
|
|
return classLoaders.get(urlMd5); |
|
|
|
|
String md5 = urls.length > 1 ? urls[1].trim() : ""; |
|
|
|
|
File cache = new File(App.getInstance().getFilesDir().getAbsolutePath() + "/" + urlMd5 + ".jar"); |
|
|
|
|
if (!md5.isEmpty()) { |
|
|
|
|
if (cache.exists() && MD5.getFileMd5(cache).equalsIgnoreCase(md5)) { |
|
|
|
|
loadClassLoader(cache.getAbsolutePath(), urlMd5); |
|
|
|
|
return classLoaders.get(urlMd5); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
Response response = OkGo.<File>get(jarUrl).execute(); |
|
|
|
|
InputStream is = response.body().byteStream(); |
|
|
|
|
OutputStream os = new FileOutputStream(cache); |
|
|
|
|
try { |
|
|
|
|
byte[] buffer = new byte[2048]; |
|
|
|
|
int length; |
|
|
|
|
while ((length = is.read(buffer)) > 0) { |
|
|
|
|
os.write(buffer, 0, length); |
|
|
|
|
} |
|
|
|
|
} finally { |
|
|
|
|
try { |
|
|
|
|
is.close(); |
|
|
|
|
os.close(); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
loadClassLoader(cache.getAbsolutePath(), urlMd5); |
|
|
|
|
return classLoaders.get(urlMd5); |
|
|
|
|
} catch (Throwable e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Spider getSpider(String key, String cls, String ext, String jar) { |
|
|
|
|
String clsKey = cls.replace("csp_", ""); |
|
|
|
|
if (spiders.containsKey(key)) |
|
|
|
|
return spiders.get(key); |
|
|
|
|
DexClassLoader classLoader = null; |
|
|
|
|
if (jar.isEmpty()) |
|
|
|
|
classLoader = classLoaders.get("main"); |
|
|
|
|
else { |
|
|
|
|
classLoader = loadJarInternal(jar); |
|
|
|
|
} |
|
|
|
|
if (classLoader == null) |
|
|
|
|
return new SpiderNull(); |
|
|
|
|
try { |
|
|
|
|
@ -84,6 +148,7 @@ public class JarLoader { |
|
|
|
|
|
|
|
|
|
public JSONObject jsonExt(String key, LinkedHashMap<String, String> jxs, String url) { |
|
|
|
|
try { |
|
|
|
|
DexClassLoader classLoader = classLoaders.get("main"); |
|
|
|
|
String clsKey = "Json" + key; |
|
|
|
|
String hotClass = "com.github.catvod.parser." + clsKey; |
|
|
|
|
Class jsonParserCls = classLoader.loadClass(hotClass); |
|
|
|
|
@ -97,6 +162,7 @@ public class JarLoader { |
|
|
|
|
|
|
|
|
|
public JSONObject jsonExtMix(String flag, String key, String name, LinkedHashMap<String, HashMap<String, String>> jxs, String url) { |
|
|
|
|
try { |
|
|
|
|
DexClassLoader classLoader = classLoaders.get("main"); |
|
|
|
|
String clsKey = "Mix" + key; |
|
|
|
|
String hotClass = "com.github.catvod.parser." + clsKey; |
|
|
|
|
Class jsonParserCls = classLoader.loadClass(hotClass); |
|
|
|
|
|