Skip to content

Instantly share code, notes, and snippets.

@janmalec
Created January 17, 2025 07:22
Show Gist options
  • Save janmalec/70b03f4adad5b8adcfe03e683cae0a3b to your computer and use it in GitHub Desktop.
Save janmalec/70b03f4adad5b8adcfe03e683cae0a3b to your computer and use it in GitHub Desktop.
from flask import Flask, request
import os
app = Flask(__name__)
from display_secret import secret
SECRET_TOKEN = secret
def check_token():
"""Helper to verify that the caller provided the correct token."""
token = request.args.get('token') or request.form.get('token')
return token == SECRET_TOKEN
# --------------------------------------------------------------------
# DISPLAY ROUTES
# --------------------------------------------------------------------
@app.route('/display/external', methods=['GET','POST'])
def switch_to_external():
if not check_token():
return "Unauthorized\n", 401
os.system(r"C:\Windows\System32\DisplaySwitch.exe /external")
return "Switched display to external (TV)\n"
@app.route('/display/internal', methods=['GET','POST'])
def switch_to_internal():
if not check_token():
return "Unauthorized\n", 401
os.system(r"C:\Windows\System32\DisplaySwitch.exe /internal")
return "Switched display to internal (Monitor)\n"
@app.route('/display/extend', methods=['GET','POST'])
def switch_to_extend():
if not check_token():
return "Unauthorized\n", 401
os.system(r"C:\Windows\System32\DisplaySwitch.exe /extend")
return "Extended display\n"
@app.route('/display/clone', methods=['GET','POST'])
def switch_to_clone():
if not check_token():
return "Unauthorized\n", 401
os.system(r"C:\Windows\System32\DisplaySwitch.exe /clone")
return "Cloned display\n"
# --------------------------------------------------------------------
# SYSTEM ROUTES
# --------------------------------------------------------------------
@app.route('/system/suspend', methods=['GET','POST'])
def system_suspend():
if not check_token():
return "Unauthorized\n", 401
# Attempt to suspend/sleep the system
os.system(r"rundll32.exe powrprof.dll,SetSuspendState Sleep")
return "System suspend requested\n"
@app.route('/system/shutdown', methods=['GET','POST'])
def system_shutdown():
if not check_token():
return "Unauthorized\n", 401
# Shuts down the computer immediately
os.system("shutdown /s /t 0")
return "System shutdown requested\n"
if __name__ == '__main__':
# Listen on all interfaces (0.0.0.0) and port 5000 by default
app.run(host='0.0.0.0', port=5000, debug=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment