-
Notifications
You must be signed in to change notification settings - Fork 1
/
wake.py
76 lines (65 loc) · 3.65 KB
/
wake.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
#!/usr/bin/env python3
# Copyright Jay Townsend 2018-2023
import yaml
from flask import Flask, redirect, url_for, make_response, after_this_request
from flask import render_template
from flask import request
from wakeonlan import *
from werkzeug.wrappers.response import Response
app = Flask(__name__)
class Computers:
"""
class to house the things relating to computer related things
"""
@staticmethod
def config() -> dict:
"""
method that reads the computers.yaml file and to make it available
in the html via jinja2 templating
:return: dict
"""
try:
with open('computers.yaml', 'r') as computers:
return yaml.safe_load(computers).items()
except FileNotFoundError:
with open('/var/www/html/wake/computers.yaml', 'r') as computers:
return yaml.safe_load(computers).items()
@app.route('/', methods=['GET'])
def homepage() -> Response:
"""
main webpage of the app
:return:
"""
response = make_response(render_template('index.html', computers=Computers.config()))
response.headers['X-Frame-Options'] = 'SAMEORIGIN'
response.headers['Permissions-Policy'] = 'accelerometer=(), camera=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=(), interest-cohort=()'
response.headers['Referrer-Policy'] = 'no-referrer-when-downgrade'
response.headers['X-Content-Type-Options'] = 'nosniff'
response.headers['Content-Security-Policy'] = "default-src 'self' cdnjs.cloudflare.com 'report-sample'; script-src 'self' 'sha256-ajGjo5eD0JzFPdnpuutKT6Sb5gLu+Q9ru594rwJogGQ=' cdnjs.cloudflare.com 'report-sample'; script-src-elem 'self' 'sha256-ajGjo5eD0JzFPdnpuutKT6Sb5gLu+Q9ru594rwJogGQ=' cdnjs.cloudflare.com 'report-sample'; connect-src 'self' 'report-sample'; img-src 'self' data: w3.org/svg/2000 'report-sample'; base-uri 'self'; style-src 'self' cdnjs.cloudflare.com 'unsafe-inline' 'report-sample'; style-src-elem 'self' cdnjs.cloudflare.com 'unsafe-inline' 'report-sample'"
return response
@app.route('/', methods=['POST'])
def send_mac() -> Response:
"""
function that sends the magic packet to turn your
computer on via wake on lan and does it as a post request
:return:
"""
mac = request.form.get('macaddr')
send_magic_packet(str(mac))
@after_this_request
def add_header(response):
"""
Allows setting server headers after doing a POST request
as doing it the same way for the homepage() breaks sending the MAC addr
:param response:
:return:
"""
response.headers['X-Frame-Options'] = 'SAMEORIGIN'
response.headers['Permissions-Policy'] = 'accelerometer=(), camera=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=(), interest-cohort=()'
response.headers['Referrer-Policy'] = 'no-referrer-when-downgrade'
response.headers['X-Content-Type-Options'] = 'nosniff'
response.headers['Content-Security-Policy'] = "default-src 'self' cdnjs.cloudflare.com 'report-sample'; script-src 'self' 'sha256-ajGjo5eD0JzFPdnpuutKT6Sb5gLu+Q9ru594rwJogGQ=' cdnjs.cloudflare.com 'report-sample'; script-src-elem 'self' 'sha256-ajGjo5eD0JzFPdnpuutKT6Sb5gLu+Q9ru594rwJogGQ=' cdnjs.cloudflare.com 'report-sample'; connect-src 'self' 'report-sample'; img-src 'self' data: w3.org/svg/2000 'report-sample'; base-uri 'self'; style-src 'self' cdnjs.cloudflare.com 'unsafe-inline' 'report-sample'; style-src-elem 'self' cdnjs.cloudflare.com 'unsafe-inline' 'report-sample'"
return response
return redirect(url_for('homepage'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)