07ccff4241
tools/gs-probe (own workspace, never published): `probe serverinfo | pair | apps | launch <app-id> | quit | unpair` against one host address, driving DomiStream's pairing, HTTP, RTSP, ENet, control, video and audio state machines over real sockets. Structured JSONL log per run (requests, answers, timings, the U each record bears on, parser errors with their bytes), secrets redacted (rikey, pairing values; PIN never logged; registered key hex scrubbed from every line). Test-only client key and cert in --out. Video: frames, decode-ready frames, FEC recoveries, losses, elementary-stream dump, live U14 parity check; audio: packets, losses, decrypting vs plain receivers (U17), packet dump and offline U16 solver (`solve-audio-fec`). START_A/START_B/PERIODIC_PING/IDR bodies come from the command line (U10). docs/LIVE-INTEROP.md: the sitting's run book (preparation, sequence S0-S5 with the U each step settles, variants, log reading, where each fact lands in PROTOCOL.md and the code, stop-and-report rule, clean-up, Mark's timeline). Built and unit-tested only (41 tests, clippy -D warnings, fmt); never run against a host.
223 lines
8.5 KiB
Rust
223 lines
8.5 KiB
Rust
//! `serverinfo`, `pair`, `apps`, `quit`, `unpair`.
|
|
|
|
use domistream::http::{
|
|
Scheme, app_list_request, cancel_request, parse_app_list, parse_server_info, parse_status_response,
|
|
server_info_request, unpair_request,
|
|
};
|
|
use domistream::pairing::{PairingSession, PairingState, PairingStep, Pin, ServerCert, parse_pair_response};
|
|
|
|
use crate::log::{J, Kind};
|
|
use crate::rng::sha256_hex;
|
|
use crate::{Ctx, body_j, state};
|
|
|
|
/// Non-200 HTTP status: stop and report (U27 is in the body).
|
|
fn check_status(ctx: &mut Ctx, step: &str, x: &crate::http::Exchange) -> Result<(), String> {
|
|
if x.response.status == 200 {
|
|
return Ok(());
|
|
}
|
|
let m = format!("HTTP status {} {}", x.response.status, x.response.reason);
|
|
ctx.log.stop(step, &["U27"], m.clone(), body_j(&x.response.body));
|
|
Err(m)
|
|
}
|
|
|
|
fn server_info(ctx: &mut Ctx, scheme: Scheme) -> Result<(), String> {
|
|
let step = match scheme {
|
|
Scheme::Http => "serverinfo.http",
|
|
Scheme::Https => "serverinfo.https",
|
|
};
|
|
let req = server_info_request(scheme, &ctx.id.unique_id).map_err(|e| e.to_string())?;
|
|
let x = ctx.http(step, &["U25", "U27"], &req, None)?;
|
|
check_status(ctx, step, &x)?;
|
|
let info = match parse_server_info(&x.response.body) {
|
|
Ok(i) => i,
|
|
Err(e) => {
|
|
ctx.log.parse_error(step, &["U25", "U27"], &e, body_j(&x.response.body));
|
|
ctx.log.stop(step, &["U25"], format!("serverinfo refused: {e}"), J::Null);
|
|
return Err(format!("serverinfo refused: {e}"));
|
|
}
|
|
};
|
|
let codec_bits = info.codec_mode_support.map_or(J::Null, |b| J::s(format!("{b:#010x} = {b:032b}")));
|
|
ctx.log.record(
|
|
step,
|
|
Kind::Note,
|
|
&["U25"],
|
|
J::O(vec![("parsed", J::dbg(&info)), ("codec_mode_support_bits", codec_bits)]),
|
|
);
|
|
println!("{step}:");
|
|
println!(" hostname {} appversion {} GfeVersion {:?}", info.hostname, info.app_version, info.gfe_version);
|
|
println!(
|
|
" HttpsPort {} ExternalPort {:?} state {} currentgame {}",
|
|
info.https_port, info.http_port, info.state, info.current_game
|
|
);
|
|
println!(
|
|
" ServerCodecModeSupport {:?} MaxLumaPixelsHEVC {:?} PairStatus {}",
|
|
info.codec_mode_support, info.max_luma_pixels_hevc, info.paired
|
|
);
|
|
if info.https_port != ctx.opts.https_port {
|
|
let m = format!(
|
|
"the host's HttpsPort is {}; pass --https-port {} for HTTPS calls",
|
|
info.https_port, info.https_port
|
|
);
|
|
ctx.log.note(step, &[], m.clone());
|
|
println!(" note: {m}");
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub fn serverinfo(ctx: &mut Ctx) -> Result<(), String> {
|
|
server_info(ctx, Scheme::Http)?;
|
|
if ctx.host_cert.is_some() {
|
|
server_info(ctx, Scheme::Https)?;
|
|
} else {
|
|
println!("(not paired yet: HTTPS serverinfo skipped)");
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub fn pair(ctx: &mut Ctx) -> Result<(), String> {
|
|
let shown = Pin::generate(&mut ctx.rng).map_err(|e| e.to_string())?;
|
|
let used = if ctx.opts.wrong_pin {
|
|
loop {
|
|
let p = Pin::generate(&mut ctx.rng).map_err(|e| e.to_string())?;
|
|
if p != shown {
|
|
break p;
|
|
}
|
|
}
|
|
} else {
|
|
shown
|
|
};
|
|
// The PIN goes to the operator's terminal only, never to the log.
|
|
let digits = String::from_utf8_lossy(shown.as_bytes()).into_owned();
|
|
println!();
|
|
println!(" PIN for the operator: {digits}");
|
|
println!(" Submit it through Sunshine's API now (docs/LIVE-INTEROP.md, step P). Waiting up to 120 s.");
|
|
if ctx.opts.wrong_pin {
|
|
println!(" (--wrong-pin: the probe itself uses another PIN; pairing is expected to fail, U28)");
|
|
}
|
|
println!();
|
|
ctx.log.note(
|
|
"pair",
|
|
&["U1", "U2", "U28"],
|
|
format!(
|
|
"PIN generated and shown to the operator (not logged); wrong_pin={}; device name '{}'; client cert sha256 {}",
|
|
ctx.opts.wrong_pin,
|
|
ctx.opts.name,
|
|
sha256_hex(ctx.id.client.cert_der())
|
|
),
|
|
);
|
|
let name = ctx.opts.name.clone();
|
|
let (mut session, mut req) = PairingSession::start(&ctx.id.client, &ctx.id.unique_id, &name, used, &mut ctx.rng)
|
|
.map_err(|e| format!("pairing start: {e}"))?;
|
|
let mut host_cert: Option<ServerCert> = None;
|
|
let mut n = 1;
|
|
loop {
|
|
let before = session.state();
|
|
let step = format!("pair.{n}");
|
|
let us: &[&str] = if n == 1 { &["U1", "U2", "U24"] } else { &["U1", "U2", "U28"] };
|
|
let x = match ctx.http(&step, us, &req, host_cert.as_ref()) {
|
|
Ok(x) => x,
|
|
Err(e) => {
|
|
println!("pairing stopped at step {n} ({before:?}): {e}");
|
|
return Err(e);
|
|
}
|
|
};
|
|
if before == PairingState::AwaitServerCert
|
|
&& let Ok(a) = parse_pair_response(&x.response.body)
|
|
&& let Some(pem) = a.plain_cert
|
|
{
|
|
match ServerCert::from_pem(&pem) {
|
|
Ok(c) => {
|
|
ctx.log.note(&step, &["U2"], format!("host certificate sha256 {}", sha256_hex(c.der())));
|
|
host_cert = Some(c);
|
|
}
|
|
Err(e) => ctx.log.parse_error(&step, &["U2"], &e, crate::log::text(&pem)),
|
|
}
|
|
}
|
|
let status = x.response.status;
|
|
match session.handle_response(&ctx.id.client, status, &x.response.body, &mut ctx.rng) {
|
|
Ok(PairingStep::Send(next)) => {
|
|
req = next;
|
|
n += 1;
|
|
}
|
|
Ok(PairingStep::Paired(p)) => {
|
|
state::save_host_cert(&ctx.out, &p.server_cert, ctx.host)?;
|
|
ctx.log.record(
|
|
"pair.done",
|
|
Kind::Summary,
|
|
&["U1", "U2"],
|
|
J::O(vec![("steps", J::U(n)), ("host_cert_sha256", J::s(sha256_hex(p.server_cert.der())))]),
|
|
);
|
|
ctx.host_cert = Some(p.server_cert);
|
|
println!(
|
|
"paired after {n} steps; host certificate pinned in {}",
|
|
state::identity_dir(&ctx.out).display()
|
|
);
|
|
// Evidence that the host now treats this certificate as paired.
|
|
return server_info(ctx, Scheme::Https);
|
|
}
|
|
Err(e) => {
|
|
let failed = session.state();
|
|
ctx.log.parse_error(&step, &["U1", "U28"], &e, body_j(&x.response.body));
|
|
ctx.log.stop(
|
|
&step,
|
|
&["U1", "U2", "U28"],
|
|
format!("pairing failed at step {n} ({before:?} -> {failed:?}): {e}"),
|
|
J::O(vec![("wrong_pin", J::Bool(ctx.opts.wrong_pin))]),
|
|
);
|
|
println!("pairing failed at step {n}: {failed:?} ({e})");
|
|
return Err(format!("pairing failed: {e}"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn apps(ctx: &mut Ctx) -> Result<(), String> {
|
|
let req = app_list_request(&ctx.id.unique_id).map_err(|e| e.to_string())?;
|
|
let x = ctx.http("apps", &["U24", "U27"], &req, None)?;
|
|
check_status(ctx, "apps", &x)?;
|
|
match parse_app_list(&x.response.body) {
|
|
Ok(apps) => {
|
|
ctx.log.record("apps", Kind::Note, &[], J::dbg(&apps));
|
|
println!("{} apps:", apps.len());
|
|
for a in &apps {
|
|
println!(" {:>12} hdr={} {}", a.id, u8::from(a.hdr_supported), a.title);
|
|
}
|
|
Ok(())
|
|
}
|
|
Err(e) => {
|
|
ctx.log.parse_error("apps", &["U27"], &e, body_j(&x.response.body));
|
|
ctx.log.stop("apps", &[], format!("applist refused: {e}"), J::Null);
|
|
Err(format!("applist refused: {e}"))
|
|
}
|
|
}
|
|
}
|
|
|
|
fn status_call(ctx: &mut Ctx, step: &str, req: domistream::http::HttpRequest) -> Result<(), String> {
|
|
let x = ctx.http(step, &["U27"], &req, None)?;
|
|
check_status(ctx, step, &x)?;
|
|
match parse_status_response(&x.response.body) {
|
|
Ok(()) => {
|
|
println!("{step}: done");
|
|
Ok(())
|
|
}
|
|
Err(e) => {
|
|
ctx.log.parse_error(step, &["U27"], &e, body_j(&x.response.body));
|
|
ctx.log.stop(step, &["U27"], format!("{step} refused: {e}"), J::Null);
|
|
Err(format!("{step} refused: {e}"))
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn quit(ctx: &mut Ctx) -> Result<(), String> {
|
|
let req = cancel_request(&ctx.id.unique_id).map_err(|e| e.to_string())?;
|
|
status_call(ctx, "quit", req)
|
|
}
|
|
|
|
pub fn unpair(ctx: &mut Ctx) -> Result<(), String> {
|
|
let req = unpair_request(&ctx.id.unique_id).map_err(|e| e.to_string())?;
|
|
status_call(ctx, "unpair", req)?;
|
|
state::forget_host_cert(&ctx.out)?;
|
|
ctx.host_cert = None;
|
|
Ok(())
|
|
}
|