Added option to enable/disable domain blocking

Thanks @rgarza

Fixes #241
Closes #245
This commit is contained in:
M66B 2016-01-23 21:30:54 +01:00
parent db71907dff
commit 47be2c367e
5 changed files with 40 additions and 25 deletions

View File

@ -179,9 +179,14 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
// Handle hosts
Preference pref_hosts = screen.findPreference("hosts");
Preference pref_block_domains = screen.findPreference("use_hosts");
if (Util.isPlayStoreInstall(this)) {
PreferenceCategory pref_backup = (PreferenceCategory) screen.findPreference("category_backup");
pref_backup.removePreference(pref_hosts);
PreferenceCategory pref_category = (PreferenceCategory) screen.findPreference("category_options");
pref_category.removePreference(pref_block_domains);
} else {
pref_hosts.setEnabled(getIntentOpenHosts().resolveActivity(getPackageManager()) != null);
pref_hosts.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@ -240,7 +245,7 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
options.removePreference(screen.findPreference("national_roaming"));
}
if (!Util.getSelfVersionName(this).endsWith("beta"))
if (!(Util.isDebuggable(this) || Util.getSelfVersionName(this).endsWith("beta")))
screen.removePreference(screen.findPreference("category_development"));
}
@ -388,6 +393,9 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
.create().show();
}
} else if ("use_hosts".equals(name)) {
SinkholeService.reload(null, "setting changed", this);
} else if ("auto_enable".equals(name))
getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_auto, prefs.getString(name, "0")));

View File

