You are here

function _varnish_terminal_run in Varnish 8

Same name and namespace in other branches
  1. 5 varnish.module \_varnish_terminal_run()
  2. 6 varnish.module \_varnish_terminal_run()
  3. 7 varnish.module \_varnish_terminal_run()

Send one or more commands to Varnish.

Parameters

mixed $commands: Either a single command (expressed as a string), or multiple commands (expressed as an array of strings).

Return value

array A multi-dimensional array, indexed firstly by the terminal IP address and port, then secondly by the command. The value is the response from Varnish.

2 calls to _varnish_terminal_run()
varnish_get_status in ./varnish.module
Get the status (up/down) of each of the varnish servers.
varnish_purge in ./varnish.module
Helper function to purge items for a host that matches the provided pattern.

File

./varnish.module, line 231
varnish.module

Code

function _varnish_terminal_run($commands) {
  $config = \Drupal::config('varnish.settings');

  // Convert single commands to an array so we can handle everything in the same way.
  if (!is_array($commands)) {
    $commands = [
      $commands,
    ];
  }
  $ret = [];
  $terminals = explode(' ', $config
    ->get('varnish_control_terminal'));

  // The variable varnish_socket_timeout defines the timeout in milliseconds.
  $timeout = $config
    ->get('varnish_socket_timeout');
  $seconds = (int) ($timeout / 1000);
  $microseconds = (int) ($timeout % 1000 * 1000);
  foreach ($terminals as $terminal) {
    list($server, $port) = explode(':', $terminal);
    $client = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
    socket_set_option($client, SOL_SOCKET, SO_SNDTIMEO, [
      'sec' => $seconds,
      'usec' => $microseconds,
    ]);
    socket_set_option($client, SOL_SOCKET, SO_RCVTIMEO, [
      'sec' => $seconds,
      'usec' => $microseconds,
    ]);
    if (@(!socket_connect($client, $server, $port))) {
      \Drupal::logger('varnish')
        ->error('Unable to connect to server socket @server:@port: %error', [
        '@server' => $server,
        '@port' => $port,
        '%error' => socket_strerror(socket_last_error($client)),
      ]);
      $ret[$terminal] = FALSE;

      // If a varnish server is unavailable, move on to the next in the list.
      continue;
    }
    $status = _varnish_read_socket($client);

    // Do we need to authenticate?
    if ($status['code'] == 107) {

      // Require authentication
      $secret = $config
        ->get('varnish_control_key');
      $challenge = substr($status['msg'], 0, 32);
      $pack = $challenge . "\n" . $secret . "\n" . $challenge . "\n";
      $key = hash('sha256', $pack);
      socket_write($client, "auth {$key}\n");
      $status = _varnish_read_socket($client);
      if ($status['code'] != 200) {
        \Drupal::logger('varnish')
          ->error('Authentication to server failed!');

        // Mark this terminal as an authentication failure, and move on to the
        // next terminal in the list.
        $ret[$terminal] = VARNISH_SERVER_STATUS_AUTHENTICATION_FAILURE;
        continue;
      }
    }
    foreach ($commands as $command) {
      if ($status = _varnish_execute_command($client, $command)) {
        $ret[$terminal][$command] = $status;
      }
    }
  }
  return $ret;
}