DEV Community

Rabist
Rabist

Posted on

PHP Tor Controller

Screenshot

Control your Tor service with a PHP script.

Features:

  • Renew - Change current mask IP.
  • Restart - Restart Tor service.
  • Display connected clients to the service.

It must be used on the same server with the Tor service.

Visit Gist Page and contribute to add more features.

<?php

/**
 * Desc: PHP Tor Controller
 * Author: Rabist - https://www.linkedin.com/in/rabist
 * Source URI: https://gist.github.com/geraked/334c6f1c3b8fbd607f8c7788e7d92f81
 * Date: 2021-07-14
 */


// Set config parameters
$IP = '127.0.0.1';
$SOCKS_PORT = 9050;
$CONTROL_PORT = 9051;
$CONTROL_PASSWORD = '';

// Fetch user's cookie and the requested operation
$usr = $_COOKIE['usr'] ?? null;
$op = $_GET['op'] ?? null;
$ct = time() + (86400 * 30);
$class = '';

// Handle logout request
if ($op == 'logout') {
    setcookie('usr', '', $ct);
    header('location:?op=login');
}

// Handle login request
if ($op == 'login') {
    $password = $_POST['password'] ?? '';
    $passwordErr = "";
    setcookie('usr', password_hash($password, PASSWORD_DEFAULT), $ct);
    $class = 'd-flex';

    // Handle successful authentication
    if ($CONTROL_PASSWORD == $password) {
        header('location:?op=0');
    }

    // Display password error
    if (!empty($password)) {
        $passwordErr = "<p>Wrong password!</p>";
    }
} else {

    // Verify current user
    if (!$usr || !password_verify($CONTROL_PASSWORD, $usr)) {
        header('location:?op=login');
    } else {
        $cmd = '';

        // Handle renew request
        if ($op == 1) {
            $cmd = shell_exec("printf 'AUTHENTICATE \"$CONTROL_PASSWORD\"\r\nSIGNAL NEWNYM\r\n' | nc -q 1 $IP $CONTROL_PORT");
        }

        // Handle restart request
        if ($op == 2) {
            $cmd = shell_exec("systemctl restart tor");
        }

        // Check connection through the proxy and get the mask ip
        $ip = shell_exec("curl --socks5 $IP:$SOCKS_PORT http://checkip.amazonaws.com/");

        // Get all the connected clients to the tor service
        $cons = shell_exec("netstat -ntu | grep :$SOCKS_PORT | grep -v LISTEN | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | grep -v $IP");
    }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tor</title>

    <style>
        body {
            background: #000;
            color: #fff;
            font-family: Arial, Helvetica, sans-serif;
            margin: 0;
        }

        main {
            padding: 15px;
        }

        .d-flex {
            display: flex;
            align-items: center;
            justify-content: center;
            height: calc(100vh - 30px);
        }

        ul {
            list-style: none;
            margin: 0;
            padding: 0;
            display: flex;
        }

        li {
            flex-grow: 1;
        }

        a {
            display: block;
            color: #000;
            background: #fff;
            padding: 15px 10px;
            margin-bottom: 15px;
            text-decoration: none;
            text-align: center;
            font-weight: bold;
        }

        pre {
            border: 5px solid #fff;
            padding: 10px;
            margin: 0;
            height: calc(100vh - 130px);
            line-height: 1.8;
            font-size: 14px;
        }

        pre p {
            margin: 0;
        }

        form p {
            text-align: center;
        }

        input {
            background: #000;
            color: #fff;
            padding: 15px 10px;
            border: 2px solid #fff;
            display: block;
            margin: 0 auto 15px;
            width: 280px;
        }

        input[type="submit"] {
            cursor: pointer;
            width: 200px;
        }
    </style>
</head>

<body>
    <main class="<?php echo $class; ?>">
        <?php if ($op == 'login') : ?>
            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>?op=login">
                <?php echo $passwordErr; ?>
                <input type="password" name="password" value="<?php echo $password; ?>" placeholder="Password">
                <input type="submit" name="submit" value="LOGIN">
            </form>
        <?php else : ?>
            <ul>
                <li><a href="?op=0">REFRESH</a></li>
                <li><a href="?op=1">RENEW</a></li>
                <li><a href="?op=2">RESTART</a></li>
                <li><a href="?op=logout">LOGOUT</a></li>
            </ul>
            <pre>
                <p>IP: <?php echo $ip; ?></p><p>Connections:</p><?php echo $cons; ?><p>Cmd:</p><?php echo $cmd; ?>
            </pre>
        <?php endif; ?>
    </main>
</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)