//! The DPI bypass: zapret with Flowseal's presets, or zapret2 with its strategies; and the //! sites it handles beside the lists it comes with. use gpui_kit::component::button::Button; use gpui_kit::component::input::{Textarea, TextareaState}; use gpui_kit::component::radio::RadioGroup; use gpui_kit::component::select::{Select, SelectEvent, SelectState}; use gpui_kit::component::switch::Switch; use gpui_kit::component::*; use gpui_kit::*; use ps_client::edits; use serde_json::Value; use super::widgets::{card, hint, page, state_tag}; use crate::model::Model; const ENGINES: [(&str, &str); 2] = [("zapret-discord-youtube", "Flowseal's presets (zapret)"), ("zapret2", "zapret2")]; const GAME_FILTERS: [(&str, &str); 4] = [("null", "Off"), ("all", "TCP and UDP"), ("tcp", "TCP only"), ("udp", "UDP only")]; /// Preset names as the service matches them: without spaces, any case. fn same_preset(a: &str, b: &str) -> bool { let squash = |s: &str| s.chars().filter(|c| !c.is_whitespace()).collect::().to_lowercase(); squash(a) == squash(b) } fn lines(v: &Value) -> String { v.as_array().into_iter().flatten().filter_map(Value::as_str).collect::>().join("\n") } pub struct DpiPage { model: Entity, preset: Entity>>, presets: Vec, domains: Entity, exclude: Entity, /// The engine and lists the text areas were last filled from: news from the service /// replaces what the user has not changed, never what they have. shown: (String, String, String), _subscriptions: Vec, } impl DpiPage { pub fn new(model: Entity, window: &mut Window, cx: &mut Context) -> Self { let preset = cx.new(|cx| SelectState::new(Vec::::new(), None, window, cx).searchable(true)); let domains = cx.new(|cx| TextareaState::new(window, cx).rows(6).placeholder("example.com — one per line")); let exclude = cx.new(|cx| TextareaState::new(window, cx).rows(6).placeholder("bank.example — one per line")); let subscriptions = vec![ cx.observe_in(&model, window, |this, _, window, cx| this.sync(window, cx)), cx.subscribe_in(&preset, window, |this, _, event: &SelectEvent>, _, cx| { if let SelectEvent::Confirm(Some(name)) = event { let name = name.to_string(); this.model.update(cx, |m, cx| { m.edit( format!("Preset: {name}"), move |c| { edits::set_zapret_preset(c, &name); Ok(()) }, cx, ) }); } }), ]; let mut this = Self { model, preset, presets: Vec::new(), domains, exclude, shown: Default::default(), _subscriptions: subscriptions }; this.sync(window, cx); this } fn engine(config: &Value) -> String { config["zapret"]["engine"].as_str().unwrap_or("zapret-discord-youtube").to_owned() } /// Brings the controls up to what the service says. fn sync(&mut self, window: &mut Window, cx: &mut Context) { let m = self.model.read(cx); let config = m.config().clone(); let presets = m.presets().to_vec(); if config.is_null() { return; } if presets != self.presets { self.presets = presets.clone(); self.preset.update(cx, |s, cx| s.set_items(presets, window, cx)); } let configured = config["zapret"]["zapret-discord-youtube"]["configName"].as_str().unwrap_or(""); if let Some(name) = self.presets.iter().find(|p| same_preset(p, configured)).cloned() && self.preset.read(cx).selected_value() != Some(&name) { self.preset.update(cx, |s, cx| s.set_selected_value(&name, window, cx)); } let engine = Self::engine(&config); let fresh = (engine.clone(), lines(&config["zapret"][&engine]["domains"]), lines(&config["zapret"][&engine]["excludeDomains"])); if fresh != self.shown { let switched = fresh.0 != self.shown.0; if switched || self.domains.read(cx).value() == self.shown.1.as_str() { let text = fresh.1.clone(); self.domains.update(cx, |s, cx| s.set_value(text, window, cx)); } if switched || self.exclude.read(cx).value() == self.shown.2.as_str() { let text = fresh.2.clone(); self.exclude.update(cx, |s, cx| s.set_value(text, window, cx)); } self.shown = fresh; } } fn save_sites(&mut self, cx: &mut Context) { let split = |t: &str| t.split(['\n', ',', ' ']).map(str::to_owned).filter(|d| !d.trim().is_empty()).collect::>(); let (domains_text, exclude_text) = (self.domains.read(cx).value().to_string(), self.exclude.read(cx).value().to_string()); let (domains, exclude) = (split(&domains_text), split(&exclude_text)); // Handed over: the lists the service saves (tidied) replace what was typed. self.shown.1 = domains_text; self.shown.2 = exclude_text; let engine = Self::engine(self.model.read(cx).config()); self.model.update(cx, |m, cx| m.edit("Sites saved", move |c| edits::set_zapret_sites(c, &engine, &domains, &exclude), cx)); } /// A switch for one option of the configuration, by its path. fn option_switch(&self, id: &'static str, label: &'static str, path: &'static [&'static str], config: &Value) -> Switch { let mut v = config; for p in path { v = &v[*p]; } let model = self.model.clone(); Switch::new(id).label(label).checked(v == true).on_change(move |on, _, cx| { let on = *on; model.update(cx, |m, cx| { m.edit( format!("{label}: {}", if on { "on" } else { "off" }), move |c| { edits::set_path(c, path, on.into()); Ok(()) }, cx, ) }) }) } } impl Render for DpiPage { fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { let m = self.model.read(cx); let status = m.status()["zapret"].clone(); let config = m.config().clone(); let engine = Self::engine(&config); let body = page("DPI bypass", cx); if config.is_null() { return body.child(hint("Waiting for the service…", cx)); } let model = self.model.clone(); let enable = Switch::new("dpi-on").checked(config["zapret"]["enable"] == true).on_change(move |on, _, cx| { let on = *on; model.update(cx, |m, cx| { m.edit( if on { "The DPI bypass is on" } else { "The DPI bypass is off" }, move |c| { edits::set_zapret_enabled(c, on); Ok(()) }, cx, ) }) }); let model = self.model.clone(); let engines = RadioGroup::horizontal("engine") .children(ENGINES.map(|(_, label)| label)) .selected_index(ENGINES.iter().position(|(e, _)| *e == engine)) .on_change(move |ix, _, cx| { let (engine, label) = ENGINES[*ix]; model.update(cx, |m, cx| m.edit(format!("Engine: {label}"), move |c| edits::set_zapret_engine(c, engine), cx)); }); let mut body = body.child( card("zapret", Some(enable.into_any_element()), cx) .child(div().h_flex().child(state_tag(status["state"].as_str().unwrap_or("unknown")))) .child(engines) .child(hint( if engine == "zapret2" { "zapret2 (winws2) with nfqws2-keenetic's strategies; it learns which sites are blocked as it goes." } else { "Flowseal's zapret-discord-youtube: winws with one of its presets, for YouTube, Discord and the sites in its lists." }, cx, )), ); if engine == "zapret2" { body = body.child( card("zapret2", None, cx) .child(self.option_switch("autohostlist", "Learn which sites are blocked", &["zapret", "zapret2", "autoHostlist", "enable"], &config)) .child(self.option_switch("ipv6", "IPv6 too", &["zapret", "zapret2", "ipv6"], &config)) .child(self.option_switch("strategy-log", "Log the strategy each site gets", &["zapret", "zapret2", "strategyLog"], &config)), ); } else { let model = self.model.clone(); let filter = config["zapret"]["zapret-discord-youtube"]["gameFilter"].as_str().unwrap_or("null"); body = body .child( card("Preset", None, cx) .child(hint("One of the release's strategies. If a site still does not open, try the next one.", cx)) .child(Select::new(&self.preset).placeholder("Choose a preset").menu_max_h(px(320.))), ) .child( card("Game traffic", None, cx).child(hint("Also handle game traffic, on ports above 1023.", cx)).child( RadioGroup::horizontal("game-filter") .children(GAME_FILTERS.map(|(_, label)| label)) .selected_index(GAME_FILTERS.iter().position(|(v, _)| *v == filter)) .on_change(move |ix, _, cx| { let (value, label) = GAME_FILTERS[*ix]; model.update(cx, |m, cx| m.edit(format!("Game traffic: {label}"), move |c| edits::set_game_filter(c, value), cx)); }), ), ); } body.child( card( "Your sites", Some(Button::new("save-sites").small().label("Save").on_click(cx.listener(|this, _, _, cx| this.save_sites(cx))).into_any_element()), cx, ) .child(hint("Sites to handle besides the engine's own lists, and sites to leave alone.", cx)) .child( div() .h_flex() .gap_4() .items_start() .child(div().v_flex().gap_1().flex_1().child(div().text_sm().child("Also handle")).child(Textarea::new(&self.domains))) .child(div().v_flex().gap_1().flex_1().child(div().text_sm().child("Leave alone")).child(Textarea::new(&self.exclude))), ), ) .child(card("Windows", None, cx).child(self.option_switch("tcp-timestamps", "TCP timestamps (some strategies need them)", &["zapret", "tcpTimestamps"], &config))) } }