import { useState, useEffect } from "react"; import { Trans, useTranslation } from "react-i18next"; import { useVpnStore } from "@/store/useVpnStore"; import { useSettingsStore } from "@/store/useSettingsStore"; import { TunnelConfig, startVpn, stopVpn, launchApp } from "vpn-plugin"; import { Button } from "@/components/ui/button"; import { ShieldAlert, Loader2, Send } from "lucide-react"; import { motion, AnimatePresence } from "framer-motion"; import { isDesktop } from "@/lib/platform"; export function TelegramWeb() { const { t } = useTranslation(); const { status, setStatus } = useVpnStore(); const { killSwitch, excludedDomains } = useSettingsStore(); const [sessionRequested, setSessionRequested] = useState(false); const [errorMsg, setErrorMsg] = useState(""); const handleStopSession = async () => { setSessionRequested(false); try { await stopVpn(); } catch (e) { console.error("Stop VPN error:", e); } }; // Следим за возвратом в наше приложение useEffect(() => { const handleVisibilityChange = () => { // Если мы запросили сессию и юзер вернулся в приложение (документ стал видимым) if (document.visibilityState === "visible" && sessionRequested) { console.log( "Anti-Abuse: User returned to app, killing Telegram tunnel.", ); handleStopSession(); } }; document.addEventListener("visibilitychange", handleVisibilityChange); return () => document.removeEventListener("visibilitychange", handleVisibilityChange); }, [sessionRequested]); const handleStartSession = async () => { setErrorMsg(""); setSessionRequested(true); // Пакеты официальных клиентов Telegram const tgPackages = [ "org.telegram.messenger", "org.telegram.messenger.web", "org.telegram.plus", "org.thunderdog.challegram", ]; if (status !== "connected") { setStatus("connecting"); try { const config: TunnelConfig = { address: "10.0.0.1", prefix: 24, dns: "10.0.0.2", mtu: 1380, killswitchEnabled: true, // Всегда киллсвитч excludedApps: [], // Игнорируем исключения excludedDomains, allowedApps: tgPackages, // УКАЗЫВАЕМ, ЧТО ТОЛЬКО ЭТИ ПРИЛОЖЕНИЯ ИДУТ В VPN }; // Decoy-хост зашит вместе с адресом — этот тоннель не завязан на // выбор узла в UI (сэндбокс-режим только для Telegram). await startVpn(config, "147.45.43.70", 443, "ubuntu.com"); } catch (error) { setStatus("idle"); setSessionRequested(false); setErrorMsg(t("tg_error_connect_failed")); return; } } // Пытаемся запустить Telegram через Intent let launched = false; for (const pkg of tgPackages) { try { await launchApp(pkg); launched = true; break; // Если успешно, прерываем цикл } catch (e) { console.warn(`App ${pkg} not found`); } } if (!launched) { setErrorMsg(t("tg_error_not_installed")); handleStopSession(); } }; // Блокируем на ПК if (isDesktop) { return (

{t("tg_desktop_only_title")}

{t("tg_desktop_only_desc")}

); } const isSessionActive = sessionRequested && status === "connected"; return (
{!isSessionActive ? (
{t("tg_allowlist_badge")}

{t("tg_title")}

{t("tg_desc")}

}} />

{errorMsg && (

{errorMsg}

)}
) : (
{t("tg_active_status")}

{t("tg_session_bound_label")}

{t("tg_active_desc")}
)}
); }