69 lines
2.4 KiB
Python
69 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
import json
|
|
import subprocess
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
|
|
XAMPP = '/opt/lampp/xampp'
|
|
ALLOWED = {'start','stop','restart','reload','status','startapache','stopapache','startmysql','stopmysql','startftp','stopftp'}
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
def log_message(self, fmt, *args):
|
|
pass
|
|
|
|
def _cors(self):
|
|
self.send_header('Access-Control-Allow-Origin', '*')
|
|
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
|
|
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
|
|
|
|
def _send(self, code, body):
|
|
data = json.dumps(body).encode()
|
|
try:
|
|
self.send_response(code)
|
|
self.send_header('Content-Type', 'application/json')
|
|
self._cors()
|
|
self.send_header('Content-Length', str(len(data)))
|
|
self.end_headers()
|
|
self.wfile.write(data)
|
|
except BrokenPipeError:
|
|
pass
|
|
|
|
def do_OPTIONS(self):
|
|
self.send_response(204)
|
|
self._cors()
|
|
self.end_headers()
|
|
|
|
def do_GET(self):
|
|
if self.path == '/ping':
|
|
self._send(200, {'ok': True})
|
|
else:
|
|
self._send(404, {'error': 'Not found'})
|
|
|
|
def do_POST(self):
|
|
if self.path != '/run':
|
|
self._send(404, {'error': 'Not found'})
|
|
return
|
|
length = int(self.headers.get('Content-Length', 0))
|
|
body = json.loads(self.rfile.read(length))
|
|
cmd = body.get('cmd', '').strip()
|
|
if cmd not in ALLOWED:
|
|
self._send(400, {'error': f'Command not allowed: {cmd}'})
|
|
return
|
|
try:
|
|
result = subprocess.run(['sudo', XAMPP, cmd], capture_output=True, text=True, timeout=180)
|
|
output = (result.stdout + result.stderr).strip()
|
|
self._send(200, {'output': output, 'code': result.returncode})
|
|
except subprocess.TimeoutExpired:
|
|
self._send(200, {'output': 'Timeout after 180 seconds.', 'code': -1})
|
|
except Exception as e:
|
|
self._send(500, {'error': str(e)})
|
|
|
|
if __name__ == '__main__':
|
|
server = ThreadingHTTPServer(('127.0.0.1', 5050), Handler)
|
|
print('XAMPP GUI Backend - Port 5050')
|
|
print('Open: http://localhost:8080/')
|
|
print('Stop: Ctrl+C')
|
|
try:
|
|
server.serve_forever()
|
|
except KeyboardInterrupt:
|
|
pass
|