site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Mirror WordPress http proxy configuration for Google Client

Open aaemnnosttv opened this issue 7 years ago • 2 comments

Feature Description

Some sites use/require an HTTP proxy to make requests. WordPress provides a number of constants for configuring the WP HTTP API to use a proxy.

The HTTP client used by Google_Client should mirror the same http proxy configuration for WordPress if its constants are defined: WP_PROXY_HOST and WP_PROXY_PORT

Proxy credentials should be supported as well if set: WP_PROXY_USERNAME, WP_PROXY_PASSWORD


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • Allow users to rely on WP_PROXY_* constants to use an HTTP proxy for requests issued by Google_Client (now mostly Google_Site_Kit_Client).

Implementation Brief

  • Use core's WP_HTTP_Proxy class to detect the active state, and retreive values from core constants
    • There is no global object for this (nor does there need to be), we can instantiate and keep this as a instance property on OAuth_Client as $http_proxy
  • In OAuth_Client::get_client(), check if $http_proxy->is_enabled() - configure the Google client with the proxy configuration
    $proxy_host = $this->http_proxy->host() . ':' . $this->http_proxy->port();
    if ( $this->http_proxy->use_authentication() ) {
        $proxy_host = $this->http_proxy->authentication() . '@' . $proxy_host;
    }
    $client->getHttpClient()->setDefaultOption( 'proxy', $proxy_host );
    $ssl_verify = $client->getHttpClient()->getDefaultOption( 'verify' );
    $client->getHttpClient()->setDefaultOption(
        'verify',
        apply_filters( 'https_ssl_verify', $ssl_verify, null )
    );
    

QA Brief

This is documented for testing locally as this might be more difficult to test on a live site

  1. Install an HTTP proxy locally, if you don't have one already
    https://docs.mitmproxy.org/stable/overview-installation/
    • Start the proxy by running mitmproxy note the port in the bottom right (default: 8080)
  2. Configure the proxy host and port in your wp-config.php
    define( 'WP_PROXY_HOST', 'host.docker.internal' );
    define( 'WP_PROXY_PORT', '8080' );
    

    For me, this is host.docker.internal because I'm using Docker locally If you're using something else, this may be localhost, but you want to target your host machine, where the proxy is listening

  3. Install this plugin to disable SSL verification
  4. Click around Site Kit, you should start to see lines appearing (see below)
    • If you see errors, your proxy is not configured properly
  5. Go to the WordPress Dashboard > Updates page, and click Check Again to force a request from WordPress to check for updates. You should see this in mitmproxy.
  6. Go to the Site Kit dashboard (setup if necessary).
    A few ways to trigger requests:
    • Set up a new module that makes requests, such as Analytics or Tag Manager
    • Switch the date range on a dashboard page
    • Clear your browser's Session Storage and reload a Site Kit dashboard page
  7. You should see requests to googleapis.com in mitmproxy to confirm that requests are going through it
  8. When done, press q to stop mitmproxy, and y to confirm from the processes terminal window

Example of requests seen in mitmproxy image

Changelog entry

  • Allow users to rely on WP_PROXY_* constants to use an HTTP proxy for requests issued by Google_Client.

aaemnnosttv avatar Oct 16 '19 10:10 aaemnnosttv

IB ✅

Let's clarify in a comment, and probably also in naming variables/properties $http_proxy, that this is not related to the "Site Kit proxy".

felixarntz avatar Dec 19 '19 16:12 felixarntz

Tested Ran through the proxy setup, confirmed proxy is functional image

Ran through some tests setting up and removing GA. Confirmed googleapi requests are coming through: image

Passed QA ✅

cole10up avatar Jan 15 '20 16:01 cole10up