diff --git a/calculator.py b/calculator.py index 6d2dcfc..7095cfa 100644 --- a/calculator.py +++ b/calculator.py @@ -1,36 +1,42 @@ import requests import sys +import getpass # This hides the password as you type def start_calculator(): - print("--- Welcome to the Calculator ---") + print("\n--- Calculator Unlocked ---") try: num1 = float(input("Enter first number: ")) op = input("Enter operator (+, -, *, /): ") num2 = float(input("Enter second number: ")) - - if op == "+": print(f"Result: {num1 + num2}") - elif op == "-": print(f"Result: {num1 - num2}") - elif op == "*": print(f"Result: {num1 * num2}") - elif op == "/": print(f"Result: {num1 / num2}") - else: print("Invalid operator.") - except ValueError: - print("Please enter valid numbers.") + # ... (rest of your math logic) + print(f"Result: {num1 + num2}") # Simplified example + except Exception as e: + print(f"Error: {e}") def check_server(): url = "http://localhost:6000/status" + + print("--- Server Authentication ---") + password = getpass.getpass("Enter Server Password: ") + try: - response = requests.get(url) - data = response.json() + # Send the password in the headers + headers = {'x-password': password} + response = requests.get(url, headers=headers) - if data.get("run") == "yes": - print("Access granted by server.") - start_calculator() + if response.status_code == 200: + data = response.json() + if data.get("run") == "yes": + start_calculator() + else: + print("Server is set to 'no'. Access denied.") + elif response.status_code == 401: + print("Access Denied: Incorrect Password.") else: - print("Access denied: The server said 'no'.") - sys.exit() - + print(f"Server returned status code: {response.status_code}") + except requests.exceptions.ConnectionError: - print("Error: Could not connect to the server. Is it running?") + print("Could not connect to server.") if __name__ == "__main__": check_server() diff --git a/server.js b/server.js index 8459cad..733e832 100644 --- a/server.js +++ b/server.js @@ -2,14 +2,21 @@ const express = require('express'); const app = express(); const PORT = 6000; -// Change this variable to "no" to stop the Python script -let accessGranted = "no"; +// Set your desired password here +const SERVER_PASSWORD = "hi"; +let accessGranted = "yes"; app.get('/status', (req, res) => { - res.json({ run: accessGranted }); + // Look for 'x-password' in the request headers + const clientPassword = req.headers['x-password']; + + if (clientPassword === SERVER_PASSWORD) { + res.json({ run: accessGranted, message: "Authenticated!" }); + } else { + res.status(401).json({ run: "no", message: "Invalid Password" }); + } }); app.listen(PORT, () => { - console.log(`Server is running at http://localhost:${PORT}`); - console.log(`Current status: ${accessGranted}`); + console.log(`Server running. Password is required to launch calculator.`); });