|
| 1 | +from flask import Flask, Response, request, jsonify |
| 2 | +from wakeonlan import send_magic_packet as wakeup |
| 3 | +from pythonping import ping |
| 4 | +from time import sleep |
| 5 | +import logging |
| 6 | + |
| 7 | +app = Flask("WoL Client") |
| 8 | + |
| 9 | +app.logger.setLevel(logging.INFO) |
| 10 | + |
| 11 | +@app.route("/", methods=["POST"]) |
| 12 | +def listen(): |
| 13 | + data = request.json |
| 14 | + |
| 15 | + response = { |
| 16 | + "output": "", |
| 17 | + "success": False |
| 18 | + } |
| 19 | + |
| 20 | + if data: |
| 21 | + macAddr = data.get("mac", None) |
| 22 | + ipAddr = data.get("ip", None) |
| 23 | + startupTime = data.get("startupTime", None) |
| 24 | + |
| 25 | + if macAddr and ipAddr: |
| 26 | + output = "" |
| 27 | + |
| 28 | + infoLog(f"Pinging {ipAddr}.") |
| 29 | + initPong = ping(ipAddr) |
| 30 | + |
| 31 | + if (initPong.success()): |
| 32 | + infoLog(f"Host at {ipAddr} is reachable.") |
| 33 | + |
| 34 | + output += "Host is reachable.\n" |
| 35 | + response["output"] = output |
| 36 | + response["success"] = True |
| 37 | + else: |
| 38 | + infoLog(f"Host at {ipAddr} is unreachable.") |
| 39 | + |
| 40 | + output += "Host is unreachable.\n" |
| 41 | + |
| 42 | + infoLog(f"Sending Wakeup Packets to {macAddr}.") |
| 43 | + |
| 44 | + output += "Sending Wakeup Packets.\n" |
| 45 | + wakeup(macAddr, ip_address=ipAddr) |
| 46 | + |
| 47 | + output += "Waiting for Host to Turn On.\n" |
| 48 | + |
| 49 | + if (startupTime): |
| 50 | + infoLog(f"Waiting {startupTime}s for Host to Turn On.") |
| 51 | + |
| 52 | + sleep(startupTime) |
| 53 | + |
| 54 | + infoLog(f"Pinging {ipAddr} again.") |
| 55 | + finalPong = ping(ipAddr) |
| 56 | + |
| 57 | + if (finalPong.success()): |
| 58 | + infoLog(f"Host at {ipAddr} is reachable.") |
| 59 | + |
| 60 | + output += "Host is now reachable.\n" |
| 61 | + |
| 62 | + response["success"] = True |
| 63 | + response["output"] = output |
| 64 | + else: |
| 65 | + infoLog(f"Host at {ipAddr} is still unreachable.") |
| 66 | + |
| 67 | + output += "Host is still unreachable.\n" |
| 68 | + else: |
| 69 | + infoLog(f"Waiting for Host to Turn On.") |
| 70 | + |
| 71 | + for i in range(1, 8): |
| 72 | + sleep(4) |
| 73 | + |
| 74 | + infoLog(f"Pinging {ipAddr} again. ({i}x)") |
| 75 | + finalPong = ping(ipAddr, count=3) |
| 76 | + |
| 77 | + if (finalPong.success()): |
| 78 | + infoLog(f"Host at {ipAddr} is reachable.") |
| 79 | + |
| 80 | + output += "Host is now reachable.\n" |
| 81 | + |
| 82 | + response["success"] = True |
| 83 | + response["output"] = output |
| 84 | + |
| 85 | + return jsonify(message=response) |
| 86 | + |
| 87 | + infoLog(f"Host at {ipAddr} is still unreachable.") |
| 88 | + |
| 89 | + output += "Host is still unreachable.\n" |
| 90 | + |
| 91 | + return jsonify(message=response) |
| 92 | + |
| 93 | +def infoLog(msg): |
| 94 | + app.logger.info(msg) |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + app.run(debug=False, port=5555, host='0.0.0.0') |
0 commit comments