Browse Source

cleaned up adv prefs. also hooked prefs up in code

Fabio Arnold 9 years ago
parent
commit
29b16718ff

+ 6 - 6
res/values/strings_preferences.xml

@@ -17,13 +17,13 @@
 	<string name="pref_upload_server">Server address</string>	
 	<string name="pref_connection_settings">Connection Settings</string>
 	<string name="pref_max_connections">Max Connections</string>	
-	<string name="pref_max_connections_default">5</string>	
+	<integer name="pref_max_connections_default">5</integer>
 	<string name="pref_timeout">Socket Timeout</string>
-	<string name="pref_timeout_summary">Socket timeout in seconds</string>
-	<string name="pref_timeout_default">30</string>	
-	<string name="pref_sleeptime">Stream sleep time</string>
-	<string name="pref_sleeptime_summary">Time in ms for the stream sleep time</string>
-	<string name="pref_sleeptime_default">500</string>
+	<string name="pref_timeout_summary">After this many seconds close socket when no communication occurred</string>
+	<integer name="pref_timeout_default">30</integer>
+	<string name="pref_portscan_timeout">Portscan Timeout</string>
+	<string name="pref_portscan_timeout_summary">Should match the maximum duration of a portscan in seconds</string>
+	<integer name="pref_portscan_timeout_default">60</integer>
 	<string name="pref_location_settings">Location Settings</string>
 	<string name="pref_location_time">Location Data Timeout</string>
 	<string name="pref_location_time_summary">Timeout when requesting location data in ms</string>

+ 0 - 76
res/xml/preferences.xml

@@ -1,76 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
-
-    <PreferenceCategory android:title="@string/pref_storage" >
-        <CheckBoxPreference
-            android:defaultValue="false"
-            android:key="pref_external_storage"
-            android:summary="@string/pref_external_storage_summ"
-            android:title="@string/pref_external_storage_title" />
-        
-        <EditTextPreference
-            android:key="pref_external_location"
-            android:dependency="pref_external_storage"
-            android:defaultValue="/HosTaGe/LogFiles/"
-            android:title="@string/pref_external_location_title"
-            />
-        
-    </PreferenceCategory>
-    <PreferenceCategory android:title="@string/pref_upload" >
-         <CheckBoxPreference
-            android:defaultValue="false"
-            android:key="pref_auto_synchronize"
-            android:summary="@string/pref_auto_synchronize_summ"
-            android:title="@string/pref_auto_synchronize_title" />
-                
-        <EditTextPreference
-            android:key="pref_upload_server"
-            android:defaultValue="https://ssi.cased.de"
-            android:title="@string/pref_upload_server" />
-        
-    </PreferenceCategory>
-    <PreferenceCategory android:title="@string/pref_notification" >
-        <CheckBoxPreference
-            android:defaultValue="true"
-            android:key="pref_vibration"
-            android:summary="@string/pref_vibration_summ"
-            android:title="@string/pref_vibration" />
-
-        <RingtonePreference
-            android:defaultValue="content://settings/system/notification_sound"
-            android:key="pref_notification_sound"
-            android:ringtoneType="notification"
-            android:showDefault="true"
-            android:showSilent="true"
-            android:summary="@string/pref_alarm_summ"
-            android:title="@string/pref_alarm" />
-    </PreferenceCategory>
-    <PreferenceCategory android:title="@string/pref_connection_settings" >
-        <EditTextPreference
-            android:key="pref_max_connections"
-            android:defaultValue="@string/pref_max_connections_default"
-            android:title="@string/pref_max_connections" />
-
-          <EditTextPreference
-            android:key="pref_timeout"
-            android:defaultValue="@string/pref_timeout_default"
-            android:title="@string/pref_timeout" />
-
-          <EditTextPreference
-            android:key="pref_sleeptime"
-            android:defaultValue="@string/pref_sleeptime_default"
-            android:title="@string/pref_sleeptime" />
-    </PreferenceCategory>
-    <PreferenceCategory android:title="@string/pref_location_settings" >
-        <EditTextPreference
-            android:key="pref_location_time"
-            android:defaultValue="@string/pref_location_time_default"
-            android:title="@string/pref_location_time" />
-
-        <EditTextPreference
-            android:key="pref_location_retries"
-            android:defaultValue="@string/pref_location_retries_default"
-            android:title="@string/pref_location_retries" />
-
-    </PreferenceCategory>
-</PreferenceScreen>

