from flask import Flask, jsonify, render_template_string
import psutil
import mtprotoproxy
import time
import os
app = Flask(__name__)
HTML_TEMPLATE = """
MTProto Proxy Dashboard
MTProto Proxy Dashboard
Active Connections
0
CPU Load
0%
Memory Load
0%
User Statistics
User Name
Active Connections
Total Connects
Data Transferred
"""
@app.route('/')
def index():
return render_template_string(HTML_TEMPLATE)
@app.route('/api/stats')
def stats():
user_stats = {}
try:
# mtprotoproxy.user_stats is a defaultdict of Counter
# Need to copy list of items first since dictionary can change size during iteration
items = list(mtprotoproxy.user_stats.items())
for user, counter in items:
user_stats[user] = dict(counter)
except Exception as e:
print(f"Error reading stats: {e}")
return jsonify({
"cpu_percent": psutil.cpu_percent(interval=None),
"mem_percent": psutil.virtual_memory().percent,
"user_stats": user_stats
})
def start_webui():
# Make sure we don't start the reloader, as we're running alongside asyncio loop
app.run(host='0.0.0.0', port=7860, debug=False, use_reloader=False)
if __name__ == "__main__":
start_webui()