|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FOSHttpCache package. |
| 5 | + * |
| 6 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace FOS\HttpCache\ProxyClient; |
| 13 | + |
| 14 | +use FOS\HttpCache\Exception\ProxyUnreachableException; |
| 15 | +use FOS\HttpCache\ProxyClient\Invalidation\BanInterface; |
| 16 | +use FOS\HttpCache\ProxyClient\VarnishAdmin\Response; |
| 17 | + |
| 18 | +/** |
| 19 | + * Varnish Admin CLI (also known as Management Port) client |
| 20 | + */ |
| 21 | +class VarnishAdmin extends AbstractVarnishClient implements BanInterface |
| 22 | +{ |
| 23 | + const CLIS_CLOSE = 50; |
| 24 | + const CLIS_SYNTAX = 100; |
| 25 | + const CLIS_UNKNOWN = 101; |
| 26 | + const CLIS_UNIMPL = 102; |
| 27 | + const CLIS_TOOFEW = 104; |
| 28 | + const CLIS_TOOMANY = 105; |
| 29 | + const CLIS_PARAM = 106; |
| 30 | + const CLIS_AUTH = 107; |
| 31 | + const CLIS_OK = 200; |
| 32 | + const CLIS_TRUNCATED = 201; |
| 33 | + const CLIS_CANT = 300; |
| 34 | + const CLIS_COMMS = 400; |
| 35 | + |
| 36 | + const TIMEOUT = 3; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var string |
| 40 | + */ |
| 41 | + private $host; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var int |
| 45 | + */ |
| 46 | + private $port; |
| 47 | + |
| 48 | + private $connection; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var string[] |
| 52 | + */ |
| 53 | + private $queuedBans = []; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var string |
| 57 | + */ |
| 58 | + private $secret; |
| 59 | + |
| 60 | + public function __construct($host, $port, $secret = null) |
| 61 | + { |
| 62 | + $this->host = $host; |
| 63 | + $this->port = $port; |
| 64 | + $this->secret = $secret; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * {@inheritdoc} |
| 69 | + */ |
| 70 | + public function ban(array $headers) |
| 71 | + { |
| 72 | + $mappedHeaders = array_map( |
| 73 | + function ($name, $value) { |
| 74 | + return sprintf('obj.http.%s ~ "%s"', $name, $value); |
| 75 | + }, |
| 76 | + array_keys($headers), |
| 77 | + $headers |
| 78 | + ); |
| 79 | + |
| 80 | + $this->queuedBans[] = implode('&&', $mappedHeaders); |
| 81 | + |
| 82 | + return $this; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * {@inheritdoc} |
| 87 | + */ |
| 88 | + public function banPath($path, $contentType = null, $hosts = null) |
| 89 | + { |
| 90 | + $ban = sprintf('obj.http.%s ~ "%s"', self::HTTP_HEADER_URL, $path); |
| 91 | + |
| 92 | + if ($contentType) { |
| 93 | + $ban .= sprintf( |
| 94 | + ' && obj.http.content-type ~ "%s"', |
| 95 | + $contentType |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + if ($hosts) { |
| 100 | + $ban .= sprintf( |
| 101 | + ' && obj.http.%s ~ "%s"', |
| 102 | + self::HTTP_HEADER_HOST, |
| 103 | + $this->createHostsRegex($hosts) |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + $this->queuedBans[] = $ban; |
| 108 | + |
| 109 | + return $this; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * {@inheritdoc} |
| 114 | + */ |
| 115 | + public function flush() |
| 116 | + { |
| 117 | + foreach ($this->queuedBans as $ban) { |
| 118 | + $this->executeCommand('ban', $ban); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private function getConnection() |
| 123 | + { |
| 124 | + if ($this->connection === null) { |
| 125 | + $connection = fsockopen($this->host, $this->port, $errno, $errstr, self::TIMEOUT); |
| 126 | + if ($connection === false) { |
| 127 | + throw new ProxyUnreachableException('Unreachable'); |
| 128 | + } |
| 129 | + |
| 130 | + stream_set_timeout($connection, self::TIMEOUT); |
| 131 | + $response = $this->read($connection); |
| 132 | + |
| 133 | + switch ($response->getStatusCode()) { |
| 134 | + case self::CLIS_AUTH: |
| 135 | + $this->authenticate(substr($response->getResponse(), 0, 32), $connection); |
| 136 | + break; |
| 137 | + } |
| 138 | + |
| 139 | + $this->connection = $connection; |
| 140 | + } |
| 141 | + |
| 142 | + return $this->connection; |
| 143 | + } |
| 144 | + |
| 145 | + private function read($connection) |
| 146 | + { |
| 147 | + while (!feof($connection)) { |
| 148 | + $line = fgets($connection, 1024); |
| 149 | + if ($line === false) { |
| 150 | + throw new ProxyUnreachableException('bla'); |
| 151 | + } |
| 152 | + if (strlen($line) === 13 |
| 153 | + && preg_match('/^(?P<status>\d{3}) (?P<length>\d+)/', $line, $matches) |
| 154 | + ) { |
| 155 | + $response = ''; |
| 156 | + while (!feof($connection) && strlen($response) < $matches['length']) { |
| 157 | + $response .= fread($connection, $matches['length']); |
| 158 | + } |
| 159 | + |
| 160 | + return new Response($matches['status'], $response); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private function authenticate($challenge, $connection = null) |
| 166 | + { |
| 167 | + $data = sprintf("%1\$s\n%2\$s\n%1\$s\n", $challenge, $this->secret); |
| 168 | + $hash = hash('sha256', $data); |
| 169 | + |
| 170 | + $this->executeCommand('auth', $hash, $connection); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Execute a command |
| 175 | + * |
| 176 | + * @param string $command |
| 177 | + * @param string $param |
| 178 | + * @param \resource $connection |
| 179 | + * |
| 180 | + * @return Response |
| 181 | + */ |
| 182 | + private function executeCommand($command, $param = null, $connection = null) |
| 183 | + { |
| 184 | + $connection = $connection ?: $this->getConnection(); |
| 185 | + $all = sprintf("%s %s\n", $command, $param); |
| 186 | + fwrite($connection, $all); |
| 187 | + |
| 188 | + $response = $this->read($connection); |
| 189 | + if ($response->getStatusCode() !== 200) { |
| 190 | + throw new \RuntimeException($response->getResponse()); |
| 191 | + } |
| 192 | + |
| 193 | + return $response; |
| 194 | + } |
| 195 | +} |
0 commit comments