[dfc97ff] | 1 | import threading |
---|
| 2 | import platform |
---|
| 3 | import time |
---|
[694bc49] | 4 | from enum import Enum |
---|
[e20daf6] | 5 | import json |
---|
| 6 | import queue |
---|
[d5dca0f] | 7 | import sys |
---|
| 8 | import os |
---|
| 9 | import signal |
---|
[694bc49] | 10 | |
---|
[2fa8aa4] | 11 | from src.HTTPParser import * |
---|
| 12 | |
---|
[dfc97ff] | 13 | if platform.system() == 'Linux': |
---|
| 14 | from src.linux import ogOperations |
---|
| 15 | |
---|
[7c26c55] | 16 | class jsonResponse(): |
---|
| 17 | def __init__(self): |
---|
| 18 | self.jsontree = {} |
---|
| 19 | |
---|
| 20 | def addElement(self, key, value): |
---|
| 21 | self.jsontree[key] = value |
---|
| 22 | |
---|
| 23 | def dumpMsg(self): |
---|
| 24 | return json.dumps(self.jsontree) |
---|
| 25 | |
---|
[0f32b9c] | 26 | class restResponse(): |
---|
| 27 | def getResponse(response, jsonResp=None): |
---|
| 28 | msg = '' |
---|
| 29 | if response == ogResponses.BAD_REQUEST: |
---|
[3a44e48] | 30 | msg = 'HTTP/1.0 400 Bad Request' |
---|
[0f32b9c] | 31 | elif response == ogResponses.IN_PROGRESS: |
---|
| 32 | msg = 'HTTP/1.0 202 Accepted' |
---|
| 33 | elif response == ogResponses.OK: |
---|
| 34 | msg = 'HTTP/1.0 200 OK' |
---|
| 35 | elif response == ogResponses.INTERNAL_ERR: |
---|
| 36 | msg = 'HTTP/1.0 500 Internal Server Error' |
---|
[d5dca0f] | 37 | elif response == ogResponses.UNAUTHORIZED: |
---|
| 38 | msg = 'HTTP/1.0 401 Unauthorized' |
---|
[0f32b9c] | 39 | else: |
---|
| 40 | return msg |
---|
| 41 | |
---|
[3a44e48] | 42 | msg += '\r\n' |
---|
| 43 | |
---|
| 44 | if jsonResp: |
---|
| 45 | msg += 'Content-Length:' + str(len(jsonResp.dumpMsg())) |
---|
| 46 | msg += '\r\nContent-Type:application/json' |
---|
| 47 | msg += '\r\n\r\n' + jsonResp.dumpMsg() |
---|
| 48 | else: |
---|
| 49 | msg += '\r\n' |
---|
[0f32b9c] | 50 | |
---|
| 51 | return msg |
---|
| 52 | |
---|
[59a2823] | 53 | class ogThread(): |
---|
| 54 | # Executing cmd thread |
---|
[d5dca0f] | 55 | def execcmd(client, httpparser, ogRest): |
---|
[230bdca] | 56 | if httpparser.getCMD() == None: |
---|
| 57 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
| 58 | return |
---|
| 59 | |
---|
| 60 | try: |
---|
[d5dca0f] | 61 | shellout = ogOperations.execCMD(httpparser, ogRest) |
---|
[230bdca] | 62 | except ValueError as err: |
---|
| 63 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
| 64 | return |
---|
| 65 | |
---|
| 66 | if httpparser.getEcho(): |
---|
| 67 | jsonResp = jsonResponse() |
---|
| 68 | jsonResp.addElement('out', shellout) |
---|
| 69 | client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) |
---|
| 70 | else: |
---|
| 71 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
[59a2823] | 72 | |
---|
| 73 | # Powering off thread |
---|
[af90350] | 74 | def poweroff(): |
---|
[59a2823] | 75 | time.sleep(2) |
---|
| 76 | ogOperations.poweroff() |
---|
| 77 | |
---|
| 78 | # Rebooting thread |
---|
[af90350] | 79 | def reboot(): |
---|
[59a2823] | 80 | ogOperations.reboot() |
---|
| 81 | |
---|
[2fa8aa4] | 82 | # Process session |
---|
[d5dca0f] | 83 | def procsession(client, httpparser, ogRest): |
---|
[0f32b9c] | 84 | try: |
---|
[d5dca0f] | 85 | ogOperations.procsession(httpparser, ogRest) |
---|
[0f32b9c] | 86 | except ValueError as err: |
---|
| 87 | client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR)) |
---|
| 88 | return |
---|
| 89 | |
---|
| 90 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
[2fa8aa4] | 91 | |
---|
[6d1e79b] | 92 | # Process software |
---|
[d5dca0f] | 93 | def procsoftware(client, httpparser, path, ogRest): |
---|
[683afa6] | 94 | try: |
---|
[d5dca0f] | 95 | ogOperations.procsoftware(httpparser, path, ogRest) |
---|
[683afa6] | 96 | except ValueError as err: |
---|
| 97 | client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR)) |
---|
| 98 | return |
---|
| 99 | |
---|
| 100 | jsonResp = jsonResponse() |
---|
| 101 | jsonResp.addElement('disk', httpparser.getDisk()) |
---|
| 102 | jsonResp.addElement('partition', httpparser.getPartition()) |
---|
| 103 | |
---|
| 104 | f = open(path, "r") |
---|
| 105 | lines = f.readlines() |
---|
| 106 | f.close() |
---|
| 107 | jsonResp.addElement('software', lines[0]) |
---|
| 108 | |
---|
| 109 | client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) |
---|
[6d1e79b] | 110 | |
---|
[261a5ed] | 111 | # Process hardware |
---|
[d5dca0f] | 112 | def prochardware(client, path, ogRest): |
---|
[1ced3dd] | 113 | try: |
---|
[d5dca0f] | 114 | ogOperations.prochardware(path, ogRest) |
---|
[1ced3dd] | 115 | except ValueError as err: |
---|
| 116 | client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR)) |
---|
| 117 | return |
---|
| 118 | |
---|
| 119 | jsonResp = jsonResponse() |
---|
| 120 | f = open(path, "r") |
---|
| 121 | lines = f.readlines() |
---|
| 122 | f.close() |
---|
| 123 | jsonResp.addElement('hardware', lines[0]) |
---|
| 124 | client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) |
---|
[261a5ed] | 125 | |
---|
[efbe8a7] | 126 | # Process setup |
---|
[d5dca0f] | 127 | def procsetup(client, httpparser, ogRest): |
---|
[38b57c4] | 128 | jsonResp = jsonResponse() |
---|
| 129 | jsonResp.addElement('disk', httpparser.getDisk()) |
---|
| 130 | jsonResp.addElement('cache', httpparser.getCache()) |
---|
| 131 | jsonResp.addElement('cache_size', httpparser.getCacheSize()) |
---|
[d5dca0f] | 132 | listconfig = ogOperations.procsetup(httpparser, ogRest) |
---|
[38b57c4] | 133 | jsonResp.addElement('partition_setup', listconfig) |
---|
| 134 | client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) |
---|
[efbe8a7] | 135 | |
---|
[cc11d8f] | 136 | # Process image restore |
---|
[d5dca0f] | 137 | def procirestore(httpparser, ogRest): |
---|
[a306b8b] | 138 | try: |
---|
[d5dca0f] | 139 | ogOperations.procirestore(httpparser, ogRest) |
---|
[a306b8b] | 140 | except ValueError as err: |
---|
| 141 | client.send(restResponse.getResponse(ogResponses.INTERNAL_ERR)) |
---|
| 142 | return |
---|
| 143 | |
---|
| 144 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
[cc11d8f] | 145 | |
---|
[694bc49] | 146 | class ogResponses(Enum): |
---|
| 147 | BAD_REQUEST=0 |
---|
| 148 | IN_PROGRESS=1 |
---|
| 149 | OK=2 |
---|
[0f32b9c] | 150 | INTERNAL_ERR=3 |
---|
[d5dca0f] | 151 | UNAUTHORIZED=4 |
---|
[694bc49] | 152 | |
---|
[dfc97ff] | 153 | class ogRest(): |
---|
[d5dca0f] | 154 | def __init__(self): |
---|
| 155 | self.proc = None |
---|
| 156 | self.terminated = False |
---|
| 157 | |
---|
[2fa8aa4] | 158 | def processOperation(self, httpparser, client): |
---|
| 159 | op = httpparser.getRequestOP() |
---|
| 160 | URI = httpparser.getURI() |
---|
[d5dca0f] | 161 | |
---|
| 162 | if (not "stop" in URI and not self.proc == None and self.proc.poll() == None): |
---|
| 163 | client.send(restResponse.getResponse(ogResponses.UNAUTHORIZED)) |
---|
| 164 | return |
---|
| 165 | |
---|
[6764fc4] | 166 | if ("GET" in op): |
---|
[9c34a8e] | 167 | if "hardware" in URI: |
---|
[261a5ed] | 168 | self.process_hardware(client) |
---|
[9fd8f2d] | 169 | elif ("run/schedule" in URI): |
---|
| 170 | self.process_schedule(client) |
---|
[6764fc4] | 171 | else: |
---|
[0f32b9c] | 172 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
[6764fc4] | 173 | elif ("POST" in op): |
---|
| 174 | if ("poweroff" in URI): |
---|
| 175 | self.process_poweroff(client) |
---|
[9c34a8e] | 176 | elif "probe" in URI: |
---|
| 177 | self.process_probe(client) |
---|
[6764fc4] | 178 | elif ("reboot" in URI): |
---|
| 179 | self.process_reboot(client) |
---|
| 180 | elif ("shell/run" in URI): |
---|
[2e342b5] | 181 | self.process_shellrun(client, httpparser) |
---|
[2fa8aa4] | 182 | elif ("session" in URI): |
---|
[2e342b5] | 183 | self.process_session(client, httpparser) |
---|
[6d1e79b] | 184 | elif ("software" in URI): |
---|
[2e342b5] | 185 | self.process_software(client, httpparser) |
---|
[efbe8a7] | 186 | elif ("setup" in URI): |
---|
[2e342b5] | 187 | self.process_setup(client, httpparser) |
---|
[cc11d8f] | 188 | elif ("image/restore" in URI): |
---|
[2e342b5] | 189 | self.process_irestore(client, httpparser) |
---|
[d5dca0f] | 190 | elif ("stop" in URI): |
---|
| 191 | self.process_stop(client) |
---|
[6764fc4] | 192 | else: |
---|
[0f32b9c] | 193 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
[dfc97ff] | 194 | else: |
---|
[0f32b9c] | 195 | client.send(restResponse.getResponse(ogResponses.BAD_REQUEST)) |
---|
[dfc97ff] | 196 | |
---|
| 197 | return 0 |
---|
| 198 | |
---|
| 199 | def process_reboot(self, client): |
---|
[0f32b9c] | 200 | client.send(restResponse.getResponse(ogResponses.IN_PROGRESS)) |
---|
[dfc97ff] | 201 | client.disconnect() |
---|
[af90350] | 202 | threading.Thread(target=ogThread.reboot).start() |
---|
[dfc97ff] | 203 | |
---|
| 204 | def process_poweroff(self, client): |
---|
[0f32b9c] | 205 | client.send(restResponse.getResponse(ogResponses.IN_PROGRESS)) |
---|
[dfc97ff] | 206 | client.disconnect() |
---|
[af90350] | 207 | threading.Thread(target=ogThread.poweroff).start() |
---|
[dfc97ff] | 208 | |
---|
| 209 | def process_probe(self, client): |
---|
[336b023] | 210 | jsonResp = jsonResponse() |
---|
| 211 | jsonResp.addElement('status', 'OPG') |
---|
| 212 | client.send(restResponse.getResponse(ogResponses.OK, jsonResp)) |
---|
[e20daf6] | 213 | |
---|
[2e342b5] | 214 | def process_shellrun(self, client, httpparser): |
---|
[d5dca0f] | 215 | threading.Thread(target=ogThread.execcmd, args=(client, httpparser, self,)).start() |
---|
[2fa8aa4] | 216 | |
---|
[2e342b5] | 217 | def process_session(self, client, httpparser): |
---|
[d5dca0f] | 218 | threading.Thread(target=ogThread.procsession, args=(client, httpparser, self,)).start() |
---|
[6d1e79b] | 219 | |
---|
[2e342b5] | 220 | def process_software(self, client, httpparser): |
---|
[683afa6] | 221 | path = '/tmp/CSft-' + client.ip + '-' + httpparser.getPartition() |
---|
[d5dca0f] | 222 | threading.Thread(target=ogThread.procsoftware, args=(client, httpparser, path, self,)).start() |
---|
[261a5ed] | 223 | |
---|
| 224 | def process_hardware(self, client): |
---|
| 225 | path = '/tmp/Chrd-' + client.ip |
---|
[d5dca0f] | 226 | threading.Thread(target=ogThread.prochardware, args=(client, path, self,)).start() |
---|
[9fd8f2d] | 227 | |
---|
| 228 | def process_schedule(self, client): |
---|
[0f32b9c] | 229 | client.send(restResponse.getResponse(ogResponses.OK)) |
---|
[efbe8a7] | 230 | |
---|
[2e342b5] | 231 | def process_setup(self, client, httpparser): |
---|
[d5dca0f] | 232 | threading.Thread(target=ogThread.procsetup, args=(client, httpparser, self,)).start() |
---|
[cc11d8f] | 233 | |
---|
[2e342b5] | 234 | def process_irestore(self, client, httpparser): |
---|
[d5dca0f] | 235 | threading.Thread(target=ogThread.procirestore, args=(client, httpparser, self,)).start() |
---|
| 236 | |
---|
| 237 | def process_stop(self, client): |
---|
| 238 | client.disconnect() |
---|
| 239 | if self.proc == None: |
---|
| 240 | return |
---|
| 241 | |
---|
| 242 | if self.proc.poll() == None: |
---|
| 243 | os.killpg(os.getpgid(self.proc.pid), signal.SIGTERM) |
---|
| 244 | self.terminated = True |
---|
| 245 | sys.exit(0) |
---|