-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
49 lines (40 loc) · 1.74 KB
/
main.py
File metadata and controls
49 lines (40 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import sys
import uvicorn
import Quartz
from dotenv import load_dotenv, find_dotenv, set_key
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
load_dotenv(find_dotenv())
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
def read_root():
return "MacAPI is online"
@app.get("/lock")
async def lock(request: Request):
if request.headers.get("Authorization") != owner_token:
raise HTTPException(status_code=401, detail="Unauthorized")
os.system("osascript -e 'tell application \"System Events\" to keystroke \"q\" using {command down, control down}'")
return "Locked the mac"
@app.post("/unlock")
async def unlock(request: Request):
data = await request.json()
if request.headers.get("Authorization") != owner_token or str(hash(data.get("password"))) != os.getenv("HASHED_PASSWORD"):
raise HTTPException(status_code=401, detail="Unauthorized")
session_dictionary = Quartz.CGSessionCopyCurrentDictionary()
if not (session_dictionary and session_dictionary.get("CGSSessionScreenIsLocked", 0) == 0 and session_dictionary.get("kCGSSessionOnConsoleKey", 0) == 1):
os.system(f"osascript -e 'tell application \"System Events\" to keystroke \"{data.get("password")}\"'")
os.system(f"osascript -e 'tell application \"System Events\" to keystroke return'")
return "Unlocked the mac"
if __name__ == "__main__":
owner_token = os.getenv("OWNER_TOKEN")
if not os.getenv("HASHED_PASSWORD"):
set_key(find_dotenv(), "HASHED_PASSWORD", str(hash(input("Enter Mac Password: "))))
uvicorn.run(app, host="0.0.0.0", port=2201)