Browse Source

Made ConnectionRegister instantiable
Added setting for connections per port
Added setting for sockettimeout

lp-tu 10 years ago
parent
commit
b754e00b43

+ 0 - 15
res/values/preference_array.xml

@@ -1,15 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<resources>
-	
-	<string-array name="upload_entries">
-	    <item>Always</item>
-        <item>Only with WiFi Connection</item>
-    </string-array>
-    
-	<string-array name="upload_values">
-	    <item>ALWAYS</item>
-        <item>WIFI</item>
-    </string-array>
-    
-
-</resources>

+ 5 - 1
res/values/strings_preferences.xml

@@ -14,6 +14,10 @@
 	<string name="pref_vibration_summ">Enable Vibration</string>
 	<string name="pref_upload">Upload of Records</string>	
 	<string name="pref_upload_server">Server address</string>	
-	<string name="pref_upload_server_summ">Enter server address for upload</string>	
+	<string name="pref_max_connections">Max Connections</string>	
+	<string name="pref_max_connections_default">5</string>	
+	<string name="pref_timeout">SocketTimeout(in seconds)</string>	
+	<string name="pref_timeout_default">30</string>	
+	<string name="pref_connection_settings">Connection Settings</string>
 	
 </resources>

+ 14 - 2
res/xml/preferences.xml

@@ -11,8 +11,8 @@
         <EditTextPreference
             android:key="pref_external_location"
             android:dependency="pref_external_storage"
-            android:title="@string/pref_external_location_title"
             android:defaultValue="/HOsTaGe/LogFiles/"
+            android:title="@string/pref_external_location_title"
             />
         
     </PreferenceCategory>
@@ -20,7 +20,6 @@
         <EditTextPreference
             android:key="pref_upload_server"
             android:defaultValue="https://ssi.cased.de"
-            android:summary="@string/pref_upload_server_summ"
             android:title="@string/pref_upload_server" />
         
     </PreferenceCategory>
@@ -39,6 +38,19 @@
             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" />
+            
     </PreferenceCategory>
 
+
 </PreferenceScreen>

+ 23 - 18
src/de/tudarmstadt/informatik/hostage/ConnectionRegister.java

