squid.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. HTTPSEverywhere for Desktopd
  5. Copyright (C) 2015 Desktopd Developer(s)
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Affero General Public License as
  8. published by the Free Software Foundation, either version 3 of the
  9. License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Affero General Public License for more details.
  14. You should have received a copy of the GNU Affero General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. https://www.gnu.org/licenses/agpl.html
  17. */
  18. /**
  19. An illustrative use of PHP HTTPS Everywhere (HTTPS Everywhere for Desktopd)
  20. Works as a 'redirect_program' for a Squid proxy server. [1]
  21. This is a functional equivalent of squid.pl in perl-HTTPSEverywhere. [2]
  22. [1]: http://www.squid-cache.org/Doc/config/redirect_program/
  23. [2]: https://github.com/mikecardwell/perl-HTTPSEverywhere
  24. */
  25. use Desktopd\HTTPSEverywhere\HTTPSEverywhere;
  26. // autoloader
  27. require_once dirname(__DIR__) . '/tests/autoloader.php';
  28. $input = defined('STDIN') && is_resource(STDIN) ? STDIN : fopen('php://stdin', 'rb');
  29. if (!is_resource($input)) {
  30. file_put_contents('php://stderr', "Failed to open the input stream\n");
  31. exit(1);
  32. }
  33. /*
  34. Set up HTTPS Everywhere.
  35. This can take up to thousands of milliseconds.
  36. */
  37. $ruleDir = dirname(__DIR__) . '/https-everywhere/https-everywhere/src/chrome/content/rules';
  38. $userRuleDir = dirname(__DIR__) . '/https-everywhere/HTTPSEverywhereUserRules';
  39. $httpsEverywhere = new HTTPSEverywhere($ruleDir, $userRuleDir);
  40. file_put_contents('php://stderr', sprintf(
  41. "[HTTPS Everywhere] %d ruleset(s) available\n"
  42. , count($httpsEverywhere)
  43. ));
  44. $time = time(); // timestamp
  45. $total = 0;
  46. $rewrittenCount = 0;
  47. while (!feof($input)) {
  48. if (!is_resource($input)) {
  49. file_put_contents('php://stderr', "Invalid input stream\n");
  50. break;
  51. }
  52. $line = explode(' ', trim(fgets($input)), 2)[0];
  53. if ('' === $line) {
  54. // At the end of the stream, we need to read an empty line
  55. // but an empty line does not mean the end of the stream
  56. // this also occurs when the timeout is hit (trim(false) === '')
  57. continue;
  58. }
  59. // refresh rulesets every 3600 seconds
  60. if (time() - $time >= 3600) {
  61. $httpsEverywhere = new HTTPSEverywhere($ruleDir, $userRuleDir);
  62. file_put_contents('php://stderr', sprintf(
  63. "[HTTPS Everywhere] %d ruleset(s) available\n"
  64. , count($httpsEverywhere)
  65. ));
  66. $time = time();
  67. }
  68. // rewrite!
  69. $rewritten = $httpsEverywhere->rewrite($line);
  70. if ($rewritten !== $line) {
  71. // rewritten
  72. echo '301:', $rewritten, PHP_EOL;
  73. $rewrittenCount++;
  74. } else {
  75. // not rewritten
  76. echo $line, PHP_EOL;
  77. }
  78. $total++;
  79. }
  80. file_put_contents('php://stderr', "[HTTPS Everywhere] Processed: $total, Rewritten: $rewrittenCount\n");
  81. // vim: ts=4 et ai