feat: /tui-command endepunkt — ekte shell-eksekvering med timeout og auth
This commit is contained in:
parent
bc8eb70c16
commit
638f69d335
31
main.py
31
main.py
|
|
@ -1068,3 +1068,34 @@ if __name__ == "__main__":
|
||||||
import uvicorn
|
import uvicorn
|
||||||
port = int(os.environ.get("PORT", 8080))
|
port = int(os.environ.get("PORT", 8080))
|
||||||
uvicorn.run(app, host="0.0.0.0", port=port)
|
uvicorn.run(app, host="0.0.0.0", port=port)
|
||||||
|
|
||||||
|
|
||||||
|
class TuiCommandRequest(BaseModel):
|
||||||
|
command: str
|
||||||
|
timeout: int = 30
|
||||||
|
|
||||||
|
async def _tui_command(request: Request, req: TuiCommandRequest):
|
||||||
|
import asyncio, shlex
|
||||||
|
cmd = req.command.strip()
|
||||||
|
if not cmd:
|
||||||
|
return JSONResponse({"output": "", "exit_code": 1, "error": "Tom kommando"})
|
||||||
|
try:
|
||||||
|
proc = await asyncio.create_subprocess_shell(
|
||||||
|
cmd,
|
||||||
|
stdout=asyncio.subprocess.PIPE,
|
||||||
|
stderr=asyncio.subprocess.PIPE,
|
||||||
|
cwd="/home/chris.christiansen/OSVauco",
|
||||||
|
)
|
||||||
|
stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=req.timeout)
|
||||||
|
return JSONResponse({
|
||||||
|
"output": stdout.decode(errors="replace"),
|
||||||
|
"stderr": stderr.decode(errors="replace"),
|
||||||
|
"exit_code": proc.returncode,
|
||||||
|
})
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
return JSONResponse({"output": "", "stderr": f"Timeout etter {req.timeout}s", "exit_code": 124})
|
||||||
|
except Exception as e:
|
||||||
|
return JSONResponse({"output": "", "stderr": str(e), "exit_code": 1})
|
||||||
|
|
||||||
|
app.add_api_route("/tui-command", endpoint=require_auth(_tui_command), methods=["POST"])
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user