+ 24 - 10
res/xml/settings_preferences.xml

@@ -37,10 +37,10 @@
 				android:defaultValue="https://ssi.cased.de"
 				android:title="@string/pref_upload_server" />
 
-        <EditTextPreference
+        <!--<EditTextPreference
             android:key="pref_download_server"
             android:defaultValue="http://ssi.cased.de/api"
-            android:title="@string/pref_download_server" />
+            android:title="@string/pref_download_server" />-->
 	</PreferenceCategory>
 
 	<PreferenceCategory android:title="@string/advanced_settings">
@@ -50,21 +50,35 @@
 				android:persistent="false">
 			<PreferenceCategory android:title="@string/pref_connection_settings" >
 				<EditTextPreference
+						android:inputType="number"
+						android:numeric="decimal"
 						android:key="pref_max_connections"
-						android:defaultValue="@string/pref_max_connections_default"
+						android:defaultValue="@integer/pref_max_connections_default"
 						android:title="@string/pref_max_connections" />
 
 				<EditTextPreference
+						android:inputType="number"
+						android:numeric="decimal"
 						android:key="pref_timeout"
-						android:defaultValue="@string/pref_timeout_default"
-						android:summary="@string/pref_timeout_summary"
-						android:title="@string/pref_timeout" />
+						android:defaultValue="@integer/pref_timeout_default"
+						android:title="@string/pref_timeout"
+						android:summary="@string/pref_timeout_summary" />
 
-				<EditTextPreference
+				<!--<EditTextPreference
+						android:inputType="number"
+						android:numeric="decimal"
 						android:key="pref_sleeptime"
-						android:defaultValue="@string/pref_sleeptime_default"
-						android:summary="@string/pref_sleeptime_summary"
-						android:title="@string/pref_sleeptime" />
+						android:defaultValue="@integer/pref_sleeptime_default"
+						android:title="@string/pref_sleeptime"
+						android:summary="@string/pref_sleeptime_summary" />-->
+
+				<EditTextPreference
+						android:inputType="number"
+						android:numeric="decimal"
+						android:key="pref_portscan_timeout"
+						android:defaultValue="@integer/pref_portscan_timeout_default"
+						android:title="@string/pref_portscan_timeout"
+						android:summary="@string/pref_portscan_timeout_summary" />
 			</PreferenceCategory>
             <PreferenceCategory android:title="@string/pref_location_settings" >
                 <EditTextPreference

+ 16 - 7
src/de/tudarmstadt/informatik/hostage/ConnectionGuard.java

@@ -1,5 +1,7 @@
 package de.tudarmstadt.informatik.hostage;
 
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
 import android.util.Log;
 
 /**
@@ -14,16 +16,23 @@ public class ConnectionGuard {
 	private ConnectionGuard() {
 	}
 
-	/**
-	 * Intervall between 2 connection in wich we assume a port scan
-	 */
-	public final static long TIMESTAMP_THRESHOLD_MS = 60000;
-
 	private static long lastConnectionTimestamp = 0;
 	private static long lastPortscanTimestamp = 0;
 	private static String lastIP = "";
 	private static int lastPort = 0;
 
