Files
proxy-suite-flake/windows/crates/ps-service/src/tg.rs
T
faumarayandClaude Opus 5.5 63aede0dc1 windows: the service names zapret's presets and tg-ws-proxy's links
- zapret-presets: the presets of the installed zapret release and the one
  configured; proxy-ctl zapret presets.
- tg-links: tg-ws-proxy's tg:// links (dd, and ee with a fake-TLS
  domain), as it prints them; they carry its secret, so the secrets scope.
  proxy-ctl tg link. No more reading them out of its log.
- tools/dev-service.sh: the service on Linux with a demo outbound, for
  working on the app.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 01:18:59 +03:00

134 lines
4.9 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 links tg-ws-proxy prints for itself: `dd` with the secret, and `ee` with the fake-TLS
/// domain after it when one is set. A wildcard listener is named by this machine's address.
pub fn links(cfg: &Config, secret: &str) -> Vec<String> {
let t = &cfg.tg_ws_proxy;
let host = match t.listener.address.as_str() {
"0.0.0.0" => std::net::UdpSocket::bind("0.0.0.0:0")
.and_then(|s| s.connect("8.8.8.8:80").and_then(|()| s.local_addr()))
.map(|a| a.ip().to_string())
.unwrap_or_else(|_| "127.0.0.1".into()),
h => h.to_owned(),
};
let base = format!("tg://proxy?server={host}&port={}&secret=", t.listener.port);
let mut out = vec![format!("{base}dd{secret}")];
if let Some(domain) = t.fake_tls_domain.as_deref().filter(|d| !d.is_empty()) {
let hex: String = domain.bytes().map(|b| format!("{b:02x}")).collect();
out.push(format!("{base}ee{secret}{hex}"));
}
out
}
/// 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 links_are_tg_ws_proxys() {
let mut cfg = Config::default();
cfg.tg_ws_proxy.listener.port = 1443;
assert_eq!(links(&cfg, "ab"), ["tg://proxy?server=127.0.0.1&port=1443&secret=ddab"]);
cfg.tg_ws_proxy.fake_tls_domain = Some("x.io".into());
assert_eq!(links(&cfg, "ab")[1], "tg://proxy?server=127.0.0.1&port=1443&secret=eeab782e696f");
}
#[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:?}");
}
}
}