from flask import Flask, Response, request, jsonify import json import os import hmac import hashlib from urllib.parse import unquote app = Flask(__name__) HOTSPOTS_FILE = 'hotspots.json' BOT_TOKEN = '6909363967:AAGl58czIt7Vra_V8wWR7MyXkN_ayS27Soo' def get_all_hotspots(): if not os.path.exists(HOTSPOTS_FILE): return [] try: with open(HOTSPOTS_FILE, 'r', encoding='utf-8') as f: return json.load(f) except (json.JSONDecodeError, FileNotFoundError): return [] def save_hotspot(new_hotspot): hotspots = get_all_hotspots() hotspots.append(new_hotspot) with open(HOTSPOTS_FILE, 'w', encoding='utf-8') as f: json.dump(hotspots, f, ensure_ascii=False, indent=4) def is_data_safe(init_data: str) -> (bool, dict): try: encoded_data = unquote(init_data) data_check_string = [] recieved_hash = '' for pair in encoded_data.split('&'): key, value = pair.split('=', 1) if key == 'hash': recieved_hash = value else: data_check_string.append(f"{key}={value}") data_check_string.sort() data_check_string = "\n".join(data_check_string) secret_key = hmac.new("WebAppData".encode(), BOT_TOKEN.encode(), hashlib.sha256).digest() calculated_hash = hmac.new(secret_key, data_check_string.encode(), hashlib.sha256).hexdigest() if calculated_hash == recieved_hash: data = {k: v for k, v in [pair.split('=', 1) for pair in encoded_data.split('&')]} user_data = json.loads(unquote(data['user'])) return True, user_data return False, None except Exception: return False, None @app.route('/') def index(): html_content = '''