|
@@ -4,6 +4,8 @@ import android.accounts.Account;
|
|
|
import android.accounts.AccountManager;
|
|
|
import android.content.ContentResolver;
|
|
|
import android.content.Context;
|
|
|
+import android.content.SharedPreferences;
|
|
|
+import android.location.Location;
|
|
|
import android.os.Bundle;
|
|
|
import android.preference.PreferenceManager;
|
|
|
import android.util.Log;
|
|
@@ -26,20 +28,31 @@ import org.apache.http.params.HttpParams;
|
|
|
import org.apache.http.params.HttpProtocolParams;
|
|
|
import org.apache.http.protocol.HTTP;
|
|
|
import org.json.JSONArray;
|
|
|
+import org.json.JSONException;
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
import java.io.Writer;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.security.KeyStore;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.GregorianCalendar;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import de.tudarmstadt.informatik.hostage.location.MyLocationManager;
|
|
|
+import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
|
|
|
import de.tudarmstadt.informatik.hostage.logging.Record;
|
|
|
import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
|
|
|
+import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
|
|
|
|
|
|
|
|
|
* Created by abrakowski
|
|
@@ -162,21 +175,20 @@ public class SyncUtils {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- public static List<String[]> getCountriesFromServer(String serverAddress){
|
|
|
+ public static <T> T downloadFromServer(String address, Class<T> klass){
|
|
|
HttpGet httpget;
|
|
|
- List<String[]> ret = new ArrayList<String[]>();
|
|
|
|
|
|
try {
|
|
|
HttpClient httpClient = createHttpClient();
|
|
|
-
|
|
|
- httpget = new HttpGet(serverAddress + "/get_countries");
|
|
|
- httpget.addHeader("content-type", "application/json+newline");
|
|
|
|
|
|
-
|
|
|
+ httpget = new HttpGet(address);
|
|
|
+ httpget.addHeader("Accept", "application/json");
|
|
|
+
|
|
|
HttpResponse response = httpClient.execute(httpget);
|
|
|
+ Log.i("downloadFromServer", "Status Code: " + response.getStatusLine().getStatusCode());
|
|
|
|
|
|
if(response.getStatusLine().getStatusCode() >= 400 && response.getStatusLine().getStatusCode() < 600){
|
|
|
- return null;
|
|
|
+ return klass.newInstance();
|
|
|
}
|
|
|
|
|
|
BufferedReader bReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
@@ -186,17 +198,145 @@ public class SyncUtils {
|
|
|
builder.append(line);
|
|
|
}
|
|
|
|
|
|
- JSONArray array = new JSONArray(builder.toString());
|
|
|
- for(int i = 0; i < array.length(); i++){
|
|
|
+ return klass.getConstructor(klass).newInstance(builder.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String urlEncodeUTF8(String s) {
|
|
|
+ try {
|
|
|
+ return URLEncoder.encode(s, "UTF-8");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ throw new UnsupportedOperationException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String[] convertMapToStringArray(Map<String, String> map){
|
|
|
+ String[] array = new String[map.size() * 2];
|
|
|
+ int i = 0;
|
|
|
+ for(Map.Entry<String, String> entry: map.entrySet()){
|
|
|
+ array[i] = entry.getKey();
|
|
|
+ array[i + 1] = entry.getValue();
|
|
|
+ i += 2;
|
|
|
+ }
|
|
|
+
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String buildUrlFromBase(String baseUrl, String... query){
|
|
|
+ StringBuilder sb = new StringBuilder(baseUrl);
|
|
|
+
|
|
|
+ if(query.length >= 2){
|
|
|
+ sb.append("?");
|
|
|
+ }
|
|
|
+
|
|
|
+ for(int i=0; i<query.length - 2; i+=2){
|
|
|
+ if(i > 0){
|
|
|
+ sb.append("&");
|
|
|
+ }
|
|
|
+
|
|
|
+ sb.append(String.format("%s=%s",
|
|
|
+ urlEncodeUTF8(query[i]),
|
|
|
+ urlEncodeUTF8(query[i + 1])
|
|
|
+ ));
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String buildUrlFromBase(String baseUrl, Map<String, String> query){
|
|
|
+ return buildUrlFromBase(baseUrl, convertMapToStringArray(query));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String buildUrl(String protocol, String domain, int port, String path, String ... query){
|
|
|
+ return buildUrlFromBase(
|
|
|
+ String.format("%s://%s:%d/%s", urlEncodeUTF8(protocol), urlEncodeUTF8(domain), port, path),
|
|
|
+ query
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String buildUrl(String protocol, String domain, int port, String path, Map<String, String> query){
|
|
|
+ return buildUrl(protocol, domain, port, path, convertMapToStringArray(query));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<String[]> getCountriesFromServer(String serverAddress){
|
|
|
+ List<String[]> ret = new ArrayList<String[]>();
|
|
|
+ JSONArray array = downloadFromServer(serverAddress + "/get_countries", JSONArray.class);
|
|
|
+
|
|
|
+ try {
|
|
|
+ for (int i = 0; i < array.length(); i++) {
|
|
|
JSONObject ob = array.getJSONObject(i);
|
|
|
ret.add(new String[]{ob.getString("cc"), ob.getString("country")});
|
|
|
}
|
|
|
-
|
|
|
- Log.i("TracingSyncService", "Status Code: " + response.getStatusLine().getStatusCode());
|
|
|
- return ret;
|
|
|
- } catch (Exception e) {
|
|
|
+ } catch(Exception e){
|
|
|
e.printStackTrace();
|
|
|
- return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String fromCalendar(final Calendar calendar) {
|
|
|
+ Date date = calendar.getTime();
|
|
|
+ String formatted = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
|
|
|
+ .format(date);
|
|
|
+ return formatted.substring(0, 22) + ":" + formatted.substring(22);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Calendar toCalendar(final String iso8601string)
|
|
|
+ throws ParseException {
|
|
|
+ Calendar calendar = GregorianCalendar.getInstance();
|
|
|
+ String s = iso8601string.replace("Z", "+00:00");
|
|
|
+
|
|
|
+ try {
|
|
|
+ s = s.substring(0, 22) + s.substring(23);
|
|
|
+ } catch (IndexOutOfBoundsException e) {
|
|
|
+ throw new ParseException("Invalid length", 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(s);
|
|
|
+ calendar.setTime(date);
|
|
|
+ return calendar;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static JSONArray retrieveNewAttacks(Context context, boolean fromPosition){
|
|
|
+ SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
+ String serverAddress = pref.getString("pref_download_server", "http://ssi.cased.de/api");
|
|
|
+ long lastDownloadTime = pref.getLong("pref_download_last_time", 0);
|
|
|
+
|
|
|
+ Calendar calendar = GregorianCalendar.getInstance();
|
|
|
+ calendar.setTimeInMillis(lastDownloadTime);
|
|
|
+
|
|
|
+ String baseUri = serverAddress + "/get_attacks";
|
|
|
+ Map<String, String> query = new HashMap<String, String>();
|
|
|
+ query.put("start", fromCalendar(calendar));
|
|
|
+
|
|
|
+ if(fromPosition){
|
|
|
+ Location location = MyLocationManager.getNewestLocation();
|
|
|
+
|
|
|
+ if(location != null) {
|
|
|
+ query.put("latitude", String.valueOf(location.getLatitude()));
|
|
|
+ query.put("longitude", String.valueOf(location.getLongitude()));
|
|
|
+ query.put("distance", "300");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return downloadFromServer(buildUrlFromBase(baseUri, "start", fromCalendar(calendar)), JSONArray.class);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void logNewAttacks(Context context, JSONArray attacks){
|
|
|
+ Map<String, NetworkRecord> networks = new HashMap<String, NetworkRecord>();
|
|
|
+
|
|
|
+ for(int i=0; i<attacks.length(); i++){
|
|
|
+ try {
|
|
|
+ JSONObject obj = attacks.getJSONObject(i);
|
|
|
+
|
|
|
+
|
|
|
+ } catch (JSONException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|