Added debug setting

This commit is contained in:
M66B 2016-01-26 08:03:59 +01:00
parent 3787fc9190
commit 0ab9849552
4 changed files with 36 additions and 18 deletions

View File

@ -249,8 +249,14 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
options.removePreference(screen.findPreference("national_roaming"));
}
if (!(Util.isDebuggable(this) || Util.getSelfVersionName(this).contains("beta")))
if (!(Util.isDebuggable(this) || Util.getSelfVersionName(this).contains("beta"))) {
screen.removePreference(screen.findPreference("category_development"));
SharedPreferences.Editor edit = prefs.edit();
edit.remove("debug");
edit.remove("loglevel");
edit.remove("dns");
edit.apply();
}
}
@Override
@ -453,7 +459,7 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
} else if ("stats_samples".equals(name)) {
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_stats_samples, prefs.getString(name, "90")));
} else if ("loglevel".equals(name))
} else if ("debug".equals(name) || "loglevel".equals(name))
SinkholeService.reload(null, "changed " + name, this);
else if ("dns".equals(name)) {

View File

@ -120,7 +120,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
private native void jni_init();
private native void jni_start(int tun, int[] uids, String hosts, boolean log, boolean filter, int loglevel);
private native void jni_start(int tun, int[] uids, String hosts, boolean log, boolean filter, boolean debug, int loglevel);
private native void jni_stop(int tun, boolean clear);
@ -666,6 +666,8 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
String vpnDns = prefs.getString("dns", sysDns);
Log.i(TAG, "DNS system=" + sysDns + " VPN=" + vpnDns);
try {
if (TextUtils.isEmpty(vpnDns.trim()))
throw new IllegalArgumentException();
InetAddress.getByName(vpnDns);
Log.i(TAG, "DNS using=" + vpnDns);
return vpnDns;
@ -756,10 +758,13 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
boolean filter = prefs.getBoolean("filter", false);
boolean use_hosts = prefs.getBoolean("use_hosts", false);
if (log || filter) {
boolean debug = prefs.getBoolean("debug", false);
int prio = Integer.parseInt(prefs.getString("loglevel", Integer.toString(Log.INFO)));
if (!debug)
prio = Log.WARN;
File hosts = new File(getFilesDir(), "hosts.txt");
String hname = (use_hosts && hosts.exists() ? hosts.getAbsolutePath() : null);
jni_start(vpn.getFd(), getAllowedUids(listAllowed), hname, log, filter, prio);
jni_start(vpn.getFd(), getAllowedUids(listAllowed), hname, log, filter, debug, prio);
}
}

View File

@ -56,6 +56,7 @@ jboolean signaled = 0;
struct udp_session *udp_session = NULL;
struct tcp_session *tcp_session = NULL;
int debug = 0;
int loglevel = 0;
FILE *pcap_file = NULL;
@ -106,11 +107,13 @@ Java_eu_faircode_netguard_SinkholeService_jni_1start(
jint tun, jintArray uids_,
jstring hosts_,
jboolean log, jboolean filter,
jint loglevel_) {
jboolean debug_, jint loglevel_) {
debug = debug_;
loglevel = loglevel_;
log_android(ANDROID_LOG_WARN, "Starting tun=%d log %d filter %d level %d",
tun, log, filter, loglevel_);
log_android(ANDROID_LOG_WARN, "Starting tun=%d log %d filter %d debug %d level %d",
tun, log, filter, debug, loglevel);
// Set blocking
int flags = fcntl(tun, F_GETFL, 0);
@ -1050,23 +1053,23 @@ void handle_ip(const struct arguments *args, const uint8_t *buffer, const size_t
// Handle allowed traffic
int log = 0;
if (allowed) {
if (protocol == IPPROTO_UDP)
if (protocol == IPPROTO_UDP) {
allowed = handle_udp(args, buffer, length, uid);
else if (protocol == IPPROTO_TCP) {
log = (debug || dport != 53);
} else if (protocol == IPPROTO_TCP) {
allowed = handle_tcp(args, buffer, length, uid);
if (!allowed && loglevel < ANDROID_LOG_WARN)
log = (debug || syn);
}
else {
allowed = 0;
log = 1;
}
else
allowed = 0;
}
// Log traffic
if (args->log) {
if (!args->filter || syn || log || protocol != IPPROTO_TCP)
if (args->log && (!args->filter || log))
log_packet(args, version, protocol, flags, source, sport, dest, dport, uid, allowed);
}
}
jboolean handle_udp(const struct arguments *args, const uint8_t *buffer, size_t length, int uid) {
// Check version
@ -1802,7 +1805,6 @@ int write_fin_ack(const struct arguments *args, struct tcp_session *cur, size_t
}
void write_rst(const struct arguments *args, struct tcp_session *cur) {
log_android(ANDROID_LOG_WARN, "Sending RST");
if (write_tcp(args, cur, NULL, 0, 0, 0, 0, 0, 1) < 0)
log_android(ANDROID_LOG_ERROR, "write RST error %d: %s", errno, strerror((errno)));
cur->state = TCP_TIME_WAIT;
@ -1918,7 +1920,7 @@ ssize_t write_udp(const struct arguments *args, const struct udp_session *cur,
#endif
if (res >= 0) {
if (args->log)
if (args->log && (debug || ntohs(cur->dest) != 53))
log_packet(args, cur->version, IPPROTO_UDP, "",
source, ntohs(udp->source), dest, ntohs(udp->dest), cur->uid, 1);

View File

@ -147,8 +147,13 @@
<PreferenceCategory
android:key="category_development"
android:title="Development">
<SwitchPreference
android:defaultValue="false"
android:key="debug"
android:title="Debug" />
<ListPreference
android:defaultValue="4"
android:dependency="debug"
android:entries="@array/logLevelNames"
android:entryValues="@array/logLevelValues"
android:key="loglevel"