@@ -1,37 +1,42 @@
 package de.tudarmstadt.informatik.hostage;
 
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
+
 /**
  *  Saves the amount of active connections and limits them to a specific number.  
  * @author Wulf Pfeiffer
  */
 public class ConnectionRegister {
 	
-	/** Maximum connections that are allowed to be open. */
-	private static int maxConnections = 2;
-	/** Active connections . */
+	/** Active connections . **/
 	private static int openConnections = 0;
+	/** Context in which ConnectionRegister is created. **/
+	private Context context;
 	
-	/** 
-	 * Returns the maximum number of active connections.
-	 * @return maximum number of active connections.
+	/**
+	 * Constructor sets context.
+	 * @param context Context in which ConnectionRegister is created.
 	 */
-	public static int getMaxConnections() {
-		return maxConnections;
+	public ConnectionRegister(Context context){
+		this.context = context;
 	}
 	
-	/**
-	 * Sets the maximum number of active connections.
-	 * @param maxConnections the new maximum.
+	/** 
+	 * Returns the maximum number of active connections.
+	 * @return maximum number of active connections.
 	 */
-	public static void setMaxConnections(int maxConnections) {
-		ConnectionRegister.maxConnections = maxConnections;
+	public int getMaxConnections() {
+		SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(context);		
+		return defaultPref.getInt("max_connections", 5);
 	}
 	
 	/**
 	 * Returns the number of active connections.
 	 * @return number of active connections.
 	 */
-	public static int getOpenConnections() {
+	public int getOpenConnections() {
 		return openConnections;
 	}
 	
@@ -39,15 +44,15 @@ public class ConnectionRegister {
 	 * Returns if there are new connections allowed or not.
 	 * @return true if a new connection is allowed, else false.
 	 */
-	public static boolean isConnectionFree() {
-		return openConnections < maxConnections;
+	public boolean isConnectionFree() {
+		return openConnections < getMaxConnections();
 	}
 	
 	/**
 	 * Registers a new active connection if there are connections allowed.
 	 * @return true if a new connection has been successfully registered, else false.
 	 */
-	public static boolean newOpenConnection() {
+	public boolean newOpenConnection() {
 		if(isConnectionFree()) {
 			openConnections++;
 			return true;
@@ -60,7 +65,7 @@ public class ConnectionRegister {
 	 * Deregisters a active connection if at least one active connection is registered.
 	 * @return true if the connection has been successfully unregistered, else false.
 	 */
-	public static boolean closeConnection() {
+	public boolean closeConnection() {
 		if(openConnections > 0) {
 			openConnections--;
 			return true;

+ 5 - 2
src/de/tudarmstadt/informatik/hostage/HoneyListener.java

@@ -46,6 +46,7 @@ public class HoneyListener implements Runnable {
 	private HoneyService service;
 	// Shared Preferences
 	private SharedPreferences pref;
+	private ConnectionRegister conReg;
 
 	// Editor for Shared preferences
 	private Editor editor;
@@ -71,6 +72,7 @@ public class HoneyListener implements Runnable {
 		pref = service.getApplicationContext().getSharedPreferences(
 				MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
 		editor = pref.edit();
+		conReg = new ConnectionRegister(service);
 	}
 
 	@Override
@@ -132,6 +134,7 @@ public class HoneyListener implements Runnable {
 				.hasNext();) {
 			AbstractHandler handler = iterator.next();
 			if (handler.isTerminated()) {
+				conReg.closeConnection();
 				iterator.remove();
 			}
 		}
@@ -141,10 +144,10 @@ public class HoneyListener implements Runnable {
 	 * Waits for an incoming connection, accepts it and starts a {@link AbstractHandler}
 	 */
 	private void addHandler() {
-		if(ConnectionRegister.isConnectionFree()) {
+		if(conReg.isConnectionFree()) {
 			try {
 				Socket client = server.accept();
-				ConnectionRegister.newOpenConnection();
+				conReg.newOpenConnection();
 				if (protocol.isSecure()) {
 					startSecureHandler(client);
 				} else {

+ 2 - 3
src/de/tudarmstadt/informatik/hostage/handler/AbstractHandler.java

@@ -22,7 +22,7 @@ public abstract class AbstractHandler implements Runnable {
 	/**
 	 * Time until the socket throws a time out. The time is in milliseconds.
 	 */
-	protected static final int TIMEOUT = 30 * 1000;
+	private int TIMEOUT;
 
 	protected Protocol protocol;
 	private Socket client;
@@ -54,6 +54,7 @@ public abstract class AbstractHandler implements Runnable {
 		SharedPreferences pref = PreferenceManager
 				.getDefaultSharedPreferences(service);
 		Editor editor = pref.edit();
+		TIMEOUT = pref.getInt("timeout", 30) * 1000;
 		attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
 		editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
 		editor.commit();
@@ -91,7 +92,6 @@ public abstract class AbstractHandler implements Runnable {
 			talkToClient(in, out);
 		} catch (Exception e) {
 			e.printStackTrace();
-			ConnectionRegister.closeConnection();
 		}
 		kill();
 	}
@@ -103,7 +103,6 @@ public abstract class AbstractHandler implements Runnable {
 		thread.interrupt();
 		try {
 			client.close();
-			ConnectionRegister.closeConnection();
 		} catch (Exception e) {
 			e.printStackTrace();
 		}

+ 40 - 1
src/de/tudarmstadt/informatik/hostage/ui/SettingsActivity.java

@@ -7,6 +7,8 @@ import android.os.Bundle;
 import android.preference.EditTextPreference;
 import android.preference.Preference;
 import android.preference.PreferenceActivity;
+import android.preference.PreferenceManager;
+import android.util.Log;
 import android.widget.Toast;
 /**
  * SettingsActivity creates the settings defined in /xml/preferences.xml.
@@ -26,6 +28,20 @@ public class SettingsActivity extends PreferenceActivity implements OnSharedPref
         pref = findPreference("pref_upload_server");
         etp = (EditTextPreference) pref;
         pref.setSummary(etp.getText());
+        
+        SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(this);
+        //Set the value of the preference as the summary for the preference
+        pref = findPreference("pref_max_connections");
+        etp = (EditTextPreference) pref;
+        defaultPref.edit().putInt("max_connections", Integer.valueOf(etp.getText()).intValue()).commit();
+        pref.setSummary(etp.getText());
+        
+        //Set the value of the preference as the summary for the preference
+        pref = findPreference("pref_timeout");
+        etp = (EditTextPreference) pref;
+        defaultPref.edit().putInt("timeout", Integer.valueOf(etp.getText()).intValue()).commit();
+        pref.setSummary(etp.getText());
+        
     }    
 
     protected void onResume() {
@@ -54,8 +70,9 @@ public class SettingsActivity extends PreferenceActivity implements OnSharedPref
             if(!path.endsWith("/"))
             	path = path.concat(new String("/"));
             if (!path.matches("/(([a-zA-Z_0-9])+/)*")){
-            	Toast.makeText(getApplicationContext(), "Path not valid. Must only contain a-zA-Z_0-9", Toast.LENGTH_SHORT).show();
+            	Toast.makeText(this, "Path not valid. Must only contain a-zA-Z_0-9", Toast.LENGTH_SHORT).show();
             	path = "/";
+            	sharedPreferences.edit().putString(key, path).commit();            	
             }
             pref.setSummary(path);
         }
@@ -64,6 +81,28 @@ public class SettingsActivity extends PreferenceActivity implements OnSharedPref
             EditTextPreference etp = (EditTextPreference) pref;
             pref.setSummary(etp.getText());
         }
+    	else if(key.equals("pref_max_connections")){
+    		Preference pref = findPreference(key);
+    		EditTextPreference etp = (EditTextPreference) pref;
+    		String value = etp.getText();
+    		if(!value.matches("([0-9])+")){
+    			Toast.makeText(getApplicationContext(), "Enter a valid number.", Toast.LENGTH_SHORT).show();
+    			value = getResources().getString(R.string.pref_max_connections_default);
+    		}
+        	sharedPreferences.edit().putInt("max_connections", Integer.valueOf(value).intValue()).commit();
+    		pref.setSummary(value);
+    	}
+    	else if(key.equals("pref_timeout")){
+    		Preference pref = findPreference(key);
+    		EditTextPreference etp = (EditTextPreference) pref;
+    		String value = etp.getText();
+    		if(!value.matches("([0-9])+")){
+    			Toast.makeText(getApplicationContext(), "Enter a valid number.", Toast.LENGTH_SHORT).show();
+    			value = getResources().getString(R.string.pref_timeout_default);
+    		}
+        	sharedPreferences.edit().putInt("timeout", Integer.valueOf(value).intValue()).commit();
+    		pref.setSummary(value);
+    	}
     	 
     }
 }