Update app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,155 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import threading
|
| 3 |
import requests
|
| 4 |
from flask import Flask
|
| 5 |
from pyrogram import Client, filters
|
|
|
|
| 6 |
|
| 7 |
-
# -------------------
|
| 8 |
-
#
|
| 9 |
-
# -------------------
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
if r.status_code == 200:
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return f"BridgeDB error: {r.status_code}"
|
|
|
|
| 19 |
except Exception as e:
|
|
|
|
| 20 |
return f"Error: {str(e)}"
|
| 21 |
|
| 22 |
|
| 23 |
-
# -------------------
|
| 24 |
-
#
|
| 25 |
-
# -------------------
|
| 26 |
API_ID = int(os.getenv("API_ID"))
|
| 27 |
API_HASH = os.getenv("API_HASH")
|
| 28 |
BOT_TOKEN = os.getenv("BOT_TOKEN")
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
"tor_bridge_bot",
|
| 32 |
api_id=API_ID,
|
| 33 |
api_hash=API_HASH,
|
| 34 |
bot_token=BOT_TOKEN
|
| 35 |
)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
#
|
| 39 |
-
#
|
|
|
|
| 40 |
web = Flask(__name__)
|
| 41 |
|
| 42 |
@web.route("/")
|
| 43 |
def home():
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
@web.route("/health")
|
| 47 |
def health():
|
| 48 |
return {"status": "ok"}
|
| 49 |
|
| 50 |
|
| 51 |
-
# -------------------
|
| 52 |
-
#
|
| 53 |
-
# -------------------
|
| 54 |
-
@
|
| 55 |
-
def start(
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
if len(bridges) > 3500:
|
|
|
|
| 68 |
bridges = bridges[:3500] + "\n\n...truncated"
|
| 69 |
|
| 70 |
-
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
#
|
| 75 |
-
#
|
|
|
|
| 76 |
def run_bot():
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
|
| 81 |
def run_web():
|
| 82 |
-
|
| 83 |
web.run(host="0.0.0.0", port=7860)
|
| 84 |
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 87 |
t1 = threading.Thread(target=run_bot)
|
| 88 |
t2 = threading.Thread(target=run_web)
|
| 89 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
import threading
|
| 4 |
import requests
|
| 5 |
from flask import Flask
|
| 6 |
from pyrogram import Client, filters
|
| 7 |
+
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
|
| 8 |
|
| 9 |
+
# -----------------------------
|
| 10 |
+
# Logging helper
|
| 11 |
+
# -----------------------------
|
| 12 |
+
def log(msg):
|
| 13 |
+
print(f"[LOG {time.strftime('%Y-%m-%d %H:%M:%S')}] {msg}", flush=True)
|
| 14 |
|
| 15 |
|
| 16 |
+
# -----------------------------
|
| 17 |
+
# BridgeDB fetcher
|
| 18 |
+
# -----------------------------
|
| 19 |
+
def fetch_bridges(transport: str):
|
| 20 |
+
url = f"https://bridges.torproject.org/bridges?transport={transport}"
|
| 21 |
+
|
| 22 |
+
log(f"Requesting BridgeDB → {url}")
|
| 23 |
+
|
| 24 |
try:
|
| 25 |
+
start = time.time()
|
| 26 |
+
r = requests.get(url, timeout=20)
|
| 27 |
+
elapsed = round(time.time() - start, 2)
|
| 28 |
+
|
| 29 |
+
log(f"Response status: {r.status_code} | Time: {elapsed}s")
|
| 30 |
+
|
| 31 |
if r.status_code == 200:
|
| 32 |
+
data = r.text.strip()
|
| 33 |
+
log(f"Bridge data length: {len(data)} chars")
|
| 34 |
+
|
| 35 |
+
if not data:
|
| 36 |
+
log("WARNING: Empty bridge response")
|
| 37 |
+
return "⚠️ No bridges received (may be unavailable right now)."
|
| 38 |
+
|
| 39 |
+
return data
|
| 40 |
+
|
| 41 |
+
log("ERROR: Non-200 response from BridgeDB")
|
| 42 |
return f"BridgeDB error: {r.status_code}"
|
| 43 |
+
|
| 44 |
except Exception as e:
|
| 45 |
+
log(f"EXCEPTION: {str(e)}")
|
| 46 |
return f"Error: {str(e)}"
|
| 47 |
|
| 48 |
|
| 49 |
+
# -----------------------------
|
| 50 |
+
# ENV
|
| 51 |
+
# -----------------------------
|
| 52 |
API_ID = int(os.getenv("API_ID"))
|
| 53 |
API_HASH = os.getenv("API_HASH")
|
| 54 |
BOT_TOKEN = os.getenv("BOT_TOKEN")
|
| 55 |
|
| 56 |
+
|
| 57 |
+
# -----------------------------
|
| 58 |
+
# Telegram bot
|
| 59 |
+
# -----------------------------
|
| 60 |
+
bot = Client(
|
| 61 |
"tor_bridge_bot",
|
| 62 |
api_id=API_ID,
|
| 63 |
api_hash=API_HASH,
|
| 64 |
bot_token=BOT_TOKEN
|
| 65 |
)
|
| 66 |
|
| 67 |
+
|
| 68 |
+
# -----------------------------
|
| 69 |
+
# Flask heartbeat
|
| 70 |
+
# -----------------------------
|
| 71 |
web = Flask(__name__)
|
| 72 |
|
| 73 |
@web.route("/")
|
| 74 |
def home():
|
| 75 |
+
log("Heartbeat endpoint hit")
|
| 76 |
+
return "Tor Bridge Bot Running ✅"
|
| 77 |
|
| 78 |
@web.route("/health")
|
| 79 |
def health():
|
| 80 |
return {"status": "ok"}
|
| 81 |
|
| 82 |
|
| 83 |
+
# -----------------------------
|
| 84 |
+
# Start
|
| 85 |
+
# -----------------------------
|
| 86 |
+
@bot.on_message(filters.command("start"))
|
| 87 |
+
def start(_, msg):
|
| 88 |
+
log(f"/start from user {msg.from_user.id}")
|
| 89 |
+
|
| 90 |
+
msg.reply_text(
|
| 91 |
+
"👋 Tor Bridge Bot\n\n"
|
| 92 |
+
"Use /bridge to choose bridge type."
|
| 93 |
)
|
| 94 |
|
| 95 |
|
| 96 |
+
# -----------------------------
|
| 97 |
+
# Bridge menu
|
| 98 |
+
# -----------------------------
|
| 99 |
+
@bot.on_message(filters.command("bridge"))
|
| 100 |
+
def bridge_menu(_, msg):
|
| 101 |
+
log(f"/bridge menu requested by {msg.from_user.id}")
|
| 102 |
+
|
| 103 |
+
keyboard = InlineKeyboardMarkup([
|
| 104 |
+
[
|
| 105 |
+
InlineKeyboardButton("🔵 obfs4", callback_data="obfs4"),
|
| 106 |
+
InlineKeyboardButton("🌐 webtunnel", callback_data="webtunnel")
|
| 107 |
+
]
|
| 108 |
+
])
|
| 109 |
+
|
| 110 |
+
msg.reply_text("🌉 Select bridge type:", reply_markup=keyboard)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# -----------------------------
|
| 114 |
+
# Callback handler
|
| 115 |
+
# -----------------------------
|
| 116 |
+
@bot.on_callback_query()
|
| 117 |
+
def callback(_, query):
|
| 118 |
+
transport = query.data
|
| 119 |
|
| 120 |
+
log(f"User {query.from_user.id} selected {transport}")
|
| 121 |
+
|
| 122 |
+
query.message.edit_text(f"🔄 Fetching {transport} bridges...")
|
| 123 |
+
|
| 124 |
+
bridges = fetch_bridges(transport)
|
| 125 |
|
| 126 |
if len(bridges) > 3500:
|
| 127 |
+
log("Truncating long response")
|
| 128 |
bridges = bridges[:3500] + "\n\n...truncated"
|
| 129 |
|
| 130 |
+
log("Sending response to user")
|
| 131 |
|
| 132 |
+
query.message.edit_text(
|
| 133 |
+
f"🌉 {transport.upper()} BRIDGES\n\n{bridges}"
|
| 134 |
+
)
|
| 135 |
|
| 136 |
+
|
| 137 |
+
# -----------------------------
|
| 138 |
+
# Run bot + web
|
| 139 |
+
# -----------------------------
|
| 140 |
def run_bot():
|
| 141 |
+
log("Bot starting...")
|
| 142 |
+
bot.run()
|
| 143 |
|
| 144 |
|
| 145 |
def run_web():
|
| 146 |
+
log("Flask server starting on port 7860...")
|
| 147 |
web.run(host="0.0.0.0", port=7860)
|
| 148 |
|
| 149 |
|
| 150 |
if __name__ == "__main__":
|
| 151 |
+
log("Starting Tor Bridge Bot system")
|
| 152 |
+
|
| 153 |
t1 = threading.Thread(target=run_bot)
|
| 154 |
t2 = threading.Thread(target=run_web)
|
| 155 |
|