Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug: NullPointException when hook view click; do not init trojan when space is not enough; rename version 0.1.2 #11

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand Down
4 changes: 2 additions & 2 deletions trojan/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 16
def trojan_version = "0.1.6"

android {
//这是为了debug C++而加的,后面要去掉
Expand Down
13 changes: 7 additions & 6 deletions trojan/src/main/cpp/LogWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ErrInfo.h"
#include <iostream>
#include <sys/file.h>
#include <cstring>

LogWriter::LogWriter() {
this->cipherStart = strlen(CIPHER_START);
Expand Down Expand Up @@ -89,9 +90,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;
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion trojan/src/main/cpp/LogWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#define ALLOC_PAGE_NUM 40
//含有\n的目的是为了将Cipher_Start与密文写在不同的行,这样读取时比较方便
#define CIPHER_START "<Cipher>"
#define CIPHER_END "<Cipher>\n"
#define CIPHER_END "<Cipher>\n>"

class LogWriter {
public:
Expand Down
1 change: 1 addition & 0 deletions trojan/src/main/cpp/tea/TEACipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <malloc.h>
#include "TEACipher.h"
#include <string.h>

void encryptLong(uint32_t *firstChunk, uint32_t *secondChunk, uint32_t *key) {
uint32_t y = *firstChunk;
Expand Down
20 changes: 19 additions & 1 deletion trojan/src/main/java/me/ele/trojan/Trojan.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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.executor.TrojanExecutor;
import me.ele.trojan.listener.WaitUploadListener;
import me.ele.trojan.utils.AppUtils;

/**
* Created by Eric on 17/2/14.
Expand All @@ -16,8 +20,22 @@ public class Trojan {

private static Context sContext;

public static void init(TrojanConfig config) {
public static void init(final TrojanConfig config) {
TrojanExecutor.getInstance().execute(new Runnable() {
@Override
public void run() {
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);
}

Expand Down
7 changes: 4 additions & 3 deletions trojan/src/main/java/me/ele/trojan/config/TrojanConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void executeRecord(Runnable runnable) {
}


public void executeUpload(Runnable runnable) {
public void execute(Runnable runnable) {
if (runnable == null) {
return;
}
Expand Down
33 changes: 6 additions & 27 deletions trojan/src/main/java/me/ele/trojan/helper/FileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,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;
Expand Down Expand Up @@ -202,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;
}

Expand Down Expand Up @@ -404,7 +404,7 @@ public static List<File> renameToUpAllIfNeed(Context context, String writeFileNa
}

public static void deleteBlankContent(File file) {
if (isFileExist(file)) {
if (!isFileExist(file)) {
return;
}
RandomAccessFile raf = null;
Expand All @@ -418,7 +418,7 @@ public static void deleteBlankContent(File file) {
while (pos > 0) {
--pos;
raf.seek(pos);
if (raf.readByte() == '\n') {
if (raf.readByte() == '>') {
break;
}
}
Expand All @@ -437,25 +437,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;
}
}
14 changes: 7 additions & 7 deletions trojan/src/main/java/me/ele/trojan/internal/ActivityLife.java
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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, "onStart:" + activity.getClass().getName());
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, "onResume:" + activity.getClass().getName());
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, "onPause:" + activity.getClass().getName());
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, "onStop:" + activity.getClass().getName());
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, "onSaveInstanceState:" + activity.getClass().getName());
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, "onDestroy:" + activity.getClass().getName());
Trojan.log(LogConstants.ACTIVITY_LIFE_TAG, android.os.Process.myPid() + "#*" + activity.getClass().getName() + "#*onDestroy");
}
}
);
Expand Down
2 changes: 1 addition & 1 deletion trojan/src/main/java/me/ele/trojan/lancet/LancetHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
23 changes: 17 additions & 6 deletions trojan/src/main/java/me/ele/trojan/receiver/TrojanReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -68,7 +79,7 @@ private void showBatteryState(Intent intent) {
break;
}

List<String> msgList = new LinkedList<>();
List<String> msgList = new ArrayList<>();
msgList.add(String.valueOf((level * 1.00 / 100)));
msgList.add(statusResult);
Trojan.log(LogConstants.BATTERY_TAG, msgList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -58,7 +58,7 @@ public String format(String tag, int version, List<String> msgFieldList, boolean
}
}
if (!crypt) {
content.append('\n');
content.append("\n>");
}
return content.toString();
}
Expand Down
Loading