<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Web Shell with Password</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            background: #141414;
            color: #fff;
            font-family: Arial, sans-serif;
        }

        .password-section {
            display: block;
            text-align: center;
            margin-top: 50px;
        }

        .password-section input {
            padding: 10px;
            font-size: 16px;
            width: 300px;
            background: #333;
            color: #fff;
            border: 1px solid #555;
            border-radius: 5px;
        }

        .password-section button {
            padding: 10px 20px;
            background: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            border-radius: 5px;
            margin-top: 10px;
        }

        .password-section button:hover {
            background: #45a049;
        }

        #term {
            display: none;
            margin: 20px auto;
            width: 80%;
            height: 400px;
            background: #000;
            color: #fff;
            font-family: monospace;
            padding: 10px;
            border-radius: 10px;
            overflow-y: auto;
        }

        #inputField {
            display: none;
            margin-top: 10px;
        }
        
        .kd {
            display: none;
        }
    </style>
</head>
<body>
    <div class="password-section">
        <h2>Enter Password</h2>
        <input type="password" id="password" placeholder="Enter password" />
        <button onclick="checkPassword()">Submit</button>
    </div>

    <div id="term">
        <h3>Terminal</h3>
        <div id="terminal-output"></div>
        <input type="text" id="inputField" placeholder="Type a command" />
    </div>

    <script>
        // Example password (encrypted with SHA-256)
        const encryptedPassword = "ef797c8118f02d4c7c1a5e6079d5a4e92ff1614b2f07f61e0f5a676bd5a4e89d"; // "secret"

        // SHA-256 hash function (use Web Crypto API)
        function sha256(str) {
            return crypto.subtle.digest("SHA-256", new TextEncoder().encode(str))
                .then(hashBuffer => {
                    const hashArray = Array.from(new Uint8Array(hashBuffer));
                    return hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
                });
        }

        // Function to check the entered password
        function checkPassword() {
            const passwordField = document.getElementById('password');
            const password = passwordField.value;

            // Encrypt the entered password and compare with the stored hash
            sha256(password).then(hashedPassword => {
                console.log("Entered Password Hash: ", hashedPassword); // Log the hash for debugging
                if (hashedPassword === encryptedPassword) {
                    // Correct password, show the terminal
                    document.querySelector('.password-section').style.display = 'none';
                    document.getElementById('term').style.display = 'block';
                    document.getElementById('inputField').style.display = 'block';
                    document.querySelector('.kd').style.display = 'flex';
                    alert('Password correct! Access granted.');
                    initializeWebShell();
                } else {
                    // Incorrect password
                    alert('Incorrect password.');
                }
            }).catch(error => {
                console.error("Error during password hashing:", error);
            });
        }

        // WebShell integration
        var webshell = {};
        webshell.TerminalClass = function(id, width, height, handler) {
            var ie = 0;
            if(window.ActiveXObject) ie = 1;
            var sid = "" + Math.round(Math.random() * 1000000000);
            var qs = "s=" + sid + "&w=" + width + "&h=" + height + "&k=";
            var kb = [];
            var isactive = false;
            var islocked = false;
            var qtimer;
            var qtime = 100;
            var retry = 0;
            var cy = 0;
            var div = document.getElementById(id);

            function update() {
                if (!islocked) {
                    islocked = true;
                    var r = new XMLHttpRequest();
                    var send = "";
                    while (kb.length > 0) send += kb.pop();
                    r.open("GET", "u?" + qs + send, true);
                    r.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                    r.onreadystatechange = function() {
                        if (r.readyState != 4) return;
                        islocked = false;
                        if (!isactive) {
                            isactive = true;
                            handler('conn', 0);
                        }
                        if (r.status == 200) {
                            retry = 0;
                            cy = r.responseText.substring(45, 48);
                            var html = r.responseText.substring(52);
                            if (html.length > 0) {
                                div.innerHTML = html;
                                handler('curs', cy);
                                qtime = 100;
                            } else {
                                qtime *= 2;
                                if (qtime > 2000) qtime = 2000;
                            }
                            qtimer = window.setTimeout(update, qtime);
                        } else if (r.status == 400) {
                            handler('disc', 0);
                        } else {
                            retry++;
                            if (retry < 3)
                                qtimer = window.setTimeout(update, 2000);
                            else
                                handler('disc', 1);
                        }
                    };
                    r.send(null);
                }
            }

            function queue(s) {
                kb.unshift(s);
                qtime = 100;
                if (!islocked) {
                    window.clearTimeout(qtimer);
                    qtimer = window.setTimeout(update, 1);
                }
            }

            function private_sendkey(kc) {
                var k = "";
                switch (kc) {
                    case 126:
                        k = "~~";
                        break;
                    case 63232:
                        k = "~A"; break;
                    case 63233:
                        k = "~B"; break;
                    case 63234:
                        k = "~D"; break;
                    case 63235:
                        k = "~C"; break;
                    case 63276:
                        k = "~1"; break;
                    case 63277:
                        k = "~2"; break;
                    case 63273:
                        k = "~H"; break;
                    case 63275:
                        k = "~F"; break;
                    case 63302:
                        k = "~3"; break;
                    case 63272:
                        k = "~4"; break;
                    default:
                        k = String.fromCharCode(kc);
                }
                queue(encodeURIComponent(k));
            }

            this.sendkey = function(kc) {
                private_sendkey(kc);
            }

            this.keypress = function(ev) {
                if (!ev) var ev = window.event;
                var kc;
                if (ev.keyCode) kc = ev.keyCode;
                if (ev.which) kc = ev.which;
                if (ev.ctrlKey) {
                    if (kc >= 0 && kc <= 32) kc = kc;
                    else if (kc >= 65 && kc <= 90) kc -= 64;
                    else if (kc >= 97 && kc <= 122) kc -= 96;
                }
                if (kc == 8) kc = 127;
                private_sendkey(kc);
                ev.cancelBubble = true;
                if (ev.stopPropagation) ev.stopPropagation();
                if (ev.preventDefault) ev.preventDefault();
                return true;
            }

            qtimer = window.setTimeout(update, 1);
        };

        function initializeWebShell() {
            var terminal = new webshell.Terminal('term', 500, 400, function(event, data) {
                if (event === 'conn') {
                    console.log('Connected to terminal');
                } else if (event === 'disc') {
                    alert('Disconnected');
                } else if (event === 'curs') {
                    console.log('Cursor position:', data);
                }
            });

            // This simulates a key press in the terminal.
            document.getElementById('inputField').addEventListener('keydown', function(e) {
                terminal.keypress(e);
            });
        }
    </script>
</body>
</html>
