Download app.py from Cxops/Brg: direct link, hf CLI and curl.
- Browser
- Download file 4 kB
-
https://huggingface.co/spaces/Cxops/Brg/resolve/main/app.py
- Command line
-
hf download hf://spaces/Cxops/Brg/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Cxops/Brg/resolve/main/app.py
4 kB
| import os | |
| import time | |
| import requests | |
| import qrcode | |
| from io import BytesIO | |
| from flask import Flask | |
| from pyrogram import Client, filters | |
| from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton | |
| # ----------------------------- | |
| # LOGGING | |
| # ----------------------------- | |
| def log(msg): | |
| print(f"[LOG {time.strftime('%H:%M:%S')}] {msg}", flush=True) | |
| # ----------------------------- | |
| # ENDPOINTS (FROM SECRETS) | |
| # ----------------------------- | |
| OBFS4_ENDPOINT = os.getenv("OBFS4_ENDPOINT") | |
| WEBTUNNEL_ENDPOINT = os.getenv("WEBTUNNEL_ENDPOINT") | |
| # fallback safety (prevents crash if secrets missing) | |
| if not OBFS4_ENDPOINT: | |
| OBFS4_ENDPOINT = "https://bridges.torproject.org/bridges?transport=obfs4" | |
| if not WEBTUNNEL_ENDPOINT: | |
| WEBTUNNEL_ENDPOINT = "https://bridges.torproject.org/bridges/en?transport=webtunnel" | |
| # ----------------------------- | |
| # HF SPACE KEEP ALIVE | |
| # ----------------------------- | |
| SPACE_URL = "https://cxops-brg.hf.space" | |
| def keep_space_alive(): | |
| while True: | |
| try: | |
| requests.get(SPACE_URL, timeout=10) | |
| log("HF Space pinged") | |
| except Exception as e: | |
| log(f"HF Space ping failed: {e}") | |
| time.sleep(300) | |
| # ----------------------------- | |
| # FETCH BRIDGES (FAST) | |
| # ----------------------------- | |
| def fetch_bridges(transport: str): | |
| url = WEBTUNNEL_ENDPOINT if transport == "webtunnel" else OBFS4_ENDPOINT | |
| try: | |
| r = requests.get(url, timeout=10, headers={"User-Agent": "Mozilla/5.0"}) | |
| except: | |
| return [] | |
| if r.status_code != 200: | |
| return [] | |
| bridges = [ | |
| line.strip() | |
| for line in r.text.splitlines() | |
| if transport in line and ":" in line | |
| ] | |
| if transport == "webtunnel" and not bridges: | |
| return fetch_bridges("obfs4") | |
| return bridges | |
| # ----------------------------- | |
| # QR (FAST MEMORY) | |
| # ----------------------------- | |
| def make_qr(data: str): | |
| qr = qrcode.QRCode(version=1, box_size=8, border=2) | |
| qr.add_data(data) | |
| qr.make(fit=True) | |
| img = qr.make_image(fill="black", back_color="white") | |
| bio = BytesIO() | |
| bio.name = "bridges.png" | |
| img.save(bio, "PNG") | |
| bio.seek(0) | |
| return bio | |
| # ----------------------------- | |
| # FLASK KEEP ALIVE | |
| # ----------------------------- | |
| app = Flask(__name__) | |
| def home(): | |
| return "OK" | |
| def run_web(): | |
| app.run(host="0.0.0.0", port=7860, debug=False, use_reloader=False) | |
| # ----------------------------- | |
| # BOT CONFIG | |
| # ----------------------------- | |
| API_ID = int(os.getenv("API_ID")) | |
| API_HASH = os.getenv("API_HASH") | |
| BOT_TOKEN = os.getenv("BOT_TOKEN") | |
| bot = Client( | |
| "tor_bridge_bot", | |
| api_id=API_ID, | |
| api_hash=API_HASH, | |
| bot_token=BOT_TOKEN | |
| ) | |
| # ----------------------------- | |
| # MENU | |
| # ----------------------------- | |
| def menu(): | |
| return InlineKeyboardMarkup([ | |
| [ | |
| InlineKeyboardButton("🔵 obfs4", callback_data="obfs4"), | |
| InlineKeyboardButton("🌐 webtunnel", callback_data="webtunnel") | |
| ] | |
| ]) | |
| # ----------------------------- | |
| # START | |
| # ----------------------------- | |
| def start(_, msg): | |
| msg.reply_text("🌉 Tor Bridge Bot", reply_markup=menu()) | |
| # ----------------------------- | |
| # FAST HANDLER | |
| # ----------------------------- | |
| def callback(_, query): | |
| transport = query.data | |
| query.message.edit_text("🔄 Fetching...") | |
| bridges = fetch_bridges(transport) | |
| if not bridges: | |
| query.message.edit_text("❌ No bridges found") | |
| return | |
| text = "\n".join(bridges) | |
| qr_img = make_qr(text) | |
| query.message.edit_text(text) | |
| query.message.reply_photo(photo=qr_img, caption="") | |
| # ----------------------------- | |
| # RUN | |
| # ----------------------------- | |
| if __name__ == "__main__": | |
| log("Starting system...") | |
| import threading | |
| threading.Thread(target=run_web, daemon=True).start() | |
| threading.Thread(target=keep_space_alive, daemon=True).start() | |
| log("Bot running...") | |
| bot.run() |