mirror of
https://github.com/emanuele-f/PCAPdroid.git
synced 2026-06-16 21:10:57 +08:00
Add ability to read the root daemon log
This commit is contained in:
parent
f777ed16c2
commit
e65ff93972
@ -58,6 +58,10 @@
|
||||
android:name=".activities.AboutActivity"
|
||||
android:launchMode="singleTop"
|
||||
android:parentActivityName=".activities.MainActivity" />
|
||||
<activity
|
||||
android:name=".activities.LogviewActivity"
|
||||
android:launchMode="singleTop"
|
||||
android:parentActivityName=".activities.MainActivity" />
|
||||
|
||||
<service
|
||||
android:name=".CaptureService"
|
||||
|
||||
@ -671,8 +671,11 @@ public class CaptureService extends VpnService implements Runnable {
|
||||
});
|
||||
}
|
||||
|
||||
public static String getPcapdWorkingDir(Context ctx) {
|
||||
return ctx.getCacheDir().getAbsolutePath();
|
||||
}
|
||||
public String getPcapdWorkingDir() {
|
||||
return getCacheDir().getAbsolutePath();
|
||||
return getPcapdWorkingDir(this);
|
||||
}
|
||||
|
||||
public String getLibprogPath(String prog_name) {
|
||||
|
||||
@ -23,6 +23,8 @@ import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.app.UiModeManager;
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
@ -491,4 +493,12 @@ public class Utils {
|
||||
|
||||
return root_available;
|
||||
}
|
||||
|
||||
public static void copyToClipboard(Context ctx, String contents) {
|
||||
ClipboardManager clipboard = (ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
ClipData clip = ClipData.newPlainText(ctx.getString(R.string.stats), contents);
|
||||
clipboard.setPrimaryClip(clip);
|
||||
|
||||
Utils.showToast(ctx, R.string.copied_to_clipboard);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,108 @@
|
||||
/*
|
||||
* This file is part of PCAPdroid.
|
||||
*
|
||||
* PCAPdroid is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* PCAPdroid is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with PCAPdroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2020-21 - Emanuele Faranda
|
||||
*/
|
||||
|
||||
package com.emanuelef.remote_capture.activities;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.text.Html;
|
||||
import android.text.method.LinkMovementMethod;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.emanuelef.remote_capture.CaptureService;
|
||||
import com.emanuelef.remote_capture.R;
|
||||
import com.emanuelef.remote_capture.Utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public class LogviewActivity extends BaseActivity {
|
||||
private static final String TAG = "LogviewActivity";
|
||||
private String mLogText;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setTitle(R.string.root_log);
|
||||
setContentView(R.layout.logview_activity);
|
||||
|
||||
TextView logView = findViewById(R.id.log);
|
||||
mLogText = readLog();
|
||||
|
||||
logView.setText(!mLogText.isEmpty() ? mLogText : getString(R.string.error));
|
||||
}
|
||||
|
||||
private String readLog() {
|
||||
try {
|
||||
String logpath = CaptureService.getPcapdWorkingDir(this) + "/pcapd.log";
|
||||
BufferedReader reader = new BufferedReader(new FileReader(logpath));
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String line;
|
||||
|
||||
while((line = reader.readLine()) != null) {
|
||||
builder.append(line);
|
||||
builder.append("\n");
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
MenuInflater inflater = getMenuInflater();
|
||||
inflater.inflate(R.menu.copy_share_menu, menu);
|
||||
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
||||
int id = item.getItemId();
|
||||
|
||||
if(id == R.id.copy_to_clipboard) {
|
||||
Utils.copyToClipboard(this, mLogText);
|
||||
return true;
|
||||
} else if(id == R.id.share) {
|
||||
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
|
||||
intent.setType("text/plain");
|
||||
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.root_log));
|
||||
intent.putExtra(android.content.Intent.EXTRA_TEXT, mLogText);
|
||||
|
||||
startActivity(Intent.createChooser(intent, getResources().getString(R.string.share)));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
@ -51,6 +51,7 @@ import android.provider.DocumentsContract;
|
||||
import android.provider.OpenableColumns;
|
||||
import android.util.Log;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
@ -187,6 +188,11 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
|
||||
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(GITHUB_PROJECT_URL + "/tree/" + verStr));
|
||||
startActivity(browserIntent);
|
||||
});
|
||||
|
||||
if(Prefs.isRootCaptureEnabled(mPrefs)) {
|
||||
Menu navMenu = navView.getMenu();
|
||||
navMenu.findItem(R.id.open_root_log).setVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -345,6 +351,9 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig
|
||||
startActivity(intent);
|
||||
} else
|
||||
Utils.showToast(this, R.string.capture_not_started);
|
||||
} else if(id == R.id.open_root_log) {
|
||||
Intent intent = new Intent(MainActivity.this, LogviewActivity.class);
|
||||
startActivity(intent);
|
||||
} else if (id == R.id.action_donate) {
|
||||
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(DONATE_URL));
|
||||
startActivity(browserIntent);
|
||||
|
||||
@ -145,12 +145,7 @@ public class StatsActivity extends BaseActivity {
|
||||
|
||||
if(id == R.id.copy_to_clipboard) {
|
||||
String contents = Utils.table2Text(mTable);
|
||||
|
||||
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
|
||||
ClipData clip = ClipData.newPlainText(getString(R.string.stats), contents);
|
||||
clipboard.setPrimaryClip(clip);
|
||||
|
||||
Utils.showToast(this, R.string.copied_to_clipboard);
|
||||
Utils.copyToClipboard(this, contents);
|
||||
return true;
|
||||
} else if(id == R.id.share) {
|
||||
String contents = Utils.table2Text(mTable);
|
||||
|
||||
@ -34,8 +34,8 @@ void set_log_level(int lvl) {
|
||||
|
||||
/* ******************************************************* */
|
||||
|
||||
void log_android(int prio, const char *fmt, ...) {
|
||||
if(prio >= loglevel) {
|
||||
void log_android(int lvl, const char *fmt, ...) {
|
||||
if(lvl >= loglevel) {
|
||||
char line[1024];
|
||||
va_list argptr;
|
||||
|
||||
@ -43,10 +43,23 @@ void log_android(int prio, const char *fmt, ...) {
|
||||
vsnprintf(line, sizeof(line), fmt, argptr);
|
||||
va_end(argptr);
|
||||
|
||||
__android_log_print(prio, logtag, "%s", line);
|
||||
__android_log_print(lvl, logtag, "%s", line);
|
||||
|
||||
if(logcallback != NULL)
|
||||
logcallback(prio, line);
|
||||
logcallback(lvl, line);
|
||||
}
|
||||
}
|
||||
|
||||
/* ******************************************************* */
|
||||
|
||||
char loglvl2char(int lvl) {
|
||||
switch (lvl) {
|
||||
case ANDROID_LOG_DEBUG: return 'D';
|
||||
case ANDROID_LOG_INFO: return 'I';
|
||||
case ANDROID_LOG_WARN: return 'W';
|
||||
case ANDROID_LOG_ERROR: return 'E';
|
||||
case ANDROID_LOG_FATAL: return 'F';
|
||||
default: return '?';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -35,10 +35,11 @@ extern void (*logcallback)(int lvl, const char *msg);
|
||||
#define log_e(...) log_android(ANDROID_LOG_ERROR, __VA_ARGS__)
|
||||
#define log_f(...) log_android(ANDROID_LOG_FATAL, __VA_ARGS__)
|
||||
|
||||
void log_android(int prio, const char *fmt, ...);
|
||||
void log_android(int lvl, const char *fmt, ...);
|
||||
ssize_t xwrite(int fd, const void *buf, size_t count);
|
||||
ssize_t xread(int fd, void *buf, size_t count);
|
||||
void tupleSwapPeers(zdtun_5tuple_t *tuple);
|
||||
char loglvl2char(int lvl);
|
||||
|
||||
jclass jniFindClass(JNIEnv *env, const char *name);
|
||||
jmethodID jniGetMethodID(JNIEnv *env, jclass cls, const char *name, const char *signature);
|
||||
|
||||
@ -71,6 +71,8 @@ typedef struct {
|
||||
} pcapd_runtime_t;
|
||||
|
||||
static char errbuf[PCAP_ERRBUF_SIZE];
|
||||
static FILE *logf = NULL;
|
||||
static uint8_t no_logf = 0;
|
||||
|
||||
/* ******************************************************* */
|
||||
|
||||
@ -84,6 +86,35 @@ static uint64_t bytes2mac(const uint8_t *buf) {
|
||||
|
||||
/* ******************************************************* */
|
||||
|
||||
static void log_to_file(int lvl, const char *msg) {
|
||||
char datetime[64];
|
||||
struct tm res;
|
||||
time_t now;
|
||||
|
||||
if(no_logf)
|
||||
return;
|
||||
|
||||
if(logf == NULL) {
|
||||
mode_t old_mask = umask(033);
|
||||
logf = fopen(PCAPD_LOGFILE_PATH, "w");
|
||||
umask(old_mask);
|
||||
|
||||
if(logf == NULL) {
|
||||
log_e("Could not open log file[%d]: %s", errno, strerror(errno));
|
||||
no_logf = 1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
now = time(NULL);
|
||||
strftime(datetime, sizeof(datetime), "%d/%b/%Y %H:%M:%S", localtime_r(&now, &res));
|
||||
|
||||
fprintf(logf, "[%c] %s - %s\n", loglvl2char(lvl), datetime, msg);
|
||||
fflush(logf);
|
||||
}
|
||||
|
||||
/* ******************************************************* */
|
||||
|
||||
static int get_iface_mac(const char *iface, uint64_t *mac) {
|
||||
struct ifreq ifr;
|
||||
int fd;
|
||||
@ -103,7 +134,7 @@ static int get_iface_mac(const char *iface, uint64_t *mac) {
|
||||
|
||||
/* ******************************************************* */
|
||||
|
||||
int get_iface_ip(const char *iface, uint32_t *ip, uint32_t *netmask) {
|
||||
static int get_iface_ip(const char *iface, uint32_t *ip, uint32_t *netmask) {
|
||||
struct ifreq ifr;
|
||||
int fd;
|
||||
int rv;
|
||||
@ -148,6 +179,9 @@ static void sighandler(__unused int signo) {
|
||||
log_i("SIGTERM received, terminating");
|
||||
unlink(PCAPD_PID);
|
||||
|
||||
if(logf)
|
||||
fclose(logf);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
@ -493,7 +527,7 @@ static int run_pcap_dump(int uid_filter) {
|
||||
}
|
||||
|
||||
if(FD_ISSET(rt.client, &fds)) {
|
||||
log_i("client closed");
|
||||
log_i("Client closed");
|
||||
break;
|
||||
}
|
||||
if(FD_ISSET(rt.nlsock, &fds)) {
|
||||
@ -593,6 +627,7 @@ static void usage() {
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
logtag = "pcapd";
|
||||
logcallback = log_to_file;
|
||||
|
||||
if(argc < 2)
|
||||
usage();
|
||||
|
||||
@ -21,6 +21,7 @@
|
||||
#define __PCAPD_H__
|
||||
|
||||
#define PCAPD_SOCKET_PATH "pcapsock"
|
||||
#define PCAPD_LOGFILE_PATH "pcapd.log"
|
||||
#define PCAPD_PID "pcapd.pid"
|
||||
|
||||
#define PCAPD_FLAG_TX (1 << 0)
|
||||
|
||||
5
app/src/main/res/drawable/ic_bug_report.xml
Normal file
5
app/src/main/res/drawable/ic_bug_report.xml
Normal file
@ -0,0 +1,5 @@
|
||||
<vector android:height="24dp" android:tint="#FFFFFF"
|
||||
android:viewportHeight="24" android:viewportWidth="24"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="@android:color/white" android:pathData="M20,8h-2.81c-0.45,-0.78 -1.07,-1.45 -1.82,-1.96L17,4.41 15.59,3l-2.17,2.17C12.96,5.06 12.49,5 12,5c-0.49,0 -0.96,0.06 -1.41,0.17L8.41,3 7,4.41l1.62,1.63C7.88,6.55 7.26,7.22 6.81,8L4,8v2h2.09c-0.05,0.33 -0.09,0.66 -0.09,1v1L4,12v2h2v1c0,0.34 0.04,0.67 0.09,1L4,16v2h2.81c1.04,1.79 2.97,3 5.19,3s4.15,-1.21 5.19,-3L20,18v-2h-2.09c0.05,-0.33 0.09,-0.66 0.09,-1v-1h2v-2h-2v-1c0,-0.34 -0.04,-0.67 -0.09,-1L20,10L20,8zM14,16h-4v-2h4v2zM14,12h-4v-2h4v2z"/>
|
||||
</vector>
|
||||
14
app/src/main/res/layout/logview_activity.xml
Normal file
14
app/src/main/res/layout/logview_activity.xml
Normal file
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ScrollView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/log"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:textIsSelectable="true" />
|
||||
</ScrollView>
|
||||
@ -9,6 +9,11 @@
|
||||
android:id="@+id/action_stats"
|
||||
android:title="@string/stats"
|
||||
android:icon="@drawable/ic_list" />
|
||||
<item
|
||||
android:id="@+id/open_root_log"
|
||||
android:title="@string/root_log"
|
||||
android:icon="@drawable/ic_bug_report"
|
||||
android:visible="false" />
|
||||
</group>
|
||||
<group android:id="@+id/group_social">
|
||||
<item
|
||||
|
||||
@ -129,5 +129,6 @@
|
||||
<string name="root_capture_summary">Capturing packets as root allows PCAPdroid to run with other VPN apps.</string>
|
||||
<string name="donate">Donate</string>
|
||||
<string name="http_request">HTTP Request</string>
|
||||
<string name="root_log">Root Log</string>
|
||||
</resources>
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user