@ -325,15 +325,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
if (vpn == null)
throw new IllegalStateException("VPN start failed");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
boolean log = prefs.getBoolean("log", false);
boolean filter = prefs.getBoolean("filter", false);
if (log || filter) {
int prio = Integer.parseInt(prefs.getString("loglevel", Integer.toString(Log.INFO)));
File hosts = new File(getCacheDir(), "hosts.txt");
String hname = (hosts.exists() ? hosts.getAbsolutePath() : null);
jni_start(vpn.getFd(), getAllowedUids(listAllowed), hname, log, filter, prio);
}
startNative(listAllowed);
removeWarningNotifications();
updateEnforcingNotification(listAllowed.size(), listRule.size());
@ -368,15 +360,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
// TODO drain old VPN
jni_stop(vpn.getFd(), false);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
boolean log = prefs.getBoolean("log", false);
boolean filter = prefs.getBoolean("filter", false);
if (log || filter) {
int prio = Integer.parseInt(prefs.getString("loglevel", Integer.toString(Log.INFO)));
File hosts = new File(getCacheDir(), "hosts.txt");
String hname = (hosts.exists() ? hosts.getAbsolutePath() : null);
jni_start(vpn.getFd(), getAllowedUids(listAllowed), hname, log, filter, prio);
}
startNative(listAllowed);
if (prev != null)
stopVPN(prev);
@ -717,6 +701,19 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
}
}
private void startNative(List<Rule> listAllowed) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
boolean log = prefs.getBoolean("log", false);
boolean filter = prefs.getBoolean("filter", false);
boolean use_hosts = prefs.getBoolean("use_hosts", false);
if (log || filter) {
int prio = Integer.parseInt(prefs.getString("loglevel", Integer.toString(Log.INFO)));
File hosts = new File(getCacheDir(), "hosts.txt");
String hname = (use_hosts && hosts.exists() ? hosts.getAbsolutePath() : null);
jni_start(vpn.getFd(), getAllowedUids(listAllowed), hname, log, filter, prio);
}
}
private List<Rule> getAllowedRules(List<Rule> listRule) {
List<Rule> listAllowed = new ArrayList<>();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

View File

@ -106,7 +106,7 @@ Java_eu_faircode_netguard_SinkholeService_jni_1start(
jint loglevel_) {
loglevel = loglevel_;
log_android(ANDROID_LOG_INFO, "Starting tun=%d log %d filter %d level %d",
log_android(ANDROID_LOG_WARN, "Starting tun=%d log %d filter %d level %d",
tun, log, filter, loglevel_);
// Set blocking
@ -139,9 +139,10 @@ Java_eu_faircode_netguard_SinkholeService_jni_1start(
if (hosts_ == NULL) {
args->hcount = 0;
args->hosts = NULL;
log_android(ANDROID_LOG_WARN, "No hosts file");
} else {
const char *hosts = (*env)->GetStringUTFChars(env, hosts_, 0);
log_android(ANDROID_LOG_INFO, "hosts file %s", hosts);
log_android(ANDROID_LOG_WARN, "hosts file %s", hosts);
read_hosts(hosts, args);
(*env)->ReleaseStringUTFChars(env, hosts_, hosts);
}
@ -161,7 +162,7 @@ Java_eu_faircode_netguard_SinkholeService_jni_1start(
JNIEXPORT void JNICALL
Java_eu_faircode_netguard_SinkholeService_jni_1stop(JNIEnv *env, jobject instance,
jint tun, jboolean clear) {
log_android(ANDROID_LOG_INFO, "Stop tun %d clear %d", tun, (int) clear);
log_android(ANDROID_LOG_WARN, "Stop tun %d clear %d", tun, (int) clear);
if (pthread_kill(thread_id, 0) == 0) {
stopping = 1;
log_android(ANDROID_LOG_DEBUG, "Kill thread %lu", thread_id);
@ -302,7 +303,7 @@ void *handle_events(void *a) {
log_android(ANDROID_LOG_WARN, "pselect signaled");
break;
} else {
log_android(ANDROID_LOG_WARN, "pselect interrupted");
log_android(ANDROID_LOG_DEBUG, "pselect interrupted");
continue;
}
} else {
@ -384,7 +385,7 @@ void *handle_events(void *a) {
free(args->hosts);
free(args);
log_android(ANDROID_LOG_INFO, "Stopped events tun=%d thread %lu", args->tun, thread_id);
log_android(ANDROID_LOG_WARN, "Stopped events tun=%d thread %lu", args->tun, thread_id);
return NULL;
}
@ -2062,7 +2063,7 @@ char *trim(char *str) {
}
void read_hosts(const char *name, struct arguments *args) {
log_android(ANDROID_LOG_WARN, "Reading %s", name);
log_android(ANDROID_LOG_INFO, "Reading %s", name);
args->hcount = 0;
args->hosts = NULL;

View File

@ -43,6 +43,8 @@ These issues are caused by bugs in Android, or in the software provided by the m
<string name="setting_tethering">Allow tethering</string>
<string name="setting_log">Log traffic</string>
<string name="setting_filter">Filter traffic</string>
<string name="setting_block_domains">Block domain names</string>
<string name="setting_auto">Auto enable after %1$s minutes</string>
<string name="setting_delay">Delay screen off %1$s minutes</string>
<string name="setting_theme">Theme: %1$s</string>
@ -71,6 +73,7 @@ These issues are caused by bugs in Android, or in the software provided by the m
<string name="summary_system">Define rules for system applications (for experts)</string>
<string name="summary_log">Log addresses of IP packets going into the VPN sinkhole. This might result in extra battery usage.</string>
<string name="summary_filter">Filter IP packets going out of the VPN sinkhole. This might result in extra battery usage.</string>
<string name="summary_block_domains">Redirect blocked domain names to local device</string>
<string name="summary_stats">Show network speed graph in status bar notification</string>
<string name="summary_auto">After disabling using the widget, automatically enable NetGuard again after the selected number of minutes (enter zero to disable this option)</string>
<string name="summary_delay">After turning the screen off, keep screen on rules active for the selected number of minutes (enter zero to disable this option)</string>

View File

@ -46,6 +46,12 @@
android:key="filter"
android:summary="@string/summary_filter"
android:title="@string/setting_filter" />
<SwitchPreference
android:defaultValue="true"
android:dependency="filter"
android:key="use_hosts"
android:summary="@string/summary_block_domains"
android:title="@string/setting_block_domains" />
<EditTextPreference
android:defaultValue="0"
android:inputType="number"