Implemented Log

This commit is contained in:
PeratX 2017-05-25 20:56:27 +08:00
parent 4bfb1cd12b
commit 620933d44c
17 changed files with 258 additions and 35 deletions

View File

@ -19,10 +19,7 @@ import com.google.gson.JsonParseException;
import com.google.gson.stream.JsonReader;
import org.itxtech.daedalus.activity.MainActivity;
import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.Configurations;
import org.itxtech.daedalus.util.DnsServer;
import org.itxtech.daedalus.util.Rule;
import org.itxtech.daedalus.util.RulesResolver;
import org.itxtech.daedalus.util.*;
import java.io.File;
import java.util.ArrayList;
@ -97,21 +94,22 @@ public class Daedalus extends Application {
private static Daedalus instance = null;
private SharedPreferences prefs;
private Thread mHostsResolver;
private Thread mResolver;
@Override
public void onCreate() {
super.onCreate();
mHostsResolver = new Thread(new RulesResolver());
mHostsResolver.start();
Logger.init();
mResolver = new Thread(new RulesResolver());
mResolver.start();
if (getExternalFilesDir(null) != null) {
rulesPath = getExternalFilesDir(null).getPath() + "/rules/";
configPath = getExternalFilesDir(null).getPath() + "/config.json";
File file = new File(rulesPath);
Log.d(TAG, "mkdir result: " + file.mkdirs());
}
initData();
@ -172,9 +170,10 @@ public class Daedalus extends Application {
instance = null;
prefs = null;
RulesResolver.shutdown();
mHostsResolver.interrupt();
mResolver.interrupt();
RulesResolver.clean();
mHostsResolver = null;
mResolver = null;
Logger.shutdown();
}
public Intent getServiceIntent() {

View File

@ -56,6 +56,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
public static final int FRAGMENT_ABOUT = 3;
public static final int FRAGMENT_RULES = 4;
public static final int FRAGMENT_DNS_SERVERS = 5;
public static final int FRAGMENT_LOG = 6;
private static MainActivity instance = null;
@ -223,6 +224,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
break;
case FRAGMENT_SETTINGS:
switchFragment(new SettingsFragment());
break;
case FRAGMENT_LOG:
switchFragment(new LogFragment());
break;
}
if (currentFragment == null) {
switchFragment(new MainFragment());
@ -256,6 +261,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
case R.id.nav_settings:
switchFragment(new SettingsFragment());
break;
case R.id.nav_log:
switchFragment(new LogFragment());
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main_drawer_layout);

View File

@ -0,0 +1,65 @@
package org.itxtech.daedalus.fragment;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.util.Logger;
/**
* Daedalus Project
*
* @author iTX Technologies
* @link https://itxtech.org
* <p>
* This program 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.
*/
public class LogFragment extends ToolbarFragment implements Toolbar.OnMenuItemClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_log, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
refresh();
}
private void refresh() {
((TextView) getView().findViewById(R.id.textView_log)).setText(Logger.getLog());
}
@Override
public void checkStatus() {
menu.findItem(R.id.nav_log).setChecked(true);
toolbar.setTitle(R.string.action_log);
toolbar.inflateMenu(R.menu.log);
toolbar.setOnMenuItemClickListener(this);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_delete:
Logger.init();
refresh();
break;
case R.id.action_refresh:
refresh();
break;
}
return true;
}
}

View File

@ -34,7 +34,6 @@ import java.util.ArrayList;
* (at your option) any later version.
*/
public class RulesFragment extends ToolbarFragment implements Toolbar.OnMenuItemClickListener {
private View view = null;
private RuleAdapter adapter;
private Rule rule = null;
private int currentType;
@ -49,7 +48,7 @@ public class RulesFragment extends ToolbarFragment implements Toolbar.OnMenuItem
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_rules, container, false);
View view = inflater.inflate(R.layout.fragment_rules, container, false);
currentType = Daedalus.configurations.getUsingRuleType();
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_rules);
@ -63,7 +62,7 @@ public class RulesFragment extends ToolbarFragment implements Toolbar.OnMenuItem
if (viewHolder instanceof RulesFragment.ViewHolder) {
Rule rule = Rule.getRuleById(((ViewHolder) viewHolder).getId());
if (rule != null && rule.isServiceAndUsing()) {
Snackbar.make(view, R.string.notice_after_stop, Snackbar.LENGTH_LONG)
Snackbar.make(getView(), R.string.notice_after_stop, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
return 0;
}
@ -81,7 +80,7 @@ public class RulesFragment extends ToolbarFragment implements Toolbar.OnMenuItem
int position = viewHolder.getAdapterPosition();
rule = getRules().get(position);
getRules().remove(position);
Snackbar.make(view, R.string.action_removed, Snackbar.LENGTH_LONG)
Snackbar.make(getView(), R.string.action_removed, Snackbar.LENGTH_LONG)
.setAction(R.string.action_undo, new SnackbarClickListener(position)).show();
adapter.notifyItemRemoved(position);
}
@ -131,7 +130,6 @@ public class RulesFragment extends ToolbarFragment implements Toolbar.OnMenuItem
super.onDestroyView();
Daedalus.configurations.save();
view = null;
adapter = null;
rule = null;
}