+	private static long getPortscanTimeout() {
+		// this might be called in a time critical context so maybe
+		// we don't want to do this each time and instead just once
+		SharedPreferences defaultPref = PreferenceManager
+				.getDefaultSharedPreferences(Hostage.getContext());
+		// the pref value is in seconds
+		int portscanTimeoutS = Integer.parseInt(defaultPref.getString("pref_timeout", "60"));
+		long portscanTimeoutMS = 1000 * portscanTimeoutS;
+
+		return portscanTimeoutMS;
+	}
+
 	/**
 	 * Register a connection for port scan detection. Stores information about the last connection.
 	 * @param port The local port used for communication.
@@ -44,7 +53,7 @@ public class ConnectionGuard {
 	}
 
 	public synchronized static boolean portscanInProgress() {
-		return (System.currentTimeMillis() - lastPortscanTimestamp) < TIMESTAMP_THRESHOLD_MS;
+		return (System.currentTimeMillis() - lastPortscanTimestamp) < getPortscanTimeout();
 	}
 	
 	/**
@@ -58,7 +67,7 @@ public class ConnectionGuard {
 		Log.i("Alte Werte:", "LastTime: " + lastConnectionTimestamp + " ,LastIP: " + lastIP + ", lastPort:" + port);
 		Log.i("Alte Werte:", "Time: " + timestamp + " ,IP: " + ip + ", Port:" + port);
 		boolean result = false;
-		boolean belowThreshold = ((timestamp - lastConnectionTimestamp) < TIMESTAMP_THRESHOLD_MS);
+		boolean belowThreshold = ((timestamp - lastConnectionTimestamp) < getPortscanTimeout());
 		boolean sameIP = (lastIP.equals(ip));
 		boolean samePort = (lastPort == port);
 		if (sameIP && belowThreshold && !samePort) {

+ 1 - 1
src/de/tudarmstadt/informatik/hostage/ConnectionRegister.java

@@ -51,7 +51,7 @@ public class ConnectionRegister {
 	public int getMaxConnections() {
 		SharedPreferences defaultPref = PreferenceManager
 				.getDefaultSharedPreferences(context);
-		return defaultPref.getInt("max_connections", 5);
+		return Integer.parseInt(defaultPref.getString("pref_max_connections", "5"));
 	}
 
 	/**

+ 4 - 0
src/de/tudarmstadt/informatik/hostage/Hostage.java

@@ -467,6 +467,10 @@ public class Hostage extends Service {
 	 * Creates a Notification in the notification bar.
 	 */
 	private void createNotification() {
+		if (MainActivity.getInstance() == null) {
+			return; // prevent NullPointerException
+		}
+
 		HostageDBOpenHelper dbh = new HostageDBOpenHelper(this);
 		boolean activeHandlers = false;
 		boolean bssidSeen = false;

+ 1 - 1
src/de/tudarmstadt/informatik/hostage/protocol/SMB.java

@@ -112,7 +112,7 @@ public class SMB implements Protocol {
 	private int initMaxPackets() {
 		int maxPackets;
 		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Hostage.getContext());
-		maxPackets = prefs.getInt("timeout", 30) / 5;
+		maxPackets = Integer.parseInt(prefs.getString("pref_timeout", "30")) / 5;
 		return maxPackets;
 	}
 

+ 6 - 2
src/de/tudarmstadt/informatik/hostage/ui/fragment/PreferenceHostageFragment.java

@@ -43,7 +43,8 @@ public class PreferenceHostageFragment extends PreferenceFragment implements Sha
 				"pref_timeout",
 				"pref_sleeptime",
 				"pref_location_time",
-				"pref_location_retries"
+				"pref_location_retries",
+				"pref_portscan_timeout"
 		};
 
 		// map the text preferences to suffixes
@@ -51,6 +52,7 @@ public class PreferenceHostageFragment extends PreferenceFragment implements Sha
 		this.mSuffixMap.put("pref_timeout", "s");
 		this.mSuffixMap.put("pref_sleeptime", "ms");
 		this.mSuffixMap.put("pref_location_time", "ms");
+		this.mSuffixMap.put("pref_portscan_timeout", "s");
 
 		addPreferencesFromResource(R.xml.settings_preferences);
 
@@ -74,7 +76,9 @@ public class PreferenceHostageFragment extends PreferenceFragment implements Sha
 				suffix = this.mSuffixMap.get(key);
 			}
 
-			p.setSummary(sharedPreferences.getString(key, "") + " " + suffix);
+			if (p.getSummary() == null) {
+				p.setSummary(sharedPreferences.getString(key, "") + " " + suffix);
+			}
 		}
 	}