Hostage.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. package de.tudarmstadt.informatik.hostage;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.client.HttpClient;
  9. import org.apache.http.client.methods.HttpGet;
  10. import org.apache.http.impl.client.DefaultHttpClient;
  11. import org.apache.http.util.EntityUtils;
  12. import org.json.JSONObject;
  13. import android.app.AlertDialog;
  14. import android.app.NotificationManager;
  15. import android.app.PendingIntent;
  16. import android.app.Service;
  17. import android.content.BroadcastReceiver;
  18. import android.content.Context;
  19. import android.content.DialogInterface;
  20. import android.content.Intent;
  21. import android.content.IntentFilter;
  22. import android.content.SharedPreferences;
  23. import android.content.SharedPreferences.Editor;
  24. import android.net.ConnectivityManager;
  25. import android.net.DhcpInfo;
  26. import android.net.NetworkInfo;
  27. import android.net.Uri;
  28. import android.net.wifi.WifiInfo;
  29. import android.net.wifi.WifiManager;
  30. import android.os.AsyncTask;
  31. import android.os.Binder;
  32. import android.os.IBinder;
  33. import android.preference.CheckBoxPreference;
  34. import android.preference.PreferenceManager;
  35. import android.support.v4.app.NotificationCompat;
  36. import android.support.v4.app.TaskStackBuilder;
  37. import android.support.v4.content.LocalBroadcastManager;
  38. import android.util.Log;
  39. import android.widget.CheckBox;
  40. import android.widget.Toast;
  41. import de.tudarmstadt.informatik.hostage.R;
  42. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  43. import de.tudarmstadt.informatik.hostage.location.MyLocationManager;
  44. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  45. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  46. import de.tudarmstadt.informatik.hostage.services.MultiStageAlarm;
  47. import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
  48. import static de.tudarmstadt.informatik.hostage.commons.HelperUtils.*;
  49. /**
  50. * Background service running as long as at least one protocol is active.
  51. * Service controls start and stop of protocol listener. Notifies GUI about
  52. * events happening in the background. Creates Notifications to inform the user
  53. * what it happening.
  54. *
  55. * @author Mihai Plasoianu
  56. * @author Lars Pandikow
  57. * @author Wulf Pfeiffer
  58. */
  59. public class Hostage extends Service {
  60. private HashMap<String, Boolean> mProtocolActiveAttacks;
  61. private Boolean multistage_service;
  62. MultiStageAlarm alarm = new MultiStageAlarm();
  63. public class LocalBinder extends Binder {
  64. public Hostage getService() {
  65. return Hostage.this;
  66. }
  67. }
  68. /**
  69. * Task to find out the external IP.
  70. *
  71. */
  72. private class SetExternalIPTask extends AsyncTask<String, Void, String> {
  73. @Override
  74. protected String doInBackground(String... url) {
  75. String ipAddress = null;
  76. try {
  77. HttpClient httpclient = new DefaultHttpClient();
  78. HttpGet httpget = new HttpGet(url[0]);
  79. HttpResponse response;
  80. response = httpclient.execute(httpget);
  81. HttpEntity entity = response.getEntity();
  82. entity.getContentLength();
  83. String str = EntityUtils.toString(entity);
  84. JSONObject json_data = new JSONObject(str);
  85. ipAddress = json_data.getString("ip");
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89. return ipAddress;
  90. }
  91. @Override
  92. protected void onPostExecute(String result) {
  93. connectionInfoEditor.putString(getString(R.string.connection_info_external_ip), result);
  94. connectionInfoEditor.commit();
  95. notifyUI(this.getClass().getName(), new String[] { getString(R.string.broadcast_connectivity) });
  96. }
  97. }
  98. private static Context context;
  99. Listener listener;
  100. /**
  101. * Returns the application context.
  102. *
  103. * @return context.
  104. */
  105. public static Context getContext() {
  106. return Hostage.context;
  107. }
  108. private LinkedList<Protocol> implementedProtocols;
  109. private ArrayList<Listener> listeners = new ArrayList<Listener>();
  110. private NotificationCompat.Builder builder;
  111. private SharedPreferences connectionInfo;
  112. private Editor connectionInfoEditor;
  113. private final IBinder mBinder = new LocalBinder();
  114. /**
  115. * Receiver for connectivity change broadcast.
  116. *
  117. * @see MainActivity#BROADCAST
  118. */
  119. private BroadcastReceiver netReceiver = new BroadcastReceiver() {
  120. @Override
  121. public void onReceive(Context context, Intent intent) {
  122. String bssid_old = connectionInfo.getString(getString(R.string.connection_info_bssid), "");
  123. String bssid_new = getBSSID(context);
  124. if (bssid_new == null || !bssid_new.equals(bssid_old)) {
  125. deleteConnectionData();
  126. updateConnectionInfo();
  127. getLocationData();
  128. notifyUI(this.getClass().getName(), new String[] { getString(R.string.broadcast_connectivity) });
  129. }
  130. }
  131. };
  132. public List<Listener> getListeners() {
  133. return listeners;
  134. }
  135. /**
  136. * Determines the number of active connections for a protocol running on its
  137. * default port.
  138. *
  139. * @param protocolName
  140. * The protocol name
  141. * @return Number of active connections
  142. */
  143. public int getNumberOfActiveConnections(String protocolName) {
  144. int port = getDefaultPort(protocolName);
  145. return getNumberOfActiveConnections(protocolName, port);
  146. }
  147. /**
  148. * Determines the number of active connections for a protocol running on the
  149. * given port.
  150. *
  151. * @param protocolName
  152. * The protocol name
  153. * @param port
  154. * Specific port
  155. * @return Number of active connections
  156. */
  157. public int getNumberOfActiveConnections(String protocolName, int port) {
  158. for (Listener listener : listeners) {
  159. if (listener.getProtocolName().equals(protocolName) && listener.getPort() == port) {
  160. return listener.getHandlerCount();
  161. }
  162. }
  163. return 0;
  164. }
  165. /**
  166. * Determines if there any listener is currently running.
  167. *
  168. * @return True if there is a running listener, else false.
  169. */
  170. public boolean hasRunningListeners() {
  171. for (Listener listener : listeners) {
  172. if (listener.isRunning())
  173. return true;
  174. }
  175. return false;
  176. }
  177. /**
  178. * Determines if a protocol with the given name is running on its default
  179. * port.
  180. *
  181. * @param protocolName
  182. * The protocol name
  183. * @return True if protocol is running, else false.
  184. */
  185. public boolean isRunning(String protocolName) {
  186. int port = getDefaultPort(protocolName);
  187. return isRunning(protocolName, port);
  188. }
  189. public boolean isRunningAnyPort(String protocolName){
  190. for(Listener listener: listeners){
  191. if(listener.getProtocolName().equals(protocolName)){
  192. if(listener.isRunning()) return true;
  193. }
  194. }
  195. return false;
  196. }
  197. /**
  198. * Determines if a protocol with the given name is running on the given
  199. * port.
  200. *
  201. * @param protocolName
  202. * The protocol name
  203. * @param port
  204. * Specific port
  205. * @return True if protocol is running, else false.
  206. */
  207. public boolean isRunning(String protocolName, int port) {
  208. for (Listener listener : listeners) {
  209. if (listener.getProtocolName().equals(protocolName) && listener.getPort() == port) {
  210. return listener.isRunning();
  211. }
  212. }
  213. return false;
  214. }
  215. /**
  216. * Notifies the GUI about a event.
  217. *
  218. * @param sender
  219. * Source where the event took place.
  220. * @param key
  221. * Detailed information about the event.
  222. */
  223. public void notifyUI(String sender, String[] values) {
  224. createNotification();
  225. // Send Notification
  226. if (sender.equals(Handler.class.getName()) && values[0].equals(getString(R.string.broadcast_started))) {
  227. this.mProtocolActiveAttacks.put(values[1], true);
  228. attackNotification();
  229. }
  230. // Inform UI of Preference Change
  231. Intent intent = new Intent(getString(R.string.broadcast));
  232. intent.putExtra("SENDER", sender);
  233. intent.putExtra("VALUES", values);
  234. Log.i("Sender", sender);
  235. LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  236. }
  237. @Override
  238. public IBinder onBind(Intent intent) {
  239. return mBinder;
  240. }
  241. @Override
  242. public void onCreate() {
  243. super.onCreate();
  244. Hostage.context = getApplicationContext();
  245. implementedProtocols = getImplementedProtocols();
  246. connectionInfo = getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  247. connectionInfoEditor = connectionInfo.edit();
  248. mProtocolActiveAttacks = new HashMap<String, Boolean>();
  249. createNotification();
  250. registerNetReceiver();
  251. updateConnectionInfo();
  252. getLocationData();
  253. }
  254. @Override
  255. public void onDestroy() {
  256. cancelNotification();
  257. unregisterNetReceiver();
  258. super.onDestroy();
  259. }
  260. @Override
  261. public int onStartCommand(Intent intent, int flags, int startId) {
  262. // We want this service to continue running until it is explicitly
  263. // stopped, so return sticky.
  264. /*SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
  265. multistage_service=sp.getBoolean("pref_multistage",true);*/
  266. startMultiStage();
  267. return START_STICKY;
  268. }
  269. private void stopMultiStage() {
  270. Context context =this;
  271. alarm.CancelAlarm(context);
  272. }
  273. private void startMultiStage() {
  274. Context context = this;
  275. if (alarm != null) {
  276. alarm.SetAlarm(context);
  277. } else {
  278. Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
  279. }
  280. }
  281. /**
  282. * Starts the listener for the specified protocol. Creates a new
  283. * HoneyService if no matching HoneyListener is found.
  284. *
  285. * @param protocolName
  286. * Name of the protocol that should be started.
  287. */
  288. public boolean startListener(String protocolName) {
  289. return startListener(protocolName, getDefaultPort(protocolName));
  290. }
  291. /**
  292. * Starts the listener for the specified protocol and port. Creates a new
  293. * HoneyService if no matching HoneyListener is found.
  294. *
  295. * @param protocolName
  296. * Name of the protocol that should be started.
  297. * @param port
  298. * The port number in which the listener should run.
  299. */
  300. public boolean startListener(String protocolName, int port) {
  301. for (Listener listener : listeners) {
  302. if (listener.getProtocolName().equals(protocolName) && listener.getPort() == port) {
  303. if (!listener.isRunning()) {
  304. if (listener.start()) {
  305. // Toast.makeText(getApplicationContext(), protocolName
  306. // + " SERVICE STARTED!", Toast.LENGTH_SHORT).show();
  307. return true;
  308. }
  309. Toast.makeText(getApplicationContext(), protocolName + " SERVICE COULD NOT BE STARTED!", Toast.LENGTH_SHORT).show();
  310. return false;
  311. }
  312. }
  313. }
  314. Listener listener = createListener(protocolName, port);
  315. if (listener != null) {
  316. if (listener.start()) {
  317. // Toast.makeText(getApplicationContext(), protocolName +
  318. // " SERVICE STARTED!", Toast.LENGTH_SHORT).show();
  319. return true;
  320. }
  321. }
  322. Toast.makeText(getApplicationContext(), protocolName + " SERVICE COULD NOT BE STARTED!", Toast.LENGTH_SHORT).show();
  323. return false;
  324. }
  325. /**
  326. * Starts all listeners which are not already running.
  327. */
  328. public void startListeners() {
  329. for (Listener listener : listeners) {
  330. if (!listener.isRunning()) {
  331. listener.start();
  332. }
  333. }
  334. // Toast.makeText(getApplicationContext(), "SERVICES STARTED!",
  335. // Toast.LENGTH_SHORT).show();
  336. }
  337. /**
  338. * Stops the listener for the specified protocol.
  339. *
  340. * @param protocolName
  341. * Name of the protocol that should be stopped.
  342. */
  343. public void stopListener(String protocolName) {
  344. stopListener(protocolName, getDefaultPort(protocolName));
  345. }
  346. /**
  347. * Stops the listener for the specified protocol.
  348. *
  349. * @param protocolName
  350. * Name of the protocol that should be stopped.
  351. * @param port
  352. * The port number in which the listener is running.
  353. */
  354. public void stopListener(String protocolName, int port) {
  355. for (Listener listener : listeners) {
  356. if (listener.getProtocolName().equals(protocolName) && listener.getPort() == port) {
  357. if (listener.isRunning()) {
  358. listener.stop();
  359. mProtocolActiveAttacks.remove(protocolName);
  360. }
  361. }
  362. }
  363. // Toast.makeText(getApplicationContext(), protocolName +
  364. // " SERVICE STOPPED!", Toast.LENGTH_SHORT).show();
  365. }
  366. public void stopListenerAllPorts(String protocolName){
  367. for(Listener listener: listeners){
  368. if(listener.getProtocolName().equals(protocolName)){
  369. if(listener.isRunning()){
  370. listener.stop();
  371. mProtocolActiveAttacks.remove(protocolName);
  372. }
  373. }
  374. }
  375. }
  376. /**
  377. * Stops all running listeners.
  378. */
  379. public void stopListeners() {
  380. for (Listener listener : listeners) {
  381. if (listener.isRunning()) {
  382. listener.stop();
  383. mProtocolActiveAttacks.remove(listener.getProtocolName());
  384. }
  385. }
  386. // Toast.makeText(getApplicationContext(), "SERVICES STOPPED!",
  387. // Toast.LENGTH_SHORT).show();
  388. }
  389. /**
  390. * Updates the notification when a attack is registered.
  391. */
  392. private void attackNotification() {
  393. SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(this);
  394. String strRingtonePreference = defaultPref.getString("pref_notification_sound", "content://settings/system/notification_sound");
  395. builder = new NotificationCompat.Builder(this).setContentTitle(getString(R.string.app_name)).setTicker(getString(R.string.honeypot_live_threat))
  396. .setContentText(getString(R.string.honeypot_live_threat)).setSmallIcon(R.drawable.ic_service_red).setAutoCancel(true).setWhen(System.currentTimeMillis())
  397. .setSound(Uri.parse(strRingtonePreference));
  398. Intent launchIntent = new Intent(getApplicationContext(), MainActivity.class);
  399. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  400. stackBuilder.addParentStack(MainActivity.class);
  401. Intent intent = MainActivity.getInstance().getIntent();
  402. intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  403. intent.setAction("SHOW_HOME");
  404. stackBuilder.addNextIntent(intent);
  405. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
  406. builder.setContentIntent(resultPendingIntent);
  407. if (defaultPref.getBoolean("pref_vibration", false)) {
  408. builder.setVibrate(new long[] { 100, 200, 100, 200 });
  409. }
  410. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  411. mNotificationManager.notify(1, builder.build());
  412. }
  413. /**
  414. * Cancels the Notification
  415. */
  416. private void cancelNotification() {
  417. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  418. mNotificationManager.cancel(1);
  419. }
  420. /**
  421. * Creates a HoneyListener for a given protocol on a specific port. After
  422. * creation the HoneyListener is not started. Checks if the protocol is
  423. * implemented first.
  424. *
  425. * @param protocolName
  426. * Name of the protocol
  427. * @param port
  428. * Port on which to start the HoneyListener
  429. * @return Returns the created HoneyListener, if creation failed returns
  430. * null.
  431. */
  432. private Listener createListener(String protocolName, int port) {
  433. for (Protocol protocol : implementedProtocols) {
  434. if (protocolName.equals(protocol.toString())) {
  435. Listener listener = new Listener(this, protocol, port);
  436. listeners.add(listener);
  437. return listener;
  438. }
  439. }
  440. return null;
  441. }
  442. /**
  443. * Creates a Notification in the notification bar.
  444. */
  445. private void createNotification() {
  446. if (MainActivity.getInstance() == null) {
  447. return; // prevent NullPointerException
  448. }
  449. HostageDBOpenHelper dbh = new HostageDBOpenHelper(this);
  450. boolean activeHandlers = false;
  451. boolean bssidSeen = false;
  452. boolean listening = false;
  453. for (Listener listener : listeners) {
  454. if (listener.isRunning())
  455. listening = true;
  456. if (listener.getHandlerCount() > 0) {
  457. activeHandlers = true;
  458. }
  459. if (dbh.bssidSeen(listener.getProtocolName(), getBSSID(getApplicationContext()))) {
  460. bssidSeen = true;
  461. }
  462. }
  463. builder = new NotificationCompat.Builder(this).setContentTitle(getString(R.string.app_name)).setWhen(System.currentTimeMillis());
  464. if (!listening) {
  465. builder.setSmallIcon(R.drawable.ic_launcher);
  466. builder.setContentText(getString(R.string.hostage_not_monitoring));
  467. } else if (activeHandlers) {
  468. builder.setSmallIcon(R.drawable.ic_service_red);
  469. builder.setContentText(getString(R.string.hostage_live_threat));
  470. } else if (bssidSeen) {
  471. builder.setSmallIcon(R.drawable.ic_service_yellow);
  472. builder.setContentText(getString(R.string.hostage_past_threat));
  473. } else {
  474. builder.setSmallIcon(R.drawable.ic_service_green);
  475. builder.setContentText(getString(R.string.hostage_no_threat));
  476. }
  477. Intent launchIntent = new Intent(getApplicationContext(), MainActivity.class);
  478. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  479. stackBuilder.addParentStack(MainActivity.class);
  480. Intent intent = MainActivity.getInstance().getIntent();
  481. intent.addCategory(Intent.ACTION_MAIN);
  482. intent.addCategory(Intent.CATEGORY_LAUNCHER);
  483. intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  484. //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
  485. intent.setAction("SHOW_HOME");
  486. stackBuilder.addNextIntent(intent);
  487. PendingIntent resultPendingIntent = PendingIntent.getActivity(MainActivity.context, 0, intent, 0); //stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
  488. builder.setContentIntent(resultPendingIntent);
  489. builder.setOngoing(true);
  490. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  491. mNotificationManager.notify(1, builder.build());
  492. }
  493. /**
  494. * Deletes all session related data.
  495. */
  496. private void deleteConnectionData() {
  497. connectionInfoEditor.clear();
  498. connectionInfoEditor.commit();
  499. }
  500. /**
  501. * Returns the default port number, if the protocol is implemented.
  502. *
  503. * @param protocolName
  504. * The name of the protocol
  505. * @return Returns the default port number, if the protocol is implemented.
  506. * Else returns -1.
  507. */
  508. private int getDefaultPort(String protocolName) {
  509. for (Protocol protocol : implementedProtocols) {
  510. if (protocolName.equals(protocol.toString())) {
  511. return protocol.getPort();
  512. }
  513. }
  514. return -1;
  515. };
  516. /**
  517. * Returns an LinkedList<String> with the names of all implemented
  518. * protocols.
  519. *
  520. * @return ArrayList of
  521. * {@link de.tudarmstadt.informatik.hostage.protocol.Protocol
  522. * Protocol}
  523. */
  524. private LinkedList<Protocol> getImplementedProtocols() {
  525. String[] protocols = getResources().getStringArray(R.array.protocols);
  526. String packageName = Protocol.class.getPackage().getName();
  527. LinkedList<Protocol> implementedProtocols = new LinkedList<Protocol>();
  528. for (String protocol : protocols) {
  529. try {
  530. implementedProtocols.add((Protocol) Class.forName(String.format("%s.%s", packageName, protocol)).newInstance());
  531. } catch (Exception e) {
  532. e.printStackTrace();
  533. }
  534. }
  535. return implementedProtocols;
  536. }
  537. /**
  538. * Starts an Instance of MyLocationManager to set the location within this
  539. * class.
  540. */
  541. private void getLocationData() {
  542. MyLocationManager locationManager = new MyLocationManager(this);
  543. locationManager.getUpdates(60 * 1000, 3);
  544. };
  545. // Notifications
  546. /**
  547. * Register broadcast receiver for connectivity changes
  548. */
  549. private void registerNetReceiver() {
  550. // register BroadcastReceiver on network state changes
  551. IntentFilter intent = new IntentFilter();
  552. intent.addAction(ConnectivityManager.CONNECTIVITY_ACTION); // "android.net.conn.CONNECTIVITY_CHANGE"
  553. registerReceiver(netReceiver, intent);
  554. }
  555. /**
  556. * Unregister broadcast receiver for connectivity changes
  557. */
  558. private void unregisterNetReceiver() {
  559. unregisterReceiver(netReceiver);
  560. }
  561. public boolean hasProtocolActiveAttacks(String protocol){
  562. if(!mProtocolActiveAttacks.containsKey(protocol)) return false;
  563. return mProtocolActiveAttacks.get(protocol);
  564. }
  565. public boolean hasActiveAttacks(){
  566. for(boolean b: mProtocolActiveAttacks.values()){
  567. if(b) return true;
  568. }
  569. return false;
  570. }
  571. /**
  572. * Updates the connection info and saves them in the the SharedPreferences
  573. * for session data.
  574. *
  575. * @param context
  576. * Needs a context to get system resources.
  577. * @see MainActivity#CONNECTION_INFO
  578. */
  579. private void updateConnectionInfo() {
  580. ConnectivityManager connManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
  581. NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  582. if (networkInfo == null || !networkInfo.isConnected()) {
  583. return; // no connection
  584. }
  585. final WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
  586. final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
  587. if (connectionInfo == null) {
  588. return; // no wifi connection
  589. }
  590. final DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
  591. if (dhcpInfo == null) {
  592. return;
  593. }
  594. String ssid = connectionInfo.getSSID();
  595. if (ssid.startsWith("\"") && ssid.endsWith("\"")) { // trim those quotes
  596. ssid = ssid.substring(1, ssid.length() - 1);
  597. }
  598. SharedPreferences pref = context.getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  599. Editor editor = pref.edit();
  600. editor.putString(getString(R.string.connection_info_ssid), ssid);
  601. editor.putString(getString(R.string.connection_info_bssid), connectionInfo.getBSSID());
  602. editor.putInt(getString(R.string.connection_info_internal_ip), dhcpInfo.ipAddress);
  603. editor.putInt(getString(R.string.connection_info_subnet_mask), dhcpInfo.netmask);
  604. editor.commit();
  605. SetExternalIPTask async = new SetExternalIPTask();
  606. async.execute(new String[] { "http://ip2country.sourceforge.net/ip2c.php?format=JSON" });
  607. this.mProtocolActiveAttacks.clear();
  608. }
  609. }