Native added on/off switch

This commit is contained in:
M66B 2016-01-18 13:07:00 +01:00
parent d21d75edaf
commit 1102c65507
5 changed files with 39 additions and 25 deletions

View File

@ -8,7 +8,7 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.vending.BILLING" />
<!--uses-permission android:name="android.permission.INTERNET" /-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous -->
<uses-feature

View File

@ -230,11 +230,11 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
options.removePreference(screen.findPreference("national_roaming"));
}
//if (!Util.isDebuggable(this)) {
PreferenceCategory backup = (PreferenceCategory) screen.findPreference("category_backup");
backup.removePreference(screen.findPreference("pcap"));
//}
// Development
if (!Util.isDebuggable(this)) {
PreferenceCategory development = (PreferenceCategory) screen.findPreference("category_development");
screen.removePreference(development);
}
}
@Override
@ -410,7 +410,9 @@ 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 ("native".equals(name))
SinkholeService.reload(null, "setting changed", this);
}
@Override

View File

@ -112,9 +112,9 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
private static final String ACTION_SCREEN_OFF_DELAYED = "eu.faircode.netguard.SCREEN_OFF_DELAYED";
private native void jni_init(int loglevel, String pcap);
private native void jni_init(String pcap);
private native void jni_start(int tun);
private native void jni_start(int tun, int loglevel, boolean native_);
private native void jni_stop(int tun, boolean clear);
@ -247,7 +247,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
if (vpn == null)
throw new IllegalStateException("VPN start failed");
if (prefs.getBoolean("log", false))
jni_start(vpn.getFd());
jni_start(vpn.getFd(), Log.INFO, prefs.getBoolean("native", false));
removeDisabledNotification();
}
break;
@ -276,7 +276,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
}
jni_stop(vpn.getFd(), false);
if (prefs.getBoolean("log", false))
jni_start(vpn.getFd());
jni_start(vpn.getFd(), Log.INFO, prefs.getBoolean("native", false));
if (prev != null)
stopVPN(prev);
break;
@ -858,8 +858,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
File pcap = new File(getCacheDir(), "netguard.pcap");
if (pcap.exists())
pcap.delete();
//jni_init(Log.INFO, pcap.getAbsolutePath());
jni_init(Log.INFO, null);
jni_init(pcap.getAbsolutePath());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);

View File

@ -34,15 +34,14 @@ pthread_t thread_id;
int signaled = 0;
struct session *session = NULL;
int loglevel = ANDROID_LOG_WARN;
char *pcap_fn = NULL;
int loglevel = ANDROID_LOG_WARN;
int native = 0;
// JNI
JNIEXPORT void JNICALL
Java_eu_faircode_netguard_SinkholeService_jni_1init(JNIEnv *env, jobject instance,
jint loglevel_, jstring pcap_) {
loglevel = loglevel_;
Java_eu_faircode_netguard_SinkholeService_jni_1init(JNIEnv *env, jobject instance, jstring pcap_) {
if (pcap_ == NULL)
pcap_fn = NULL;
else {
@ -65,14 +64,19 @@ Java_eu_faircode_netguard_SinkholeService_jni_1init(JNIEnv *env, jobject instanc
pcap_hdr.network = LINKTYPE_RAW;
pcap_write(&pcap_hdr, sizeof(struct pcap_hdr_s));
// TODO limit pcap file size
(*env)->ReleaseStringUTFChars(env, pcap_, pcap);
}
}
JNIEXPORT void JNICALL
Java_eu_faircode_netguard_SinkholeService_jni_1start(JNIEnv *env, jobject instance,
jint tun) {
ng_log(ANDROID_LOG_INFO, "Starting tun=%d", tun);
jint tun, jint loglevel_, jboolean native_) {
loglevel = loglevel_;
native = native_;
ng_log(ANDROID_LOG_INFO, "Starting tun=%d level %d native %d", tun, loglevel, native);
if (pthread_kill(thread_id, 0) == 0)
ng_log(ANDROID_LOG_WARN, "Already running thread %ld", thread_id);
@ -340,7 +344,7 @@ int check_tun(const struct arguments *args, fd_set *rfds, fd_set *wfds, fd_set *
}
else if (length > 0) {
// Write pcap record
if (pcap_fn != NULL) {
if (native && pcap_fn != NULL) {
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts))
ng_log(ANDROID_LOG_ERROR, "clock_gettime error %d: %s",
@ -603,14 +607,13 @@ void handle_ip(const struct arguments *args, const uint8_t *buffer, const uint16
"Packet v%d %s/%u -> %s/%u proto %d flags %s uid %d",
version, source, sport, dest, dport, protocol, flags, uid);
//if (protocol == IPPROTO_TCP)
// handle_tcp(env, instance, args, buffer, length, uid);
if (protocol == IPPROTO_TCP && native)
handle_tcp(args, buffer, length, uid);
// Call back
//if ((protocol == IPPROTO_TCP && syn) || protocol == IPPROTO_UDP) {
JNIEnv *env = args->env;
jobject instance = args->instance;
if (protocol == IPPROTO_TCP || protocol == IPPROTO_UDP) {
if ((protocol == IPPROTO_TCP && (syn || !native)) || protocol == IPPROTO_UDP) {
jclass cls = (*env)->GetObjectClass(env, instance);
jmethodID mid = (*env)->GetMethodID(
env, cls, "logPacket",
@ -1063,7 +1066,7 @@ int write_tcp(const struct session *cur,
int res = write(tun, buffer, len);
// Write pcap record
if (pcap_fn != NULL) {
if (native && pcap_fn != NULL) {
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts))
ng_log(ANDROID_LOG_ERROR, "clock_gettime error %d: %s", errno, strerror(errno));

View File

@ -124,8 +124,18 @@
<Preference
android:key="import"
android:title="@string/setting_import" />
</PreferenceCategory>
<PreferenceCategory
android:key="category_development"
android:title="Development">
<SwitchPreference
android:defaultValue="false"
android:dependency="log"
android:key="native"
android:title="Native code" />
<Preference
android:key="pcap"
android:dependency="native"
android:title="Export PCAP" />
</PreferenceCategory>
<PreferenceCategory