Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: src/org/adblockplus/android/ProxyService.java

Issue 4705284891082752: Proxy configurators (Closed)
Left Patch Set: valueOf Created Aug. 19, 2014, 1:24 p.m.
Right Patch Set: Last batch of review issues Created Aug. 24, 2014, 11:52 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/org/adblockplus/android/ProxyServerType.java ('k') | src/org/adblockplus/android/ProxySettings.java » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 public static final String BROADCAST_STATE_CHANGED = "org.adblockplus.android. SERVICE_STATE_CHANGED"; 69 public static final String BROADCAST_STATE_CHANGED = "org.adblockplus.android. SERVICE_STATE_CHANGED";
70 70
71 /** 71 /**
72 * Broadcasted if proxy fails to start. 72 * Broadcasted if proxy fails to start.
73 */ 73 */
74 public static final String BROADCAST_PROXY_FAILED = "org.adblockplus.android.P ROXY_FAILURE"; 74 public static final String BROADCAST_PROXY_FAILED = "org.adblockplus.android.P ROXY_FAILURE";
75 75
76 /** 76 /**
77 * Common command bridge 77 * Common command bridge
78 */ 78 */
79 public static final String COMMAND_BRIDGE_ACTION = "org.adblockplus.android.CO MMAND_BRIDGE"; 79 public static final String PROXY_STATE_CHANGED_ACTION = "org.adblockplus.andro id.PROXY_STATE_CHANGED";
80 80
81 boolean hideIcon; 81 boolean hideIcon;
82 82
83 protected ProxyServer proxy = null; 83 protected ProxyServer proxy = null;
84 84
85 protected int port; 85 protected int port;
86 86
87 private final Properties proxyConfiguration = new Properties(); 87 private final Properties proxyConfiguration = new Properties();
88 88
89 private ProxyConfigurator proxyConfigurator = null; 89 private ProxyConfigurator proxyConfigurator = null;
90 90
91 @SuppressLint("NewApi") 91 @SuppressLint("NewApi")
92 @Override 92 @Override
93 public void onCreate() 93 public void onCreate()
94 { 94 {
95 super.onCreate(); 95 super.onCreate();
96 96
97 // FIXME 'StrictMode' is API9
Felix Dahlke 2014/08/19 16:14:46 I still think this should be part of a larger "sup
René Jeschke 2014/08/19 16:33:24 We talked about this some weeks ago. I told you th
98 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 97 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
99 .permitAll() 98 .permitAll()
100 .penaltyLog() 99 .penaltyLog()
101 .build()); 100 .build());
102 101
103 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(this); 102 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(this);
104 103
105 // Try to read user proxy settings 104 // Try to read user proxy settings
106 String proxyHost = System.getProperty("http.proxyHost");; 105 String proxyHost = System.getProperty("http.proxyHost");;
107 String proxyPort = System.getProperty("http.proxyPort"); 106 String proxyPort = System.getProperty("http.proxyPort");
108 final String proxyExcl = System.getProperty("http.nonProxyHosts"); 107 final String proxyExcl = System.getProperty("http.nonProxyHosts");
109 String proxyUser = null; 108 String proxyUser = null;
110 String proxyPass = null; 109 String proxyPass = null;
111 110
112 if (proxyHost == null || proxyPort == null) 111 if (proxyHost == null || proxyPort == null)
113 { 112 {
114 // Read application settings 113 // Read application settings
115 proxyHost = prefs.getString(getString(R.string.pref_proxyhost), null); 114 proxyHost = prefs.getString(getString(R.string.pref_proxyhost), null);
116 proxyPort = prefs.getString(getString(R.string.pref_proxyport), null); 115 proxyPort = prefs.getString(getString(R.string.pref_proxyport), null);
117 proxyUser = prefs.getString(getString(R.string.pref_proxyuser), null); 116 proxyUser = prefs.getString(getString(R.string.pref_proxyuser), null);
118 proxyPass = prefs.getString(getString(R.string.pref_proxypass), null); 117 proxyPass = prefs.getString(getString(R.string.pref_proxypass), null);
119 } 118 }
120 119
121 registerReceiver(this.proxyReceiver, new IntentFilter(ProxyService.BROADCAST _PROXY_FAILED)); 120 registerReceiver(this.proxyReceiver, new IntentFilter(ProxyService.BROADCAST _PROXY_FAILED));
122 registerReceiver(this.commandBridge, new IntentFilter(ProxyService.COMMAND_B RIDGE_ACTION)); 121 registerReceiver(this.proxyStateChangedReceiver, new IntentFilter(ProxyServi ce.PROXY_STATE_CHANGED_ACTION));
123 122
124 final InetAddress inetAddress; 123 final InetAddress inetAddress;
125 try 124 try
126 { 125 {
127 inetAddress = InetAddress.getByName(LOCALHOST); 126 inetAddress = InetAddress.getByName(LOCALHOST);
128 } 127 }
129 catch (final UnknownHostException e) 128 catch (final UnknownHostException e)
130 { 129 {
131 sendBroadcast(new Intent(BROADCAST_PROXY_FAILED) 130 sendBroadcast(new Intent(BROADCAST_PROXY_FAILED)
132 .putExtra("msg", "Could not resolve 'localhost'")); 131 .putExtra("msg", "Could not resolve 'localhost'"));
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 { 226 {
228 return START_STICKY; 227 return START_STICKY;
229 } 228 }
230 229
231 @Override 230 @Override
232 public void onDestroy() 231 public void onDestroy()
233 { 232 {
234 super.onDestroy(); 233 super.onDestroy();
235 234
236 unregisterReceiver(this.proxyReceiver); 235 unregisterReceiver(this.proxyReceiver);
237 unregisterReceiver(this.commandBridge); 236 unregisterReceiver(this.proxyStateChangedReceiver);
238 237
239 if (this.proxyConfigurator != null) 238 if (this.proxyConfigurator != null)
240 { 239 {
241 this.proxyConfigurator.unregisterProxy(); 240 this.proxyConfigurator.unregisterProxy();
242 this.proxyConfigurator.shutdown(); 241 this.proxyConfigurator.shutdown();
243 } 242 }
244 243
245 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false) ); 244 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false) );
246 245
247 // Stop proxy server 246 // Stop proxy server
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 final String proxyAuth = "Basic " + new String(Base64.encode(proxyUser + " :" + proxyPass)); 331 final String proxyAuth = "Basic " + new String(Base64.encode(proxyUser + " :" + proxyPass));
333 config.put("adblock.auth", proxyAuth); 332 config.put("adblock.auth", proxyAuth);
334 if (regType.getProxyType() == ProxyServerType.HTTPS) 333 if (regType.getProxyType() == ProxyServerType.HTTPS)
335 { 334 {
336 config.put("https.auth", proxyAuth); 335 config.put("https.auth", proxyAuth);
337 } 336 }
338 } 337 }
339 } 338 }
340 339
341 /** 340 /**
342 * Lean method to send a {@link BridgeCommand} without additional extras.
343 *
344 * @param context
345 * the context to use for sending the broadcast
346 * @param command
347 * the command
348 */
349 public static void sendBridgeCommand(final Context context, final BridgeComman d command)
350 {
351 context.sendBroadcast(
352 new Intent(COMMAND_BRIDGE_ACTION)
353 .putExtra("command", command.toString()));
354 }
355
356 /**
357 * @return {@code true} if the given host string resolves to {@code localhost} 341 * @return {@code true} if the given host string resolves to {@code localhost}
358 */ 342 */
359 public static boolean isLocalhost(final String host) 343 public static boolean isLocalhost(final String host)
360 { 344 {
361 try 345 try
362 { 346 {
363 if (StringUtils.isEmpty(host)) 347 if (StringUtils.isEmpty(host))
364 { 348 {
365 return false; 349 return false;
366 } 350 }
(...skipping 20 matching lines...) Expand all
387 } 371 }
388 catch (final ClassCastException e) 372 catch (final ClassCastException e)
389 { 373 {
390 // ignore - default handler in use 374 // ignore - default handler in use
391 } 375 }
392 } 376 }
393 377
394 @Override 378 @Override
395 public void onSharedPreferenceChanged(final SharedPreferences sharedPreference s, final String key) 379 public void onSharedPreferenceChanged(final SharedPreferences sharedPreference s, final String key)
396 { 380 {
397 // TODO verify this
398 if (this.getProxyRegistrationType() != ProxyRegistrationType.NATIVE) 381 if (this.getProxyRegistrationType() != ProxyRegistrationType.NATIVE)
399 { 382 {
400 final String keyHost = getString(R.string.pref_proxyhost); 383 final String keyHost = getString(R.string.pref_proxyhost);
401 final String keyPort = getString(R.string.pref_proxyport); 384 final String keyPort = getString(R.string.pref_proxyport);
402 final String keyUser = getString(R.string.pref_proxyuser); 385 final String keyUser = getString(R.string.pref_proxyuser);
403 final String keyPass = getString(R.string.pref_proxypass); 386 final String keyPass = getString(R.string.pref_proxypass);
404 if (key.equals(keyHost) || key.equals(keyPort) || key.equals(keyUser) || k ey.equals(keyPass)) 387 if (key.equals(keyHost) || key.equals(keyPort) || key.equals(keyUser) || k ey.equals(keyPass))
405 { 388 {
406 final String proxyHost = sharedPreferences.getString(keyHost, null); 389 final String proxyHost = sharedPreferences.getString(keyHost, null);
407 final String proxyPort = sharedPreferences.getString(keyPort, null); 390 final String proxyPort = sharedPreferences.getString(keyPort, null);
408 final String proxyUser = sharedPreferences.getString(keyUser, null); 391 final String proxyUser = sharedPreferences.getString(keyUser, null);
409 final String proxyPass = sharedPreferences.getString(keyPass, null); 392 final String proxyPass = sharedPreferences.getString(keyPass, null);
410 if (proxy != null) 393 if (proxy != null)
411 { 394 {
412 configureUserProxy(proxyConfiguration, proxyHost, proxyPort, null, pro xyUser, proxyPass); 395 configureUserProxy(proxyConfiguration, proxyHost, proxyPort, null, pro xyUser, proxyPass);
413 proxy.restart(proxyConfiguration.getProperty("handler")); 396 proxy.restart(proxyConfiguration.getProperty("handler"));
414 } 397 }
415 } 398 }
416 } 399 }
417 } 400 }
418 401
419 /** 402 /**
420 * @return the active proxy configuration's type or {@link ProxyRegistrationTy pe#UNKNOWN} if none is configured 403 * @return the active proxy configuration's type or {@link ProxyRegistrationTy pe#UNKNOWN} if none is configured
421 */ 404 */
422 public ProxyRegistrationType getProxyRegistrationType() 405 private ProxyRegistrationType getProxyRegistrationType()
423 { 406 {
424 return this.proxyConfigurator != null ? this.proxyConfigurator.getType() : P roxyRegistrationType.UNKNOWN; 407 return this.proxyConfigurator != null ? this.proxyConfigurator.getType() : P roxyRegistrationType.UNKNOWN;
425 } 408 }
426 409
427 /** 410 /**
428 * @return {@code true} if there is a valid proxy configurator and registratio n succeeded 411 * @return {@code true} if there is a valid proxy configurator and registratio n succeeded
429 */ 412 */
430 public boolean isRegistered() 413 public boolean isRegistered()
431 { 414 {
432 return this.proxyConfigurator != null && this.proxyConfigurator.isRegistered (); 415 return this.proxyConfigurator != null && this.proxyConfigurator.isRegistered ();
433 } 416 }
434 417
435 /** 418 /**
436 * @return {@code true} if registration type is {@link ProxyRegistrationType#N ATIVE} and registration succeeded 419 * @return {@code true} if registration type is {@link ProxyRegistrationType#N ATIVE} and registration succeeded
437 */ 420 */
438 public boolean isNativeProxyAutoConfigured() 421 public boolean isNativeProxyAutoConfigured()
439 { 422 {
440 return this.getProxyRegistrationType() == ProxyRegistrationType.NATIVE && th is.isRegistered(); 423 return this.getProxyRegistrationType() == ProxyRegistrationType.NATIVE && th is.isRegistered();
441 } 424 }
442 425
443 /** 426 /**
444 * @return {@code true} if registration type is {@link ProxyRegistrationType#M ANUAL} 427 * @return {@code true} if registration type is {@link ProxyRegistrationType#M ANUAL}
445 */ 428 */
446 public boolean isManual() 429 public boolean isManual()
Felix Dahlke 2014/08/19 16:14:46 IMO it'd be neat to have isNative() and isIptables
René Jeschke 2014/08/19 16:33:24 At least 'isIptables()'. Done.
447 { 430 {
448 return this.getProxyRegistrationType() == ProxyRegistrationType.MANUAL; 431 return this.getProxyRegistrationType() == ProxyRegistrationType.MANUAL;
432 }
433
434 /**
435 * @return {@code true} if registration type is {@link ProxyRegistrationType#I PTABLES}
436 */
437 public boolean isIptables()
438 {
439 return this.getProxyRegistrationType() == ProxyRegistrationType.IPTABLES;
449 } 440 }
450 441
451 @SuppressLint("NewApi") 442 @SuppressLint("NewApi")
452 private Notification getNotification() 443 private Notification getNotification()
453 { 444 {
454 final boolean filtering = AdblockPlus.getApplication().isFilteringEnabled(); 445 final boolean filtering = AdblockPlus.getApplication().isFilteringEnabled();
455 446
456 final int msgId; 447 final int msgId;
457 switch(this.getProxyRegistrationType()) 448 switch(this.getProxyRegistrationType())
458 { 449 {
459 case MANUAL: 450 case MANUAL:
460 if (this.isRegistered()) 451 if (this.isRegistered())
461 { 452 {
462 msgId = filtering ? R.string.notif_wifi : R.string.notif_wifi_nofilterin g; 453 msgId = filtering ? R.string.notif_wifi : R.string.notif_wifi_nofilterin g;
463 } 454 }
464 else 455 else
465 { 456 {
466 msgId = filtering ? R.string.notif_waiting : R.string.notif_wifi_nofilte ring; 457 msgId = filtering ? R.string.notif_waiting : R.string.notif_wifi_nofilte ring;
467 } 458 }
468 break; 459 break;
469 case NATIVE: 460 case NATIVE:
470 msgId = filtering ? R.string.notif_wifi : R.string.notif_wifi_nofiltering; 461 msgId = filtering ? R.string.notif_wifi : R.string.notif_wifi_nofiltering;
471 break; 462 break;
472 case IPTABLES: 463 case IPTABLES:
473 case CYANOGENMOD: 464 case CYANOGENMOD:
Felix Dahlke 2014/08/19 16:14:46 Do we currently filter non-wifi traffic on Cyanoge
René Jeschke 2014/08/19 16:33:24 Of course we are filtering all connection traffic
Felix Dahlke 2014/08/19 16:47:05 How about adding this to ProxyConfigurationType? S
René Jeschke 2014/08/23 11:33:31 As discussed in RL, we leave it like it is.
474 msgId = R.string.notif_all; 465 msgId = R.string.notif_all;
475 break; 466 break;
476 default: 467 default:
477 msgId = R.string.notif_waiting; 468 msgId = R.string.notif_waiting;
478 break; 469 break;
479 } 470 }
480 471
481 final NotificationCompat.Builder builder = new NotificationCompat.Builder(th is); 472 final NotificationCompat.Builder builder = new NotificationCompat.Builder(th is);
482 if (hideIcon && msgId != R.string.notif_waiting) 473 if (hideIcon && msgId != R.string.notif_waiting)
483 { 474 {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 { 540 {
550 if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED)) 541 if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED))
551 { 542 {
552 stopSelf(); 543 stopSelf();
553 } 544 }
554 } 545 }
555 }; 546 };
556 547
557 /** 548 /**
558 * <p> 549 * <p>
559 * A BR for simple message passing from various ProxyConfigurators. 550 * Proxy state change receiver.
560 * </p> 551 * </p>
561 * <p> 552 */
562 * The purpose of this BroadcastReceiver is to allow other components to send notifications to the service and to be prepared for upcoming service 553 private final BroadcastReceiver proxyStateChangedReceiver = new BroadcastRecei ver()
563 * refactorings.
Felix Dahlke 2014/08/19 16:14:46 As discussed on IRC before, I don't understand yet
564 * </p>
565 */
566 private final BroadcastReceiver commandBridge = new BroadcastReceiver()
567 { 554 {
568 @Override 555 @Override
569 public void onReceive(final Context context, final Intent intent) 556 public void onReceive(final Context context, final Intent intent)
570 { 557 {
571 if (intent != null) 558 if (intent != null && PROXY_STATE_CHANGED_ACTION.equals(intent.getAction() ))
572 { 559 {
573 switch (BridgeCommand.fromString(intent.getStringExtra("command"))) 560 final NotificationManager notificationManager = (NotificationManager) ge tSystemService(NOTIFICATION_SERVICE);
574 { 561 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification());
575 case STATE_CHANGED: 562 ProxyService.this.sendStateChangedBroadcast();
576 final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
577 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()) ;
578 ProxyService.this.sendStateChangedBroadcast();
579 break;
580 default:
581 // ignore
582 break;
583 }
584 } 563 }
585 } 564 }
586 }; 565 };
587 566
588 final class ProxyServer extends Server 567 final class ProxyServer extends Server
589 { 568 {
590 @Override 569 @Override
591 public void close() 570 public void close()
592 { 571 {
593 try 572 try
(...skipping 12 matching lines...) Expand all
606 @Override 585 @Override
607 public void log(final int level, final Object obj, final String message) 586 public void log(final int level, final Object obj, final String message)
608 { 587 {
609 if (level <= logLevel) 588 if (level <= logLevel)
610 { 589 {
611 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); 590 Log.println(7 - level, obj != null ? obj.toString() : TAG, message);
612 } 591 }
613 } 592 }
614 } 593 }
615 } 594 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld