NoProxyPattern.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Util;
  12. /**
  13. * Tests URLs against no_proxy patterns.
  14. */
  15. class NoProxyPattern
  16. {
  17. /**
  18. * @var string[]
  19. */
  20. protected $rules = array();
  21. /**
  22. * @param string $pattern no_proxy pattern
  23. */
  24. public function __construct($pattern)
  25. {
  26. $this->rules = preg_split("/[\s,]+/", $pattern);
  27. }
  28. /**
  29. * Test a URL against the stored pattern.
  30. *
  31. * @param string $url
  32. *
  33. * @return true if the URL matches one of the rules.
  34. */
  35. public function test($url)
  36. {
  37. $host = parse_url($url, PHP_URL_HOST);
  38. $port = parse_url($url, PHP_URL_PORT);
  39. if (empty($port)) {
  40. switch (parse_url($url, PHP_URL_SCHEME)) {
  41. case 'http':
  42. $port = 80;
  43. break;
  44. case 'https':
  45. $port = 443;
  46. break;
  47. }
  48. }
  49. foreach ($this->rules as $rule) {
  50. if ($rule == '*') {
  51. return true;
  52. }
  53. $match = false;
  54. list($ruleHost) = explode(':', $rule);
  55. list($base) = explode('/', $ruleHost);
  56. if (filter_var($base, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  57. // ip or cidr match
  58. if (!isset($ip)) {
  59. $ip = gethostbyname($host);
  60. }
  61. if (strpos($ruleHost, '/') === false) {
  62. $match = $ip === $ruleHost;
  63. } else {
  64. // gethostbyname() failed to resolve $host to an ip, so we assume
  65. // it must be proxied to let the proxy's DNS resolve it
  66. if ($ip === $host) {
  67. $match = false;
  68. } else {
  69. // match resolved IP against the rule
  70. $match = self::inCIDRBlock($ruleHost, $ip);
  71. }
  72. }
  73. } else {
  74. // match end of domain
  75. $haystack = '.' . trim($host, '.') . '.';
  76. $needle = '.'. trim($ruleHost, '.') .'.';
  77. $match = stripos(strrev($haystack), strrev($needle)) === 0;
  78. }
  79. // final port check
  80. if ($match && strpos($rule, ':') !== false) {
  81. list(, $rulePort) = explode(':', $rule);
  82. if (!empty($rulePort) && $port != $rulePort) {
  83. $match = false;
  84. }
  85. }
  86. if ($match) {
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. /**
  93. * Check an IP address against a CIDR
  94. *
  95. * http://framework.zend.com/svn/framework/extras/incubator/library/ZendX/Whois/Adapter/Cidr.php
  96. *
  97. * @param string $cidr IPv4 block in CIDR notation
  98. * @param string $ip IPv4 address
  99. *
  100. * @return bool
  101. */
  102. private static function inCIDRBlock($cidr, $ip)
  103. {
  104. // Get the base and the bits from the CIDR
  105. list($base, $bits) = explode('/', $cidr);
  106. // Now split it up into it's classes
  107. list($a, $b, $c, $d) = explode('.', $base);
  108. // Now do some bit shifting/switching to convert to ints
  109. $i = ($a << 24) + ($b << 16) + ($c << 8) + $d;
  110. $mask = $bits == 0 ? 0 : (~0 << (32 - $bits));
  111. // Here's our lowest int
  112. $low = $i & $mask;
  113. // Here's our highest int
  114. $high = $i | (~$mask & 0xFFFFFFFF);
  115. // Now split the ip we're checking against up into classes
  116. list($a, $b, $c, $d) = explode('.', $ip);
  117. // Now convert the ip we're checking against to an int
  118. $check = ($a << 24) + ($b << 16) + ($c << 8) + $d;
  119. // If the ip is within the range, including highest/lowest values,
  120. // then it's within the CIDR range
  121. return $check >= $low && $check <= $high;
  122. }
  123. }