-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebclient.py
100 lines (82 loc) · 2.95 KB
/
webclient.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
#
# written by ZyzonixDev
# published by ZyzonixDevelopments
#
# Copyright (c) 2023 ZyzonixDevelopments
#
# date created | 27-07-2023 21:53:19
#
# file | webclient.py
# project | wolserver
# file version | 1.0
#
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
import uvicorn
from datetime import datetime
from starlette.responses import FileResponse
import subprocess
# import custom scripts
from scripts import hostinformationHandler
from scripts import pagebuilder
from scripts.logHandler import logging
from config import *
app=FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins="*",
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# time for logging / console out
class ctime():
def getTime():
curTime = "" + str(datetime.now().strftime("%Y-%m-%d_%H-%M-%S"))
return curTime
# check if required packages are installed
class checkPackages():
def packagesInstalled():
allPackagesInstalled = True
for package in REQUIREDAPTPACKAGES:
resultPackageCheckEncoded = subprocess.run("/usr/bin/dpkg -s " + package, capture_output=True, shell=True)
resultPackageCheck = resultPackageCheckEncoded.stdout.decode()[:-1]
resultPackageCheckErr = resultPackageCheckEncoded.stderr.decode()[:-1]
if "is not installed" in (resultPackageCheck or resultPackageCheckErr):
allPackagesInstalled = False
return allPackagesInstalled
# web register points
class server():
# general WOL info
@app.get("/", response_class=HTMLResponse)
async def startpage(request: Request):
answer = pagebuilder.startpage(request)
return answer
# url to wakeup with wake on lan
@app.get("/wol/{clientName}", response_class=HTMLResponse)
async def wol_init(clientName, request: Request):
answer = pagebuilder.wol(clientName, request)
return answer
# url to wakeup with IPMI
@app.get("/ipmi/{clientName}", response_class=HTMLResponse)
async def ipmi_init(clientName, request: Request):
answer = pagebuilder.ipmi(clientName, request)
return answer
# favicon page
@app.get('/favicon.ico')
async def favicon():
return FileResponse(BASEDIR + "favicon.ico") #full path here to make icon work
# initialize server
def __init__(self):
logging.write("Running earlybird version: " + VERSION)
logging.write("Initializing webserver on " + hostinformationHandler.getFullHostname())
# check if required packages are installed:
if not checkPackages.packagesInstalled():
logging.writeError("Not all required packages " + str(REQUIREDAPTPACKAGES) + " are installed, exiting.")
return
uvicorn.run(app, port=80, host=SERVERIP)
# init server class
if __name__ == "__main__":
server()