*/ protected $endpoints = array(); /** * Working transport, or '' to probe. * * @var string */ protected $transport; /** * Seconds to add to our clock (source's clock skew). * * @var int */ protected $clock_offset = 0; /** * Bytes received (stats). * * @var int */ public $received = 0; /** * Creates a client. * * @param array $conn Connection (url, kid, secret, endpoints, transport, clock_offset). * @throws Remote_Exception For damaged connections. */ public function __construct( array $conn ) { $this->url = untrailingslashit( (string) $conn['url'] ); $this->kid = (string) $conn['kid']; $secret = Secrets::b64url_decode( (string) $conn['secret'] ); $this->endpoints = Connections::clean_endpoints( $conn['endpoints'] ?? array() ); $this->transport = isset( $conn['transport'] ) ? (string) $conn['transport'] : ''; $this->clock_offset = isset( $conn['clock_offset'] ) ? (int) $conn['clock_offset'] : 0; if ( '' !== $this->transport && 'local' !== $this->transport && '' === $this->endpoint( $this->transport ) ) { // A shape saved before the source reported its addresses: find a working one again. $this->transport = ''; } if ( false === $secret || 32 !== strlen( $secret ) ) { throw new Remote_Exception( 'bad_connection', __( 'This connection is damaged. Remove it and add the key again.', 'accessnow-site-replica' ) ); } $this->secret = $secret; } /** * The transport that worked (to save on the connection). */ public function transport() { return $this->transport; } /** * Takes the addresses the source reported in its hello answer. * * @param array $endpoints {ajax, rest}. * @return array The validated addresses. */ public function learn_endpoints( $endpoints ) { $clean = Connections::clean_endpoints( $endpoints ); if ( $clean ) { $this->endpoints = $clean; } return $this->endpoints; } /** * Clock offset learned from the source. */ public function clock_offset() { return $this->clock_offset; } /** * Calls an operation and returns decoded frames. * * @param string $op Operation. * @param array $args Arguments. * @param array $budget {ms, bytes}. * @return array{frames: array[], end: array} * @throws Remote_Exception On failure. */ public function call( $op, array $args, array $budget ) { $transports = '' !== $this->transport ? array( $this->transport ) : array_values( array_filter( self::TRANSPORTS, array( $this, 'endpoint' ) ) ); $last_error = null; foreach ( $transports as $transport ) { try { $result = $this->call_via( $transport, $op, $args, $budget, true ); $this->transport = $transport; return $result; } catch ( Remote_Exception $e ) { $last_error = $e; // Only keep probing when this shape looks blocked or missing, not for real API errors. if ( ! in_array( $e->error_code, array( 'blocked', 'not_found', 'html_response', 'http_error' ), true ) || '' !== $this->transport ) { throw $e; } } } throw $last_error; } /** * Calls several operations at once (parallel connections), once the URL shape is known. * * Hosts that cap the speed of each connection are much faster this way. Each result is * either a call() result or the Remote_Exception for that call; keys are preserved. * * @param array[] $calls key => array( op, args, budget ). * @return array key => result array or Remote_Exception. */ public function call_multi( array $calls ) { $out = array(); if ( count( $calls ) < 2 || '' === $this->transport || ! class_exists( '\WpOrg\Requests\Requests' ) ) { foreach ( $calls as $k => $c ) { try { $out[ $k ] = $this->call( $c[0], $c[1], $c[2] ); } catch ( Remote_Exception $e ) { $out[ $k ] = $e; } } return $out; } $requests = array(); $envelopes = array(); $timeout = 0; foreach ( $calls as $k => $c ) { $envelopes[ $k ] = Signer::envelope( $this->secret, $this->kid, $c[0], $c[1], $c[2], $this->clock_offset ); $requests[ $k ] = array( 'url' => $this->endpoint( $this->transport ), 'type' => 'POST', 'data' => $envelopes[ $k ]['body'], 'headers' => array( 'Accept-Encoding' => 'identity', 'Content-Type' => 'application/x-www-form-urlencoded', ), ); $timeout = max( $timeout, self::timeout_for( $c[2] ) ); } $hooks = new \WpOrg\Requests\Hooks(); $hooks->register( 'curl.before_multi_add', static function ( $handle ) { self::stall_guard( $handle ); } ); self::allow_time( $timeout ); $t0 = microtime( true ); $responses = \WpOrg\Requests\Requests::request_multiple( $requests, array( 'timeout' => $timeout, 'connect_timeout' => 20, 'useragent' => $this->user_agent(), 'follow_redirects' => false, 'verify' => $this->ssl_verify() ? ABSPATH . WPINC . '/certificates/ca-bundle.crt' : false, 'verifyname' => $this->ssl_verify(), 'hooks' => $hooks, ) ); foreach ( $calls as $k => $c ) { $r = isset( $responses[ $k ] ) ? $responses[ $k ] : null; if ( $r instanceof \WpOrg\Requests\Response ) { $headers = array(); foreach ( $r->headers as $name => $value ) { $headers[ strtolower( (string) $name ) ] = $value; } $response = array( 'headers' => $headers, 'body' => (string) $r->body, 'response' => array( 'code' => (int) $r->status_code, 'message' => '', ), ); } else { $response = new \WP_Error( 'http_request_failed', $r instanceof \Exception ? $r->getMessage() : 'No response.' ); } try { $out[ $k ] = $this->parse( $response, $envelopes[ $k ], $t0 ); } catch ( Remote_Exception $e ) { if ( 'clock_skew_fixed' === $e->error_code ) { try { $out[ $k ] = $this->call( $c[0], $c[1], $c[2] ); // Clock corrected: this one again, alone. } catch ( Remote_Exception $e2 ) { $out[ $k ] = $e2; } } else { $out[ $k ] = $e; } } } // The HTTP library leaves reference cycles that hold every answer until PHP's cycle // collector happens to run: free them now, or memory grows with each round. unset( $responses, $requests, $hooks ); gc_collect_cycles(); return $out; } /** * One attempt over one URL shape. Retries once after a clock-skew correction. * * @param string $transport Transport. * @param string $op Operation. * @param array $args Args. * @param array $budget Budget. * @param bool $allow_skew Retry after a skew correction. * @throws Remote_Exception On failure. */ private function call_via( $transport, $op, array $args, array $budget, $allow_skew ) { $envelope = Signer::envelope( $this->secret, $this->kid, $op, $args, $budget, $this->clock_offset ); $stall = static function ( $handle ) { self::stall_guard( $handle ); }; add_action( 'http_api_curl', $stall ); self::allow_time( self::timeout_for( $budget ) ); $t0 = microtime( true ); $response = wp_remote_post( $this->endpoint( $transport ), array( 'body' => $envelope['body'], 'timeout' => self::timeout_for( $budget ), 'redirection' => 0, 'httpversion' => '1.1', 'user-agent' => $this->user_agent(), 'headers' => array( 'Accept-Encoding' => 'identity' ), 'sslverify' => $this->ssl_verify(), ) ); remove_action( 'http_api_curl', $stall ); try { return $this->parse( $response, $envelope, $t0 ); } catch ( Remote_Exception $e ) { if ( 'clock_skew_fixed' !== $e->error_code ) { throw $e; } if ( $allow_skew ) { return $this->call_via( $transport, $op, $args, $budget, false ); } throw new Remote_Exception( 'clock_skew', __( 'The clocks on the two servers differ too much.', 'accessnow-site-replica' ) ); } } /** * Total time allowed for one answer. * * Some hosts cap each connection at a few dozen KB/s, so a full answer can take minutes to * arrive: allow about 16 KB/s for the budget's bytes. Stalls are caught much earlier by * stall_guard(). * * @param array $budget {ms, bytes}. */ private static function timeout_for( array $budget ) { return (int) ceil( ( isset( $budget['ms'] ) ? (int) $budget['ms'] : 8000 ) / 1000 ) + 60 + (int) ceil( ( isset( $budget['bytes'] ) ? (int) $budget['bytes'] : 8388608 ) / 16384 ); } /** * Makes sure PHP's own time limit can't end the request first. On some systems (macOS, * some hosts) waiting for the network counts towards max_execution_time. * * @param int $timeout Request timeout in seconds. */ private static function allow_time( $timeout ) { if ( function_exists( 'set_time_limit' ) ) { @set_time_limit( (int) $timeout + 30 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, Squiz.PHP.DiscouragedFunctions.Discouraged -- Waiting for a slow host counts as run time on some servers. } } /** * Gives up on a connection only when data stops flowing: below 512 B/s for a whole minute. * * @param resource|\CurlHandle $handle cURL handle. */ private static function stall_guard( $handle ) { if ( defined( 'CURLOPT_LOW_SPEED_LIMIT' ) ) { curl_setopt( $handle, CURLOPT_LOW_SPEED_LIMIT, 512 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt( $handle, CURLOPT_LOW_SPEED_TIME, 60 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 20 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt } } /** * User agent for requests. */ private function user_agent() { return 'SiteReplica/' . ACCESSNOW_REPLICA_VERSION . '; ' . home_url(); } /** * Whether to verify the source's certificate (not for local development hosts). */ private function ssl_verify() { return ! Environment::is_local_host( (string) wp_parse_url( $this->url, PHP_URL_HOST ) ); } /** * Turns an HTTP response (WordPress format) into decoded frames or a plain-language error. * * @param array|\WP_Error $response Response. * @param array $envelope Envelope sent (for the nonce). * @param float $t0 When the request started. * @return array{frames: array[], end: array} * @throws Remote_Exception On failure ('clock_skew_fixed' when the clock was corrected). */ private function parse( $response, array $envelope, $t0 ) { if ( is_wp_error( $response ) ) { $message = $response->get_error_message(); if ( false !== stripos( $message, 'Connection timed out' ) || ( false !== stripos( $message, 'connect' ) && false !== stripos( $message, 'timed out' ) ) ) { // Not a size problem: the host did not accept the connection in time (busy or rate-limiting). throw new Remote_Exception( 'connect_timeout', __( 'The source server did not accept the connection within 20 s (busy or limiting connections).', 'accessnow-site-replica' ), true, 10 ); } if ( false !== stripos( $message, 'timed out' ) || false !== stripos( $message, 'timeout' ) || false !== stripos( $message, 'too slow' ) ) { throw new Remote_Exception( 'timeout', sprintf( /* translators: %d: seconds. */ __( 'The source site stopped sending data (after %d s). Retrying with smaller pieces.', 'accessnow-site-replica' ), (int) round( microtime( true ) - $t0 ) ), true, 5 ); } if ( false !== stripos( $message, 'SSL' ) || false !== stripos( $message, 'certificate' ) ) { throw new Remote_Exception( 'ssl', sprintf( /* translators: %s: error. */ __( 'Secure connection to the source failed: %s', 'accessnow-site-replica' ), $message ) ); } throw new Remote_Exception( 'network', sprintf( /* translators: %s: error. */ __( 'Could not reach the source site: %s', 'accessnow-site-replica' ), $message ), true, 10 ); } $code = (int) wp_remote_retrieve_response_code( $response ); $body = (string) wp_remote_retrieve_body( $response ); $content_type = (string) wp_remote_retrieve_header( $response, 'content-type' ); $this->received += strlen( $body ); if ( 200 === $code && 0 === strpos( $body, Frame_Codec::MAGIC ) ) { try { $frames = Frame_Codec::decode( $body, Signer::enc_key( $this->secret ), Signer::sign_key( $this->secret ), $envelope['nonce'] ); } catch ( \RuntimeException $e ) { throw new Remote_Exception( 'bad_response', sprintf( /* translators: %s: reason. */ __( 'The response from the source was cut off or altered (%s). Retrying.', 'accessnow-site-replica' ), $e->getMessage() ), true, 5 ); } $end = json_decode( $frames[ count( $frames ) - 1 ][1], true ); foreach ( $frames as $frame ) { if ( 'X' === $frame[0] ) { $error = json_decode( $frame[1], true ); throw new Remote_Exception( 'remote_error', sprintf( /* translators: %s: error. */ __( 'The source site reported an error: %s', 'accessnow-site-replica' ), is_array( $error ) ? (string) $error['error'] : 'unknown' ) ); } } return array( 'frames' => $frames, 'end' => is_array( $end ) ? $end : array(), ); } if ( 200 === $code && ( '' === $body || 0 === strpos( Frame_Codec::MAGIC, substr( $body, 0, 4 ) ) ) ) { // Empty or cut-off answer: the host killed the request part-way. throw new Remote_Exception( 'truncated', __( 'The source server stopped part-way through an answer (host limits). Retrying with smaller pieces.', 'accessnow-site-replica' ), true, 5 ); } if ( in_array( $code, array( 301, 302, 303, 307, 308 ), true ) ) { $location = (string) wp_remote_retrieve_header( $response, 'location' ); throw new Remote_Exception( 'redirect', sprintf( /* translators: %s: URL. */ __( 'The source site redirects to %s. Generate a new connection key on that address.', 'accessnow-site-replica' ), $location ) ); } $json = false !== strpos( $content_type, 'json' ) ? json_decode( $body, true ) : null; if ( is_array( $json ) && isset( $json['error'] ) ) { switch ( $json['error'] ) { case 'clock_skew': if ( isset( $json['server_time'] ) ) { $this->clock_offset = (int) $json['server_time'] - time(); throw new Remote_Exception( 'clock_skew_fixed', 'Clock corrected.', true, 1 ); } throw new Remote_Exception( 'clock_skew', __( 'The clocks on the two servers differ too much.', 'accessnow-site-replica' ) ); case 'unauthorized': throw new Remote_Exception( 'unauthorized', __( 'The source site did not accept the connection key. It may have been revoked or have expired: generate a new key on the source.', 'accessnow-site-replica' ) ); case 'locked_out': throw new Remote_Exception( 'locked_out', __( 'The source site has temporarily blocked this computer after failed attempts. Wait 30 minutes.', 'accessnow-site-replica' ), true, 600 ); case 'too_large': throw new Remote_Exception( 'too_large', __( 'The request was too large for the source site.', 'accessnow-site-replica' ) ); case 'busy': throw new Remote_Exception( 'busy', __( 'The source site is busy with another request for this key.', 'accessnow-site-replica' ), true, 3 ); case 'https_required': throw new Remote_Exception( 'https_required', __( 'The source site only accepts HTTPS. Use its https:// address.', 'accessnow-site-replica' ) ); case 'protocol_mismatch': throw new Remote_Exception( 'protocol_mismatch', __( 'The two sites run different Site Replica versions. Update the plugin on both.', 'accessnow-site-replica' ) ); case 'not_found': throw new Remote_Exception( 'not_found', __( 'Pulling is turned off on the source site (Site Replica → This site as a source).', 'accessnow-site-replica' ) ); case 'server_error': throw new Remote_Exception( 'server_error', (string) $json['message'], true, 10 ); } } if ( 508 === $code ) { throw new Remote_Exception( 'http_508', __( 'The source host’s resource limit was reached (HTTP 508). Using fewer connections and waiting.', 'accessnow-site-replica' ), true, 20 ); } if ( in_array( $code, array( 500, 502, 503, 504, 520, 521, 522, 523, 524, 408 ), true ) ) { throw new Remote_Exception( 'http_' . $code, sprintf( /* translators: %d: HTTP status. */ __( 'The source server returned HTTP %d (overloaded or timed out). Retrying with smaller pieces.', 'accessnow-site-replica' ), $code ), true, 10 ); } if ( 429 === $code ) { throw new Remote_Exception( 'rate_limited', __( 'The source host is rate-limiting requests. Waiting before retrying.', 'accessnow-site-replica' ), true, 30 ); } // Once a working URL shape is known, blocks are often temporary (rate limits, bot checks): // retry with long waits. While probing, report them so the next shape can be tried. $established = '' !== $this->transport; $waf = self::detect_waf( $body, $response ); if ( '' !== $waf ) { throw new Remote_Exception( 'blocked', sprintf( /* translators: 1: firewall name, 2: IP hint. */ __( 'The request was blocked by %1$s on the source server. Ask the host (or the firewall settings) to allow requests to this site from your IP address, or allowlist "accessnow_replica_api".', 'accessnow-site-replica' ), $waf ), $established, 30 ); } if ( 404 === $code ) { // After requests have worked, a plain 404 is usually temporary (the plugin being updated). throw new Remote_Exception( 'not_found', __( 'Site Replica is not active on the source site, or pulling is turned off there.', 'accessnow-site-replica' ), $established, 30 ); } if ( false !== stripos( $content_type, 'html' ) ) { throw new Remote_Exception( 'html_response', sprintf( /* translators: %d: HTTP status. */ __( 'The source returned a web page instead of data (HTTP %d). A security plugin, cache or firewall may be intercepting the request.', 'accessnow-site-replica' ), $code ), $established, 30 ); } throw new Remote_Exception( 'http_error', sprintf( /* translators: %d: HTTP status. */ __( 'Unexpected answer from the source site (HTTP %d).', 'accessnow-site-replica' ), $code ) ); } /** * URL for a transport, or '' when the source has not said where it is. * * @param string $transport Transport. * @return string */ public function endpoint( $transport ) { if ( 'query' === $transport ) { // Needs only the home URL, which every key carries. return add_query_arg( 'accessnow_replica_api', '1', trailingslashit( $this->url ) ); } // admin-ajax and REST can live anywhere, so only the addresses the source gave are used. return isset( $this->endpoints[ $transport ] ) ? $this->endpoints[ $transport ] : ''; } /** * Names the firewall behind a block page, if recognisable. * * @param string $body Body. * @param array $response Response. */ private static function detect_waf( $body, $response ) { $server = strtolower( (string) wp_remote_retrieve_header( $response, 'server' ) ); $sample = strtolower( substr( $body, 0, 20000 ) ); if ( wp_remote_retrieve_header( $response, 'cf-mitigated' ) || ( false !== strpos( $server, 'cloudflare' ) && ( false !== strpos( $sample, 'attention required' ) || false !== strpos( $sample, 'just a moment' ) ) ) ) { return 'Cloudflare'; } if ( false !== strpos( $sample, 'imunify' ) ) { return 'Imunify360'; } if ( false !== strpos( $sample, 'wordfence' ) ) { return 'Wordfence'; } if ( false !== strpos( $sample, 'mod_security' ) || false !== strpos( $sample, 'modsecurity' ) || false !== strpos( $sample, 'not acceptable' ) ) { return 'ModSecurity'; } if ( false !== strpos( $sample, 'sucuri' ) ) { return 'Sucuri'; } $code = (int) wp_remote_retrieve_response_code( $response ); if ( in_array( $code, array( 403, 406, 501 ), true ) ) { return __( 'a firewall', 'accessnow-site-replica' ); } return ''; } /** * First frame of a type from a call result. * * @param array $result Result from call(). * @param string $type Frame type. * @return string|null Payload. */ public static function frame( array $result, $type ) { foreach ( $result['frames'] as $frame ) { if ( $type === $frame[0] ) { return $frame[1]; } } return null; } /** * Decoded H frame. * * @param array $result Result. * @return array */ public static function header( array $result ) { $raw = self::frame( $result, 'H' ); $data = null === $raw ? null : json_decode( $raw, true ); return is_array( $data ) ? $data : array(); } }