Files
proxy-suite-flake/windows/crates/ps-core/src/warp.rs
T
faumarayandClaude Opus 5.5 62adadc207 windows: WARP as a sing-box outbound
- Devices registered as wgcf registers them: an X25519 key made by the
  service, its public half posted to Cloudflare's client API (through the
  local proxy first, as upstream, then directly, then warp.generatorUrl),
  the answer made into wgcf's profile and kept below the protected state
  root; or a profile given (config, configFile). The renderer's endpoint
  for it was there; the service now supplies the profiles.
- Registration runs outside the service's lock, at start, after a change,
  and every ten minutes until it succeeds; the proxy is applied again
  with the new device.
- Checked: a device registered live with Cloudflare, its profile rendered
  as a sing-box WireGuard endpoint and the proxy running with it.
  WARP through AmneziaWG (userspace, interface) is still to come, and said.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 08:13:29 +03:00

95 lines
4.1 KiB
Rust

//! Cloudflare WARP's registration, as wgcf makes it: a device's public key sent to Cloudflare's
//! client API, and the answer turned into the WireGuard profile `wgcf generate` writes.
use serde_json::{Value, json};
/// Cloudflare's client API, the version wgcf speaks.
pub const API: &str = "https://api.cloudflareclient.com/v0a2158/reg";
/// The headers wgcf sends: the API answers the clients it knows.
pub const HEADERS: [(&str, &str); 2] = [("User-Agent", "okhttp/3.12.1"), ("CF-Client-Version", "a-6.30-3596")];
/// The registration's body: the device's public key (base64), the time the terms were
/// accepted (RFC 3339).
pub fn registration(public_key: &str, tos: &str) -> Value {
json!({
"install_id": "",
"fcm_token": "",
"tos": tos,
"key": public_key,
"type": "Android",
"model": "PC",
"locale": "en_US",
})
}
/// The WireGuard profile of a registration's answer, with the device's private key: as
/// `wgcf generate` writes it.
pub fn profile(answer: &Value, private_key: &str) -> Result<String, String> {
let c = &answer["config"];
let v4 = c["interface"]["addresses"]["v4"].as_str().ok_or("the answer has no IPv4 address")?;
let v6 = c["interface"]["addresses"]["v6"].as_str();
let peer = &c["peers"][0];
let public_key = peer["public_key"].as_str().ok_or("the answer has no peer key")?;
let endpoint = peer["endpoint"]["host"].as_str().unwrap_or("engage.cloudflareclient.com:2408");
let mut address = format!("{v4}/32");
if let Some(v6) = v6 {
address.push_str(&format!(", {v6}/128"));
}
Ok(format!(
"[Interface]\nPrivateKey = {private_key}\nAddress = {address}\nDNS = 1.1.1.1, 1.0.0.1, 2606:4700:4700::1111, 2606:4700:4700::1001\nMTU = 1280\n\n\
[Peer]\nPublicKey = {public_key}\nAllowedIPs = 0.0.0.0/0, ::/0\nEndpoint = {endpoint}\n"
))
}
/// What a generator (`warp.generatorUrl`) hands back: the profile, or `{"content": base64}`.
pub fn generated(body: &str) -> Result<String, String> {
let text = if body.trim_start().starts_with("[Interface]") {
body.to_owned()
} else {
let v: Value = serde_json::from_str(body).map_err(|_| "the generator's answer is neither a profile nor JSON")?;
let content = v["content"].as_str().ok_or("the generator's JSON has no content")?;
use base64::Engine;
let bytes = base64::engine::general_purpose::STANDARD.decode(content.trim()).map_err(|_| "the generator's content is not base64")?;
String::from_utf8(bytes).map_err(|_| "the generator's profile is not text")?
};
if !text.lines().any(|l| l.trim_start().starts_with("PrivateKey")) {
return Err("the generator's profile has no private key".into());
}
Ok(text)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_answer_becomes_wgcfs_profile() {
let answer = json!({
"config": {
"interface": { "addresses": { "v4": "172.16.0.2", "v6": "2606:4700:110:8a36::1" } },
"peers": [{ "public_key": "bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=", "endpoint": { "host": "engage.cloudflareclient.com:2408" } }],
}
});
let p = profile(&answer, "PRIV").unwrap();
assert!(p.contains("PrivateKey = PRIV\n"));
assert!(p.contains("Address = 172.16.0.2/32, 2606:4700:110:8a36::1/128\n"));
assert!(p.contains("PublicKey = bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=\n"));
// What sing-box gets of it.
let (ep, _) = crate::wg::sing_box_endpoint(&p, "warp", None).unwrap();
assert_eq!(ep["peers"][0]["address"], "engage.cloudflareclient.com");
assert_eq!(ep["peers"][0]["port"], 2408);
assert!(profile(&json!({}), "PRIV").is_err());
}
#[test]
fn a_generators_answer_either_way() {
let p = "[Interface]\nPrivateKey = K\n";
assert_eq!(generated(p).unwrap(), p);
use base64::Engine;
let b = base64::engine::general_purpose::STANDARD.encode(p);
assert_eq!(generated(&format!(r#"{{"content":"{b}"}}"#)).unwrap(), p);
assert!(generated("<html>").is_err());
}
}