| LEFT | RIGHT | 
|---|
| 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  Loading... | 
| 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 |  | 
| 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  Loading... | 
| 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  Loading... | 
| 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  Loading... | 
| 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() | 
| 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     { | 
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 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. |  | 
| 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  Loading... | 
| 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 } | 
| LEFT | RIGHT | 
|---|