Browse Source

-HTTP/HTTPS: now correctly change the Qotd after dis- and enabling

Wulf Pfeiffer 10 years ago
parent
commit
96bb1c7217

+ 1 - 1
project.properties

@@ -15,4 +15,4 @@ target=android-19
 android.library=false
 
 
-android.library.reference.1=../google-play-services_lib
+android.library.reference.1=../../development/Android/HosTaGe/google-play-services_lib

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

@@ -61,40 +61,6 @@ public class Hostage extends Service {
 		}
 	}
 
-	/**
-	 * Task for accuiring a qotd from one of four possible servers.
-	 * 
-	 * @author Wulf Pfeiffer
-	 */
-	private class QotdTask extends AsyncTask<String, Void, String> {
-		@Override
-		protected String doInBackground(String... unused) {
-			String[] sources = new String[] { "djxmmx.net", "ota.iambic.com", "alpha.mike-r.com", "electricbiscuit.org" };
-			SecureRandom rndm = new SecureRandom();
-			StringBuffer sb = new StringBuffer();
-			try {
-				Socket client = new Socket(sources[rndm.nextInt(4)], 17);
-				BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
-				while (!in.ready())
-					;
-				while (in.ready()) {
-					sb.append(in.readLine());
-				}
-				in.close();
-				client.close();
-			} catch (Exception e) {
-				e.printStackTrace();
-			}
-			return sb.toString();
-		}
-
-		@Override
-		protected void onPostExecute(String result) {
-			if (result != null)
-				HTTP.setHtmlDocumentContent(result);
-		}
-	}
-
 	/**
 	 * Task to find out the external IP.
 	 * 
@@ -299,10 +265,6 @@ public class Hostage extends Service {
 		registerNetReceiver();
 		updateConnectionInfo();
 		getLocationData();
-		boolean useQotd = context.getSharedPreferences(getString(R.string.shared_preference_path), Hostage.MODE_PRIVATE).getBoolean("useQotd", true);
-		if (useQotd) {
-			new QotdTask().execute(new String[] {});
-		}
 	}
 
 	@Override

+ 49 - 1
src/de/tudarmstadt/informatik/hostage/Listener.java

@@ -1,8 +1,11 @@
 package de.tudarmstadt.informatik.hostage;
 
+import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.security.SecureRandom;
 import java.util.ArrayList;
 import java.util.Iterator;
 
@@ -10,7 +13,10 @@ import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLSocket;
 import javax.net.ssl.SSLSocketFactory;
 
+import android.os.AsyncTask;
+
 import de.tudarmstadt.informatik.hostage.net.MyServerSocketFactory;
+import de.tudarmstadt.informatik.hostage.protocol.HTTP;
 import de.tudarmstadt.informatik.hostage.protocol.Protocol;
 import de.tudarmstadt.informatik.hostage.protocol.SMB;
 import de.tudarmstadt.informatik.hostage.protocol.SSLProtocol;
@@ -22,7 +28,7 @@ import de.tudarmstadt.informatik.hostage.protocol.SSLProtocol;
  * For each connection creates a Socket and instantiate an {@link Handler}.
  * 
  * @author Mihai Plasoianu
- * 
+ * @author Wulf Pfeiffer
  */
 public class Listener implements Runnable {
 
@@ -132,6 +138,13 @@ public class Listener implements Runnable {
 				((SMB) protocol).setIP(Hostage.getContext()
 						.getSharedPreferences(Hostage.getContext().getString(R.string.connection_info), Hostage.MODE_PRIVATE)
 						.getString(Hostage.getContext().getString(R.string.connection_info_internal_ip), ""));
+			} else if ((protocol.toString().equals("HTTP") && !service.isRunning("HTTPS")) 
+					|| (protocol.toString().equals("HTTPS") && !service.isRunning("HTTP"))) {
+				boolean useQotd = Hostage.getContext().getSharedPreferences(Hostage.getContext().getString(R.string.shared_preference_path), Hostage.MODE_PRIVATE).getBoolean("useQotd", true);
+				if (useQotd) {
+					new QotdTask().execute(new String[] {});
+					System.out.println("YIPPIIIII");
+				}
 			}
 			(this.thread = new Thread(this)).start();
 			running = true;
@@ -220,4 +233,39 @@ public class Listener implements Runnable {
 		sslClient.setUseClientMode(false);
 		handlers.add(newInstance(service, this, protocol.getClass().newInstance(), sslClient));
 	}
+
+	/**
+	 * Task for accuiring a qotd from one of four possible servers.
+	 * 
+	 * @author Wulf Pfeiffer
+	 */
+	private class QotdTask extends AsyncTask<String, Void, String> {
+		@Override
+		protected String doInBackground(String... unused) {
+			String[] sources = new String[] { "djxmmx.net", "ota.iambic.com", "alpha.mike-r.com", "electricbiscuit.org" };
+			SecureRandom rndm = new SecureRandom();
+			StringBuffer sb = new StringBuffer();
+			try {
+				Socket client = new Socket(sources[rndm.nextInt(4)], 17);
+				BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
+				while (!in.ready())
+					;
+				while (in.ready()) {
+					sb.append(in.readLine());
+				}
+				in.close();
+				client.close();
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+			return sb.toString();
+		}
+
+		@Override
+		protected void onPostExecute(String result) {
+			if (result != null)
+				HTTP.setHtmlDocumentContent(result);
+		}
+	}
+	
 }

+ 4 - 4
src/de/tudarmstadt/informatik/hostage/wrapper/Packet.java

@@ -46,11 +46,11 @@ public class Packet {
 
 	/**
 	 * Returns a String representation of the payload.
-	 * If the payload contains a ASCII character the whole
-	 * String will be represented as a String of a byte values.
+	 * Depending on the protocol, the String will be represented 
+	 * as a String of it's byte values.
 	 * E.g.: the byte[] {0x01, 0x4A, 0x03} would look like
-	 * the String "01, 4A, 03".
-	 * Otherwise a normal String will be created with the payload.
+	 * the String "01, 4A, 03", or
+	 * otherwise a normal String will be created with the payload.
 	 * 
 	 * @return String representation.
 	 */