Skip to main content

index.php (Source)

<?php
    header("Access-Control-Allow-Origin: *");
    $host = gethostname();
?>
<!doctype html>
<html>
<head>
        <title><?= $host ?>!</title>
        <style>
            body {
                display: grid;
                justify-items: center;
                list-style: none;
                grid-template-columns: auto 1fr;
            }
            ul {
                font-size: 3rem;
            }
            li ~ li {
                margin-top: 1em;
            }
            dl {
                font-size: small;
                display: grid;
            }
            dt {
                grid-row: 1;
            }
        </style>
</head>
<body>
        <section>
            <h2>On port 80</h2>
            <ul>
<?php
    foreach(scandir(dirname(__FILE__)) as $p) {
        if(is_dir($p) && !str_starts_with($p, ".")) {
?>
                <li><a href="http://<?= $host ?>/<?= $p ?>"><?= $p ?></a></li>
<?php
        }
    }
?>
            </ul>
        </section>
        <section>
            <h2>Other open ports</h2>
            <ul>
<?php
for($port=1000; $port < 10000; $port++) {
    $connection = @fsockopen($host, $port);
    if (is_resource($connection)) {
        fclose($connection);
        // Find the ID of the process with this port open
        $pids = explode("\n",shell_exec("lsof -i :$port -Fp"));
        foreach($pids as $line) {
            if(preg_match('/^p(\d+)/',$line, $m)) {
                $pid = $m[1];
            }
        }
        $cwd = readlink("/proc/$pid/cwd");
        $HOME = getenv('HOME');
        if(str_starts_with(substr($cwd,0,strlen($HOME)), $HOME)) {
            $cwd = substr($cwd,strlen($HOME));
        } else {
            continue;
        }
        $URL = "http://${host}:${port}";
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_URL => $URL,
            CURLOPT_RETURNTRANSFER => true
        ]);
        $output = curl_exec($ch);
        curl_close($ch);
        if(!$output) {
            // Not an HTML server
            continue;
        }
        libxml_use_internal_errors(true);
        $doc = new DOMDocument();
        if(!$doc->loadHTML($output)) {
            foreach (libxml_get_errors() as $error) {
                $title = "<div>$error</div>";
            }
        } else {
            $titles = $doc->getElementsByTagName('title');
            foreach($titles as $title_node) {
                $title = $title_node->nodeValue;
            }
        }
        libxml_use_internal_errors(false);
?>
        <li>
            <a href="http://<?= $host ?>:<?= $port ?>"><?= $title ?></a>
            <dl>
                <dt>port</dt>
                <dd><?= $port ?></dd>
                <dt><abbr title="Current working directory of the process">cwd</abbr></dt>
                <dd>~<?= $cwd ?></dd>
                <dt><abbr title="Process ID">pid</abbr></dt>
                <dd><?= $pid ?></dd>
            </dl>
        </li>
<?php
    }
}
?>
            </ul>
        </section>
</body>
</html>