Skip to content

Instantly share code, notes, and snippets.

@AlexanderGW
Created October 9, 2020 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexanderGW/27eb766ca306a4d0e39dcf5618c2b5f1 to your computer and use it in GitHub Desktop.
Save AlexanderGW/27eb766ca306a4d0e39dcf5618c2b5f1 to your computer and use it in GitHub Desktop.
Return traffic inspection (session) JSON of current NGROK environment
<?php
/**
* Return JSON traffic inspection (just the session part) of current NGROK (https://ngrok.com/docs) environment
* @author Alexander Gailey-White
*
* Example data:
* {
"Session": {
"Status": 1,
"LastError": "",
"Version": "prod",
"Tunnels": {
"foobar": {
"URL": "https:\/\/xxxxxxxxxxxxx.ngrok.io",
"Proto": "https",
"Metrics": ...,
"Session": ...
},
"Config": {
"name": "foobar",
"inspect": true,
"addr": "localhost:8080",
"Subdomain": "",
"Hostname": "",
"Auth": "",
"HostHeader": "",
"BindTLS": "both"
}
},
...
}
*/
$return = false;
// Get NGROK traffic inspection
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'http://localhost:4040/inspect/http',
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true
]);
// Empty response
if (($raw = curl_exec($ch)) !== false) {
// Response info
$response = curl_getinfo($ch);
curl_close($ch);
}
// Fall-back to ./ngrok.txt
if (empty($raw) || $response['http_code'] <> 200)
$raw = file_get_contents('./ngrok.txt');
// We have some data
if (!empty($raw)) {
// Look for HTML escaped JSON strings
preg_match_all('#JSON.parse\(\"(.*)\"\)#i', $raw, $matches);
// Matches
if (count($matches) && array_key_exists(1, $matches)) {
$return = [];
// Add each match to return
foreach ($matches[1] as $i => $match) {
// We only want the last JSON.parse block, which contains the session data.
// The first block contains traffic data, comment the next line to include it
if ($i)
$return[] = json_decode(str_replace('\"', '"', $match), true);
}
}
}
echo json_encode($return);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment