//! The Telegram proxy (Flowseal's tg-ws-proxy): on or off, the links to open in Telegram, and //! where it listens. use gpui_kit::assets::IconName; use gpui_kit::component::button::{Button, ButtonVariants as _}; use gpui_kit::component::input::{Input, InputState}; use gpui_kit::component::notification::Notification; use gpui_kit::component::switch::Switch; use gpui_kit::component::*; use gpui_kit::*; use ps_client::edits; use serde_json::{Value, json}; use super::widgets::{card, hint, page, state_tag}; use crate::model::Model; pub struct TelegramPage { model: Entity, port: Entity, fake_tls: Entity, /// The port and domain the inputs were last filled from (see the DPI page's `shown`). shown: (String, String), _subscriptions: Vec, } impl TelegramPage { pub fn new(model: Entity, window: &mut Window, cx: &mut Context) -> Self { let port = cx.new(|cx| InputState::new(window, cx).placeholder("1443")); let fake_tls = cx.new(|cx| InputState::new(window, cx).placeholder("none")); let subscriptions = vec![cx.observe_in(&model, window, |this, _, window, cx| this.sync(window, cx))]; let mut this = Self { model, port, fake_tls, shown: Default::default(), _subscriptions: subscriptions }; this.sync(window, cx); this } fn sync(&mut self, window: &mut Window, cx: &mut Context) { let t = self.model.read(cx).config()["tgWsProxy"].clone(); if t.is_null() { return; } let fresh = (t["listener"]["port"].to_string(), t["fakeTlsDomain"].as_str().unwrap_or("").to_owned()); if fresh != self.shown { if self.port.read(cx).value() == self.shown.0.as_str() { let v = fresh.0.clone(); self.port.update(cx, |s, cx| s.set_value(v, window, cx)); } if self.fake_tls.read(cx).value() == self.shown.1.as_str() { let v = fresh.1.clone(); self.fake_tls.update(cx, |s, cx| s.set_value(v, window, cx)); } self.shown = fresh; } } fn save(&mut self, window: &mut Window, cx: &mut Context) { let port_text = self.port.read(cx).value().trim().to_owned(); let Ok(port) = port_text.parse::() else { window.push_notification(Notification::error(format!("“{port_text}” is not a port (1–65535)")), cx); return; }; let domain = self.fake_tls.read(cx).value().trim().to_owned(); self.shown = (self.port.read(cx).value().to_string(), self.fake_tls.read(cx).value().to_string()); self.model.update(cx, |m, cx| { m.edit( "Telegram proxy saved", move |c| { edits::set_path(c, &["tgWsProxy", "listener", "port"], port.into()); edits::set_path(c, &["tgWsProxy", "fakeTlsDomain"], if domain.is_empty() { Value::Null } else { domain.clone().into() }); Ok(()) }, cx, ) }); } } impl Render for TelegramPage { fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { let m = self.model.read(cx); let status = m.status()["tgWsProxy"].clone(); let t = m.config()["tgWsProxy"].clone(); let links = m.tg_links().to_vec(); let body = page("Telegram proxy", cx); if t.is_null() { return body.child(hint("Waiting for the service…", cx)); } let model = self.model.clone(); let enable = Switch::new("tg-enable").checked(t["enable"] == true).on_change(move |on, _, cx| { let on = *on; model.update(cx, |m, cx| { m.edit( if on { "The Telegram proxy is on" } else { "The Telegram proxy is off" }, move |c| { edits::set_tg_ws_proxy_enabled(c, on); Ok(()) }, cx, ) }) }); let mut link_card = card("Links", None, cx).child(hint("Open one in Telegram, or copy it to a phone: Telegram then connects through this proxy.", cx)); if links.is_empty() { link_card = link_card.child(hint("No links: reading them needs the secrets scope (userControl).", cx)); } for (i, link) in links.into_iter().enumerate() { let (copy, open) = (link.clone(), link.clone()); link_card = link_card.child( div() .h_flex() .gap_2() .items_center() .child(div().flex_1().min_w_0().truncate().text_sm().font_family("monospace").child(link)) .child(Button::new(("copy-link", i)).small().icon(IconName::Copy).label("Copy").on_click(move |_, window, cx| { cx.write_to_clipboard(ClipboardItem::new_string(copy.to_string())); window.push_notification(Notification::success("Copied"), cx); })) .child(Button::new(("open-link", i)).small().primary().icon(IconName::Send).label("Open in Telegram").on_click(move |_, _, cx| cx.open_url(&open))), ); } let shared = t["listener"]["address"] == "0.0.0.0"; let model = self.model.clone(); let lan = Switch::new("tg-lan").label("Let other devices on this network connect (phones, other computers)").checked(shared).on_change(move |on, _, cx| { let address = if *on { "0.0.0.0" } else { "127.0.0.1" }; model.update(cx, |m, cx| { m.edit( if address == "0.0.0.0" { "Other devices may connect" } else { "Only this computer may connect" }, move |c| { edits::set_path(c, &["tgWsProxy", "listener", "address"], json!(address)); Ok(()) }, cx, ) }) }); body.child( card("tg-ws-proxy", Some(enable.into_any_element()), cx) .child(div().h_flex().child(state_tag(status["state"].as_str().unwrap_or("unknown")))) .child(hint("Telegram over WebSocket to its own servers, for networks that block its usual connections.", cx)), ) .child(link_card) .child( card( "Listening", Some(Button::new("tg-save").small().label("Save").on_click(cx.listener(|this, _, window, cx| this.save(window, cx))).into_any_element()), cx, ) .child(lan) .child( div() .h_flex() .gap_4() .child(div().v_flex().gap_1().w(px(160.)).child(div().text_sm().child("Port")).child(Input::new(&self.port))) .child(div().v_flex().gap_1().flex_1().child(div().text_sm().child("Fake-TLS domain (ee links)")).child(Input::new(&self.fake_tls))), ), ) } }