TracingSyncService.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package de.tudarmstadt.informatik.hostage.sync.tracing;
  2. import java.security.KeyStore;
  3. import java.util.ArrayList;
  4. import org.apache.http.HttpResponse;
  5. import org.apache.http.HttpVersion;
  6. import org.apache.http.client.HttpClient;
  7. import org.apache.http.client.methods.HttpPost;
  8. import org.apache.http.conn.ClientConnectionManager;
  9. import org.apache.http.conn.scheme.PlainSocketFactory;
  10. import org.apache.http.conn.scheme.Scheme;
  11. import org.apache.http.conn.scheme.SchemeRegistry;
  12. import org.apache.http.conn.ssl.SSLSocketFactory;
  13. import org.apache.http.entity.StringEntity;
  14. import org.apache.http.impl.client.DefaultHttpClient;
  15. import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
  16. import org.apache.http.params.BasicHttpParams;
  17. import org.apache.http.params.HttpParams;
  18. import org.apache.http.params.HttpProtocolParams;
  19. import org.apache.http.protocol.HTTP;
  20. import android.app.IntentService;
  21. import android.content.Intent;
  22. import android.content.SharedPreferences;
  23. import android.content.SharedPreferences.Editor;
  24. import android.os.Bundle;
  25. import android.os.ResultReceiver;
  26. import android.preference.PreferenceManager;
  27. import android.util.Log;
  28. import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
  29. import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
  30. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  31. /**
  32. * Service that synchronizes with a specified remote server.
  33. * @author Lars Pandikow
  34. */
  35. public class TracingSyncService extends IntentService{
  36. public static final String REMOTE_DEVICE = "de.tudarmstadt.informatik.hostage.REMOTE_DEVICE";
  37. public static final String ACTION_START_SYNC = "de.tudarmstadt.informatik.hostage.ACTION_START_SYNC";
  38. public static final String EXTRA_RECEIVER = "de.tudarmstadt.informatik.hostage.EXTRA_HANDLER";
  39. public static final String UPLOAD_SIZE = "de.tudarmstadt.informatik.hostage.UPLOAD_SIZE";
  40. public static final String UPLOAD_PROGRESS = "de.tudarmstadt.informatik.hostage.UPLOAD_PROGRESS";
  41. public static final int RECORD_UPLOADED = 0x00;
  42. public static final int SYNC_COMPLETE = 0x01;
  43. private HttpClient httpClient;
  44. private ResultReceiver receiver;
  45. HostageDBOpenHelper dbh;
  46. SharedPreferences pref;
  47. Editor editor;
  48. public TracingSyncService() {
  49. super(TracingSyncService.class.getName());
  50. }
  51. @Override
  52. public void onCreate() {
  53. super.onCreate();
  54. pref = PreferenceManager.getDefaultSharedPreferences(this);
  55. editor = pref.edit();
  56. dbh = new HostageDBOpenHelper(this);
  57. }
  58. /**
  59. * The IntentService calls this method from the default worker thread with
  60. * the intent that started the service. When this method returns, IntentService
  61. * stops the service, as appropriate.
  62. */
  63. @Override
  64. protected void onHandleIntent(Intent intent) {
  65. if (intent != null) {
  66. final String action = intent.getAction();
  67. if (ACTION_START_SYNC.equals(action)) {
  68. receiver = intent.getParcelableExtra(EXTRA_RECEIVER);
  69. uploadNewRecords();
  70. //TODO add: dbh.clearSyncInfos();
  71. getRemoteData();
  72. if(receiver != null){
  73. receiver.send(SYNC_COMPLETE, null);
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. * Uploads all new Records to a server, specified in the settings.
  80. */
  81. private void uploadNewRecords() {
  82. int lastUploadedAttackId = pref.getInt("LAST_UPLOADED_ATTACK_ID", -1);
  83. String serverAddress = pref.getString("pref_upload", "https://ssi.cased.de");
  84. ArrayList<AttackRecord> recordList = dbh.getRecordOfEachAttack(lastUploadedAttackId);
  85. int size = recordList.size();
  86. int offset = 1;
  87. for(AttackRecord record: recordList){
  88. editor.putInt("LAST_UPLOADED_ATTACK_ID", lastUploadedAttackId + offset);
  89. editor.commit();
  90. boolean success = uploadSingleRecord(record, serverAddress);
  91. Log.i("Tracing upload", "Upload of record: " + offset +"/" + size + ((success) ? " successful.": " failed."));
  92. if(receiver != null){
  93. Bundle data = new Bundle();
  94. data.putInt(UPLOAD_SIZE, size);
  95. data.putInt(UPLOAD_PROGRESS, offset);
  96. receiver.send(RECORD_UPLOADED, data);
  97. }
  98. offset++;
  99. }
  100. }
  101. /**
  102. * Uploads a single Record to a server, specified in the settings.
  103. *
  104. * @param record The Record to upload.
  105. * @serverAddress Address of the target server
  106. * @return True if the upload was successful, else false.
  107. */
  108. private boolean uploadSingleRecord(AttackRecord record, String serverAddress) {
  109. // Create a https client. Uses MySSLSocketFactory to accept all certificates
  110. HttpPost httppost;
  111. try {
  112. httpClient = createHttpClient();
  113. // Create HttpPost
  114. httppost = new HttpPost(serverAddress);
  115. // Create JSON String of Record
  116. //TODO FIX ME
  117. //StringEntity se = new StringEntity( record.toString(TraCINgFormatter.getInstance()) );
  118. StringEntity se = new StringEntity(record.toString());
  119. httppost.setEntity(se);
  120. // Execute HttpPost
  121. HttpResponse response = httpClient.execute(httppost);
  122. Log.i("TracingSyncService", "Status Code: " + response.getStatusLine().getStatusCode());
  123. } catch (Exception e) {
  124. e.printStackTrace();
  125. return false;
  126. }
  127. return true;
  128. }
  129. /**
  130. * Gets the data from the server and updates the database.
  131. */
  132. private void getRemoteData() {
  133. //TODO GET DATA FROM SERVER
  134. //TODO SAVE DATA IN DATABASE
  135. }
  136. /**
  137. * Creates a HttpClient with an own SSL Socket.
  138. *
  139. * @return HttpsClient who accepts accepts all certificates.
  140. * @see MySSLSocketFactory
  141. */
  142. private HttpClient createHttpClient() {
  143. try {
  144. KeyStore trustStore = KeyStore.getInstance(KeyStore
  145. .getDefaultType());
  146. trustStore.load(null, null);
  147. SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
  148. sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  149. HttpParams params = new BasicHttpParams();
  150. HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  151. HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
  152. SchemeRegistry registry = new SchemeRegistry();
  153. registry.register(new Scheme("http", PlainSocketFactory
  154. .getSocketFactory(), 80));
  155. registry.register(new Scheme("https", sf, 443));
  156. ClientConnectionManager ccm = new ThreadSafeClientConnManager(
  157. params, registry);
  158. return new DefaultHttpClient(ccm, params);
  159. } catch (Exception e) {
  160. e.printStackTrace();
  161. return new DefaultHttpClient();
  162. }
  163. }
  164. }