[dfc97ff] | 1 | import threading |
---|
| 2 | import platform |
---|
| 3 | import time |
---|
[694bc49] | 4 | from enum import Enum |
---|
[e20daf6] | 5 | import json |
---|
| 6 | import queue |
---|
[694bc49] | 7 | |
---|
[2fa8aa4] | 8 | from src.HTTPParser import * |
---|
| 9 | |
---|
[dfc97ff] | 10 | if platform.system() == 'Linux': |
---|
| 11 | from src.linux import ogOperations |
---|
| 12 | |
---|
[7c26c55] | 13 | class jsonResponse(): |
---|
| 14 | def __init__(self): |
---|
| 15 | self.jsontree = {} |
---|
| 16 | |
---|
| 17 | def addElement(self, key, value): |
---|
| 18 | self.jsontree[key] = value |
---|
| 19 | |
---|
| 20 | def dumpMsg(self): |
---|
| 21 | return json.dumps(self.jsontree) |
---|
| 22 | |
---|
[0f32b9c] | 23 | class restResponse(): |
---|
| 24 | def getResponse(response, jsonResp=None): |
---|
| 25 | msg = '' |
---|
| 26 | if response == ogResponses.BAD_REQUEST: |
---|
| 27 | msg = 'HTTP/1.0 400 Bad request' |
---|
| 28 | elif response == ogResponses.IN_PROGRESS: |
---|
| 29 | msg = 'HTTP/1.0 202 Accepted' |
---|
| 30 | elif response == ogResponses.OK: |
---|
| 31 | msg = 'HTTP/1.0 200 OK' |
---|
| 32 | elif response == ogResponses.INTERNAL_ERR: |
---|
| 33 | msg = 'HTTP/1.0 500 Internal Server Error' |
---|
| 34 | else: |
---|
| 35 | return msg |
---|
| 36 | |
---|
| 37 | if not jsonResp == None: |
---|
| 38 | msg = msg + '\nContent-Type:application/json' |
---|
| 39 | msg = msg + '\nContent-Length:' + str(len(jsonResp.dumpMsg())) |
---|
| 40 | msg = msg + '\n' + jsonResp.dumpMsg() |
---|
| 41 | |
---|
| 42 | msg = msg + '\r\n\r\n' |
---|
| 43 | return msg |
---|
| 44 | |
---|
[59a2823] | 45 | class ogThread(): |
---|
| 46 | # Executing cmd thread |
---|
[2e342b5] | 47 | def execcmd(msgqueue, httpparser): |
---|
[b571f40] | 48 | msgqueue.queue.clear() |
---|
[2e342b5] | 49 | msgqueue.put(ogOperations.execCMD(httpparser)) |
---|
[59a2823] | 50 | |
---|
| 51 | # Powering off thread |
---|
[af90350] | 52 | def poweroff(): |
---|
[59a2823] | 53 | time.sleep(2) |
---|
| 54 | ogOperations.poweroff() |
---|
| 55 | |
---|
| 56 | # Rebooting thread |
---|
[af90350] | 57 | def reboot(): |
---|
[59a2823] | 58 | ogOperations.reboot() |
---|
| 59 | |
---|
[2fa8aa4] | 60 | # Process session |
---|
[0f32b9c] | 61 | def procsession(client, httpparser): |
---|
| 62 | try: |
---|
| 63 | ogOperations.procsession(httpparser) |
---|
| 64 | except ValueError as err: |
---|
| 65 | client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR)) |
---|
| 66 | return |
---|
| 67 | |
---|
| 68 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
[2fa8aa4] | 69 | |
---|
[6d1e79b] | 70 | # Process software |
---|
[2e342b5] | 71 | def procsoftware(msgqueue, httpparser, path): |
---|
[6d1e79b] | 72 | msgqueue.queue.clear() |
---|
[2e342b5] | 73 | msgqueue.put(ogOperations.procsoftware(httpparser, path)) |
---|
[6d1e79b] | 74 | |
---|
[261a5ed] | 75 | # Process hardware |
---|
| 76 | def prochardware(msgqueue, path): |
---|
| 77 | msgqueue.queue.clear() |
---|
| 78 | msgqueue.put(ogOperations.prochardware(path)) |
---|
| 79 | |
---|
[efbe8a7] | 80 | # Process setup |
---|
[2e342b5] | 81 | def procsetup(msgqueue, httpparser): |
---|
| 82 | ogOperations.procsetup(httpparser) |
---|
[efbe8a7] | 83 | |
---|
[cc11d8f] | 84 | # Process image restore |
---|
[2e342b5] | 85 | def procirestore(msgqueue, httpparser): |
---|
[cc11d8f] | 86 | msgqueue.queue.clear() |
---|
[2e342b5] | 87 | msgqueue.put(ogOperations.procirestore(httpparser)) |
---|
[cc11d8f] | 88 | |
---|
[694bc49] | 89 | class ogResponses(Enum): |
---|
| 90 | BAD_REQUEST=0 |
---|
| 91 | IN_PROGRESS=1 |
---|
| 92 | OK=2 |
---|
[0f32b9c] | 93 | INTERNAL_ERR=3 |
---|
[694bc49] | 94 | |
---|
[dfc97ff] | 95 | class ogRest(): |
---|
[e20daf6] | 96 | def __init__(self): |
---|
| 97 | self.msgqueue = queue.Queue(1000) |
---|
| 98 | |
---|
[2fa8aa4] | 99 | def processOperation(self, httpparser, client): |
---|
| 100 | op = httpparser.getRequestOP() |
---|
| 101 | URI = httpparser.getURI() |
---|
[6764fc4] | 102 | if ("GET" in op): |
---|
| 103 | if ("probe" in URI): |
---|
| 104 | self.process_probe(client) |
---|
| 105 | elif ("shell/output" in URI): |
---|
| 106 | self.process_shellout(client) |
---|
[261a5ed] | 107 | elif ("hardware" in URI): |
---|
| 108 | self.process_hardware(client) |
---|
[9fd8f2d] | 109 | elif ("run/schedule" in URI): |
---|
| 110 | self.process_schedule(client) |
---|
[6764fc4] | 111 | else: |
---|
[0f32b9c] | 112 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
[6764fc4] | 113 | elif ("POST" in op): |
---|
| 114 | if ("poweroff" in URI): |
---|
| 115 | self.process_poweroff(client) |
---|
| 116 | elif ("reboot" in URI): |
---|
| 117 | self.process_reboot(client) |
---|
| 118 | elif ("shell/run" in URI): |
---|
[2e342b5] | 119 | self.process_shellrun(client, httpparser) |
---|
[2fa8aa4] | 120 | elif ("session" in URI): |
---|
[2e342b5] | 121 | self.process_session(client, httpparser) |
---|
[6d1e79b] | 122 | elif ("software" in URI): |
---|
[2e342b5] | 123 | self.process_software(client, httpparser) |
---|
[efbe8a7] | 124 | elif ("setup" in URI): |
---|
[2e342b5] | 125 | self.process_setup(client, httpparser) |
---|
[cc11d8f] | 126 | elif ("image/restore" in URI): |
---|
[2e342b5] | 127 | self.process_irestore(client, httpparser) |
---|
[6764fc4] | 128 | else: |
---|
[0f32b9c] | 129 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
[dfc97ff] | 130 | else: |
---|
[0f32b9c] | 131 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
[dfc97ff] | 132 | |
---|
| 133 | return 0 |
---|
| 134 | |
---|
| 135 | def process_reboot(self, client): |
---|
[0f32b9c] | 136 | client.send(restResponse.getResponse(ogResponses.IN_PROGRESS)) |
---|
[dfc97ff] | 137 | client.disconnect() |
---|
[af90350] | 138 | threading.Thread(target=ogThread.reboot).start() |
---|
[dfc97ff] | 139 | |
---|
| 140 | def process_poweroff(self, client): |
---|
[0f32b9c] | 141 | client.send(restResponse.getResponse(ogResponses.IN_PROGRESS)) |
---|
[dfc97ff] | 142 | client.disconnect() |
---|
[af90350] | 143 | threading.Thread(target=ogThread.poweroff).start() |
---|
[dfc97ff] | 144 | |
---|
| 145 | def process_probe(self, client): |
---|
[0f32b9c] | 146 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
[e20daf6] | 147 | |
---|
[2e342b5] | 148 | def process_shellrun(self, client, httpparser): |
---|
| 149 | if httpparser.getCMD() == None: |
---|
[0f32b9c] | 150 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
[e20daf6] | 151 | return |
---|
| 152 | |
---|
[e6eba4b] | 153 | try: |
---|
[2e342b5] | 154 | ogThread.execcmd(self.msgqueue, httpparser) |
---|
[e6eba4b] | 155 | except ValueError as err: |
---|
| 156 | print(err.args[0]) |
---|
[0f32b9c] | 157 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
[e6eba4b] | 158 | return |
---|
| 159 | |
---|
[0f32b9c] | 160 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
[e20daf6] | 161 | |
---|
| 162 | def process_shellout(self, client): |
---|
[7c26c55] | 163 | jsonResp = jsonResponse() |
---|
[e20daf6] | 164 | if self.msgqueue.empty(): |
---|
[7c26c55] | 165 | jsonResp.addElement('out', '') |
---|
[0f32b9c] | 166 | client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) |
---|
[e20daf6] | 167 | else: |
---|
[7c26c55] | 168 | jsonResp.addElement('out', self.msgqueue.get()) |
---|
[0f32b9c] | 169 | client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) |
---|
[2fa8aa4] | 170 | |
---|
[2e342b5] | 171 | def process_session(self, client, httpparser): |
---|
[0f32b9c] | 172 | threading.Thread(target=ogThread.procsession, args=(client, httpparser,)).start() |
---|
[6d1e79b] | 173 | |
---|
[2e342b5] | 174 | def process_software(self, client, httpparser): |
---|
[6d1e79b] | 175 | path = '/tmp/CSft-' + client.ip + '-' + partition |
---|
[2e342b5] | 176 | threading.Thread(target=ogThread.procsoftware, args=(self.msgqueue, httpparser, path,)).start() |
---|
[0f32b9c] | 177 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
[261a5ed] | 178 | |
---|
| 179 | def process_hardware(self, client): |
---|
| 180 | path = '/tmp/Chrd-' + client.ip |
---|
| 181 | threading.Thread(target=ogThread.prochardware, args=(self.msgqueue, path,)).start() |
---|
[0f32b9c] | 182 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
[9fd8f2d] | 183 | |
---|
| 184 | def process_schedule(self, client): |
---|
[0f32b9c] | 185 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
[efbe8a7] | 186 | |
---|
[2e342b5] | 187 | def process_setup(self, client, httpparser): |
---|
| 188 | threading.Thread(target=ogThread.procsetup, args=(self.msgqueue, httpparser,)).start() |
---|
[0f32b9c] | 189 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
[cc11d8f] | 190 | |
---|
[2e342b5] | 191 | def process_irestore(self, client, httpparser): |
---|
| 192 | threading.Thread(target=ogThread.procirestore, args=(self.msgqueue, httpparser,)).start() |
---|
[0f32b9c] | 193 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|