diff --git a/plugin.xml b/plugin.xml index 7c7c76a..715b28c 100644 --- a/plugin.xml +++ b/plugin.xml @@ -15,6 +15,7 @@ + @@ -32,6 +33,9 @@ + + + diff --git a/src/android/com/cescit/integrity/ApkIntegrity.java b/src/android/com/cescit/integrity/ApkIntegrity.java index f3dc086..52cf91b 100644 --- a/src/android/com/cescit/integrity/ApkIntegrity.java +++ b/src/android/com/cescit/integrity/ApkIntegrity.java @@ -18,7 +18,6 @@ import java.nio.charset.StandardCharsets; class ApkIntegrity { private static final String MESSAGE_DIGEST_ALGORITHM = "SHA-256"; - private static final String ASSETS_BASE_PATH = ""; // Collections.unmodifiableMap 使得返回的内容只能只读访问 private static final Map hashList = Collections.unmodifiableMap( @@ -28,7 +27,8 @@ class ApkIntegrity { public static JSONObject check(Context context) throws Exception { JSONObject result = new JSONObject(); Map 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); String upHash = obj.getString("apk"); String nowHash = nowHashList.get("apk"); diff --git a/src/android/com/cescit/integrity/CescitIntegrity.java b/src/android/com/cescit/integrity/CescitIntegrity.java index c08d1c9..a6060fc 100644 --- a/src/android/com/cescit/integrity/CescitIntegrity.java +++ b/src/android/com/cescit/integrity/CescitIntegrity.java @@ -29,7 +29,7 @@ public class CescitIntegrity extends CordovaPlugin { @Override public void run () { 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); if(obj.getBoolean("ApkIntegrity")) { ApkIntegrity.check(context); @@ -39,9 +39,12 @@ public class CescitIntegrity extends CordovaPlugin { } if(obj.getBoolean("AssetsIntegrity")) { AssetsIntegrity.check(context); - } + } + if(obj.getBoolean("DebugDetection")) { + DebugDetection.check(context.getPackageName()); + } } catch (final Exception e) { - e.printStackTrace(); + //e.printStackTrace(); throw new TamperingException("Anti-Tampering check failed"); } } diff --git a/src/android/com/cescit/integrity/Config.java b/src/android/com/cescit/integrity/Config.java index 98dede0..fe598a9 100644 --- a/src/android/com/cescit/integrity/Config.java +++ b/src/android/com/cescit/integrity/Config.java @@ -2,19 +2,23 @@ package com.cescit.integrity; import java.util.HashMap; import java.util.Map; +import android.content.pm.PackageManager; +import android.content.Context; +import android.content.pm.ApplicationInfo; class Config { - private static final String APK_HASH_URL = "http://webf.cewater.com.cn/apk-hash/jm.json"; + private static final Map headers = new HashMap(); // 获取配置 - public static T getConfig(String key) throws Exception{ - // res资源(路径:文件hash)键值对 - Map config = new HashMap(); - config.put("APK_HASH_URL", (T) APK_HASH_URL); - T result = null; - for(String configKey : config.keySet()){ - result = config.get(configKey); - } - return (T) result; + public static String getConfig(Context context,String key) throws Exception{ + ApplicationInfo appInfo = context.getPackageManager() + .getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); + return appInfo.metaData.getString(key); } -} \ No newline at end of file + + public static Map getHeader(Context context) throws PackageManager.NameNotFoundException { + PackageManager packageManager = context.getPackageManager(); + headers.put("version", packageManager.getPackageInfo(context.getPackageName(), 0).versionName); + return headers; + } +} diff --git a/src/android/com/cescit/integrity/HttpUtil.java b/src/android/com/cescit/integrity/HttpUtil.java index fba9b10..225bfdb 100644 --- a/src/android/com/cescit/integrity/HttpUtil.java +++ b/src/android/com/cescit/integrity/HttpUtil.java @@ -1,37 +1,45 @@ package com.cescit.integrity; +import org.json.JSONException; +import org.json.JSONObject; + import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; - +import java.util.Map; + public class HttpUtil { - public static String getHttpRequestData(String urlPath) { - + public static String getHttpRequestData(String urlPath,Mapheader) { + // 首先抓取异常并处理 String returnString = ""; try{ // 代码实现以GET请求方式为主,POST跳过 /** 1 GET方式请求数据 start*/ - + // 1 创建URL对象,接收用户传递访问地址对象链接 URL url = new URL(urlPath); - + // 2 打开用户传递URL参数地址 HttpURLConnection connect = (HttpURLConnection) url.openConnection(); - // 3 设置HTTP请求的一些参数信息 connect.setRequestMethod("GET"); // 参数必须大写 + if(header!=null && header.isEmpty()){ + for(String key:header.keySet()){ + connect.setRequestProperty(key, header.get(key)); + } + } connect.connect(); - + // 4 获取URL请求到的数据,并创建数据流接收 InputStream isString = connect.getInputStream(); - + // 5 构建一个字符流缓冲对象,承载URL读取到的数据 BufferedReader isRead = new BufferedReader(new InputStreamReader(isString)); - + // 6 输出打印获取到的文件流 String str = ""; while ((str = isRead.readLine()) != null) { @@ -40,19 +48,23 @@ public class HttpUtil { // System.out.println(str); returnString += str; } - + // 7 关闭流 isString.close(); connect.disconnect(); - + // 8 JSON转List对象 // do somthings - - }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; - } -} \ No newline at end of file + } +}