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: Update 1 Created Aug. 19, 2014, 10:43 a.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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 import android.util.Log; 50 import android.util.Log;
51 51
52 public class ProxyService extends Service implements OnSharedPreferenceChangeLis tener 52 public class ProxyService extends Service implements OnSharedPreferenceChangeLis tener
53 { 53 {
54 private static final String TAG = Utils.getTag(ProxyService.class); 54 private static final String TAG = Utils.getTag(ProxyService.class);
55 55
56 private static final String LOCALHOST = "127.0.0.1"; 56 private static final String LOCALHOST = "127.0.0.1";
57 57
58 private static final int[] PORT_VARIANTS = new int[] {-1, 2020, 3030, 4040, 50 50, 6060, 7070, 9090, 1234, 12345, 4321, 0}; 58 private static final int[] PORT_VARIANTS = new int[] {-1, 2020, 3030, 4040, 50 50, 6060, 7070, 9090, 1234, 12345, 4321, 0};
59 59
60 // FIXME remove me
61 private static final boolean LOG_REQUESTS = false; 60 private static final boolean LOG_REQUESTS = false;
62 61
63 static final int ONGOING_NOTIFICATION_ID = R.string.app_name; 62 static final int ONGOING_NOTIFICATION_ID = R.string.app_name;
64 63
65 private static final long POSITION_RIGHT = Build.VERSION.SDK_INT >= Build.VERS ION_CODES.GINGERBREAD ? Long.MIN_VALUE : Long.MAX_VALUE; 64 private static final long POSITION_RIGHT = Build.VERSION.SDK_INT >= Build.VERS ION_CODES.GINGERBREAD ? Long.MIN_VALUE : Long.MAX_VALUE;
66 65
67 /** 66 /**
68 * Broadcasted when service starts or stops. 67 * Broadcasted when service starts or stops.
69 */ 68 */
70 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";
71 70
72 /** 71 /**
73 * Broadcasted if proxy fails to start. 72 * Broadcasted if proxy fails to start.
74 */ 73 */
75 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";
76 75
77 /** 76 /**
78 * Common command bridge 77 * Common command bridge
79 */ 78 */
80 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";
81 80
82 boolean hideIcon; 81 boolean hideIcon;
83 82
84 protected ProxyServer proxy = null; 83 protected ProxyServer proxy = null;
85 84
86 protected int port; 85 protected int port;
87 86
88 private final Properties proxyConfiguration = new Properties(); 87 private final Properties proxyConfiguration = new Properties();
89 88
90 private ProxyConfigurator proxyConfigurator = null; 89 private ProxyConfigurator proxyConfigurator = null;
91 90
92 @SuppressLint("NewApi") 91 @SuppressLint("NewApi")
93 @Override 92 @Override
94 public void onCreate() 93 public void onCreate()
95 { 94 {
96 super.onCreate(); 95 super.onCreate();
97 96
98 // FIXME 'StrictMode' is API9
99 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 97 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
100 .permitAll() 98 .permitAll()
101 .penaltyLog() 99 .penaltyLog()
102 .build()); 100 .build());
103 101
104 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(this); 102 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(this);
105 103
106 // Try to read user proxy settings 104 // Try to read user proxy settings
107 String proxyHost = System.getProperty("http.proxyHost");; 105 String proxyHost = System.getProperty("http.proxyHost");;
108 String proxyPort = System.getProperty("http.proxyPort"); 106 String proxyPort = System.getProperty("http.proxyPort");
109 final String proxyExcl = System.getProperty("http.nonProxyHosts"); 107 final String proxyExcl = System.getProperty("http.nonProxyHosts");
110 String proxyUser = null; 108 String proxyUser = null;
111 String proxyPass = null; 109 String proxyPass = null;
112 110
113 if (proxyHost == null || proxyPort == null) 111 if (proxyHost == null || proxyPort == null)
114 { 112 {
115 // Read application settings 113 // Read application settings
116 proxyHost = prefs.getString(getString(R.string.pref_proxyhost), null); 114 proxyHost = prefs.getString(getString(R.string.pref_proxyhost), null);
117 proxyPort = prefs.getString(getString(R.string.pref_proxyport), null); 115 proxyPort = prefs.getString(getString(R.string.pref_proxyport), null);
118 proxyUser = prefs.getString(getString(R.string.pref_proxyuser), null); 116 proxyUser = prefs.getString(getString(R.string.pref_proxyuser), null);
119 proxyPass = prefs.getString(getString(R.string.pref_proxypass), null); 117 proxyPass = prefs.getString(getString(R.string.pref_proxypass), null);
120 } 118 }
121 119
122 registerReceiver(this.proxyReceiver, new IntentFilter(ProxyService.BROADCAST _PROXY_FAILED)); 120 registerReceiver(this.proxyReceiver, new IntentFilter(ProxyService.BROADCAST _PROXY_FAILED));
123 registerReceiver(this.commandBridge, new IntentFilter(ProxyService.COMMAND_B RIDGE_ACTION)); 121 registerReceiver(this.proxyStateChangedReceiver, new IntentFilter(ProxyServi ce.PROXY_STATE_CHANGED_ACTION));
124 122
125 final InetAddress inetAddress; 123 final InetAddress inetAddress;
126 try 124 try
127 { 125 {
128 inetAddress = InetAddress.getByName(LOCALHOST); 126 inetAddress = InetAddress.getByName(LOCALHOST);
129 } 127 }
130 catch (final UnknownHostException e) 128 catch (final UnknownHostException e)
131 { 129 {
132 sendBroadcast(new Intent(BROADCAST_PROXY_FAILED) 130 sendBroadcast(new Intent(BROADCAST_PROXY_FAILED)
133 .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
228 { 226 {
229 return START_STICKY; 227 return START_STICKY;
230 } 228 }
231 229
232 @Override 230 @Override
233 public void onDestroy() 231 public void onDestroy()
234 { 232 {
235 super.onDestroy(); 233 super.onDestroy();
236 234
237 unregisterReceiver(this.proxyReceiver); 235 unregisterReceiver(this.proxyReceiver);
238 unregisterReceiver(this.commandBridge); 236 unregisterReceiver(this.proxyStateChangedReceiver);
239 237
240 if (this.proxyConfigurator != null) 238 if (this.proxyConfigurator != null)
241 { 239 {
242 this.proxyConfigurator.unregisterProxy(); 240 this.proxyConfigurator.unregisterProxy();
243 this.proxyConfigurator.shutdown(); 241 this.proxyConfigurator.shutdown();
244 } 242 }
245 243
246 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false) ); 244 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false) );
247 245
248 // Stop proxy server 246 // Stop proxy server
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 final String proxyAuth = "Basic " + new String(Base64.encode(proxyUser + " :" + proxyPass)); 331 final String proxyAuth = "Basic " + new String(Base64.encode(proxyUser + " :" + proxyPass));
334 config.put("adblock.auth", proxyAuth); 332 config.put("adblock.auth", proxyAuth);
335 if (regType.getProxyType() == ProxyServerType.HTTPS) 333 if (regType.getProxyType() == ProxyServerType.HTTPS)
336 { 334 {
337 config.put("https.auth", proxyAuth); 335 config.put("https.auth", proxyAuth);
338 } 336 }
339 } 337 }
340 } 338 }
341 339
342 /** 340 /**
343 * Lean method to send a {@link BridgeCommand} without additional extras.
344 *
345 * @param context
346 * the context to use for sending the broadcast
347 * @param command
348 * the command
349 */
350 public static void sendBridgeCommand(final Context context, final BridgeComman d command)
351 {
352 context.sendBroadcast(
353 new Intent(COMMAND_BRIDGE_ACTION)
354 .putExtra("command", command.toString()));
355 }
356
357 /**
358 * @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}
359 */ 342 */
360 public static boolean isLocalhost(final String host) 343 public static boolean isLocalhost(final String host)
361 { 344 {
362 try 345 try
363 { 346 {
364 if (StringUtils.isEmpty(host)) 347 if (StringUtils.isEmpty(host))
365 { 348 {
366 return false; 349 return false;
367 } 350 }
(...skipping 20 matching lines...) Expand all
388 } 371 }
389 catch (final ClassCastException e) 372 catch (final ClassCastException e)
390 { 373 {
391 // ignore - default handler in use 374 // ignore - default handler in use
392 } 375 }
393 } 376 }
394 377
395 @Override 378 @Override
396 public void onSharedPreferenceChanged(final SharedPreferences sharedPreference s, final String key) 379 public void onSharedPreferenceChanged(final SharedPreferences sharedPreference s, final String key)
397 { 380 {
398 // TODO verify this
399 if (this.getProxyRegistrationType() != ProxyRegistrationType.NATIVE) 381 if (this.getProxyRegistrationType() != ProxyRegistrationType.NATIVE)
400 { 382 {
401 final String keyHost = getString(R.string.pref_proxyhost); 383 final String keyHost = getString(R.string.pref_proxyhost);
402 final String keyPort = getString(R.string.pref_proxyport); 384 final String keyPort = getString(R.string.pref_proxyport);
403 final String keyUser = getString(R.string.pref_proxyuser); 385 final String keyUser = getString(R.string.pref_proxyuser);
404 final String keyPass = getString(R.string.pref_proxypass); 386 final String keyPass = getString(R.string.pref_proxypass);
405 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))
406 { 388 {
407 final String proxyHost = sharedPreferences.getString(keyHost, null); 389 final String proxyHost = sharedPreferences.getString(keyHost, null);
408 final String proxyPort = sharedPreferences.getString(keyPort, null); 390 final String proxyPort = sharedPreferences.getString(keyPort, null);
409 final String proxyUser = sharedPreferences.getString(keyUser, null); 391 final String proxyUser = sharedPreferences.getString(keyUser, null);
410 final String proxyPass = sharedPreferences.getString(keyPass, null); 392 final String proxyPass = sharedPreferences.getString(keyPass, null);
411 if (proxy != null) 393 if (proxy != null)
412 { 394 {
413 configureUserProxy(proxyConfiguration, proxyHost, proxyPort, null, pro xyUser, proxyPass); 395 configureUserProxy(proxyConfiguration, proxyHost, proxyPort, null, pro xyUser, proxyPass);
414 proxy.restart(proxyConfiguration.getProperty("handler")); 396 proxy.restart(proxyConfiguration.getProperty("handler"));
415 } 397 }
416 } 398 }
417 } 399 }
418 } 400 }
419 401
420 /** 402 /**
421 * @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
422 */ 404 */
423 public ProxyRegistrationType getProxyRegistrationType() 405 private ProxyRegistrationType getProxyRegistrationType()
424 { 406 {
425 return this.proxyConfigurator != null ? this.proxyConfigurator.getType() : P roxyRegistrationType.UNKNOWN; 407 return this.proxyConfigurator != null ? this.proxyConfigurator.getType() : P roxyRegistrationType.UNKNOWN;
426 } 408 }
427 409
428 /** 410 /**
429 * @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
430 */ 412 */
431 public boolean isRegistered() 413 public boolean isRegistered()
432 { 414 {
433 return this.proxyConfigurator != null && this.proxyConfigurator.isRegistered (); 415 return this.proxyConfigurator != null && this.proxyConfigurator.isRegistered ();
434 } 416 }
435 417
436 /** 418 /**
437 * @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
438 */ 420 */
439 public boolean isNativeProxyAutoConfigured() 421 public boolean isNativeProxyAutoConfigured()
440 { 422 {
441 return this.getProxyRegistrationType() == ProxyRegistrationType.NATIVE && th is.isRegistered(); 423 return this.getProxyRegistrationType() == ProxyRegistrationType.NATIVE && th is.isRegistered();
442 } 424 }
443 425
444 /** 426 /**
445 * @return {@code true} if registration type is {@link ProxyRegistrationType#M ANUAL} 427 * @return {@code true} if registration type is {@link ProxyRegistrationType#M ANUAL}
446 */ 428 */
447 public boolean isManual() 429 public boolean isManual()
448 { 430 {
449 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;
450 } 440 }
451 441
452 @SuppressLint("NewApi") 442 @SuppressLint("NewApi")
453 private Notification getNotification() 443 private Notification getNotification()
454 { 444 {
455 final boolean filtering = AdblockPlus.getApplication().isFilteringEnabled(); 445 final boolean filtering = AdblockPlus.getApplication().isFilteringEnabled();
456 446
457 final int msgId; 447 final int msgId;
458 switch(this.getProxyRegistrationType()) 448 switch(this.getProxyRegistrationType())
459 { 449 {
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 { 540 {
551 if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED)) 541 if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED))
552 { 542 {
553 stopSelf(); 543 stopSelf();
554 } 544 }
555 } 545 }
556 }; 546 };
557 547
558 /** 548 /**
559 * <p> 549 * <p>
560 * A BR for simple message passing from various ProxyConfigurators. 550 * Proxy state change receiver.
561 * </p> 551 * </p>
562 * <p> 552 */
563 * 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()
564 * refactorings.
565 * </p>
566 */
567 private final BroadcastReceiver commandBridge = new BroadcastReceiver()
568 { 554 {
569 @Override 555 @Override
570 public void onReceive(final Context context, final Intent intent) 556 public void onReceive(final Context context, final Intent intent)
571 { 557 {
572 if (intent != null) 558 if (intent != null && PROXY_STATE_CHANGED_ACTION.equals(intent.getAction() ))
573 { 559 {
574 switch (BridgeCommand.fromString(intent.getStringExtra("command"))) 560 final NotificationManager notificationManager = (NotificationManager) ge tSystemService(NOTIFICATION_SERVICE);
575 { 561 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification());
576 case STATE_CHANGED: 562 ProxyService.this.sendStateChangedBroadcast();
577 final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
578 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()) ;
579 ProxyService.this.sendStateChangedBroadcast();
580 break;
581 default:
582 // ignore
583 break;
584 }
585 } 563 }
586 } 564 }
587 }; 565 };
588 566
589 final class ProxyServer extends Server 567 final class ProxyServer extends Server
590 { 568 {
591 @Override 569 @Override
592 public void close() 570 public void close()
593 { 571 {
594 try 572 try
(...skipping 12 matching lines...) Expand all
607 @Override 585 @Override
608 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)
609 { 587 {
610 if (level <= logLevel) 588 if (level <= logLevel)
611 { 589 {
612 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); 590 Log.println(7 - level, obj != null ? obj.toString() : TAG, message);
613 } 591 }
614 } 592 }
615 } 593 }
616 } 594 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld