Files
proxy-suite-flake/windows/crates/ps-service/src/tg.rs
T
faumarayandClaude Opus 5.5 9b0d7021f9 windows: zapret-discord-youtube and tg-ws-proxy
zapret: Flowseal's release run as its service.bat runs it: the preset's
winws.exe line (continuations, cmd's caret escapes, %BIN%, %LISTS%, the
game filter) as winws's arguments, the user's domains and addresses in
the lists upstream's module puts them in (kept in the service's own run
directory, the verified release untouched), TCP timestamps on, and
zapret's sites in the proxy's direct list. tools/winws-dry-run.sh has
the release's own winws.exe verify every preset's arguments under Wine:
all 22, both game filters.

tg-ws-proxy: Flowseal's headless proxy on the embeddable CPython 3.12
with hash-pinned win_amd64 wheels (tools/build-tg-ws-proxy.sh), run with
upstream's flags; its relay goes direct under the TUN. Runs under Wine.

proxy-ctl: zapret status|on|off|preset|game-filter, components
list|install.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 23:55:45 +03:00

105 lines
3.5 KiB
Rust

//! tg-ws-proxy: Flowseal's Telegram WebSocket bridge, run as upstream's module runs it, on the
//! embedded Python the release carries.
use std::path::PathBuf;
use anyhow::{Context, Result};
use ps_core::config::Config;
use crate::paths::Paths;
use crate::units::UnitSpec;
pub const UNIT: &str = "proxy-suite-tg-ws-proxy";
/// The arguments upstream's module gives it (modules/proxy-suite/tg-ws-proxy.nix).
pub fn args(cfg: &Config, secret: &str) -> Vec<String> {
let t = &cfg.tg_ws_proxy;
let mut a = vec![
"-m".into(),
"proxy.tg_ws_proxy".into(),
format!("--port={}", t.listener.port),
format!("--host={}", t.listener.address),
format!("--secret={secret}"),
];
for (dc, ip) in &t.dc_ips {
a.push(format!("--dc-ip={dc}:{ip}"));
}
if cfg.debug.tg_ws_proxy {
a.push("--verbose".into());
}
if let Some(file) = &t.log.file {
a.push(format!("--log-file={file}"));
a.push(format!("--log-max-mb={}", t.log.max_size_mib));
a.push(format!("--log-backups={}", t.log.keep));
}
a.push(format!("--buf-kb={}", t.buffer_kib));
a.push(format!("--pool-size={}", t.pool_size));
for d in &t.cloudflare.domains {
a.push(format!("--cfproxy-domain={d}"));
}
for d in &t.cloudflare.worker_domains {
a.push(format!("--cfproxy-worker-domain={d}"));
}
if !t.cloudflare.fallback {
a.push("--no-cfproxy".into());
}
if let Some(d) = &t.fake_tls_domain {
a.push(format!("--fake-tls-domain={d}"));
}
if t.proxy_protocol {
a.push("--proxy-protocol".into());
}
a
}
/// The secret: given, or read from `secretFile`, or one generated once and kept.
pub fn secret(cfg: &Config, paths: &Paths) -> Result<String> {
let t = &cfg.tg_ws_proxy;
if let Some(s) = &t.secret {
return Ok(s.clone());
}
if let Some(f) = &t.secret_file {
return Ok(std::fs::read_to_string(f).with_context(|| format!("reading {f}"))?.trim().to_owned());
}
let kept = paths.root.join("tg-ws-proxy.secret");
if let Ok(s) = std::fs::read_to_string(&kept) {
return Ok(s.trim().to_owned());
}
let s: String = (0..32).map(|_| format!("{:x}", rand::random::<u8>() % 16)).collect();
crate::paths::write_atomic(&kept, s.as_bytes())?;
Ok(s)
}
pub fn unit(cfg: &Config, paths: &Paths, python: PathBuf) -> Result<UnitSpec> {
let app = paths.bin().join("tg-ws-proxy").join("app");
let args = args(cfg, &secret(cfg, paths)?);
Ok(UnitSpec {
name: UNIT.into(),
program: python,
generation: args.join("\n"),
args,
// The embedded Python finds its path in its ._pth file; nothing from the
// environment should change it.
env: vec![("PYTHONNOUSERSITE".into(), "1".into()), ("PYTHONDONTWRITEBYTECODE".into(), "1".into())],
cwd: Some(app),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn upstreams_flags() {
let mut cfg = Config::default();
cfg.tg_ws_proxy.dc_ips.insert("2".into(), "149.154.167.220".into());
cfg.tg_ws_proxy.cloudflare.fallback = false;
cfg.tg_ws_proxy.fake_tls_domain = Some("www.example.com".into());
let a = args(&cfg, "ab");
assert_eq!(&a[..5], ["-m", "proxy.tg_ws_proxy", "--port=1443", "--host=127.0.0.1", "--secret=ab"]);
for want in ["--dc-ip=2:149.154.167.220", "--buf-kb=256", "--pool-size=4", "--no-cfproxy", "--fake-tls-domain=www.example.com"] {
assert!(a.contains(&want.to_owned()), "{want} in {a:?}");
}
}
}