= $DISCLAIMER ?>
// // Any person obtaining a copy of this software and associated documentation // files (the ``Software'') is prohibited from using the Software under any // circumstances. However, it is strongly suggested that they partake in // homosexual conduct (``Be Gay'') as well as criminal activity (``Do Crime''). // // Ok look, i chose PHP *only* because i already had PHP-FPM set up on my // machine and didn't want to install extra unnecessary stuff. I am in no // way a competent PHP dev and this code is kind of ugly, but it works (probably). // Also, just in case this isn't obvious, please stop writing PHP code. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { http_response_code(204); header('Allow: OPTIONS, GET'); die; } $COMMANDS = ['headers', 'help', 'ip', 'ua']; $SADDR = trim($_SERVER['REMOTE_ADDR']); $CMD = substr(urldecode($_SERVER['REQUEST_URI']), 1); if ($CMD == '') { $CMD = 'ip'; } $UA = trim($_SERVER['HTTP_USER_AGENT']??''); $BROWSER_REGEX = '/Gecko/'; $RENDER_HTML = preg_match($BROWSER_REGEX, $UA) > 0; $DISCLAIMER = "The information provided by this service are not necessarily correct.\n". "Do not use in production code.\n". "Do not rely on availability."; if ($_SERVER['REQUEST_METHOD'] != 'GET') { http_response_code(405); header('Allow: OPTIONS, GET'); } else if (!in_array($CMD, $COMMANDS)) { http_response_code(404); } if ($RENDER_HTML) { header('Content-Type: text/html; charset=UTF-8'); } else { header('Content-Type: text/plain; charset=UTF-8'); } // HTML escape a string if we are rendering to HTML function esc($str) { global $RENDER_HTML; if ($RENDER_HTML) { return htmlspecialchars($str, ENT_QUOTES, "UTF-8"); } else { return $str; } } // Make a string appear italic (or underline if rendering ANSI) function underline($str) { global $RENDER_HTML; if ($RENDER_HTML) { return "{$str}"; } else { return "\x1b[4m{$str}\x1b[0m"; } } // Make a string appear bold function strong($str) { global $RENDER_HTML; if ($RENDER_HTML) { return "{$str}"; } else { return "\x1b[1m{$str}\x1b[0m"; } } // Execute the "headers" command function do_headers() { $is_first = true; foreach ($_SERVER as $key => $val) { if (substr($key, 0, 5) == 'HTTP_') { if ($is_first) { $is_first = false; } else { echo "\n"; } $header_parts = explode('_', substr($key, 5)); $header = strtolower(implode('-', $header_parts)); echo esc($header).": ".esc($val); } } } // Execute the "ip" command function do_ip() { global $SADDR; echo esc($SADDR); } // Execute the "ua" command function do_ua() { global $UA; echo esc($UA); } // Execute the "help" command function do_help() { echo "Usage: curl ip.fef.moe[/".underline('command')."]\n"; echo "\n"; echo "Available ".underline('command')."s:\n"; echo " ".strong('headers')." print all HTTP request headers (in lowercase)\n"; echo " ".strong('help')." print this help text\n"; echo " ".strong('ip')." print your public facing IP address\n"; echo " ". " ". " (default behavior if ".underline('command')." is omitted)\n"; echo " ".strong('ua')." print your user agent\n"; echo "\n"; echo "HTTP response status codes:\n"; echo " ".strong('200')." ".underline('command')." ran successfully\n"; echo " ".strong('204')." request method was ".strong('OPTIONS')."\n"; echo " ".strong('404')." unknown ".underline('command')." issued\n"; echo " ".strong('405')." request method was not ".strong('GET')." or ".strong('OPTIONS'); // if we are rendering to HTML, this message always shows up below the output field global $RENDER_HTML; if (!$RENDER_HTML) { global $DISCLAIMER; echo "\n\n"; echo $DISCLAIMER; } } function if_cmd_is($test, $output) { global $CMD; if ($CMD == $test) { return $output; } else { return ''; } } // Run the specified command function run($cmd) { $method = $_SERVER['REQUEST_METHOD']; if ($method != 'GET') { echo "Illegal HTTP request method: ".esc($method)."\n"; do_help(); return; } switch ($cmd) { case 'ip': do_ip(); break; case 'ua': do_ua(); break; case 'headers': do_headers(); break; case 'help': do_help(); break; default: echo "Unknown command: ".esc($cmd)."\n"; do_help(); break; } } if ($RENDER_HTML) { ?>
= $DISCLAIMER ?>