|
| 1 | +""" |
| 2 | +########################################################################## |
| 3 | +* |
| 4 | +* Copyright © 2019-2020 Akashdeep Dhar <[email protected]> |
| 5 | +* |
| 6 | +* This program is free software: you can redistribute it and/or modify |
| 7 | +* it under the terms of the GNU General Public License as published by |
| 8 | +* the Free Software Foundation, either version 3 of the License, or |
| 9 | +* (at your option) any later version. |
| 10 | +* |
| 11 | +* This program is distributed in the hope that it will be useful, |
| 12 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +* GNU General Public License for more details. |
| 15 | +* |
| 16 | +* You should have received a copy of the GNU General Public License |
| 17 | +* along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | +* |
| 19 | +########################################################################## |
| 20 | +""" |
| 21 | + |
| 22 | +import json |
| 23 | + |
| 24 | +import click |
| 25 | +import falcon |
| 26 | +from falcon import __version__ as flcnvers |
| 27 | +from hard import ( |
| 28 | + ConnectionManager, |
| 29 | + DeadUpdatingElements, |
| 30 | + LiveUpdatingElements, |
| 31 | + ProcessHandler, |
| 32 | +) |
| 33 | +from psutil import __version__ as psutvers |
| 34 | +from werkzeug import __version__ as wkzgvers |
| 35 | +from werkzeug import serving |
| 36 | + |
| 37 | + |
| 38 | +class LiveUpdatingEndpoint(object): |
| 39 | + def __init__(self, passcode): |
| 40 | + self.passcode = passcode |
| 41 | + |
| 42 | + def on_get(self, rqst, resp): |
| 43 | + passcode = rqst.get_param("passcode") |
| 44 | + if passcode == self.passcode: |
| 45 | + retnjson = LiveUpdatingElements().return_live_data() |
| 46 | + else: |
| 47 | + retnjson = {"retnmesg": "deny"} |
| 48 | + resp.text = json.dumps(retnjson, ensure_ascii=False) |
| 49 | + resp.set_header("Access-Control-Allow-Origin", "*") |
| 50 | + resp.status = falcon.HTTP_200 |
| 51 | + |
| 52 | + |
| 53 | +class DeadUpdatingEndpoint(object): |
| 54 | + def __init__(self, passcode): |
| 55 | + self.passcode = passcode |
| 56 | + |
| 57 | + def on_get(self, rqst, resp): |
| 58 | + passcode = rqst.get_param("passcode") |
| 59 | + if passcode == self.passcode: |
| 60 | + retnjson = DeadUpdatingElements().return_dead_data() |
| 61 | + else: |
| 62 | + retnjson = {"retnmesg": "deny"} |
| 63 | + resp.text = json.dumps(retnjson, ensure_ascii=False) |
| 64 | + resp.set_header("Access-Control-Allow-Origin", "*") |
| 65 | + resp.status = falcon.HTTP_200 |
| 66 | + |
| 67 | + |
| 68 | +class ProcessHandlingEndpoint(object): |
| 69 | + def __init__(self, passcode): |
| 70 | + self.passcode = passcode |
| 71 | + |
| 72 | + def on_get(self, rqst, resp): |
| 73 | + passcode = rqst.get_param("passcode") |
| 74 | + if passcode == self.passcode: |
| 75 | + retnjson = ProcessHandler(int(rqst.get_param("prociden"))).return_process_info() |
| 76 | + else: |
| 77 | + retnjson = {"retnmesg": "deny"} |
| 78 | + resp.text = json.dumps(retnjson, ensure_ascii=False) |
| 79 | + resp.set_header("Access-Control-Allow-Origin", "*") |
| 80 | + resp.status = falcon.HTTP_200 |
| 81 | + |
| 82 | + |
| 83 | +class ProcessKillingEndpoint(object): |
| 84 | + def __init__(self, passcode): |
| 85 | + self.passcode = passcode |
| 86 | + |
| 87 | + def on_get(self, rqst, resp): |
| 88 | + passcode = rqst.get_param("passcode") |
| 89 | + if passcode == self.passcode: |
| 90 | + retnjson = ProcessHandler(int(rqst.get_param("prociden"))).process_killer() |
| 91 | + else: |
| 92 | + retnjson = {"retnmesg": "deny"} |
| 93 | + resp.text = json.dumps(retnjson, ensure_ascii=False) |
| 94 | + resp.set_header("Access-Control-Allow-Origin", "*") |
| 95 | + resp.status = falcon.HTTP_200 |
| 96 | + |
| 97 | + |
| 98 | +class ProcessTerminatingEndpoint(object): |
| 99 | + def __init__(self, passcode): |
| 100 | + self.passcode = passcode |
| 101 | + |
| 102 | + def on_get(self, rqst, resp): |
| 103 | + passcode = rqst.get_param("passcode") |
| 104 | + if passcode == self.passcode: |
| 105 | + retnjson = ProcessHandler(int(rqst.get_param("prociden"))).process_terminator() |
| 106 | + else: |
| 107 | + retnjson = {"retnmesg": "deny"} |
| 108 | + resp.text = json.dumps(retnjson, ensure_ascii=False) |
| 109 | + resp.set_header("Access-Control-Allow-Origin", "*") |
| 110 | + resp.status = falcon.HTTP_200 |
| 111 | + |
| 112 | + |
| 113 | +class ProcessSuspendingEndpoint(object): |
| 114 | + def __init__(self, passcode): |
| 115 | + self.passcode = passcode |
| 116 | + |
| 117 | + def on_get(self, rqst, resp): |
| 118 | + passcode = rqst.get_param("passcode") |
| 119 | + if passcode == self.passcode: |
| 120 | + retnjson = ProcessHandler(int(rqst.get_param("prociden"))).process_suspender() |
| 121 | + else: |
| 122 | + retnjson = {"retnmesg": "deny"} |
| 123 | + resp.text = json.dumps(retnjson, ensure_ascii=False) |
| 124 | + resp.set_header("Access-Control-Allow-Origin", "*") |
| 125 | + resp.status = falcon.HTTP_200 |
| 126 | + |
| 127 | + |
| 128 | +class ProcessResumingEndpoint(object): |
| 129 | + def __init__(self, passcode): |
| 130 | + self.passcode = passcode |
| 131 | + |
| 132 | + def on_get(self, rqst, resp): |
| 133 | + passcode = rqst.get_param("passcode") |
| 134 | + if passcode == self.passcode: |
| 135 | + retnjson = ProcessHandler(int(rqst.get_param("prociden"))).process_resumer() |
| 136 | + else: |
| 137 | + retnjson = {"retnmesg": "deny"} |
| 138 | + resp.text = json.dumps(retnjson, ensure_ascii=False) |
| 139 | + resp.set_header("Access-Control-Allow-Origin", "*") |
| 140 | + resp.status = falcon.HTTP_200 |
| 141 | + |
| 142 | + |
| 143 | +main = falcon.API() |
| 144 | + |
| 145 | + |
| 146 | +@click.command() |
| 147 | +@click.option("-p", "--portdata", "portdata", help="Set the port value [0-65536].", default="6969") |
| 148 | +@click.option("-6", "--ipprotv6", "netprotc", flag_value="ipprotv6", help="Start the server on an IPv6 address.") |
| 149 | +@click.option("-4", "--ipprotv4", "netprotc", flag_value="ipprotv4", help="Start the server on an IPv4 address.") |
| 150 | +@click.version_option(version="1.0.1", prog_name=click.style("SuperVisor Driver Service", fg="magenta")) |
| 151 | +def mainfunc(portdata, netprotc): |
| 152 | + click.echo(" * " + click.style("SuperVisor Driver Service v1.0.1", fg="green")) |
| 153 | + netpdata = "" |
| 154 | + passcode = ConnectionManager().passphrase_generator() |
| 155 | + if netprotc == "ipprotv6": |
| 156 | + click.echo(" * " + click.style("IP version ", fg="magenta") + ": " + "6") |
| 157 | + netpdata = "::" |
| 158 | + elif netprotc == "ipprotv4": |
| 159 | + click.echo(" * " + click.style("IP version ", fg="magenta") + ": " + "4") |
| 160 | + netpdata = "0.0.0.0" |
| 161 | + click.echo(" * " + click.style("Passcode ", fg="magenta") + ": " + passcode + "\n" + |
| 162 | + " * " + click.style("Reference URI ", fg="magenta") + ": " + "http://" + netpdata + ":" + portdata + |
| 163 | + "/" + "\n" + |
| 164 | + " * " + click.style("Monitor service ", fg="magenta") + ": " + "Psutil v" + psutvers + "\n" + |
| 165 | + " * " + click.style("Endpoint service ", fg="magenta") + ": " + "Falcon v" + flcnvers + "\n" + |
| 166 | + " * " + click.style("HTTP server ", fg="magenta") + ": " + "Werkzeug v" + wkzgvers) |
| 167 | + livesync = LiveUpdatingEndpoint(passcode) |
| 168 | + deadsync = DeadUpdatingEndpoint(passcode) |
| 169 | + procinfo = ProcessHandlingEndpoint(passcode) |
| 170 | + killproc = ProcessKillingEndpoint(passcode) |
| 171 | + termproc = ProcessTerminatingEndpoint(passcode) |
| 172 | + suspproc = ProcessSuspendingEndpoint(passcode) |
| 173 | + resmproc = ProcessResumingEndpoint(passcode) |
| 174 | + main.add_route("/livesync", livesync) |
| 175 | + main.add_route("/deadsync", deadsync) |
| 176 | + main.add_route("/procinfo", procinfo) |
| 177 | + main.add_route("/killproc", killproc) |
| 178 | + main.add_route("/termproc", termproc) |
| 179 | + main.add_route("/suspproc", suspproc) |
| 180 | + main.add_route("/resmproc", resmproc) |
| 181 | + serving.run_simple(netpdata, int(portdata), main) |
| 182 | + |
| 183 | + |
| 184 | +if __name__ == "__main__": |
| 185 | + mainfunc() |
0 commit comments