Windows: the window's own refresh no longer keeps the VM awake (#668)
Co-authored-by: Karthik P <karthikparam10@gmail.com>
This commit is contained in:
1 parent
d9f5239b42
commit
2930734702
5 files changed
+269
-30
No files matched your search
+229
-22
@@ -3,14 +3,71 @@
|
||||
//! crates/kiri/src/client.rs over the unix socket: one short connection
|
||||
//! per request, and long-lived ones for `/events`, logs and stats.
|
||||
|
||||
#![cfg_attr(not(windows), allow(dead_code))]
|
||||
|
||||
use std::io::{self, BufRead, BufReader, Read, Write};
|
||||
use std::sync::{Arc, OnceLock};
|
||||
#[cfg(windows)]
|
||||
use std::sync::Arc;
|
||||
use std::sync::OnceLock;
|
||||
use std::time::Duration;
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
#[cfg(windows)]
|
||||
use crate::pipe::Pipe;
|
||||
|
||||
#[cfg(test)]
|
||||
mod mock {
|
||||
use std::cell::RefCell;
|
||||
use std::collections::VecDeque;
|
||||
use std::io::{self, Cursor, Read, Write};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
pub struct Recorded {
|
||||
pub wire: Arc<Mutex<Vec<u8>>>,
|
||||
pub reply: Cursor<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl Read for Recorded {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
self.reply.read(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for Recorded {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
self.wire.lock().unwrap().extend_from_slice(buf);
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
type Queue = VecDeque<(Arc<Mutex<Vec<u8>>>, Vec<u8>)>;
|
||||
thread_local! {
|
||||
static QUEUE: RefCell<Queue> = const { RefCell::new(VecDeque::new()) };
|
||||
}
|
||||
|
||||
pub fn push(reply: &[u8]) -> Arc<Mutex<Vec<u8>>> {
|
||||
let wire = Arc::new(Mutex::new(Vec::new()));
|
||||
QUEUE.with(|q| q.borrow_mut().push_back((wire.clone(), reply.to_vec())));
|
||||
wire
|
||||
}
|
||||
|
||||
pub fn take() -> Option<Recorded> {
|
||||
QUEUE.with(|q| q.borrow_mut().pop_front()).map(|(wire, reply)| Recorded { wire, reply: Cursor::new(reply) })
|
||||
}
|
||||
}
|
||||
|
||||
/// Queues one canned HTTP response for the next client call on this thread,
|
||||
/// returning the buffer that captures the request bytes written to the wire.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn mock_reply(reply: &[u8]) -> std::sync::Arc<std::sync::Mutex<Vec<u8>>> {
|
||||
mock::push(reply)
|
||||
}
|
||||
|
||||
static PIPE: OnceLock<String> = OnceLock::new();
|
||||
|
||||
/// The pipe the daemon is served on (once, at start).
|
||||
@@ -23,6 +80,7 @@ pub fn pipe() -> String {
|
||||
PIPE.get().cloned().unwrap_or_else(|| r"\\.\pipe\kiri".to_owned())
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn connect() -> io::Result<Arc<Pipe>> {
|
||||
Pipe::open(PIPE.get().map_or(r"\\.\pipe\kiri", String::as_str)).map(Arc::new)
|
||||
}
|
||||
@@ -30,17 +88,49 @@ fn connect() -> io::Result<Arc<Pipe>> {
|
||||
/// A connection as a byte stream. With `timeout`, a read that gets nothing
|
||||
/// for that long fails (TimedOut) instead of waiting on: for the ping, which
|
||||
/// gates the engine thread at start and must never hang it.
|
||||
struct Conn(Arc<Pipe>, Option<Duration>);
|
||||
enum Conn {
|
||||
#[cfg(windows)]
|
||||
Pipe(Arc<Pipe>, Option<Duration>),
|
||||
#[cfg(test)]
|
||||
Mock(mock::Recorded),
|
||||
}
|
||||
|
||||
fn open_conn(timeout: Option<Duration>) -> io::Result<Conn> {
|
||||
#[cfg(test)]
|
||||
if let Some(m) = mock::take() {
|
||||
let _ = timeout;
|
||||
return Ok(Conn::Mock(m));
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
Ok(Conn::Pipe(connect()?, timeout))
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
let _ = timeout;
|
||||
Err(io::Error::new(io::ErrorKind::NotConnected, "no mock response queued"))
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for Conn {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
self.0.read_client(buf, self.1)
|
||||
match self {
|
||||
#[cfg(windows)]
|
||||
Conn::Pipe(p, timeout) => p.read_client(buf, *timeout),
|
||||
#[cfg(test)]
|
||||
Conn::Mock(m) => m.read(buf),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for Conn {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
Write::write(&mut &*self.0, buf)
|
||||
match self {
|
||||
#[cfg(windows)]
|
||||
Conn::Pipe(p, _) => Write::write(&mut &**p, buf),
|
||||
#[cfg(test)]
|
||||
Conn::Mock(m) => m.write(buf),
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
@@ -150,7 +240,7 @@ const QUICK: Duration = Duration::from_secs(3);
|
||||
/// `request_with`, failing (TimedOut) when a read waits longer than `timeout`.
|
||||
pub fn request_within(method: &str, path: &str, body: Option<&Value>, background: bool, timeout: Option<Duration>) -> io::Result<Response> {
|
||||
line_safe(path)?;
|
||||
let mut s = Conn(connect()?, timeout);
|
||||
let mut s = open_conn(timeout)?;
|
||||
let payload = body.map(Value::to_string).unwrap_or_default();
|
||||
write!(
|
||||
s,
|
||||
@@ -193,8 +283,9 @@ pub fn get(path: &str) -> io::Result<Value> {
|
||||
}
|
||||
|
||||
/// A GET of the window's own refresh, failing after QUICK without an answer.
|
||||
/// Marked as the window's own, so the refresh never keeps the VM awake.
|
||||
pub fn get_quick(path: &str) -> io::Result<Value> {
|
||||
let r = request_within("GET", path, None, false, Some(QUICK))?;
|
||||
let r = request_within("GET", path, None, true, Some(QUICK))?;
|
||||
if !r.ok() {
|
||||
return Err(io::Error::other(r.error()));
|
||||
}
|
||||
@@ -212,6 +303,7 @@ pub fn get_background(path: &str) -> io::Result<Value> {
|
||||
}
|
||||
|
||||
/// The docker CLI Kiri ships, as the Mac's `docker_cli` (for builds.rs).
|
||||
#[cfg(windows)]
|
||||
pub fn docker_cli() -> Option<String> {
|
||||
Some(crate::detail::docker_cli())
|
||||
}
|
||||
@@ -219,11 +311,13 @@ pub fn docker_cli() -> Option<String> {
|
||||
/// A POST whose answer is a stream of JSON lines (`/images/create`), each
|
||||
/// given to `on_line` as it arrives; returns the HTTP status. As the Mac's
|
||||
/// client::post_lines.
|
||||
#[cfg(windows)]
|
||||
pub fn post_lines(path: &str, on_line: impl FnMut(Value)) -> io::Result<u16> {
|
||||
post_lines_with(path, None, on_line)
|
||||
}
|
||||
|
||||
/// `post_lines` with a registry sign-in (`X-Registry-Auth`, base64url) for a pull.
|
||||
#[cfg(windows)]
|
||||
pub fn post_lines_with(path: &str, auth: Option<&str>, on_line: impl FnMut(Value)) -> io::Result<u16> {
|
||||
post_lines_closable(path, auth, |_| {}, on_line)
|
||||
}
|
||||
@@ -231,6 +325,7 @@ pub fn post_lines_with(path: &str, auth: Option<&str>, on_line: impl FnMut(Value
|
||||
/// `post_lines_with`, handing `opened` a Closer for the connection once it
|
||||
/// is made, so another thread can stop the request (Cancel on a pull). A
|
||||
/// stopped read ends as if the body had.
|
||||
#[cfg(windows)]
|
||||
pub fn post_lines_closable(path: &str, auth: Option<&str>, opened: impl FnOnce(Closer), mut on_line: impl FnMut(Value)) -> io::Result<u16> {
|
||||
line_safe(path)?;
|
||||
if let Some(a) = auth {
|
||||
@@ -239,7 +334,7 @@ pub fn post_lines_closable(path: &str, auth: Option<&str>, opened: impl FnOnce(C
|
||||
let auth = auth.map(|a| format!("X-Registry-Auth: {a}\r\n")).unwrap_or_default();
|
||||
let pipe = connect()?;
|
||||
opened(Closer(pipe.clone()));
|
||||
let mut s = Conn(pipe, None);
|
||||
let mut s = Conn::Pipe(pipe, None);
|
||||
write!(s, "POST {path} HTTP/1.1\r\nHost: kiri\r\nConnection: close\r\n{auth}Content-Type: application/json\r\nContent-Length: 0\r\n\r\n")?;
|
||||
let mut r = BufReader::new(s);
|
||||
let (status, len, chunked) = head(&mut r)?;
|
||||
@@ -270,21 +365,23 @@ pub fn post_lines_closable(path: &str, auth: Option<&str>, opened: impl FnOnce(C
|
||||
}
|
||||
|
||||
/// Whether the daemon answers at all, waiting for a start the bridge has
|
||||
/// under way (Start).
|
||||
/// under way (Start). Marked as the window's own, as on the Mac, so status
|
||||
/// and diagnostic checks never keep an idle VM awake.
|
||||
pub fn alive() -> bool {
|
||||
request_within("GET", "/_ping", None, false, Some(PING_WAIT)).is_ok_and(|r| r.ok())
|
||||
request_within("GET", "/_ping", None, true, Some(PING_WAIT)).is_ok_and(|r| r.ok())
|
||||
}
|
||||
|
||||
/// `alive`, for the event watcher: false after QUICK without an answer.
|
||||
pub fn alive_quick() -> bool {
|
||||
request_within("GET", "/_ping", None, false, Some(QUICK)).is_ok_and(|r| r.ok())
|
||||
request_within("GET", "/_ping", None, true, Some(QUICK)).is_ok_and(|r| r.ok())
|
||||
}
|
||||
|
||||
/// A long-lived streamed response (`/events`, `logs?follow=1`, `stats`),
|
||||
/// read chunk by chunk. `closer` ends it from another thread.
|
||||
#[derive(Debug)]
|
||||
pub struct Stream {
|
||||
pipe: Arc<Pipe>,
|
||||
#[cfg(windows)]
|
||||
pipe: Option<Arc<Pipe>>,
|
||||
reader: BufReader<ConnReader>,
|
||||
}
|
||||
|
||||
@@ -304,9 +401,11 @@ impl Read for ConnReader {
|
||||
}
|
||||
|
||||
/// Ends a Stream's wait from another thread.
|
||||
#[cfg(windows)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Closer(Arc<Pipe>);
|
||||
|
||||
#[cfg(windows)]
|
||||
impl Closer {
|
||||
pub fn close(&self) {
|
||||
self.0.cancel();
|
||||
@@ -315,20 +414,34 @@ impl Closer {
|
||||
|
||||
impl Stream {
|
||||
pub fn open(path: &str) -> io::Result<Stream> {
|
||||
Self::open_with(path, false)
|
||||
}
|
||||
|
||||
fn open_with(path: &str, background: bool) -> io::Result<Stream> {
|
||||
line_safe(path)?;
|
||||
let pipe = connect()?;
|
||||
let mut s = Conn(pipe.clone(), None);
|
||||
write!(s, "GET {path} HTTP/1.1\r\nHost: kiri\r\n\r\n")?;
|
||||
let mut s = open_conn(None)?;
|
||||
#[cfg(windows)]
|
||||
let pipe = match &s {
|
||||
Conn::Pipe(p, _) => Some(p.clone()),
|
||||
#[cfg(test)]
|
||||
Conn::Mock(_) => None,
|
||||
};
|
||||
write!(s, "GET {path} HTTP/1.1\r\nHost: kiri\r\n{}\r\n", background_header(background))?;
|
||||
let mut reader = BufReader::new(ConnReader(s));
|
||||
let (status, _, chunked) = head(&mut reader)?;
|
||||
if status != 200 || !chunked {
|
||||
return Err(io::Error::other(format!("{path}: HTTP {status}")));
|
||||
}
|
||||
Ok(Stream { pipe, reader })
|
||||
Ok(Stream {
|
||||
#[cfg(windows)]
|
||||
pipe,
|
||||
reader,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub fn closer(&self) -> Closer {
|
||||
Closer(self.pipe.clone())
|
||||
Closer(self.pipe.as_ref().expect("stream backed by a pipe").clone())
|
||||
}
|
||||
|
||||
pub fn next(&mut self) -> Option<Vec<u8>> {
|
||||
@@ -338,16 +451,16 @@ impl Stream {
|
||||
|
||||
/// Calls `on_event` for each event until the connection ends or it returns false.
|
||||
pub fn events(mut on_event: impl FnMut(Value) -> bool) -> io::Result<()> {
|
||||
let mut s = Stream::open("/events")?;
|
||||
let mut s = Stream::open_with("/events", true)?;
|
||||
let mut pending = Vec::new();
|
||||
while let Some(c) = s.next() {
|
||||
pending.extend_from_slice(&c);
|
||||
while let Some(i) = pending.iter().position(|&b| b == b'\n') {
|
||||
let line: Vec<u8> = pending.drain(..=i).collect();
|
||||
if let Ok(v) = serde_json::from_slice::<Value>(&line) {
|
||||
if !on_event(v) {
|
||||
return Ok(());
|
||||
}
|
||||
if let Ok(v) = serde_json::from_slice::<Value>(&line)
|
||||
&& !on_event(v)
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -358,6 +471,7 @@ pub fn events(mut on_event: impl FnMut(Value) -> bool) -> io::Result<()> {
|
||||
/// arrives unframed, read from this, until it ends. `closer()` ends a read
|
||||
/// that waits (a log that follows) from another thread; the exec itself
|
||||
/// goes on in the container.
|
||||
#[cfg(windows)]
|
||||
#[derive(Debug)]
|
||||
pub struct Exec {
|
||||
pub id: String,
|
||||
@@ -365,18 +479,21 @@ pub struct Exec {
|
||||
reader: BufReader<ConnReader>,
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
impl Exec {
|
||||
pub fn closer(&self) -> Closer {
|
||||
Closer(self.pipe.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
impl Read for Exec {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
self.reader.read(buf)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
impl BufRead for Exec {
|
||||
fn fill_buf(&mut self) -> io::Result<&[u8]> {
|
||||
self.reader.fill_buf()
|
||||
@@ -389,6 +506,7 @@ impl BufRead for Exec {
|
||||
|
||||
/// Starts `cmd` in a running container, through the exec API with a
|
||||
/// terminal; `background` marks it as the window's own.
|
||||
#[cfg(windows)]
|
||||
pub fn exec_start(container: &str, cmd: &[&str], background: bool) -> io::Result<Exec> {
|
||||
line_safe(container)?;
|
||||
let created = request_with("POST", &format!("/containers/{container}/exec"), Some(&serde_json::json!({"Cmd": cmd, "AttachStdout": true, "AttachStderr": true, "Tty": true})), background)?;
|
||||
@@ -397,7 +515,7 @@ pub fn exec_start(container: &str, cmd: &[&str], background: bool) -> io::Result
|
||||
}
|
||||
let id = created.json()["Id"].as_str().unwrap_or_default().to_owned();
|
||||
let pipe = connect()?;
|
||||
let mut s = Conn(pipe.clone(), None);
|
||||
let mut s = Conn::Pipe(pipe.clone(), None);
|
||||
let body = r#"{"Detach":false,"Tty":true}"#;
|
||||
write!(
|
||||
s,
|
||||
@@ -415,6 +533,7 @@ pub fn exec_start(container: &str, cmd: &[&str], background: bool) -> io::Result
|
||||
|
||||
/// Runs `cmd` in a running container and returns (exit code, output),
|
||||
/// through the exec API with a terminal, so output arrives unframed.
|
||||
#[cfg(windows)]
|
||||
pub fn exec(container: &str, cmd: &[&str]) -> io::Result<(i64, String)> {
|
||||
let mut e = exec_start(container, cmd, false)?;
|
||||
let mut out = Vec::new();
|
||||
@@ -422,3 +541,91 @@ pub fn exec(container: &str, cmd: &[&str]) -> io::Result<(i64, String)> {
|
||||
let code = get(&format!("/exec/{}/json", e.id))?["ExitCode"].as_i64().unwrap_or(-1);
|
||||
Ok((code, String::from_utf8_lossy(&out).replace("\r\n", "\n")))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
const OK_JSON: &[u8] = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\n{}";
|
||||
const OK_PING: &[u8] = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK";
|
||||
const OK_CHUNKED: &[u8] = b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n\r\n";
|
||||
|
||||
fn wire_text(buf: &std::sync::Arc<std::sync::Mutex<Vec<u8>>>) -> String {
|
||||
String::from_utf8(buf.lock().unwrap().clone()).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn background_polls_carry_the_background_header() {
|
||||
let info = mock_reply(OK_JSON);
|
||||
get_quick("/info").unwrap();
|
||||
let req = wire_text(&info);
|
||||
assert!(req.starts_with("GET /info HTTP/1.1\r\n"), "{req}");
|
||||
assert!(req.contains("\r\nX-Kiri-Background: 1\r\n"), "get_quick must mark background: {req}");
|
||||
|
||||
let list = mock_reply(OK_JSON);
|
||||
get_quick("/containers/json?all=1").unwrap();
|
||||
let req = wire_text(&list);
|
||||
assert!(req.contains("\r\nX-Kiri-Background: 1\r\n"), "get_quick lists must mark background: {req}");
|
||||
|
||||
let proxy_get = mock_reply(OK_JSON);
|
||||
get_background("/kiri/proxy").unwrap();
|
||||
let req = wire_text(&proxy_get);
|
||||
assert!(req.starts_with("GET /kiri/proxy HTTP/1.1\r\n"), "{req}");
|
||||
assert!(req.contains("\r\nX-Kiri-Background: 1\r\n"), "get_background must mark background: {req}");
|
||||
|
||||
let proxy_put = mock_reply(OK_JSON);
|
||||
let body = json!({"http": null, "https": null});
|
||||
request_with("PUT", "/kiri/proxy/system", Some(&body), true).unwrap();
|
||||
let req = wire_text(&proxy_put);
|
||||
assert!(req.starts_with("PUT /kiri/proxy/system HTTP/1.1\r\n"), "{req}");
|
||||
assert!(req.contains("\r\nX-Kiri-Background: 1\r\n"), "background proxy push must mark background: {req}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn alive_and_events_carry_the_background_header() {
|
||||
let ping = mock_reply(OK_PING);
|
||||
assert!(alive());
|
||||
let req = wire_text(&ping);
|
||||
assert!(req.starts_with("GET /_ping HTTP/1.1\r\n"), "{req}");
|
||||
assert!(req.contains("\r\nX-Kiri-Background: 1\r\n"), "alive must mark background: {req}");
|
||||
|
||||
let quick_ping = mock_reply(OK_PING);
|
||||
assert!(alive_quick());
|
||||
let req = wire_text(&quick_ping);
|
||||
assert!(req.starts_with("GET /_ping HTTP/1.1\r\n"), "{req}");
|
||||
assert!(req.contains("\r\nX-Kiri-Background: 1\r\n"), "alive_quick must mark background: {req}");
|
||||
|
||||
let ev = mock_reply(OK_CHUNKED);
|
||||
events(|_| false).unwrap();
|
||||
let req = wire_text(&ev);
|
||||
assert!(req.starts_with("GET /events HTTP/1.1\r\n"), "{req}");
|
||||
assert!(req.contains("\r\nX-Kiri-Background: 1\r\n"), "events must mark background: {req}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn foreground_requests_omit_the_background_header() {
|
||||
let fg_get = mock_reply(OK_JSON);
|
||||
get("/info").unwrap();
|
||||
let req = wire_text(&fg_get);
|
||||
assert!(req.starts_with("GET /info HTTP/1.1\r\n"), "{req}");
|
||||
assert!(!req.contains("X-Kiri-Background"), "foreground get must not mark background: {req}");
|
||||
|
||||
let wake = mock_reply(b"HTTP/1.1 204 No Content\r\n\r\n");
|
||||
request("POST", "/kiri/wake", None).unwrap();
|
||||
let req = wire_text(&wake);
|
||||
assert!(req.starts_with("POST /kiri/wake HTTP/1.1\r\n"), "{req}");
|
||||
assert!(!req.contains("X-Kiri-Background"), "POST /kiri/wake must not mark background: {req}");
|
||||
|
||||
let start = mock_reply(b"HTTP/1.1 204 No Content\r\n\r\n");
|
||||
request("POST", "/containers/c1/start", None).unwrap();
|
||||
let req = wire_text(&start);
|
||||
assert!(!req.contains("X-Kiri-Background"), "foreground POST must not mark background: {req}");
|
||||
|
||||
let logs = mock_reply(OK_CHUNKED);
|
||||
let _ = Stream::open("/containers/c1/logs?follow=1").unwrap();
|
||||
let req = wire_text(&logs);
|
||||
assert!(req.starts_with("GET /containers/c1/logs?follow=1 HTTP/1.1\r\n"), "{req}");
|
||||
assert!(!req.contains("X-Kiri-Background"), "foreground Stream::open must not mark background: {req}");
|
||||
}
|
||||
}
|
||||
@@ -309,7 +309,7 @@ fn wakes(launch: bool, stopped: Option<bool>) -> bool {
|
||||
/// Whether Kiri was stopped (Stop in the window or the tray): then it stays
|
||||
/// off until Start. None when the engine's /info did not answer.
|
||||
fn stopped() -> Option<bool> {
|
||||
client::get("/info").ok().map(|i| i["KiriStopped"] == true)
|
||||
client::get_background("/info").ok().map(|i| i["KiriStopped"] == true)
|
||||
}
|
||||
|
||||
/// The daemon's wake; a Linux host has no VM to wake, which is fine.
|
||||
@@ -999,6 +999,28 @@ mod tests {
|
||||
assert!(!allowed_url("https://evil.example/#.kiri.localhost"));
|
||||
assert!(!allowed_url("ftp://localhost/"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn background_engine_checks_mark_background_and_wake_does_not() {
|
||||
let info = super::client::mock_reply(b"HTTP/1.1 200 OK\r\nContent-Length: 20\r\n\r\n{\"KiriStopped\":true}");
|
||||
assert_eq!(super::stopped(), Some(true));
|
||||
let req = String::from_utf8(info.lock().unwrap().clone()).unwrap();
|
||||
assert!(req.contains("\r\nX-Kiri-Background: 1\r\n"), "stopped() must mark background: {req}");
|
||||
|
||||
let wake_wire = super::client::mock_reply(b"HTTP/1.1 204 No Content\r\n\r\n");
|
||||
super::wake().unwrap();
|
||||
let req = String::from_utf8(wake_wire.lock().unwrap().clone()).unwrap();
|
||||
assert!(req.starts_with("POST /kiri/wake HTTP/1.1\r\n"), "{req}");
|
||||
assert!(!req.contains("X-Kiri-Background"), "wake() must not mark background: {req}");
|
||||
|
||||
let proxy_get = super::client::mock_reply(b"HTTP/1.1 200 OK\r\nContent-Length: 15\r\n\r\n{\"settings\":{}}");
|
||||
let proxy_put = super::client::mock_reply(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\n{}");
|
||||
super::push_windows_proxy(serde_json::json!({"http": null, "https": null, "no_proxy": [], "pac": false}), |_| std::collections::HashMap::new());
|
||||
let get_req = String::from_utf8(proxy_get.lock().unwrap().clone()).unwrap();
|
||||
let put_req = String::from_utf8(proxy_put.lock().unwrap().clone()).unwrap();
|
||||
assert!(get_req.starts_with("GET /kiri/proxy HTTP/1.1\r\n") && get_req.contains("\r\nX-Kiri-Background: 1\r\n"), "{get_req}");
|
||||
assert!(put_req.starts_with("PUT /kiri/proxy/system HTTP/1.1\r\n") && put_req.contains("\r\nX-Kiri-Background: 1\r\n"), "{put_req}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The page's "fonts" event, made once (the window asks each time Settings
|
||||
@@ -1437,10 +1459,14 @@ fn windows_proxy() -> Value {
|
||||
|
||||
/// Tells the daemon what Windows' proxy is. A proxy on Windows' own
|
||||
/// 127.0.0.1 (Windows' or one set in Settings → Network) is reached through
|
||||
/// the relay (relay.rs), and the daemon gets its ports too.
|
||||
/// the relay (relay.rs), and the daemon gets its ports too. Sent every
|
||||
/// minute, so marked as the window's own: it never keeps the VM awake.
|
||||
pub(crate) fn send_windows_proxy() {
|
||||
let mut body = windows_proxy();
|
||||
let custom = client::get("/kiri/proxy").map(|v| v["settings"].clone()).unwrap_or_default();
|
||||
push_windows_proxy(windows_proxy(), crate::relay::ports);
|
||||
}
|
||||
|
||||
fn push_windows_proxy(mut body: Value, relay_ports: impl FnOnce(&[u16]) -> std::collections::HashMap<u16, u16>) {
|
||||
let custom = client::get_background("/kiri/proxy").map(|v| v["settings"].clone()).unwrap_or_default();
|
||||
let mut targets: Vec<u16> = [&body["http"], &body["https"], &custom["http"], &custom["https"]]
|
||||
.iter()
|
||||
.filter_map(|v| v.as_str())
|
||||
@@ -1448,9 +1474,9 @@ pub(crate) fn send_windows_proxy() {
|
||||
.collect();
|
||||
targets.sort_unstable();
|
||||
targets.dedup();
|
||||
let relay = crate::relay::ports(&targets);
|
||||
let relay = relay_ports(&targets);
|
||||
body["relay"] = relay.iter().map(|(t, l)| (t.to_string(), json!(l))).collect::<serde_json::Map<_, _>>().into();
|
||||
let _ = client::request("PUT", "/kiri/proxy/system", Some(&body));
|
||||
let _ = client::request_with("PUT", "/kiri/proxy/system", Some(&body), true);
|
||||
}
|
||||
|
||||
/// Settings → Domains: reads (or moves, then reads) the routers' ports. The
|
||||
|
||||
@@ -25,7 +25,7 @@ mod assemble;
|
||||
#[cfg(windows)]
|
||||
#[path = "../../kiri/src/ui/lang.rs"]
|
||||
mod lang;
|
||||
#[cfg(windows)]
|
||||
#[cfg(any(windows, test))]
|
||||
mod client;
|
||||
#[cfg(windows)]
|
||||
mod detail;
|
||||
|
||||
@@ -634,7 +634,7 @@ pub(crate) fn output_within(cmd: &mut Command, limit: Duration) -> io::Result<st
|
||||
|
||||
/// The engine's own Kiri version, when it answers.
|
||||
fn engine_version() -> Option<String> {
|
||||
crate::client::request("GET", "/version", None).ok().filter(|r| r.ok()).and_then(|r| crate::client::kiri_version(&r.json()).map(str::to_owned))
|
||||
crate::client::get_background("/version").ok().and_then(|v| crate::client::kiri_version(&v).map(str::to_owned))
|
||||
}
|
||||
|
||||
static CATCH_UP: crate::wslsync::CatchUp = crate::wslsync::CatchUp::new();
|
||||
|
||||
@@ -13,6 +13,12 @@ A log of every merged bug fix in Kiri, newest first, together with any later bug
|
||||
|
||||
## Fixes
|
||||
|
||||
### #723 Windows: the window's own refresh no longer keeps the VM awake (unreleased)
|
||||
- Fixes: #668 Windows: the window's own refresh keeps the VM from sleeping
|
||||
- Caused: none known
|
||||
- Caused by: -
|
||||
- Check: none
|
||||
|
||||
### #524 Import: keep volume xattrs with GNU tar (unreleased)
|
||||
- Fixes: #485 kiri import loses user.* extended attributes on named volumes (Supabase Storage 500 ENODATA)
|
||||
- Caused: none known
|
||||
|
||||
Reference in new issue
Block a user