完成插件改造.抽取验证接口地址为变量
This commit is contained in:
parent
df0b9a91d7
commit
9e31998f5e
@ -15,6 +15,7 @@
|
|||||||
<js-module src="www/AntiTampering.js" name="CescitIntegrity">
|
<js-module src="www/AntiTampering.js" name="CescitIntegrity">
|
||||||
<clobbers target="cordova.plugins.CescitIntegrity" />
|
<clobbers target="cordova.plugins.CescitIntegrity" />
|
||||||
</js-module>
|
</js-module>
|
||||||
|
<preference name="APK_HASH_URL"/>
|
||||||
|
|
||||||
<!-- <hook type="after_prepare" src="scripts/clear_hashes.js" /> -->
|
<!-- <hook type="after_prepare" src="scripts/clear_hashes.js" /> -->
|
||||||
<!-- <hook type="before_run" src="scripts/clear_hashes.js" /> -->
|
<!-- <hook type="before_run" src="scripts/clear_hashes.js" /> -->
|
||||||
@ -32,6 +33,9 @@
|
|||||||
<param name="onload" value="true" />
|
<param name="onload" value="true" />
|
||||||
</feature>
|
</feature>
|
||||||
</config-file>
|
</config-file>
|
||||||
|
<config-file target="AndroidManifest.xml" parent="/manifest/application">
|
||||||
|
<meta-data android:name="APK_HASH_URL" android:value="$APK_HASH_URL" />
|
||||||
|
</config-file>
|
||||||
|
|
||||||
<source-file src="src/android/com/cescit/integrity/Config.java" target-dir="src/com/cescit/integrity" />
|
<source-file src="src/android/com/cescit/integrity/Config.java" target-dir="src/com/cescit/integrity" />
|
||||||
<source-file src="src/android/com/cescit/integrity/CescitIntegrity.java" target-dir="src/com/cescit/integrity" />
|
<source-file src="src/android/com/cescit/integrity/CescitIntegrity.java" target-dir="src/com/cescit/integrity" />
|
||||||
|
@ -18,7 +18,6 @@ import java.nio.charset.StandardCharsets;
|
|||||||
class ApkIntegrity {
|
class ApkIntegrity {
|
||||||
|
|
||||||
private static final String MESSAGE_DIGEST_ALGORITHM = "SHA-256";
|
private static final String MESSAGE_DIGEST_ALGORITHM = "SHA-256";
|
||||||
private static final String ASSETS_BASE_PATH = "";
|
|
||||||
|
|
||||||
// Collections.unmodifiableMap 使得返回的内容只能只读访问
|
// Collections.unmodifiableMap 使得返回的内容只能只读访问
|
||||||
private static final Map<String, String> hashList = Collections.unmodifiableMap(
|
private static final Map<String, String> hashList = Collections.unmodifiableMap(
|
||||||
@ -28,7 +27,8 @@ class ApkIntegrity {
|
|||||||
public static JSONObject check(Context context) throws Exception {
|
public static JSONObject check(Context context) throws Exception {
|
||||||
JSONObject result = new JSONObject();
|
JSONObject result = new JSONObject();
|
||||||
Map<String, String> nowHashList = getHashMap(context);
|
Map<String, String> nowHashList = getHashMap(context);
|
||||||
String ret = HttpUtil.getHttpRequestData((String) Config.getConfig("APK_HASH_URL"));
|
|
||||||
|
String ret = HttpUtil.getHttpRequestData(Config.getConfig(context,"APK_HASH_URL"),Config.getHeader(context));
|
||||||
JSONObject obj = new JSONObject(ret);
|
JSONObject obj = new JSONObject(ret);
|
||||||
String upHash = obj.getString("apk");
|
String upHash = obj.getString("apk");
|
||||||
String nowHash = nowHashList.get("apk");
|
String nowHash = nowHashList.get("apk");
|
||||||
|
@ -29,7 +29,7 @@ public class CescitIntegrity extends CordovaPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public void run () {
|
public void run () {
|
||||||
try {
|
try {
|
||||||
String ret = HttpUtil.getHttpRequestData((String) Config.getConfig("APK_HASH_URL"));
|
String ret = HttpUtil.getHttpRequestData(Config.getConfig(context,"APK_HASH_URL"),Config.getHeader(context));
|
||||||
JSONObject obj = new JSONObject(ret);
|
JSONObject obj = new JSONObject(ret);
|
||||||
if(obj.getBoolean("ApkIntegrity")) {
|
if(obj.getBoolean("ApkIntegrity")) {
|
||||||
ApkIntegrity.check(context);
|
ApkIntegrity.check(context);
|
||||||
@ -40,8 +40,11 @@ public class CescitIntegrity extends CordovaPlugin {
|
|||||||
if(obj.getBoolean("AssetsIntegrity")) {
|
if(obj.getBoolean("AssetsIntegrity")) {
|
||||||
AssetsIntegrity.check(context);
|
AssetsIntegrity.check(context);
|
||||||
}
|
}
|
||||||
|
if(obj.getBoolean("DebugDetection")) {
|
||||||
|
DebugDetection.check(context.getPackageName());
|
||||||
|
}
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
e.printStackTrace();
|
//e.printStackTrace();
|
||||||
throw new TamperingException("Anti-Tampering check failed");
|
throw new TamperingException("Anti-Tampering check failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,19 +2,23 @@ package com.cescit.integrity;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.ApplicationInfo;
|
||||||
|
|
||||||
class Config {
|
class Config {
|
||||||
private static final String APK_HASH_URL = "http://webf.cewater.com.cn/apk-hash/jm.json";
|
private static final Map<String,String> headers = new HashMap<String,String>();
|
||||||
|
|
||||||
// 获取配置
|
// 获取配置
|
||||||
public static <T> T getConfig(String key) throws Exception{
|
public static String getConfig(Context context,String key) throws Exception{
|
||||||
// res资源(路径:文件hash)键值对
|
ApplicationInfo appInfo = context.getPackageManager()
|
||||||
Map<String, T> config = new HashMap<String,T>();
|
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
|
||||||
config.put("APK_HASH_URL", (T) APK_HASH_URL);
|
return appInfo.metaData.getString(key);
|
||||||
T result = null;
|
}
|
||||||
for(String configKey : config.keySet()){
|
|
||||||
result = config.get(configKey);
|
public static Map<String,String> getHeader(Context context) throws PackageManager.NameNotFoundException {
|
||||||
}
|
PackageManager packageManager = context.getPackageManager();
|
||||||
return (T) result;
|
headers.put("version", packageManager.getPackageInfo(context.getPackageName(), 0).versionName);
|
||||||
|
return headers;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,14 +1,18 @@
|
|||||||
package com.cescit.integrity;
|
package com.cescit.integrity;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class HttpUtil {
|
public class HttpUtil {
|
||||||
|
|
||||||
public static String getHttpRequestData(String urlPath) {
|
public static String getHttpRequestData(String urlPath,Map<String,String>header) {
|
||||||
|
|
||||||
// 首先抓取异常并处理
|
// 首先抓取异常并处理
|
||||||
String returnString = "";
|
String returnString = "";
|
||||||
@ -21,9 +25,13 @@ public class HttpUtil {
|
|||||||
|
|
||||||
// 2 打开用户传递URL参数地址
|
// 2 打开用户传递URL参数地址
|
||||||
HttpURLConnection connect = (HttpURLConnection) url.openConnection();
|
HttpURLConnection connect = (HttpURLConnection) url.openConnection();
|
||||||
|
|
||||||
// 3 设置HTTP请求的一些参数信息
|
// 3 设置HTTP请求的一些参数信息
|
||||||
connect.setRequestMethod("GET"); // 参数必须大写
|
connect.setRequestMethod("GET"); // 参数必须大写
|
||||||
|
if(header!=null && header.isEmpty()){
|
||||||
|
for(String key:header.keySet()){
|
||||||
|
connect.setRequestProperty(key, header.get(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
connect.connect();
|
connect.connect();
|
||||||
|
|
||||||
// 4 获取URL请求到的数据,并创建数据流接收
|
// 4 获取URL请求到的数据,并创建数据流接收
|
||||||
@ -47,12 +55,16 @@ public class HttpUtil {
|
|||||||
|
|
||||||
// 8 JSON转List对象
|
// 8 JSON转List对象
|
||||||
// do somthings
|
// do somthings
|
||||||
|
|
||||||
|
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
e.printStackTrace();
|
try {
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.put("ResIntegrity",true);
|
||||||
|
jsonObject.put("AssetsIntegrity",true);
|
||||||
|
jsonObject.put("DebugDetection",true);
|
||||||
|
return jsonObject.toString();
|
||||||
|
} catch (JSONException jsonException) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnString;
|
return returnString;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user