From 8b2448a117ee63bf8869e8c23400e041a13eb758 Mon Sep 17 00:00:00 2001 From: "xiaofeng.zhong" Date: Fri, 25 May 2018 17:48:11 +0800 Subject: [PATCH 1/6] fix bug: NullPointException when hook view onclick; do not init trojan when space is not enough; rename version 0.1.2 --- trojan/build.gradle | 4 +- .../src/main/java/me/ele/trojan/Trojan.java | 8 ++++ .../me/ele/trojan/config/TrojanConstants.java | 2 +- .../java/me/ele/trojan/helper/FileHelper.java | 22 ---------- .../java/me/ele/trojan/lancet/LancetHook.java | 2 +- .../java/me/ele/trojan/utils/AppUtils.java | 42 +++++++++++++++++++ 6 files changed, 54 insertions(+), 26 deletions(-) diff --git a/trojan/build.gradle b/trojan/build.gradle index aa1334c41..2928dc295 100644 --- a/trojan/build.gradle +++ b/trojan/build.gradle @@ -8,8 +8,8 @@ buildscript { apply plugin: 'com.android.library' apply plugin: 'com.novoda.bintray-release' -def trojan_version_code = 11 -def trojan_version = "0.1.1" +def trojan_version_code = 12 +def trojan_version = "0.1.2" android { //这是为了debug C++而加的,后面要去掉 diff --git a/trojan/src/main/java/me/ele/trojan/Trojan.java b/trojan/src/main/java/me/ele/trojan/Trojan.java index fb2994f55..721a4a686 100644 --- a/trojan/src/main/java/me/ele/trojan/Trojan.java +++ b/trojan/src/main/java/me/ele/trojan/Trojan.java @@ -1,12 +1,15 @@ package me.ele.trojan; import android.content.Context; +import android.util.Log; import java.io.File; import java.util.List; import me.ele.trojan.config.TrojanConfig; +import me.ele.trojan.config.TrojanConstants; import me.ele.trojan.listener.WaitUploadListener; +import me.ele.trojan.utils.AppUtils; /** * Created by Eric on 17/2/14. @@ -17,6 +20,11 @@ public class Trojan { private static Context sContext; public static void init(TrojanConfig config) { + if (AppUtils.getDataAvailableSize() < TrojanConstants.MIN_FREE_SPACE_MB + || AppUtils.getSDAvailableSize() < TrojanConstants.MIN_FREE_SPACE_MB) { + Log.e("Trojan", "Trojan-->init,failed:space is not enough!"); + return; + } sContext = config.getContext(); TrojanManager.getInstance().init(config); } diff --git a/trojan/src/main/java/me/ele/trojan/config/TrojanConstants.java b/trojan/src/main/java/me/ele/trojan/config/TrojanConstants.java index 00f031d15..f43c618f3 100644 --- a/trojan/src/main/java/me/ele/trojan/config/TrojanConstants.java +++ b/trojan/src/main/java/me/ele/trojan/config/TrojanConstants.java @@ -8,7 +8,7 @@ public class TrojanConstants { public static final long FIVE_DAY_MILLS = 5 * 24 * 3600 * 1000L; - public static final long MIN_SDCARD_FREE_SPACE_MB = 50; + public static final long MIN_FREE_SPACE_MB = 100; public static final String UP = "-up"; diff --git a/trojan/src/main/java/me/ele/trojan/helper/FileHelper.java b/trojan/src/main/java/me/ele/trojan/helper/FileHelper.java index 07c382cc7..2d0937da7 100644 --- a/trojan/src/main/java/me/ele/trojan/helper/FileHelper.java +++ b/trojan/src/main/java/me/ele/trojan/helper/FileHelper.java @@ -3,7 +3,6 @@ import android.content.Context; import android.os.Build; import android.os.Environment; -import android.os.StatFs; import android.text.TextUtils; import java.io.File; @@ -437,25 +436,4 @@ public static void deleteBlankContent(File file) { } } - public static long getSDFreeSize() { - try { - File path = Environment.getExternalStorageDirectory(); - StatFs sf = new StatFs(path.getPath()); - long blockSize = sf.getBlockSize(); - long freeBlocks = sf.getAvailableBlocks(); - return (freeBlocks * blockSize) / 1024 / 1024; - } catch (Exception e) { - e.printStackTrace(); - } - return TrojanConstants.MIN_SDCARD_FREE_SPACE_MB; - } - - /** - * determine whether the remaining space of Sdcard is greater than 50 - * - * @return - */ - public static boolean isSDEnough() { - return getSDFreeSize() >= TrojanConstants.MIN_SDCARD_FREE_SPACE_MB; - } } \ No newline at end of file diff --git a/trojan/src/main/java/me/ele/trojan/lancet/LancetHook.java b/trojan/src/main/java/me/ele/trojan/lancet/LancetHook.java index 56d2471e7..634b5b74a 100644 --- a/trojan/src/main/java/me/ele/trojan/lancet/LancetHook.java +++ b/trojan/src/main/java/me/ele/trojan/lancet/LancetHook.java @@ -100,7 +100,7 @@ public static void hookClick(View v) { //还要获取到View在哪个页面,即如果是在Fragment中则打印出Fragment信息,否则打印出Activity信息 String pageInfo = getPageInfo(v); if (TextUtils.isEmpty(pageInfo)) { - msgList.add(This.get().toString()); + msgList.add(v.toString()); } else { msgList.add(pageInfo); } diff --git a/trojan/src/main/java/me/ele/trojan/utils/AppUtils.java b/trojan/src/main/java/me/ele/trojan/utils/AppUtils.java index c18a29dcb..9eeb833e0 100644 --- a/trojan/src/main/java/me/ele/trojan/utils/AppUtils.java +++ b/trojan/src/main/java/me/ele/trojan/utils/AppUtils.java @@ -5,6 +5,12 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.Build; +import android.os.Environment; +import android.os.StatFs; + +import java.io.File; + +import me.ele.trojan.config.TrojanConstants; /** * Created by michaelzhong on 2017/12/22. @@ -52,4 +58,40 @@ public static final String getCurProcessName(Context context) { return "UNKNOWN"; } + /** + * 获取data目录下的可用空间,MB为单位 + * + * @return + */ + public static long getDataAvailableSize() { + try { + File path = Environment.getDataDirectory(); + StatFs stat = new StatFs(path.getPath()); + long blockSize = stat.getBlockSize(); + long availableBlocks = stat.getAvailableBlocks(); + return (blockSize * availableBlocks) / TrojanConstants.FORMAT_MB; + } catch (Exception e) { + e.printStackTrace(); + } + return 0; + } + + /** + * 获取SD目录下的可用空间,MB为单位 + * + * @return + */ + public static long getSDAvailableSize() { + try { + File path = Environment.getExternalStorageDirectory(); + StatFs stat = new StatFs(path.getPath()); + long blockSize = stat.getBlockSize(); + long availableBlocks = stat.getAvailableBlocks(); + return (blockSize * availableBlocks) / TrojanConstants.FORMAT_MB; + } catch (Exception e) { + e.printStackTrace(); + } + return 0; + } + } From a86ada26aa56b190b3306f83e38910e73bb16cd9 Mon Sep 17 00:00:00 2001 From: "xiaofeng.zhong" Date: Tue, 10 Jul 2018 11:31:46 +0800 Subject: [PATCH 2/6] Format log, Clear place holder when compress log file --- trojan/build.gradle | 4 ++-- trojan/src/main/cpp/LogWriter.cpp | 6 ++--- trojan/src/main/cpp/LogWriter.h | 2 +- .../src/main/java/me/ele/trojan/Trojan.java | 22 ++++++++++++------ .../me/ele/trojan/config/TrojanConfig.java | 7 +++--- .../ele/trojan/executor/TrojanExecutor.java | 2 +- .../java/me/ele/trojan/helper/FileHelper.java | 4 ++-- .../me/ele/trojan/internal/ActivityLife.java | 14 +++++------ .../ele/trojan/receiver/TrojanReceiver.java | 23 ++++++++++++++----- .../ele/trojan/record/impl/LogFormatter.java | 4 ++-- .../ele/trojan/upload/impl/LogUploader.java | 4 ++-- 11 files changed, 56 insertions(+), 36 deletions(-) diff --git a/trojan/build.gradle b/trojan/build.gradle index 2928dc295..95a354136 100644 --- a/trojan/build.gradle +++ b/trojan/build.gradle @@ -8,8 +8,8 @@ buildscript { apply plugin: 'com.android.library' apply plugin: 'com.novoda.bintray-release' -def trojan_version_code = 12 -def trojan_version = "0.1.2" +def trojan_version_code = 13 +def trojan_version = "0.1.3" android { //这是为了debug C++而加的,后面要去掉 diff --git a/trojan/src/main/cpp/LogWriter.cpp b/trojan/src/main/cpp/LogWriter.cpp index e71cc73d0..b6cbe2578 100644 --- a/trojan/src/main/cpp/LogWriter.cpp +++ b/trojan/src/main/cpp/LogWriter.cpp @@ -89,9 +89,9 @@ ErrInfo *LogWriter::initMmap(JNIEnv *env, std::string basicInfo, std::string log bool findFlag = false; - for (off_t i = logPageSize - 1; i >= 0; i--) { - // Find the first '\n' and stop the search, if not found, then the page is still blank, just back to the beginning of the page - if (recordPtr[i] == '\n') { + for (off_t i = logPageSize - 1; i >= 1; i--) { + // Find the first "\n>" and stop the search, if not found, then the page is still blank, just back to the beginning of the page + if (recordPtr[i] == '>' && recordPtr[i - 1] == '\n') { findFlag = true; if (i != logPageSize - 1) { recordIndex = i + 1; diff --git a/trojan/src/main/cpp/LogWriter.h b/trojan/src/main/cpp/LogWriter.h index bd2bcb9fc..eeb2bbeab 100644 --- a/trojan/src/main/cpp/LogWriter.h +++ b/trojan/src/main/cpp/LogWriter.h @@ -25,7 +25,7 @@ #define ALLOC_PAGE_NUM 40 //含有\n的目的是为了将Cipher_Start与密文写在不同的行,这样读取时比较方便 #define CIPHER_START "" -#define CIPHER_END "\n" +#define CIPHER_END "\n>" class LogWriter { public: diff --git a/trojan/src/main/java/me/ele/trojan/Trojan.java b/trojan/src/main/java/me/ele/trojan/Trojan.java index 721a4a686..d9e7cd276 100644 --- a/trojan/src/main/java/me/ele/trojan/Trojan.java +++ b/trojan/src/main/java/me/ele/trojan/Trojan.java @@ -8,6 +8,7 @@ import me.ele.trojan.config.TrojanConfig; import me.ele.trojan.config.TrojanConstants; +import me.ele.trojan.executor.TrojanExecutor; import me.ele.trojan.listener.WaitUploadListener; import me.ele.trojan.utils.AppUtils; @@ -19,14 +20,21 @@ public class Trojan { private static Context sContext; - public static void init(TrojanConfig config) { - if (AppUtils.getDataAvailableSize() < TrojanConstants.MIN_FREE_SPACE_MB - || AppUtils.getSDAvailableSize() < TrojanConstants.MIN_FREE_SPACE_MB) { - Log.e("Trojan", "Trojan-->init,failed:space is not enough!"); - return; - } + public static void init(final TrojanConfig config) { + sContext = config.getContext(); - TrojanManager.getInstance().init(config); + + TrojanExecutor.getInstance().execute(new Runnable() { + @Override + public void run() { + if (AppUtils.getDataAvailableSize() < TrojanConstants.MIN_FREE_SPACE_MB + || AppUtils.getSDAvailableSize() < TrojanConstants.MIN_FREE_SPACE_MB) { + Log.e("Trojan", "Trojan-->init,failed:space is not enough!"); + return; + } + TrojanManager.getInstance().init(config); + } + }); } public static Context getContext() { diff --git a/trojan/src/main/java/me/ele/trojan/config/TrojanConfig.java b/trojan/src/main/java/me/ele/trojan/config/TrojanConfig.java index 52aeafc2e..1775a7338 100644 --- a/trojan/src/main/java/me/ele/trojan/config/TrojanConfig.java +++ b/trojan/src/main/java/me/ele/trojan/config/TrojanConfig.java @@ -44,6 +44,9 @@ public String getDeviceId() { } public String getLogDir() { + if (TextUtils.isEmpty(logDir)) { + logDir = FileHelper.getLogDir(context).getAbsolutePath(); + } File dirFile = new File(logDir); if (!dirFile.exists() || !dirFile.isDirectory()) { dirFile.mkdirs(); @@ -125,9 +128,7 @@ private void initWithDefaultValues() { if (userInfo == null) { userInfo = ""; } - if (TextUtils.isEmpty(logDir)) { - logDir = FileHelper.getLogDir(context).getAbsolutePath(); - } else { + if (!TextUtils.isEmpty(logDir)) { logDir = logDir + File.separator + AppUtils.getCurProcessName(context); } } diff --git a/trojan/src/main/java/me/ele/trojan/executor/TrojanExecutor.java b/trojan/src/main/java/me/ele/trojan/executor/TrojanExecutor.java index 91867c372..c1f5df270 100644 --- a/trojan/src/main/java/me/ele/trojan/executor/TrojanExecutor.java +++ b/trojan/src/main/java/me/ele/trojan/executor/TrojanExecutor.java @@ -39,7 +39,7 @@ public void executeRecord(Runnable runnable) { } - public void executeUpload(Runnable runnable) { + public void execute(Runnable runnable) { if (runnable == null) { return; } diff --git a/trojan/src/main/java/me/ele/trojan/helper/FileHelper.java b/trojan/src/main/java/me/ele/trojan/helper/FileHelper.java index 2d0937da7..54b9c1240 100644 --- a/trojan/src/main/java/me/ele/trojan/helper/FileHelper.java +++ b/trojan/src/main/java/me/ele/trojan/helper/FileHelper.java @@ -403,7 +403,7 @@ public static List renameToUpAllIfNeed(Context context, String writeFileNa } public static void deleteBlankContent(File file) { - if (isFileExist(file)) { + if (!isFileExist(file)) { return; } RandomAccessFile raf = null; @@ -417,7 +417,7 @@ public static void deleteBlankContent(File file) { while (pos > 0) { --pos; raf.seek(pos); - if (raf.readByte() == '\n') { + if (raf.readByte() == '>') { break; } } diff --git a/trojan/src/main/java/me/ele/trojan/internal/ActivityLife.java b/trojan/src/main/java/me/ele/trojan/internal/ActivityLife.java index bdcf7a6b9..000458e69 100644 --- a/trojan/src/main/java/me/ele/trojan/internal/ActivityLife.java +++ b/trojan/src/main/java/me/ele/trojan/internal/ActivityLife.java @@ -19,37 +19,37 @@ public static void init(Context context) { new Application.ActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle bundle) { - Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, "onCreate:" + activity.getClass().getName() + " Bundle = " + bundle); + Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, activity.getClass().getName() + "#*onCreate:Bundle=" + bundle); } @Override public void onActivityStarted(Activity activity) { - Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, "onStart:" + activity.getClass().getName()); + Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, activity.getClass().getName() + "#*onStart"); } @Override public void onActivityResumed(Activity activity) { - Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, "onResume:" + activity.getClass().getName()); + Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, activity.getClass().getName() + "#*onResume"); } @Override public void onActivityPaused(Activity activity) { - Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, "onPause:" + activity.getClass().getName()); + Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, activity.getClass().getName() + "#*onPause"); } @Override public void onActivityStopped(Activity activity) { - Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, "onStop:" + activity.getClass().getName()); + Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, activity.getClass().getName() + "#*onStop"); } @Override public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { - Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, "onSaveInstanceState:" + activity.getClass().getName()); + Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, activity.getClass().getName() + "#*onSaveInstanceState:Bundle=" + bundle); } @Override public void onActivityDestroyed(Activity activity) { - Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, "onDestroy:" + activity.getClass().getName()); + Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, activity.getClass().getName() + "#*onDestroy"); } } ); diff --git a/trojan/src/main/java/me/ele/trojan/receiver/TrojanReceiver.java b/trojan/src/main/java/me/ele/trojan/receiver/TrojanReceiver.java index 12cbbb814..1958e8090 100644 --- a/trojan/src/main/java/me/ele/trojan/receiver/TrojanReceiver.java +++ b/trojan/src/main/java/me/ele/trojan/receiver/TrojanReceiver.java @@ -6,7 +6,7 @@ import android.net.ConnectivityManager; import android.os.BatteryManager; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; import me.ele.trojan.Trojan; @@ -23,15 +23,26 @@ public class TrojanReceiver extends BroadcastReceiver { @Override - public void onReceive(Context context, Intent intent) { + public void onReceive(Context context, final Intent intent) { final String action = intent.getAction(); Logger.i("TrojanReceiver-->action:" + action); if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { - showBatteryState(intent); + TrojanExecutor.getInstance().execute(new Runnable() { + @Override + public void run() { + showBatteryState(intent); + } + }); } else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) { - showNetworkType(context); + final Context applicationContext = context.getApplicationContext(); + TrojanExecutor.getInstance().execute(new Runnable() { + @Override + public void run() { + showNetworkType(applicationContext); + } + }); } else if (Intent.ACTION_TIME_TICK.equals(action)) { - TrojanExecutor.getInstance().executeUpload(new Runnable() { + TrojanExecutor.getInstance().execute(new Runnable() { @Override public void run() { PerformanceHelper.recordMemory(); @@ -68,7 +79,7 @@ private void showBatteryState(Intent intent) { break; } - List msgList = new LinkedList<>(); + List msgList = new ArrayList<>(); msgList.add(String.valueOf((level * 1.00 / 100))); msgList.add(statusResult); Trojan.log(LogConstants.BATTERY_TAG, msgList); diff --git a/trojan/src/main/java/me/ele/trojan/record/impl/LogFormatter.java b/trojan/src/main/java/me/ele/trojan/record/impl/LogFormatter.java index 6ccdf9a58..e9771f50f 100644 --- a/trojan/src/main/java/me/ele/trojan/record/impl/LogFormatter.java +++ b/trojan/src/main/java/me/ele/trojan/record/impl/LogFormatter.java @@ -32,7 +32,7 @@ public String format(String tag, int version, String msg, boolean crypt) { content.append(LogConstants.FIELD_SEPERATOR); content.append(msg); if (!crypt) { - content.append('\n'); + content.append("\n>"); } return content.toString(); } @@ -58,7 +58,7 @@ public String format(String tag, int version, List msgFieldList, boolean } } if (!crypt) { - content.append('\n'); + content.append("\n>"); } return content.toString(); } diff --git a/trojan/src/main/java/me/ele/trojan/upload/impl/LogUploader.java b/trojan/src/main/java/me/ele/trojan/upload/impl/LogUploader.java index dc5f8ae53..df60374ae 100644 --- a/trojan/src/main/java/me/ele/trojan/upload/impl/LogUploader.java +++ b/trojan/src/main/java/me/ele/trojan/upload/impl/LogUploader.java @@ -40,7 +40,7 @@ public LogUploader(final TrojanConfig trojanConfig, ILogRecorder logRecorder) { this.logRecorder = logRecorder; // should check upload file when init - TrojanExecutor.getInstance().executeUpload(new Runnable() { + TrojanExecutor.getInstance().execute(new Runnable() { @Override public void run() { if (!PermissionHelper.hasWriteAndReadStoragePermission(context)) { @@ -68,7 +68,7 @@ public void prepareUploadLogFileAsync(final WaitUploadListener waitUploadListene @Override public void readyToUpload() { Logger.i("LogUploader-->readyToUpload"); - TrojanExecutor.getInstance().executeUpload(new Runnable() { + TrojanExecutor.getInstance().execute(new Runnable() { @Override public void run() { final List gzFileList = FileHelper.cleanUpLogFile(context, trojanConfig.getLogDir()); From 1ebb13ac6098c4df7095c577b25cb126237e3163 Mon Sep 17 00:00:00 2001 From: "xiaofeng.zhong" Date: Tue, 10 Jul 2018 14:10:46 +0800 Subject: [PATCH 3/6] support trojan sync init --- trojan/build.gradle | 4 ++-- .../src/main/java/me/ele/trojan/Trojan.java | 20 ++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/trojan/build.gradle b/trojan/build.gradle index 95a354136..df749d537 100644 --- a/trojan/build.gradle +++ b/trojan/build.gradle @@ -8,8 +8,8 @@ buildscript { apply plugin: 'com.android.library' apply plugin: 'com.novoda.bintray-release' -def trojan_version_code = 13 -def trojan_version = "0.1.3" +def trojan_version_code = 14 +def trojan_version = "0.1.4" android { //这是为了debug C++而加的,后面要去掉 diff --git a/trojan/src/main/java/me/ele/trojan/Trojan.java b/trojan/src/main/java/me/ele/trojan/Trojan.java index d9e7cd276..c3e8abc5c 100644 --- a/trojan/src/main/java/me/ele/trojan/Trojan.java +++ b/trojan/src/main/java/me/ele/trojan/Trojan.java @@ -21,22 +21,24 @@ public class Trojan { private static Context sContext; public static void init(final TrojanConfig config) { - - sContext = config.getContext(); - TrojanExecutor.getInstance().execute(new Runnable() { @Override public void run() { - if (AppUtils.getDataAvailableSize() < TrojanConstants.MIN_FREE_SPACE_MB - || AppUtils.getSDAvailableSize() < TrojanConstants.MIN_FREE_SPACE_MB) { - Log.e("Trojan", "Trojan-->init,failed:space is not enough!"); - return; - } - TrojanManager.getInstance().init(config); + initSync(config); } }); } + public static void initSync(TrojanConfig config) { + sContext = config.getContext(); + if (AppUtils.getDataAvailableSize() < TrojanConstants.MIN_FREE_SPACE_MB + || AppUtils.getSDAvailableSize() < TrojanConstants.MIN_FREE_SPACE_MB) { + Log.e("Trojan", "Trojan-->init,failed:space is not enough!"); + return; + } + TrojanManager.getInstance().init(config); + } + public static Context getContext() { if (sContext == null) { throw new IllegalArgumentException("Trojan have not init"); From 0fcf376498c91f8b4906b00826d9e591648b851a Mon Sep 17 00:00:00 2001 From: "xiaofeng.zhong" Date: Wed, 11 Jul 2018 11:04:21 +0800 Subject: [PATCH 4/6] format activity log --- trojan/build.gradle | 4 ++-- .../java/me/ele/trojan/internal/ActivityLife.java | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/trojan/build.gradle b/trojan/build.gradle index df749d537..e7f781f71 100644 --- a/trojan/build.gradle +++ b/trojan/build.gradle @@ -8,8 +8,8 @@ buildscript { apply plugin: 'com.android.library' apply plugin: 'com.novoda.bintray-release' -def trojan_version_code = 14 -def trojan_version = "0.1.4" +def trojan_version_code = 15 +def trojan_version = "0.1.5" android { //这是为了debug C++而加的,后面要去掉 diff --git a/trojan/src/main/java/me/ele/trojan/internal/ActivityLife.java b/trojan/src/main/java/me/ele/trojan/internal/ActivityLife.java index 000458e69..d94a8cc50 100644 --- a/trojan/src/main/java/me/ele/trojan/internal/ActivityLife.java +++ b/trojan/src/main/java/me/ele/trojan/internal/ActivityLife.java @@ -19,37 +19,37 @@ public static void init(Context context) { new Application.ActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle bundle) { - Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, activity.getClass().getName() + "#*onCreate:Bundle=" + bundle); + Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, android.os.Process.myPid() + "#*" + activity.getClass().getName() + (bundle == null ? "#*onCreate:Bundle=null" : "#*onCreate:Bundle!=null")); } @Override public void onActivityStarted(Activity activity) { - Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, activity.getClass().getName() + "#*onStart"); + Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, android.os.Process.myPid() + "#*" + activity.getClass().getName() + "#*onStart"); } @Override public void onActivityResumed(Activity activity) { - Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, activity.getClass().getName() + "#*onResume"); + Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, android.os.Process.myPid() + "#*" + activity.getClass().getName() + "#*onResume"); } @Override public void onActivityPaused(Activity activity) { - Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, activity.getClass().getName() + "#*onPause"); + Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, android.os.Process.myPid() + "#*" + activity.getClass().getName() + "#*onPause"); } @Override public void onActivityStopped(Activity activity) { - Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, activity.getClass().getName() + "#*onStop"); + Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, android.os.Process.myPid() + "#*" + activity.getClass().getName() + "#*onStop"); } @Override public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { - Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, activity.getClass().getName() + "#*onSaveInstanceState:Bundle=" + bundle); + Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, android.os.Process.myPid() + "#*" + activity.getClass().getName() + (bundle == null ? "#*onSaveInstanceState:Bundle=null" : "#*onSaveInstanceState:Bundle!=null")); } @Override public void onActivityDestroyed(Activity activity) { - Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, activity.getClass().getName() + "#*onDestroy"); + Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, android.os.Process.myPid() + "#*" + activity.getClass().getName() + "#*onDestroy"); } } ); From b8695476b438a5f7debd50b4bccf4e39ebe5f0c1 Mon Sep 17 00:00:00 2001 From: "xiaofeng.zhong" Date: Thu, 14 Feb 2019 17:22:23 +0800 Subject: [PATCH 5/6] Upgrade version 0.1.6; fix bug: native memory leak; create new gzip file every time --- trojan/build.gradle | 4 ++-- trojan/src/main/cpp/LogWriter.cpp | 7 ++++--- trojan/src/main/cpp/tea/TEACipher.cpp | 1 + .../java/me/ele/trojan/helper/FileHelper.java | 7 ++++--- .../me/ele/trojan/record/impl/LogRecorder.java | 16 +++++++++------- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/trojan/build.gradle b/trojan/build.gradle index e7f781f71..0232a0f6c 100644 --- a/trojan/build.gradle +++ b/trojan/build.gradle @@ -8,8 +8,8 @@ buildscript { apply plugin: 'com.android.library' apply plugin: 'com.novoda.bintray-release' -def trojan_version_code = 15 -def trojan_version = "0.1.5" +def trojan_version_code = 16 +def trojan_version = "0.1.6" android { //这是为了debug C++而加的,后面要去掉 diff --git a/trojan/src/main/cpp/LogWriter.cpp b/trojan/src/main/cpp/LogWriter.cpp index b6cbe2578..a71fe237c 100644 --- a/trojan/src/main/cpp/LogWriter.cpp +++ b/trojan/src/main/cpp/LogWriter.cpp @@ -5,6 +5,7 @@ #include "ErrInfo.h" #include #include +#include LogWriter::LogWriter() { this->cipherStart = strlen(CIPHER_START); @@ -327,13 +328,13 @@ ErrInfo *LogWriter::closeAndRenew(JNIEnv *env) { std::string LogWriter::getDate() { time_t now = time(0); tm localTime = *localtime(&now); - std::string *date; + std::string date; size_t bufSize = sizeof(char) * 20; char *buf = (char *) malloc(bufSize); strftime(buf, bufSize, "%Y-%m-%d", &localTime); - date = new std::string(buf); + date = std::string(buf); free(buf); - return *date; + return date; } ErrInfo *LogWriter::unixMunmap(int fd, void *map, size_t map_size) { diff --git a/trojan/src/main/cpp/tea/TEACipher.cpp b/trojan/src/main/cpp/tea/TEACipher.cpp index e949af18f..85bfb86b7 100644 --- a/trojan/src/main/cpp/tea/TEACipher.cpp +++ b/trojan/src/main/cpp/tea/TEACipher.cpp @@ -4,6 +4,7 @@ #include #include "TEACipher.h" +#include void encryptLong(uint32_t *firstChunk, uint32_t *secondChunk, uint32_t *key) { uint32_t y = *firstChunk; diff --git a/trojan/src/main/java/me/ele/trojan/helper/FileHelper.java b/trojan/src/main/java/me/ele/trojan/helper/FileHelper.java index 54b9c1240..1d602a9c5 100644 --- a/trojan/src/main/java/me/ele/trojan/helper/FileHelper.java +++ b/trojan/src/main/java/me/ele/trojan/helper/FileHelper.java @@ -166,7 +166,7 @@ public static File save2GZIPFile(File sourceFile, String parentPath) throws IOEx } File gzipFile = createGZIPFile(sourceFile, parentPath); fis = new FileInputStream(sourceFile); - fos = new FileOutputStream(gzipFile, true); + fos = new FileOutputStream(gzipFile); gos = new GZIPOutputStream(fos); byte buffer[] = new byte[1024]; int count; @@ -201,9 +201,10 @@ private static File createGZIPFile(File sourceFile, String parentPath) throws IO gzPathBuilder.append(getGZIPFileName(sourceFile)); File gzFile = new File(gzPathBuilder.toString()); - if (!gzFile.exists()) { - gzFile.createNewFile(); + if (gzFile.exists()) { + gzFile.delete(); } + gzFile.createNewFile(); return gzFile; } diff --git a/trojan/src/main/java/me/ele/trojan/record/impl/LogRecorder.java b/trojan/src/main/java/me/ele/trojan/record/impl/LogRecorder.java index 854582a66..7af9dcb2a 100644 --- a/trojan/src/main/java/me/ele/trojan/record/impl/LogRecorder.java +++ b/trojan/src/main/java/me/ele/trojan/record/impl/LogRecorder.java @@ -74,8 +74,7 @@ private synchronized void tryInitLogWriter() { if (!config.isEnableBackup()) { try { MmapLogWriter mmapLogWriter = new MmapLogWriter(); - String basicInfo = logFormatter.format(LogConstants.BASIC_TAG, TagUtil.getVersionByTag(LogConstants.BASIC_TAG), getBasicInfo(config), false); - mmapLogWriter.init(context, basicInfo, dirPath, cipherKey); + mmapLogWriter.init(context, getFormatBasicInfo(), dirPath, cipherKey); logWriter = mmapLogWriter; } catch (Throwable ex) { ex.printStackTrace(); @@ -97,7 +96,7 @@ public void run() { Logger.e("no permission for refreshUser"); return; } - String basicInfo = logFormatter.format(LogConstants.BASIC_TAG, TagUtil.getVersionByTag(LogConstants.BASIC_TAG), getBasicInfo(config), false); + String basicInfo = getFormatBasicInfo(); try { tryInitLogWriter(); logWriter.refreshBasicInfo(basicInfo); @@ -186,6 +185,7 @@ public void run() { return; } tryInitLogWriter(); + logWriter.refreshBasicInfo(getFormatBasicInfo()); logWriter.closeAndRenew(); final String writeFileName = DateUtils.getDate() + (logWriter instanceof MmapLogWriter ? TrojanConstants.MMAP : ""); @@ -209,6 +209,7 @@ public File prepareUploadSync(String dateTime) { } if (dateTime.equals(DateUtils.getDate())) { tryInitLogWriter(); + logWriter.refreshBasicInfo(getFormatBasicInfo()); logWriter.closeAndRenew(); } return FileHelper.getLogFileByDate(context, config.getLogDir(), dateTime); @@ -218,8 +219,7 @@ private void initNormalLogWriter() { Logger.e("initNormalLogWriter"); try { NormalLogWriter normalLogWriter = new NormalLogWriter(); - String basicInfo = logFormatter.format(LogConstants.BASIC_TAG, TagUtil.getVersionByTag(LogConstants.BASIC_TAG), getBasicInfo(config), false); - normalLogWriter.init(context, basicInfo, dirPath, cipherKey); + normalLogWriter.init(context, getFormatBasicInfo(), dirPath, cipherKey); logWriter = normalLogWriter; } catch (Throwable e) { e.printStackTrace(); @@ -234,7 +234,8 @@ private void tryWriteLog(String content, boolean encryptFlag) { } } - private String getBasicInfo(TrojanConfig config) { + private String getFormatBasicInfo() { + StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("Android") .append(LogConstants.INTERNAL_SEPERATOR) @@ -251,7 +252,8 @@ private String getBasicInfo(TrojanConfig config) { .append(DeviceUtils.getDeviceInfo()) .append(LogConstants.INTERNAL_SEPERATOR) .append(DeviceUtils.isRoot() ? 1 : 0); - return stringBuilder.toString(); + + return logFormatter.format(LogConstants.BASIC_TAG, TagUtil.getVersionByTag(LogConstants.BASIC_TAG), stringBuilder.toString(), false); } /** From a31faa627cd121f26391d2a9dabd5834b8acd251 Mon Sep 17 00:00:00 2001 From: "xiaofeng.zhong" Date: Thu, 14 Feb 2019 17:34:51 +0800 Subject: [PATCH 6/6] update readme --- README.md | 2 +- README_CN.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b3305eab9..f0c145ff0 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ apply plugin: 'me.ele.lancet' dependencies {     ......     provided 'me.ele:lancet-base:1.0.2' -    compile 'me.ele:trojan-library:0.0.5' +    compile 'me.ele:trojan-library:0.1.6' } ``` diff --git a/README_CN.md b/README_CN.md index ad85dc889..434657f55 100644 --- a/README_CN.md +++ b/README_CN.md @@ -48,7 +48,7 @@ apply plugin: 'me.ele.lancet' dependencies { ...... provided 'me.ele:lancet-base:1.0.2' - compile 'me.ele:trojan-library:0.0.5' + compile 'me.ele:trojan-library:0.1.6' } ```