|
from flask import Flask, render_template |
|
from flask_socketio import SocketIO, emit |
|
from flask_cors import CORS |
|
import pty |
|
import os |
|
import subprocess |
|
import threading |
|
|
|
app = Flask(__name__) |
|
CORS(app) |
|
app.config['SECRET_KEY'] = 'your_secret_key' |
|
socketio = SocketIO(app) |
|
|
|
master_fd, slave_fd = pty.openpty() |
|
|
|
def run_shell(): |
|
process = subprocess.Popen(['bash'], stdin=slave_fd, stdout=slave_fd, stderr=slave_fd, text=True) |
|
|
|
while True: |
|
output = os.read(master_fd, 1024).decode('utf-8') |
|
if output: |
|
socketio.emit('output', output) |
|
socketio.sleep(0.1) |
|
|
|
@app.route('/') |
|
def index(): |
|
return render_template('index.html') |
|
|
|
@socketio.on('input') |
|
def handle_input(input_data): |
|
os.write(master_fd, (input_data + '\n').encode('utf-8')) |
|
|
|
if __name__ == '__main__': |
|
shell_thread = threading.Thread(target=run_shell) |
|
shell_thread.daemon = True |
|
shell_thread.start() |
|
socketio.run(app, host='0.0.0.0', port=7860, allow_unsafe_werkzeug=True) |
|
|