//! 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 { 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 { 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::() % 16)).collect(); crate::paths::write_atomic(&kept, s.as_bytes())?; Ok(s) } pub fn unit(cfg: &Config, paths: &Paths, python: PathBuf) -> Result { 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:?}"); } } }