Skip to content

Instantly share code, notes, and snippets.

@andrewnicols andrewnicols/README.md Secret
Created Jul 2, 2018

Embed
What would you like to do?
NGrok Moodle config

Moodle Configuration for Dynamic ngrok

Instructions for installation

The following directories will be required:

  • MDK Storage Directory - you can check where this is with mdk config show dirs.storage
  • MDK Local Scripts Directory - this is in ~/.moodle-sdk/scripts (you may need to create this directory if it does not exist)
  1. Download the included ngrok.php file and place it into the MDK Storage Directory
  2. Place the included ngrok.sh script and place it int the MDK Local Scripts Directory
  3. Create an ngrok Authentication according to the ngrok Documentation
  4. Place the included ngrokrunner script somewhere within your path and make it executable
  5. Create a new config file in ~/.config/ngrokrunner with contents:
SUBDOMAIN=yoursubdomain
REGION=yourpreferredregion

Documentation for use of subdomains is available in the ngrok Documentation Documentation for the available regions is available in the ngrok Global Infrastructure docs

Usage

Moodle configuration

You can configure an MDK instance of Moodle to use ngrok by running:

mdk run ngrok

This will add a line to the config.php to load a custom ngrok configuration. This only needs to be done once, but does not harm if run multiple times.

Running ngrok

Once configured you can simply run:

ngrokrunner

This will start ngrok with your custom configuration of port, subdomain, and region.

<?php
/**
* Configuration for use of ngrok with a Moodle installation.
*
* @copyright Andrew Nicols <andrew@nicols.co.uk>
*/
if (isset($CFG) && isset($_SERVER)) {
if (isset($_SERVER['HTTP_HOST']) && strpos($_SERVER['HTTP_HOST'], 'ngrok.io') !== false) {
// Store the current wwwroot - we'll need this later.
$originalroot = $CFG->wwwroot;
if (array_key_exists('HTTP_X_FORWARDED_PROTO', $_SERVER)) {
// Proxied protocol is set.
// The requested protocol is in HTTP_X_FORWARDED_PROTO.
// Use that to form the first part of the new wwwroot.
$CFG->wwwroot = $_SERVER['HTTP_X_FORWARDED_PROTO'];
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
// This should always be an https URL, but best to check.
// Add the SSL Proxy configuration.
$CFG->sslproxy = true;
}
} else {
// Just redirect to the HTTPS Version.
header("Location: https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
die;
}
// Add the requested HTTP Host name.
$CFG->wwwroot .= "://{$_SERVER['HTTP_HOST']}";
// Add everything from the first slash onwards.
// i.e. http://example.local/stable_master
// ^^^^^^^^^^^^^^
$CFG->wwwroot .= substr($originalroot, strpos($originalroot, '/', 9));
}
}
#!/bin/bash
grep ngrok config.php > /dev/null
if [ $? -ne 0 ]
then
echo Adding ngrok
gsed -i '/^\$CFG->wwwroot.*$/ a require_once(dirname(dirname(__DIR__)) . "\/ngrok.php");' config.php
else
echo ngrok already there
fi
#!/bin/bash
set -e
# Load configuration from ~/.config/ngrokrunner
CONFIGFILE="${HOME}/.config/ngrokrunner"
if [ -f "${CONFIGFILE}" ]
then
echo "Loading configuration from ${CONFIGFILE}"
. "${CONFIGFILE}"
fi
SUBDOMAIN="${SUBDOMAIN:-}"
REGION="${REGION:-}"
PORT="${PORT:-80}"
CMD=`which ngrok`
ARGS="http ${PORT}"
if [ -n "${SUBDOMAIN}" ]
then
ARGS="${ARGS} -subdomain=${SUBDOMAIN}"
fi
if [ -n "${REGION}" ]
then
ARGS="${ARGS} -region=${REGION}"
fi
echo "Starting ngrok with ${ARGS}"
"${CMD}" ${ARGS}
# Sample configuration for ngrokrunner.
#
# This file can be placed in $HOME/.config/ngrokrunner
# All values are optional.
## The subdomain to use when running ngrok.
# Documentation for this setting is available at https://ngrok.com/docs#subdomain
# This value is optional and the default is empty.
# ngrok will generate a unique name each time if you do not specify a value here.
#SUBDOMAIN=
## The region to use for ngrok.
# Documentation for this setting is available at https://ngrok.com/docs#global-locations
# This value is optional and the default is empty.
# ngrok will use the US region (Ohio) if you do not specify a region here.
#REGION=au
## The port on which your http server runs locally.
# This value is optional and the default is 80.
#PORT=80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.