View File

@ -10,6 +10,7 @@ import android.system.StructPollfd;
import android.util.Log;
import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.DnsServerHelper;
import org.itxtech.daedalus.util.Logger;
import org.pcap4j.packet.IpPacket;
import java.io.*;
@ -116,7 +117,7 @@ public class TcpDnsProvider extends UdpDnsProvider {
service.providerLoopCallback();
}
} catch (Exception e) {
e.printStackTrace();
Logger.logException(e);
}
}
@ -138,7 +139,7 @@ public class TcpDnsProvider extends UdpDnsProvider {
SocketAddress address = new InetSocketAddress(outPacket.getAddress(), DnsServerHelper.getPortOrDefault(outPacket.getAddress(), outPacket.getPort()));
dnsSocket.connect(address, 5000);
dnsSocket.setSoTimeout(5000);
Log.d(TAG, "Sending");
Logger.info("TcpDnsProvider: Sending DNS query request");
DataOutputStream dos = new DataOutputStream(dnsSocket.getOutputStream());
byte[] packet = processUdpPacket(outPacket, parsedPacket);
dos.writeShort(packet.length);

View File

@ -14,6 +14,7 @@ import de.measite.minidns.record.A;
import de.measite.minidns.util.InetAddressUtil;
import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.DnsServerHelper;
import org.itxtech.daedalus.util.Logger;
import org.itxtech.daedalus.util.RulesResolver;
import org.pcap4j.packet.*;
import org.pcap4j.packet.factory.PacketFactoryPropertiesLoader;
@ -77,7 +78,6 @@ public class UdpDnsProvider extends DnsProvider {
private void queueDeviceWrite(IpPacket ipOutPacket) {
dnsQueryTimes++;
Log.i(TAG, "QT " + dnsQueryTimes);
deviceWrites.add(ipOutPacket.getRawData());
}
@ -343,7 +343,7 @@ public class UdpDnsProvider extends DnsProvider {
try {
String response;
if ((response = RulesResolver.resolve(dnsQueryName)) != null) {
Log.i(TAG, "handleDnsRequest: DNS Name " + dnsQueryName + " address " + response + ", using local hosts to resolve.");
Logger.info("DnsProvider: Resolved " + dnsQueryName + ". Local resolver response: " + response);
DNSMessage.Builder builder = dnsMsg.asBuilder();
int[] ip = new int[4];
byte i = 0;
@ -354,13 +354,13 @@ public class UdpDnsProvider extends DnsProvider {
builder.addAnswer(new Record<>(dnsQueryName, Record.TYPE.getType(A.class), 1, 64, new A(ip[0], ip[1], ip[2], ip[3])));
handleDnsResponse(parsedPacket, builder.build().toArray());
} else {
Log.i(TAG, "handleDnsRequest: DNS Name " + dnsQueryName + " , sending to " + destAddr);
Logger.info("DnsProvider: Resolving " + dnsQueryName + ". Sending to " + destAddr);
DatagramPacket outPacket = new DatagramPacket(dnsRawData, 0, dnsRawData.length, destAddr,
DnsServerHelper.getPortOrDefault(destAddr, parsedUdp.getHeader().getDstPort().valueAsInt()));
forwardPacket(outPacket, parsedPacket);
}
} catch (Exception e) {
Log.e(TAG, e.toString());
Logger.logException(e);
}
}

View File

@ -20,6 +20,7 @@ import org.itxtech.daedalus.provider.TcpDnsProvider;
import org.itxtech.daedalus.provider.UdpDnsProvider;
import org.itxtech.daedalus.receiver.StatusBarBroadcastReceiver;
import org.itxtech.daedalus.util.DnsServerHelper;
import org.itxtech.daedalus.util.Logger;
import org.itxtech.daedalus.util.RulesResolver;
import java.net.Inet4Address;
@ -151,10 +152,14 @@ public class DaedalusVpnService extends VpnService implements Runnable {
}
dnsServers = null;
} catch (Exception e) {
Log.d(TAG, e.toString());
Logger.logException(e);
}
stopSelf();
if (shouldRefresh) {
Logger.info("Daedalus VPN service has stopped");
}
if (shouldRefresh && MainActivity.getInstance() != null && Daedalus.getInstance().isAppOnForeground()) {
MainActivity.getInstance().startActivity(new Intent(getApplicationContext(), MainActivity.class)
.putExtra(MainActivity.LAUNCH_ACTION, MainActivity.LAUNCH_ACTION_AFTER_DEACTIVATE));
@ -191,7 +196,6 @@ public class DaedalusVpnService extends VpnService implements Runnable {
boolean advanced = Daedalus.getPrefs().getBoolean("settings_advanced_switch", false);
statisticQuery = Daedalus.getPrefs().getBoolean("settings_count_query_times", false);
Log.d(TAG, "tun0 add " + format + " pServ " + primaryServer + " sServ " + secondaryServer);
String aliasPrimary;
String aliasSecondary;
@ -208,7 +212,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
Inet4Address primaryDNSServer = InetAddressUtil.ipv4From(aliasPrimary);
Inet4Address secondaryDNSServer = InetAddressUtil.ipv4From(aliasSecondary);
Log.d(TAG, "listening on " + format + " pServ " + primaryDNSServer.getHostAddress() + " sServ " + secondaryDNSServer.getHostAddress());
Logger.info("Daedalus VPN service is listening on " + primaryDNSServer.getHostAddress() + " and " + secondaryDNSServer.getHostAddress());
builder.setSession("Daedalus")
.addDnsServer(primaryDNSServer)
.addDnsServer(secondaryDNSServer)
@ -223,6 +227,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
}
descriptor = builder.establish();
Logger.info("Daedalus VPN service is started");
if (advanced) {
if (Daedalus.getPrefs().getBoolean("settings_dns_over_tcp", false)) {
@ -238,7 +243,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
}
}
} catch (Exception e) {
Log.d(TAG, e.toString());
Logger.logException(e);
} finally {
Log.d(TAG, "quit");
stopThread();

View File

@ -0,0 +1,84 @@
package org.itxtech.daedalus.util;
import android.util.Log;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Daedalus Project
*
* @author iTX Technologies
* @link https://itxtech.org
* <p>
* This program 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.
*/
public class Logger {
private static StringBuilder builder;
public static void init() {
builder = new StringBuilder();
}
public static void shutdown() {
builder = null;
}
public static String getLog() {
return builder.toString();
}
public static void emergency(String message) {
send("[EMERGENCY] " + message);
}
public static void alert(String message) {
send("[ALERT] " + message);
}
public static void critical(String message) {
send("[CRITICAL] " + message);
}
public static void error(String message) {
send("[ERROR] " + message);
}
public static void warning(String message) {
send("[WARNING] " + message);
}
public static void notice(String message) {
send("[NOTICE] " + message);
}
public static void info(String message) {
send("[INFO] " + message);
}
public static void debug(String message) {
send("[DEBUG] " + message);
}
public static void logException(Throwable e) {
alert(getExceptionMessage(e));
}
private static String getExceptionMessage(Throwable e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
return stringWriter.toString();
}
private static void send(String message) {
String fileDateFormat = new SimpleDateFormat("Y-M-d HH:mm:ss ").format(new Date());
builder.insert(0, "\n").insert(0, message).insert(0, fileDateFormat);
Log.d("Daedalus", message);
}
}

View File

@ -48,19 +48,13 @@ public class RulesResolver implements Runnable {
shutdown = true;
}
public static boolean isLoaded() {
return status == STATUS_LOADED;
}
public static void startLoadHosts(String[] loadFile) {
Log.d(TAG, "Loading hosts file " + loadFile.length);
hostsFiles = loadFile;
mode = MODE_HOSTS;
status = STATUS_PENDING_LOAD;
}
public static void startLoadDnsmasq(String[] loadPath) {
Log.d(TAG, "Loading DNSMasq file " + loadPath.length);
dnsmasqFiles = loadPath;
mode = MODE_DNSMASQ;
status = STATUS_PENDING_LOAD;
@ -104,32 +98,36 @@ public class RulesResolver implements Runnable {
for (String hostsFile : hostsFiles) {
File file = new File(hostsFile);
if (file.canRead()) {
Logger.info("Loading hosts " + file.toString());
FileInputStream stream = new FileInputStream(file);
BufferedReader dataIO = new BufferedReader(new InputStreamReader(stream));
String strLine;
String[] data;
int count = 0;
while ((strLine = dataIO.readLine()) != null) {
//Log.d(TAG, strLine);
if (!strLine.equals("") && !strLine.startsWith("#")) {
data = strLine.split("\\s+");
rules.put(data[1], data[0]);
Log.d(TAG, "Putting " + data[0] + " " + data[1]);
count++;
}
}
dataIO.close();
stream.close();
Logger.info("Loaded " + String.valueOf(count) + " rules");
}
}
} else if (mode == MODE_DNSMASQ) {
for (String dnsmasqFile : dnsmasqFiles) {
File file = new File(dnsmasqFile);
if (file.canRead()) {
Log.d(TAG, "load: Loading DNSMasq configuration " + file.toString());
Logger.info("Loading DNSMasq configuration " + file.toString());
FileInputStream stream = new FileInputStream(file);
BufferedReader dataIO = new BufferedReader(new InputStreamReader(stream));
String strLine;
String[] data;
int count = 0;
while ((strLine = dataIO.readLine()) != null) {
if (!strLine.equals("") && !strLine.startsWith("#")) {
data = strLine.split("/");
@ -138,13 +136,15 @@ public class RulesResolver implements Runnable {
data[1] = data[1].substring(1, data[1].length());
}
rules.put(data[1], data[2]);
//Log.d(TAG, "Putting " + data[1] + " " + data[2]);
count++;
}
}
}
dataIO.close();
stream.close();
Logger.info("Loaded " + String.valueOf(count) + " rules");
}
}
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19,3h-4.18C14.4,1.84 13.3,1 12,1c-1.3,0 -2.4,0.84 -2.82,2L5,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2zM12,3c0.55,0 1,0.45 1,1s-0.45,1 -1,1 -1,-0.45 -1,-1 0.45,-1 1,-1zM14,17L7,17v-2h7v2zM17,13L7,13v-2h10v2zM17,9L7,9L7,7h10v2z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M16,1L4,1c-1.1,0 -2,0.9 -2,2v14h2L4,3h12L16,1zM19,5L8,5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h11c1.1,0 2,-0.9 2,-2L21,7c0,-1.1 -0.9,-2 -2,-2zM19,21L8,21L8,7h11v14z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"/>
</vector>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_log"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView_log"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.CoordinatorLayout>

View File

@ -28,6 +28,9 @@
<group android:checkableBehavior="single"
android:id="@+id/nav_group_more">
<item android:id="@+id/nav_log"
android:title="@string/action_log"
android:icon="@drawable/ic_assignment"/>
<item android:id="@+id/nav_settings"
android:title="@string/action_settings"
android:icon="@drawable/ic_settings"/>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_delete"
android:title="@string/delete"
android:icon="@drawable/ic_delete"
android:alphabeticShortcut="d"
android:numericShortcut="1"
app:showAsAction="always"/>
<item android:id="@+id/action_refresh"
android:title="@string/action_refresh"
android:icon="@drawable/ic_refresh"
android:alphabeticShortcut="r"
android:numericShortcut="2"
app:showAsAction="always"/>
</menu>

View File

@ -73,4 +73,6 @@
<string name="notice_after_stop">请在服务停止后更改启用的规则。</string>
<string name="settings_manual">使用手册</string>
<string name="settings_manual_summary">访问 GitHub wiki 页面。</string>
<string name="action_log">日志</string>
<string name="action_refresh">刷新</string>
</resources>

View File

@ -72,4 +72,6 @@
<string name="notice_after_stop">Please modify activated rules after stopping service.</string>
<string name="settings_manual">Manual</string>
<string name="settings_manual_summary">Visit GitHub wiki page.</string>
<string name="action_log">Log</string>
<string name="action_refresh">Refresh</string>
</resources>