Merge pull request #1026 from ludovic111/feat/launcher-servers-page

🤖 Agent: merged after verify passed (74 + 9 + 1 launcher tests; launcher-only). The Servers page: create, start/stop, players, settings, backups, updates, run at login, and Friends over the internet with Tailscale (device sharing, an optional rule limiting shared friends to the game port, never Funnel). Ludovic's real-tailnet check is described in the PR.
This commit is contained in:
Ludovic Marie
2026-10-19 16:22:08 +02:00
committed by GitHub
15 changed files with 4877 additions and 374 deletions
+1
View File
@@ -2668,6 +2668,7 @@ dependencies = [
"horizon-prepare",
"horizon-telemetry",
"horizon-theme",
"image",
"keyring",
"oo7",
"pulldown-cmark",
+3
View File
@@ -32,6 +32,9 @@ horizon-port-pack = { path = "../crates/horizon-port-pack" }
horizon-telemetry = { path = "../crates/horizon-telemetry" }
# The session file's random name.
getrandom = "0.3"
# Players' heads on the Servers page, from the default skins in the player's own game files (already
# in the lockfile through gpui).
image = { version = "0.25", default-features = false, features = ["png"] }
# Microsoft account secrets in the OS keychain: the Secret Service on Linux through oo7 (the same
# build gpui uses), Keychain and Credential Manager through keyring.
+35 -4
View File
@@ -17,8 +17,7 @@ The opening, then Play ([clip](docs/opening.webm)):
- **Play**: prepares resources as `PLAY.txt` describes (your launcher's 26.3 install, the
26.3 server jar from that version's own json, and Java 25) and copies its sounds from your
launcher's asset store (`horizon-prepare sounds`; a launcher install without them only
leaves the game silent), then starts the client, or a
server (`--dedicated --nogui`) in a folder you choose, with a live console. Preparing is
leaves the game silent), then starts the client. Preparing is
horizon-prepare: linked into the launcher, used in-process when a version ships the same
preparer sources, else that version's own `horizon-prepare`; versions released before it
still prepare with their Python script and need Python 3.10+. Before importing,
@@ -35,11 +34,42 @@ The opening, then Play ([clip](docs/opening.webm)):
**Play** starts the game as the pack's own profile (`ports/<pack>/home/` in the game's home:
its own saves and options) with those mods. Nothing is uploaded or shared: mod jars and ports
stay on this computer.
- **Servers**: host a world for friends with buttons. **Create a server** asks for a name, a world
(a new one, with an optional seed, or a copy of one of your single-player worlds), the game mode,
difficulty, most players, who can join (friends only, the default, or anyone with the address)
and your agreement to the Minecraft EULA; with the defaults (tick the EULA, press Create) you get
a friends-only survival world with you as its operator. Each server then has **Start**, **Stop** and **Restart**, its state (also
when it crashed and was restarted), players online with their heads, TPS, MSPT and RAM, the
address friends on your network type with **Copy**, a console that takes commands, **Open the
web panel**, and four tabs: **Overview**; **Players** (online players with Make operator, Kick
and Ban; who may join, with Friends only, Let in and Remove; Unban); **Settings** (the message
in the server list, game mode, difficulty, PvP, most players, view and simulation distance,
port, account checks, **Run in the background and at login**); **Backups** (Back up now, the
list with dates and sizes, Restore with a confirmation, Back up automatically, and **Updates**:
Check for updates, Update now with its automatic rollback, and how many older versions to
keep). Changes the running game can take apply at once, the others say "Restart to apply".
Everything goes through `horizon-server` itself ([docs/hosting.md](../docs/hosting.md)):
the launcher copies a prepared version's programs and game files into `server-runtime/` laid
out as `install.sh` does, runs `horizon-server init`, `config`, `start`, `backup`, `restore`,
`update` and `service` there, and talks to a running server through its control API. Problems
are said in plain words, with a button when there's a fix (another port, the game files, the
EULA).
**Friends over the internet** (on Overview) uses Tailscale: it says whether Tailscale is
installed and signed in (with Install, Sign in and Check again; signing in is always yours, in
Tailscale's own app or page), shows this computer's Tailscale name and address with **Copy**,
and invites a friend to this one computer: in the admin console, or here with an access token
you make there (kept in the system keychain), which also lists invitations with Withdraw /
Stop sharing and offers **Limit them to Minecraft**: when your tailnet has Tailscale's default
access controls, one click changes them so friends you share with reach only the server's
port (Remove the limit undoes it); custom access controls are never rewritten, the page gives
the rule to add instead. Horizon never turns on Funnel.
- **Patch notes**: each release's notes, newest first, with a link to GitHub Sponsors. Quiet "Discord" and "Website" links sit in the footer.
Data lives in the platform's data directory (`~/.local/share/horizon-launcher` on Linux,
`~/Library/Application Support/horizon-launcher` on macOS, `%APPDATA%\horizon-launcher` on
Windows): `versions/<tag>/`, `server-jars/`, `server/` and `settings.json`.
Windows): `versions/<tag>/`, `server-jars/`, `server/`, `settings.json`, and for the Servers
page `servers.json` (the list), `servers/<name>/` (the servers created there) and
`server-runtime/` (the programs and game files they run).
- **Settings**: install new Horizon versions automatically (on unless you turn it off; the
selected version only moves to the new one if you were on the newest), the opening animation,
@@ -219,7 +249,8 @@ bundled so the type is the same on every platform.
## Screenshots
![Server](docs/server.png)
![Servers](docs/servers.png)
![Friends over the internet](docs/servers-tailscale.png)
![Modpacks](docs/modpacks.png)
![Versions](docs/versions.png)
![Patch notes](docs/patch-notes.png)
Binary file not shown.

Before

Width:  |  Height:  |  Size: 300 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

+98
View File
@@ -0,0 +1,98 @@
//! Players' heads for the Servers page: the face of the skin the game shows for them. Without the skin
//! service a player has vanilla's default skin for their UUID (`DefaultPlayerSkin.get`), read from the
//! player's own prepared game files.
use std::path::Path;
/// `DefaultPlayerSkin.get(UUID)`: the default skin by `UUID.hashCode` (as the game's tab list picks it).
pub fn default_skin(uuid: u128) -> &'static str {
const SKINS: [&str; 18] = [
"slim/alex",
"slim/ari",
"slim/efe",
"slim/kai",
"slim/makena",
"slim/noor",
"slim/steve",
"slim/sunny",
"slim/zuri",
"wide/alex",
"wide/ari",
"wide/efe",
"wide/kai",
"wide/makena",
"wide/noor",
"wide/steve",
"wide/sunny",
"wide/zuri",
];
let hilo = ((uuid >> 64) as u64) ^ (uuid as u64);
let hash = ((hilo >> 32) as i32) ^ (hilo as i32);
SKINS[hash.rem_euclid(SKINS.len() as i32) as usize]
}
/// A UUID written with or without dashes.
pub fn parse_uuid(text: &str) -> Option<u128> {
let hex: String = text.chars().filter(|c| *c != '-').collect();
(hex.len() == 32).then(|| u128::from_str_radix(&hex, 16).ok())?
}
/// The 8 by 8 face, row by row as RGBA, with the hat layer over it: what the tab list draws.
pub fn face(skin_png: &[u8]) -> Option<Vec<[u8; 4]>> {
let skin = image::load_from_memory_with_format(skin_png, image::ImageFormat::Png)
.ok()?
.to_rgba8();
if skin.width() < 64 || skin.height() < 32 {
return None;
}
let mut out = Vec::with_capacity(64);
for y in 0..8 {
for x in 0..8 {
let base = skin.get_pixel(8 + x, 8 + y).0;
let hat = skin.get_pixel(40 + x, 8 + y).0;
out.push(if hat[3] > 0 { hat } else { base });
}
}
Some(out)
}
/// The face of a player with a default skin, from the prepared game files in `resources`.
pub fn default_face(resources: &Path, uuid: &str) -> Option<Vec<[u8; 4]>> {
let skin = default_skin(parse_uuid(uuid)?);
let path = resources
.join("assets/minecraft/textures/entity/player")
.join(format!("{skin}.png"));
face(&std::fs::read(path).ok()?)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_skins_follow_the_uuid_hash() {
// OfflinePlayer:Alex and OfflinePlayer:Steve, as vanilla's server makes them.
assert_eq!(
parse_uuid("36532b5e-c442-3dbb-a24c-c7e55d0f979a"),
Some(0x36532b5ec4423dbba24cc7e55d0f979a)
);
assert_eq!(default_skin(0), "slim/alex");
assert_eq!(default_skin(1), "slim/ari");
assert_eq!(default_skin(u128::MAX), "slim/alex");
assert!(parse_uuid("not-a-uuid").is_none());
}
#[test]
fn the_face_wears_its_hat() {
let mut skin = image::RgbaImage::new(64, 64);
skin.put_pixel(8, 8, image::Rgba([1, 2, 3, 255]));
skin.put_pixel(9, 8, image::Rgba([4, 5, 6, 255]));
skin.put_pixel(41, 8, image::Rgba([7, 8, 9, 255]));
let mut png = Vec::new();
skin.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
.unwrap();
let face = face(&png).unwrap();
assert_eq!(face.len(), 64);
assert_eq!(face[0], [1, 2, 3, 255]);
assert_eq!(face[1], [7, 8, 9, 255]);
}
}
+714
View File
@@ -0,0 +1,714 @@
//! The Servers page's "Friends over the internet" panel: Tailscale on this computer, the address friends
//! type, inviting a friend to this one computer, and keeping shared friends to the server's port
//! (`horizon_launcher::tailscale`). Signing in is always the player's, in Tailscale's own app or page;
//! the launcher reads `tailscale status` and, with the player's own access token, calls the API.
use super::servers_page::Field;
use super::*;
use horizon_launcher::tailscale::{self, Api, Invite, Reach, State};
use serde_json::Value;
/// How often Tailscale's state is read while the panel shows.
const POLL: Duration = Duration::from_secs(10);
/// How often the invitations and access controls are read again while the panel shows, so an
/// accepted invitation appears without a click.
const RELOAD: Duration = Duration::from_secs(30);
pub(super) enum Event {
State(State),
Token(Option<String>),
Invites(Result<Vec<Invite>, String>),
Policy(Result<(Value, Option<String>), String>),
/// An action finished: what to tell the player, and whether to read invites and policy again.
Done(Result<String, String>),
Invited(Result<Invite, String>),
}
pub(super) struct Internet {
state: Option<State>,
reading: bool,
read: Option<Instant>,
/// The access token, once read from the keychain (None: not read yet; Some(None): none).
token: Option<Option<String>>,
asking_token: bool,
invites: Option<Result<Vec<Invite>, String>>,
policy: Option<Result<(Value, Option<String>), String>>,
loaded: Option<Instant>,
busy: bool,
message: Option<Result<String, String>>,
/// The invitation just made, its link shown to copy.
fresh: Option<Invite>,
confirm_limit: bool,
copied: Option<(String, Instant)>,
tx: Sender<Event>,
rx: Receiver<Event>,
}
impl Internet {
pub(super) fn new() -> Self {
let (tx, rx) = channel();
Self {
state: None,
reading: false,
read: None,
token: None,
asking_token: false,
invites: None,
policy: None,
loaded: None,
busy: false,
message: None,
fresh: None,
confirm_limit: false,
copied: None,
tx,
rx,
}
}
fn node(&self) -> Option<&tailscale::Node> {
match &self.state {
Some(State::Running(n)) => Some(n),
_ => None,
}
}
fn token(&self) -> Option<String> {
self.token.clone().flatten()
}
}
impl Launcher {
/// What the background sent, and the next look at Tailscale while the panel shows.
pub(super) fn internet_tick(&mut self, showing: bool) -> bool {
let mut changed = false;
while let Ok(event) = self.internet.rx.try_recv() {
changed = true;
self.internet_event(event);
}
let i = &mut self.internet;
if showing && !i.reading && i.read.is_none_or(|at| at.elapsed() >= POLL) {
self.internet_refresh();
}
changed
}
fn internet_refresh(&mut self) {
let i = &mut self.internet;
i.reading = true;
i.read = Some(Instant::now());
let tx = i.tx.clone();
let want_token = i.token.is_none();
std::thread::spawn(move || {
let _ = tx.send(Event::State(tailscale::status()));
if want_token {
let token = account::store().get(tailscale::TOKEN_KEY).ok().flatten();
let _ = tx.send(Event::Token(token));
}
});
}
fn internet_event(&mut self, event: Event) {
let i = &mut self.internet;
match event {
Event::State(state) => {
i.reading = false;
let was_running = i.node().map(|n| n.id.clone());
i.state = Some(state);
let now = i.node().map(|n| n.id.clone());
let stale = i.loaded.is_none_or(|at| at.elapsed() >= RELOAD);
if now.is_some() && (now != was_running || (stale && !i.busy)) {
self.internet_load();
}
}
Event::Token(token) => {
i.token = Some(token);
self.internet_load();
}
Event::Invites(r) => i.invites = Some(r),
Event::Policy(r) => i.policy = Some(r),
Event::Done(r) => {
i.busy = false;
i.message = Some(r);
self.internet_load();
}
Event::Invited(r) => {
i.busy = false;
match r {
Ok(invite) => {
i.message = None;
i.fresh = Some(invite);
self.servers.text(Field::Email).clear();
}
Err(e) => i.message = Some(Err(e)),
}
self.internet_load();
}
}
}
/// Reads the invitations and the access controls (needs the token and a running Tailscale).
fn internet_load(&mut self) {
let i = &mut self.internet;
let (Some(token), Some(node)) = (i.token(), i.node().cloned()) else {
return;
};
i.loaded = Some(Instant::now());
let tx = i.tx.clone();
std::thread::spawn(move || {
let api = Api::new(&token);
let _ = tx.send(Event::Invites(api.invites(&node.id)));
let _ = tx.send(Event::Policy(api.policy()));
});
}
pub(super) fn save_token(&mut self) {
let token = self.servers.value(Field::Token).trim().to_string();
if !token.starts_with("tskey-") {
self.internet.message = Some(Err(
"Paste the access token Tailscale showed (it begins with tskey-).".into(),
));
return;
}
self.servers.text(Field::Token).clear();
self.internet.asking_token = false;
self.internet.message = match account::store().set(tailscale::TOKEN_KEY, &token) {
Ok(()) if account::store().persistent() => None,
Ok(()) => Some(Ok(
"No keychain here: the token is kept until the launcher closes.".into(),
)),
Err(e) => Some(Err(e)),
};
self.internet.token = Some(Some(token));
self.internet_load();
}
fn forget_token(&mut self) {
let _ = account::store().delete(tailscale::TOKEN_KEY);
let i = &mut self.internet;
i.token = Some(None);
i.invites = None;
i.policy = None;
i.fresh = None;
i.message = Some(Ok(
"The access token is forgotten. Delete it in Tailscale's admin \
console too (Settings, Keys) if you won't use it again."
.into(),
));
}
pub(super) fn invite_friend(&mut self) {
let i = &mut self.internet;
let (Some(token), Some(node)) = (i.token(), i.node().cloned()) else {
return;
};
if i.busy {
return;
}
let email = self.servers.value(Field::Email).trim().to_string();
if !email.is_empty() && !email.contains('@') {
i.message = Some(Err("That isn't an email address.".into()));
return;
}
i.busy = true;
i.fresh = None;
let tx = i.tx.clone();
std::thread::spawn(move || {
let email = Some(email.as_str()).filter(|e| !e.is_empty());
let _ = tx.send(Event::Invited(Api::new(&token).invite(&node.id, email)));
});
}
fn internet_action(
&mut self,
job: impl FnOnce(&Api) -> Result<String, String> + Send + 'static,
) {
let i = &mut self.internet;
let Some(token) = i.token() else {
return;
};
if i.busy {
return;
}
i.busy = true;
i.message = None;
let tx = i.tx.clone();
std::thread::spawn(move || {
let _ = tx.send(Event::Done(job(&Api::new(&token))));
});
}
/// Applies `change` to the access controls read at `etag`, checked by Tailscale first.
fn change_policy(&mut self, new: Value, etag: Option<String>, said: &'static str) {
self.internet.confirm_limit = false;
self.internet_action(move |api| {
api.validate(&new)?;
api.set_policy(&new, etag.as_deref())?;
Ok(said.to_string())
});
}
fn copy_button(
&self,
id: &'static str,
text: String,
kind: Kind,
cx: &mut Context<Self>,
) -> Stateful<Div> {
let copied = self
.internet
.copied
.as_ref()
.is_some_and(|(what, at)| what == &text && at.elapsed() < Duration::from_secs(2));
button(
id,
if copied { "Copied" } else { "Copy" },
kind,
&self.theme,
)
.bg(self.theme.plate)
.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
cx.write_to_clipboard(gpui::ClipboardItem::new_string(text.clone()));
this.internet.copied = Some((text.clone(), Instant::now()));
cx.notify();
}))
}
pub(super) fn internet_panel(&mut self, port: u16, cx: &mut Context<Self>) -> Div {
let t = self.theme;
let mut panel = plate(&t)
.flex()
.flex_col()
.gap(px(12.))
.p(px(20.))
.child(caps("Friends over the internet · Tailscale").text_color(t.muted));
let line = |s: String| text(theme::CAPTION).text_color(t.muted).child(s);
let check = button("ts-check", "Check again", Kind::Ghost, &t).on_click(cx.listener(
|this, _: &ClickEvent, _, cx| {
this.internet_refresh();
cx.notify();
},
));
match self.internet.state.clone() {
None => return panel.child(line("Looking for Tailscale…".into())),
Some(State::Missing) => {
return panel
.child(text(theme::UI).text_color(t.ink).child(
"Tailscale lets friends anywhere join without opening your router to the \
internet. It's free: install it, sign in, then invite them here.",
))
.child(
div()
.flex()
.gap(px(8.))
.child(
button("ts-install", "Install Tailscale", Kind::Secondary, &t)
.bg(t.plate)
.on_click(|_, _, cx| cx.open_url(tailscale::install_url())),
)
.child(check),
);
}
Some(State::Off(why)) => {
let open = button("ts-open", "Open Tailscale", Kind::Secondary, &t)
.bg(t.plate)
.on_click(cx.listener(|this, _: &ClickEvent, _, cx| {
if let Err(e) = tailscale::open_app() {
this.internet.message = Some(Err(e));
}
this.internet.read = None;
cx.notify();
}));
panel =
panel
.child(text(theme::UI).text_color(t.ink).child(
"Tailscale is installed but not running. Open it and switch it on.",
))
.when(!why.is_empty(), |d| d.child(line(why)))
.child(div().flex().gap(px(8.)).child(open).child(check));
return self.internet_message(panel);
}
Some(State::SignedOut { auth_url }) => {
let sign_in = button("ts-login", "Sign in to Tailscale", Kind::Secondary, &t)
.bg(t.plate)
.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
match &auth_url {
Some(url) => cx.open_url(url),
None => {
if let Err(e) = tailscale::open_app() {
this.internet.message = Some(Err(e));
}
}
}
this.internet.read = None;
cx.notify();
}));
panel = panel
.child(text(theme::UI).text_color(t.ink).child(
"Tailscale is installed. Sign in with your own account in Tailscale \
(Horizon never sees it), then come back here.",
))
.when(cfg!(target_os = "linux"), |d| {
d.child(line(format!(
"On Linux, run `{}` in a terminal and open the link it prints.",
tailscale::LINUX_LOGIN
)))
})
.child(div().flex().gap(px(8.)).child(sign_in).child(check));
return self.internet_message(panel);
}
Some(State::Running(_)) => {}
}
let node = self.internet.node().cloned().unwrap_or_default();
let address = node.address(port);
let ip = node.ipv4().unwrap_or_default().to_string();
let mut where_ = div().flex().flex_col().gap(px(6.)).child(
div()
.flex()
.items_center()
.gap(px(12.))
.child(
text(theme::TITLE)
.font_family(theme::MONO)
.text_color(t.ink)
.child(address.clone()),
)
.child(self.copy_button("ts-copy", address, Kind::Secondary, cx)),
);
if node.magic_dns && !ip.is_empty() {
let ip_port = if port == 25565 {
ip.clone()
} else {
format!("{ip}:{port}")
};
where_ = where_.child(line(format!(
"Or {ip_port}. Friends type it in Multiplayer, Add Server, once this computer is \
shared with them and Tailscale runs on theirs."
)));
} else {
where_ = where_.child(line(
"Friends type it in Multiplayer, Add Server, once this computer is shared with \
them and Tailscale runs on theirs."
.into(),
));
}
panel = panel.child(where_);
panel = panel.child(
div()
.border_t_1()
.border_color(t.rule)
.pt(px(12.))
.flex()
.flex_col()
.gap(px(10.))
.child(text(theme::UI).text_color(t.ink).child("Invite a friend"))
.child(self.invite_section(&ip, port, cx)),
);
panel = panel.child(line(
"Horizon never turns on Funnel: the server isn't put on the public internet, only \
shared with the people you invite."
.into(),
));
self.internet_message(panel)
}
fn internet_message(&self, panel: Div) -> Div {
let t = self.theme;
match &self.internet.message {
Some(Ok(m)) => {
panel.child(text(theme::CAPTION).text_color(t.ink_soft).child(m.clone()))
}
Some(Err(e)) => panel.child(text(theme::CAPTION).text_color(t.danger).child(e.clone())),
None => panel,
}
}
fn invite_section(&mut self, ip: &str, port: u16, cx: &mut Context<Self>) -> Div {
let t = self.theme;
let line = |s: String| text(theme::CAPTION).text_color(t.muted).child(s);
let link = |id: &'static str, label: &'static str, url: &'static str| {
button(id, label, Kind::Ghost, &t).on_click(move |_, _, cx| cx.open_url(url))
};
let token = self.internet.token();
if token.is_none() {
let mut d = div().flex().flex_col().gap(px(10.)).child(line(
"Share this computer with a friend: they get it in their own Tailscale, and \
nothing else of yours. Do it in Tailscale's admin console (Machines, this \
computer, Share), or here with an access token you make there."
.into(),
));
let machines = link(
"ts-console",
"Open the admin console",
"https://login.tailscale.com/admin/machines",
);
if self.internet.asking_token {
let save = button("ts-token-save", "Save", Kind::Secondary, &t)
.bg(t.plate)
.on_click(cx.listener(|this, _: &ClickEvent, _, cx| {
this.save_token();
cx.notify();
}));
d = d
.child(
div()
.flex()
.items_center()
.gap(px(8.))
.child(self.field(Field::Token, "tskey-api-…", 300., cx))
.child(save)
.child(link("ts-keys", "Make a token", tailscale::KEYS)),
)
.child(line(
"Settings, Keys, Generate access token. It's kept in this computer's \
keychain and only sent to Tailscale."
.into(),
));
} else {
d = d.child(div().flex().gap(px(8.)).child(machines).child(
button("ts-token", "Use an access token…", Kind::Ghost, &t).on_click(
cx.listener(|this, _: &ClickEvent, _, cx| {
this.internet.asking_token = true;
this.internet.message = None;
cx.notify();
}),
),
));
}
return d;
}
// Invite.
let busy = self.internet.busy;
let invite = button("ts-invite", "Make an invitation", Kind::Secondary, &t).bg(t.plate);
let invite = if busy {
disabled(invite)
} else {
invite.on_click(cx.listener(|this, _: &ClickEvent, _, cx| {
this.invite_friend();
cx.notify();
}))
};
let mut d = div()
.flex()
.flex_col()
.gap(px(10.))
.child(
div()
.flex()
.items_center()
.gap(px(8.))
.child(self.field(Field::Email, "Their email (optional)", 260., cx))
.child(invite),
)
.child(line(
"One invitation per friend. With an email Tailscale sends it; without, send them \
the link. They accept it in their own Tailscale account."
.into(),
));
if let Some(fresh) = self.internet.fresh.clone() {
d = d.child(
div()
.flex()
.items_center()
.gap(px(10.))
.child(
text(theme::CAPTION)
.font_family(theme::MONO)
.text_color(t.ink)
.truncate()
.child(fresh.url.clone()),
)
.child(self.copy_button("ts-fresh-copy", fresh.url, Kind::Secondary, cx)),
);
}
match self.internet.invites.clone() {
Some(Ok(list)) if !list.is_empty() => {
let mut rows = div().flex().flex_col().border_t_1().border_color(t.rule);
for (n, inv) in list.into_iter().enumerate() {
let who = match (&inv.accepted_by, &inv.email) {
(Some(by), _) => format!("Shared with {by}"),
(None, Some(email)) => format!("Sent to {email}, not accepted yet"),
(None, None) => "Link not used yet".to_string(),
};
let id = inv.id.clone();
let withdraw = button(
SharedString::from(format!("ts-revoke-{n}")),
if inv.accepted {
"Stop sharing"
} else {
"Withdraw"
},
Kind::Ghost,
&t,
);
let withdraw = if busy {
disabled(withdraw)
} else {
withdraw.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
let id = id.clone();
this.internet.fresh = None;
this.internet_action(move |api| {
api.revoke(&id)?;
Ok("Withdrawn: that friend can't reach this computer any more."
.into())
});
cx.notify();
}))
};
let date = inv.created.get(..10).unwrap_or_default().to_string();
rows = rows.child(index_row(
n + 1,
who,
Some(format!("Made {date}").into_any_element()),
Some(withdraw.into_any_element()),
&t,
));
}
d = d.child(rows);
}
Some(Err(e)) => d = d.child(text(theme::CAPTION).text_color(t.danger).child(e)),
_ => {}
}
// What shared friends reach.
let reach = match &self.internet.policy {
Some(Ok((policy, etag))) => Some((
tailscale::reach(policy, ip, port),
policy.clone(),
etag.clone(),
)),
Some(Err(e)) => {
d = d.child(line(format!("Couldn't read your access controls: {e}")));
None
}
None => None,
};
if let Some((reach, policy, etag)) = reach {
let mut safety = div().flex().flex_col().gap(px(8.)).pt(px(4.));
match reach {
Reach::Everything => {
safety = safety.child(text(theme::UI).text_color(t.ink).child(
"Friends you share this computer with can reach all of it, not only \
Minecraft (Tailscale's default).",
));
if self.internet.confirm_limit {
let new = tailscale::limit_shared(&policy, ip, port);
safety = safety
.child(line(format!(
"This changes your tailnet's access controls: the rule that lets \
everyone reach everything becomes one for your own devices and \
members, and friends you share with reach only {ip}:{port}."
)))
.child(
div()
.flex()
.gap(px(8.))
.child(
button("ts-limit-go", "Change the access controls", Kind::Primary, &t)
.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
if let Some(new) = new.clone() {
this.change_policy(
new,
etag.clone(),
"Done: friends you share this computer with reach only the Minecraft server.",
);
}
cx.notify();
})),
)
.child(button("ts-limit-cancel", "Cancel", Kind::Ghost, &t).on_click(
cx.listener(|this, _: &ClickEvent, _, cx| {
this.internet.confirm_limit = false;
cx.notify();
}),
)),
);
} else {
let b = button("ts-limit", "Limit them to Minecraft", Kind::Secondary, &t)
.bg(t.plate);
safety = safety.child(div().flex().child(if busy {
disabled(b)
} else {
b.on_click(cx.listener(|this, _: &ClickEvent, _, cx| {
this.internet.confirm_limit = true;
cx.notify();
}))
}));
}
}
Reach::ServerOnly => {
let undo = tailscale::unlimit_shared(&policy, ip, port);
let b = button("ts-unlimit", "Remove the limit", Kind::Ghost, &t);
safety = safety.child(
div()
.flex()
.items_center()
.gap(px(12.))
.child(div().flex_none().size(px(6.)).rounded_full().bg(t.success))
.child(
text(theme::UI)
.flex_1()
.text_color(t.ink)
.child(format!(
"Friends you share with reach only the Minecraft server (port {port})."
)),
)
.child(if busy {
disabled(b)
} else {
b.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
if let Some(undo) = undo.clone() {
this.change_policy(
undo,
etag.clone(),
"Tailscale's default access controls are back.",
);
}
cx.notify();
}))
}),
);
}
Reach::Custom => {
let snippet = tailscale::snippet(ip, port);
safety = safety
.child(text(theme::UI).text_color(t.ink).child(
"Your access controls are your own, so Horizon leaves them alone. To \
keep shared friends to Minecraft, add this rule in the admin console:",
))
.child(
div()
.p(px(12.))
.rounded(px(theme::RADIUS))
.bg(t.plate)
.font_family(theme::MONO)
.text_size(px(theme::CAPTION.px))
.text_color(t.ink_soft)
.child(snippet.clone()),
)
.child(
div()
.flex()
.gap(px(8.))
.child(self.copy_button(
"ts-snippet-copy",
snippet,
Kind::Secondary,
cx,
))
.child(link("ts-acls", "Open access controls", tailscale::ACCESS)),
);
}
}
d = d.child(safety);
}
d.child(div().flex().child(
button("ts-forget", "Forget the access token", Kind::Ghost, &t).on_click(cx.listener(
|this, _: &ClickEvent, _, cx| {
this.forget_token();
cx.notify();
},
)),
))
}
}
+2
View File
@@ -2,8 +2,10 @@
pub mod account;
pub mod backend;
pub mod desktop;
pub mod heads;
pub mod modpacks;
pub mod mojang;
pub mod servers;
pub mod sources;
pub mod tailscale;
pub mod telemetry;
+133 -370
View File
@@ -4,10 +4,12 @@
mod cli;
mod collection;
mod game_tools;
mod internet;
mod markdown;
mod mcp;
mod modpacks_page;
mod perf;
mod servers_page;
mod theme;
use gpui::{
@@ -21,6 +23,7 @@ use horizon_launcher::backend::{self, Job, Msg, Release, Running, Settings, Upda
use horizon_launcher::telemetry;
use std::borrow::Cow;
use std::collections::VecDeque;
use std::sync::atomic::Ordering;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::Mutex;
use std::time::{Duration, Instant};
@@ -31,15 +34,17 @@ enum Page {
Play,
Versions,
Modpacks,
Servers,
Notes,
Settings,
}
impl Page {
const ALL: [(Page, &'static str); 5] = [
const ALL: [(Page, &'static str); 6] = [
(Page::Play, "Play"),
(Page::Versions, "Versions"),
(Page::Modpacks, "Modpacks"),
(Page::Servers, "Servers"),
(Page::Notes, "Patch notes"),
(Page::Settings, "Settings"),
];
@@ -85,12 +90,6 @@ fn ease_out_cubic(x: f32) -> f32 {
/// How often the launcher looks for new releases while it is open.
const CHECK_EVERY: Duration = Duration::from_secs(4 * 3600);
#[derive(Clone, Copy, PartialEq)]
enum Mode {
Client,
Server,
}
enum Load<T> {
Loading,
Ready(T),
@@ -113,7 +112,6 @@ struct Launcher {
update_install: Option<(String, bool)>,
/// The install bar's previous and current fraction, so it moves rather than jumps.
bar: (f32, f32),
mode: Mode,
releases: Load<Vec<Release>>,
installed: Vec<String>,
installing: Option<(String, f32)>,
@@ -134,7 +132,12 @@ struct Launcher {
/// The folder the running game keeps its settings in: the game home, or a modpack's.
client_home: Option<std::path::PathBuf>,
crash: Option<Crash>,
server: Option<Running>,
/// The Servers page (`servers_page.rs`).
servers: servers_page::Servers,
/// Its Friends over the internet panel (`internet.rs`).
internet: internet::Internet,
/// The window's own focus, so Tab reaches the controls when nothing else has the keyboard.
root_focus: FocusHandle,
/// Microsoft accounts (accounts.json), the sign-in under way and what it last showed.
accounts: Accounts,
sign_in: Option<SignIn>,
@@ -142,11 +145,6 @@ struct Launcher {
keychain: bool,
/// The game is waiting for its session (a token renewal) before it starts.
starting_client: Option<String>,
server_log: VecDeque<String>,
server_error: Option<String>,
input: String,
input_focused: bool,
focus: FocusHandle,
/// Join a friend: the code typed on the Play page, and the one the game
/// starting now joins.
friend_code: String,
@@ -157,7 +155,6 @@ struct Launcher {
args_input: String,
args_focused: bool,
args_focus: FocusHandle,
console_scroll: ScrollHandle,
/// Opt-in data collection: the player's answer and Settings' section (`collection`).
collection: collection::Collection,
tx: Sender<Msg>,
@@ -203,9 +200,6 @@ impl Launcher {
if settings.minecraft_dir.is_none() {
settings.minecraft_dir = backend::default_minecraft_dir();
}
if settings.server_dir.is_none() {
settings.server_dir = Some(backend::data_dir().join("server"));
}
// Release builds on Linux keep their entry in the applications menu up to date (a moved
// binary changes it); `HORIZON_LAUNCHER_DESKTOP_ENTRY=1` does it in a development build.
if cfg!(target_os = "linux")
@@ -244,7 +238,6 @@ impl Launcher {
self_update: SelfUpdate::Idle,
update_install: None,
bar: (0., 0.),
mode: Mode::Client,
releases: Load::Loading,
installed,
installing: None,
@@ -265,22 +258,18 @@ impl Launcher {
client_started: None,
client_home: None,
crash: None,
server: None,
servers: servers_page::Servers::new(),
internet: internet::Internet::new(),
root_focus: cx.focus_handle(),
accounts: Accounts::load(),
sign_in: None,
sign_in_state: None,
keychain: true,
starting_client: None,
server_log: VecDeque::new(),
server_error: None,
input: String::new(),
input_focused: false,
focus: cx.focus_handle(),
friend_code: String::new(),
friend_focused: false,
friend_focus: cx.focus_handle(),
joining: None,
console_scroll: ScrollHandle::new(),
collection: collection::Collection::load(),
tx,
rx,
@@ -529,17 +518,13 @@ impl Launcher {
.record_since("launcher.play_to_game_frame_ms", pressed);
}
}
let log = if job == Job::Server {
&mut self.server_log
} else {
&mut self.prepare_log
};
log.push_back(line);
if log.len() > LOG_LINES {
log.pop_front();
}
if job == Job::Server {
self.console_scroll.scroll_to_bottom();
// The MCP server keeps its own server's log; the launcher's are the preparer's
// and the game's.
if job != Job::Server {
self.prepare_log.push_back(line);
if self.prepare_log.len() > LOG_LINES {
self.prepare_log.pop_front();
}
}
}
Msg::Exit(job, result) => {
@@ -599,27 +584,12 @@ impl Launcher {
});
}
}
Job::Server => {
self.server = None;
let old = self
.server_log
.iter()
.any(|l| l.contains("unknown option --dedicated"));
self.server_error = error.map(|e| {
if old {
"This version has no dedicated server. Pick a newer one."
.to_string()
} else {
let tail =
self.server_log.iter().rev().find(|l| !l.trim().is_empty());
tail.map_or(e.clone(), |t| format!("{e}: {t}"))
}
});
}
Job::Server => {}
}
}
}
}
changed |= self.servers_tick();
if let Some(sign_in) = &self.sign_in {
let state = sign_in.state();
let changed_state = format!("{state:?}") != format!("{:?}", self.sign_in_state);
@@ -744,38 +714,11 @@ impl Launcher {
let Some(tag) = self.selected().cloned() else {
return;
};
match self.mode {
Mode::Client => {
if self.client.is_some() || self.starting_client.is_some() {
return;
}
self.starting_pack = None;
self.start_client(tag);
}
Mode::Server => {
if let Some(server) = &mut self.server {
server.send("stop");
return;
}
let Some(folder) = self.settings.server_dir.clone() else {
return;
};
if let Err(e) = std::fs::create_dir_all(&folder) {
self.server_error = Some(e.to_string());
return;
}
self.server_error = None;
self.server_log.clear();
match backend::spawn(
Job::Server,
backend::server_command(&tag, &folder),
self.tx.clone(),
) {
Ok(r) => self.server = Some(r),
Err(e) => self.server_error = Some(e),
}
}
if self.client.is_some() || self.starting_client.is_some() {
return;
}
self.starting_pack = None;
self.start_client(tag);
}
/// Starts the game: the session first (a network round trip when the token needs
@@ -810,7 +753,7 @@ impl Launcher {
});
}
fn choose_folder(&mut self, server: bool, cx: &mut Context<Self>) {
fn choose_folder(&mut self, cx: &mut Context<Self>) {
let paths = cx.prompt_for_paths(PathPromptOptions {
files: false,
directories: true,
@@ -821,11 +764,7 @@ impl Launcher {
if let Ok(Ok(Some(mut paths))) = paths.await {
if let Some(path) = paths.pop() {
let _ = this.update(cx, |this, cx| {
if server {
this.settings.server_dir = Some(path);
} else {
this.settings.minecraft_dir = Some(path);
}
this.settings.minecraft_dir = Some(path);
this.settings.save();
cx.notify();
});
@@ -834,18 +773,6 @@ impl Launcher {
})
.detach();
}
fn submit_command(&mut self) {
let line = std::mem::take(&mut self.input);
if line.trim().is_empty() {
return;
}
if let Some(server) = &mut self.server {
server.send(line.trim_start_matches('/'));
self.server_log.push_back(format!("> {line}"));
self.console_scroll.scroll_to_bottom();
}
}
}
// ----- Building blocks ---------------------------------------------------------------------------
@@ -978,6 +905,31 @@ fn mark(size: f32, t: &Theme) -> Div {
)
}
/// The last thing the player did was press Tab: focused controls show a ring (a click focuses them
/// too, without one).
static KEYBOARD: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
/// Makes a control reachable with Tab, pressed with Enter or Space, with a ring when the keyboard
/// put it in focus.
fn keyboard(el: Stateful<Div>, t: &Theme) -> Stateful<Div> {
let (ring, gap) = (t.accent, t.page);
// A ring with a gap of page colour, so it shows around filled buttons too. GPUI draws nothing
// for a shadow without blur: half a pixel keeps it crisp.
let shadow = |color, spread: f32| gpui::BoxShadow {
color,
offset: gpui::point(px(0.), px(0.)),
blur_radius: px(0.5),
spread_radius: px(spread),
};
el.tab_index(0).focus(move |s| {
if KEYBOARD.load(Ordering::Relaxed) {
s.shadow(vec![shadow(ring, 4.), shadow(gap, 1.5)])
} else {
s
}
})
}
fn button(
id: impl Into<SharedString>,
label: impl Into<SharedString>,
@@ -1006,7 +958,7 @@ fn button(
.cursor_pointer()
.child(hover_layer(&id, hover, theme::RADIUS_PILL, 0.))
.child(div().relative().child(label.into()));
let base = track_hover(base, id);
let base = keyboard(track_hover(base, id), &t);
match kind {
Kind::Primary => base
.bg(t.accent)
@@ -1025,8 +977,7 @@ fn button(
/// An on/off switch whose knob slides.
fn switch(id: &'static str, on: bool, t: &Theme) -> Stateful<Div> {
div()
.id(id)
keyboard(div().id(id), t)
.relative()
.flex_none()
.w(px(36.))
@@ -1069,8 +1020,7 @@ fn segments<T: Copy + PartialEq + 'static>(
.children(options.iter().map(|&(value, label)| {
let active = current == value;
let pick = on_pick.clone();
div()
.id(SharedString::from(format!("{id}-{label}")))
keyboard(div().id(SharedString::from(format!("{id}-{label}"))), &t)
.flex()
.items_center()
.h(px(theme::SEGMENT))
@@ -1089,7 +1039,7 @@ fn segments<T: Copy + PartialEq + 'static>(
}
fn disabled(b: Stateful<Div>) -> Stateful<Div> {
b.opacity(theme::DISABLED).cursor_default()
b.opacity(theme::DISABLED).cursor_default().tab_stop(false)
}
/// A page's title in display type and one line on what it is for.
@@ -1309,22 +1259,6 @@ impl Launcher {
.child(links)
}
fn segmented(&self, cx: &mut Context<Self>) -> Stateful<Div> {
let this = cx.entity().downgrade();
segments(
"mode",
&[(Mode::Client, "Client"), (Mode::Server, "Server")],
self.mode,
&self.theme,
move |mode, _, cx| {
let _ = this.update(cx, |this, cx| {
this.mode = mode;
cx.notify();
});
},
)
}
fn picker(&self, cx: &mut Context<Self>) -> Div {
let t = self.theme;
let label = self
@@ -1443,36 +1377,14 @@ impl Launcher {
let prepared = backend::is_prepared(&tag);
let version = backend::game_version(&tag);
let hero = self.hero(&tag, &version, prepared, cx);
let running = match self.mode {
Mode::Client => self.client.is_some() || self.starting_client.is_some(),
Mode::Server => self.server.is_some(),
};
let play_label = match (self.mode, running) {
(Mode::Client, false) => "Play",
(Mode::Client, true) => "Running",
(Mode::Server, false) => "Start server",
(Mode::Server, true) => "Stop server",
};
let kind = if self.mode == Mode::Server && running {
Kind::Secondary
} else {
Kind::Primary
};
let mut play = button("play", play_label, kind, &t)
let running = self.client.is_some() || self.starting_client.is_some();
let play_label = if running { "Running" } else { "Play" };
let mut play = button("play", play_label, Kind::Primary, &t)
.min_w(px(132.))
.h(px(theme::CONTROL_LARGE))
.px(px(28.))
.text_size(px(theme::UI.px));
let eula = self
.settings
.server_dir
.as_ref()
.is_some_and(|d| backend::eula_agreed(d));
if !prepared
|| self.preparing
|| (self.mode == Mode::Client && running)
|| (self.mode == Mode::Server && !eula)
{
if !prepared || self.preparing || running {
play = disabled(play);
} else {
play = play.on_click(cx.listener(|this, _: &ClickEvent, _, cx| {
@@ -1485,7 +1397,14 @@ impl Launcher {
.flex()
.items_center()
.justify_between()
.child(self.segmented(cx))
.child(
button("host", "Host a server", Kind::Ghost, &t).on_click(cx.listener(
|this, _: &ClickEvent, _, cx| {
this.go(Page::Servers);
cx.notify();
},
)),
)
.child(
div()
.flex()
@@ -1533,33 +1452,30 @@ impl Launcher {
.into_any_element()
},
);
let mut rows =
div()
.flex()
.flex_col()
.border_t_1()
.border_color(t.rule)
.child(index_row(
1,
"Resources",
Some(div().child(resources_value).into_any_element()),
Some(prepare.into_any_element()),
&t,
))
.child(index_row(
2,
"Minecraft folder",
Some(minecraft),
Some(
button("choose-mc", "Choose…", Kind::Ghost, &t)
.on_click(cx.listener(|this, _: &ClickEvent, _, cx| {
this.choose_folder(false, cx)
}))
.into_any_element(),
),
&t,
));
if self.mode == Mode::Client {
let mut rows = div()
.flex()
.flex_col()
.border_t_1()
.border_color(t.rule)
.child(index_row(
1,
"Resources",
Some(div().child(resources_value).into_any_element()),
Some(prepare.into_any_element()),
&t,
))
.child(index_row(
2,
"Minecraft folder",
Some(minecraft),
Some(
button("choose-mc", "Choose…", Kind::Ghost, &t)
.on_click(cx.listener(|this, _: &ClickEvent, _, cx| this.choose_folder(cx)))
.into_any_element(),
),
&t,
));
{
let who = match self.accounts.selected() {
Some(a) => format!("{} (Microsoft account)", a.name),
None => "Offline: online servers need a Microsoft account.".to_string(),
@@ -1580,62 +1496,6 @@ impl Launcher {
&t,
));
}
if self.mode == Mode::Server {
let folder = self
.settings
.server_dir
.as_deref()
.map(short)
.unwrap_or_default();
rows = rows.child(index_row(
3,
"Server folder",
Some(
div()
.font_family(theme::MONO)
.child(folder)
.into_any_element(),
),
Some(
button("choose-server", "Choose…", Kind::Ghost, &t)
.on_click(
cx.listener(|this, _: &ClickEvent, _, cx| this.choose_folder(true, cx)),
)
.into_any_element(),
),
&t,
));
let value = if eula {
"Agreed"
} else {
"Running a server needs your agreement to the Minecraft EULA."
};
let mut actions = div().flex().gap(px(4.)).child(
button("eula-read", "Read", Kind::Ghost, &t).on_click(cx.listener(
|_, _: &ClickEvent, _, cx| cx.open_url("https://aka.ms/MinecraftEULA"),
)),
);
if !eula {
actions =
actions.child(button("eula-agree", "Agree", Kind::Secondary, &t).on_click(
cx.listener(|this, _: &ClickEvent, _, cx| {
if let Some(dir) = &this.settings.server_dir {
if let Err(e) = backend::agree_eula(dir) {
this.server_error = Some(e);
}
}
cx.notify();
}),
));
}
rows = rows.child(index_row(
4,
"Minecraft EULA",
Some(div().child(value).into_any_element()),
Some(actions.into_any_element()),
&t,
));
}
let mut page = div()
.flex()
@@ -1668,18 +1528,11 @@ impl Launcher {
if let Some(e) = self.prepare_error.clone() {
page = page.child(notice(e, &t));
}
if self.mode == Mode::Client {
if let Some(e) = self.client_error.clone() {
page = page.child(notice(e, &t));
}
if let Some(crash) = self.crash.clone() {
page = page.child(self.crash_notice(crash, cx));
}
} else {
if let Some(e) = self.server_error.clone() {
page = page.child(notice(e, &t));
}
page = page.child(self.console(cx));
if let Some(e) = self.client_error.clone() {
page = page.child(notice(e, &t));
}
if let Some(crash) = self.crash.clone() {
page = page.child(self.crash_notice(crash, cx));
}
// The signature: the name, huge and faint, at the foot of the page.
page.child(
@@ -1819,11 +1672,7 @@ impl Launcher {
.and_then(|r| backend::notes_summary(&r.body)),
_ => None,
};
let title = if self.mode == Mode::Server {
"Horizon server"
} else {
"Horizon"
};
let title = "Horizon";
let mut hero =
div()
.flex()
@@ -2467,8 +2316,7 @@ impl Launcher {
}
fn join_friend(&mut self) {
if self.mode != Mode::Client || horizon_friends::invite::parse(&self.friend_code).is_none()
{
if horizon_friends::invite::parse(&self.friend_code).is_none() {
return;
}
self.joining = Some(self.friend_code.clone());
@@ -2478,121 +2326,6 @@ impl Launcher {
}
}
fn console(&self, cx: &mut Context<Self>) -> Div {
let t = self.theme;
let running = self.server.is_some();
let focused = self.input_focused;
let lines = self.server_log.iter().map(|l| div().child(l.clone()));
let log = div()
.id("console")
.h(px(260.))
.px(px(20.))
.py(px(12.))
.overflow_y_scroll()
.track_scroll(&self.console_scroll)
.font_family(theme::MONO)
.text_size(px(theme::CAPTION.px))
.line_height(px(19.))
.text_color(t.ink_soft)
.when(self.server_log.is_empty(), |d| {
d.child(div().text_color(t.muted).child(if running {
"Starting…"
} else {
"The server's output shows here."
}))
})
.children(lines);
let input = div()
.id("command")
.track_focus(&self.focus)
.flex()
.items_center()
.h(px(theme::FIELD + 8.))
.px(px(20.))
.border_t_1()
.border_color(t.rule)
.font_family(theme::MONO)
.text_size(px(theme::CAPTION.px))
.cursor_text()
.child(div().text_color(t.accent).mr(px(10.)).child("›"))
.child(if self.input.is_empty() && !focused {
div().text_color(t.muted).child(if running {
"Type a command, e.g. list"
} else {
"Start the server to send commands"
})
} else {
div().text_color(t.ink).child(self.input.clone())
})
.when(focused, |d| d.child(div().w(px(1.)).h(px(14.)).bg(t.ink)))
.on_click(cx.listener(|this, _: &ClickEvent, window, cx| {
window.focus(&this.focus);
cx.notify();
}))
.on_key_down(cx.listener(|this, ev: &KeyDownEvent, _, cx| {
let k = &ev.keystroke;
match k.key.as_str() {
"enter" => this.submit_command(),
"backspace" => {
this.input.pop();
}
_ if !k.modifiers.control && !k.modifiers.platform => {
if let Some(ch) = &k.key_char {
this.input.push_str(ch);
}
}
_ => return,
}
cx.stop_propagation();
cx.notify();
}));
plate(&t)
.flex()
.flex_col()
.overflow_hidden()
.child(
div()
.flex()
.items_center()
.justify_between()
.h(px(44.))
.px(px(20.))
.border_b_1()
.border_color(t.rule)
.child(
div()
.flex()
.items_center()
.gap(px(10.))
.child(div().size(px(6.)).rounded_full().bg(if running {
t.success
} else {
t.muted
}))
.child(
caps(if running { "Running" } else { "Stopped" })
.text_color(t.muted),
),
)
.when(running, |d| {
d.child(
button("kill", "Force stop", Kind::Ghost, &t)
.h(px(26.))
.px(px(10.))
.text_size(px(theme::CAPTION.px))
.on_click(cx.listener(|this, _: &ClickEvent, _, cx| {
if let Some(s) = &this.server {
s.kill();
}
cx.notify();
})),
)
}),
)
.child(log)
.child(input)
}
fn versions_page(&mut self, cx: &mut Context<Self>) -> Div {
let t = self.theme;
let header = page_header(
@@ -2944,13 +2677,14 @@ impl Render for Launcher {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let t = self.theme;
self.perf.frame();
self.input_focused = self.focus.is_focused(window);
self.servers.note_focus(window);
self.friend_focused = self.friend_focus.is_focused(window);
self.args_focused = self.args_focus.is_focused(window);
let content = match self.page {
Page::Play => self.play_page(cx),
Page::Versions => self.versions_page(cx),
Page::Modpacks => self.modpacks_page(cx),
Page::Servers => self.servers_page(cx),
Page::Notes => self.notes_page(cx),
Page::Settings => self.settings_page(cx),
};
@@ -2969,7 +2703,30 @@ impl Render for Launcher {
);
let (reveal, held) = self.opening_phase(window);
let ui = ease_out_cubic((reveal - 0.3) / 0.7);
if self.opening.is_none() && window.focused(cx).is_none() {
window.focus(&self.root_focus);
}
let mut root = div()
.id("root")
.track_focus(&self.root_focus)
.on_key_down(cx.listener(|this, ev: &KeyDownEvent, window, cx| {
let k = &ev.keystroke;
match k.key.as_str() {
"tab" => {
KEYBOARD.store(true, Ordering::Relaxed);
if k.modifiers.shift {
window.focus_prev();
} else {
window.focus_next();
}
}
"escape" if this.servers_escape() => {}
_ => return,
}
cx.stop_propagation();
cx.notify();
}))
.capture_any_mouse_down(|_, _, _| KEYBOARD.store(false, Ordering::Relaxed))
.relative()
.flex()
.flex_col()
@@ -3005,6 +2762,11 @@ impl Render for Launcher {
)
.child(self.footer(cx)),
);
if self.page == Page::Servers {
if let Some(dialog) = self.create_dialog(cx) {
root = root.child(dialog);
}
}
if self.asking_consent() {
root = root.child(self.consent_screen(cx));
}
@@ -3126,6 +2888,7 @@ fn main() {
)
.expect("window");
cx.on_window_closed(|cx| {
servers_page::stop_on_quit();
telemetry::send_before_exit();
cx.quit()
})
+14
View File
@@ -710,6 +710,20 @@ pub fn names(dir: &Path, file: &str) -> Vec<String> {
.collect()
}
/// A list file's names with their UUIDs (empty when the file has none).
pub fn entries(dir: &Path, file: &str) -> Vec<(String, String)> {
fs::read_to_string(dir.join(file))
.ok()
.and_then(|t| serde_json::from_str::<Vec<Value>>(&t).ok())
.unwrap_or_default()
.iter()
.filter_map(|e| {
let name = e["name"].as_str()?.to_string();
Some((name, e["uuid"].as_str().unwrap_or_default().to_string()))
})
.collect()
}
/// A backup, as `horizon-server backups --json` lists it.
#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct Backup {
File diff suppressed because it is too large Load Diff
+509
View File
@@ -0,0 +1,509 @@
//! Friends over the internet through Tailscale: is it installed and signed in, this computer's name and
//! address on the tailnet, and, with the player's own API access token, inviting a friend to this one
//! computer (device sharing) and limiting what shared friends reach to the server's port.
//!
//! The launcher never signs in for anyone and never uses Funnel: signing in happens in Tailscale's own app
//! or page, and the access token is one the player makes in the admin console and pastes here (it is
//! kept in the system keychain). `HORIZON_LAUNCHER_TAILSCALE` (the CLI) and `HORIZON_TAILSCALE_API` (the
//! API's address) point the tests at stand-ins.
use serde_json::{json, Value};
use std::path::PathBuf;
use std::process::Command;
use std::time::Duration;
/// The admin console: sign-in, keys, machines, the access controls.
pub const ADMIN: &str = "https://login.tailscale.com/admin";
/// Where an API access token is made.
pub const KEYS: &str = "https://login.tailscale.com/admin/settings/keys";
/// The access controls page.
pub const ACCESS: &str = "https://login.tailscale.com/admin/acls/file";
/// The keychain entry of the player's API access token.
pub const TOKEN_KEY: &str = "tailscale-api-token";
/// Where to download Tailscale for this system.
pub fn install_url() -> &'static str {
if cfg!(target_os = "macos") {
"https://tailscale.com/download/mac"
} else if cfg!(windows) {
"https://tailscale.com/download/windows"
} else {
"https://tailscale.com/download/linux"
}
}
/// The command that signs this computer in on Linux (it opens a link to finish in the browser).
pub const LINUX_LOGIN: &str = "sudo tailscale up --operator=$USER";
/// The `tailscale` command, wherever the installer put it.
pub fn cli() -> Option<PathBuf> {
if let Some(p) = std::env::var_os("HORIZON_LAUNCHER_TAILSCALE") {
return Some(PathBuf::from(p)).filter(|p| p.is_file());
}
let mut places: Vec<PathBuf> = std::env::var_os("PATH")
.map(|p| {
std::env::split_paths(&p)
.map(|d| {
d.join(if cfg!(windows) {
"tailscale.exe"
} else {
"tailscale"
})
})
.collect()
})
.unwrap_or_default();
if cfg!(target_os = "macos") {
places.push("/Applications/Tailscale.app/Contents/MacOS/Tailscale".into());
places.push("/usr/local/bin/tailscale".into());
places.push("/opt/homebrew/bin/tailscale".into());
} else if cfg!(windows) {
places.push(r"C:\Program Files\Tailscale\tailscale.exe".into());
} else {
places.push("/usr/bin/tailscale".into());
places.push("/usr/local/bin/tailscale".into());
places.push("/run/current-system/sw/bin/tailscale".into());
}
places.into_iter().find(|p| p.is_file())
}
/// This computer on the tailnet.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Node {
/// The stable node ID (`n…`), which the API takes as the device ID.
pub id: String,
pub host: String,
/// The MagicDNS name (`host.tailnet.ts.net`), without the final dot.
pub dns_name: String,
pub ips: Vec<String>,
pub tailnet: String,
pub magic_dns: bool,
}
impl Node {
pub fn ipv4(&self) -> Option<&str> {
self.ips
.iter()
.map(String::as_str)
.find(|ip| !ip.contains(':'))
}
/// What a friend types in Minecraft: the MagicDNS name when it is on, else the address.
pub fn address(&self, port: u16) -> String {
let host = if self.magic_dns && !self.dns_name.is_empty() {
self.dns_name.as_str()
} else {
self.ipv4().unwrap_or_default()
};
if port == 25565 {
host.to_string()
} else {
format!("{host}:{port}")
}
}
}
/// Where Tailscale stands on this computer.
#[derive(Clone, Debug, PartialEq)]
pub enum State {
/// No `tailscale` command.
Missing,
/// Installed, but its service doesn't answer (the app isn't open, or it is switched off).
Off(String),
/// Installed and waiting for the player to sign in (with the link to finish it, when there is one).
SignedOut {
auth_url: Option<String>,
},
Running(Node),
}
/// Asks `tailscale status --json` (read-only).
pub fn status() -> State {
status_of(cli())
}
/// `status` with a given `tailscale` command.
pub fn status_of(cli: Option<PathBuf>) -> State {
let Some(cli) = cli else {
return State::Missing;
};
let mut command = Command::new(&cli);
command.args(["status", "--json"]);
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
command.creation_flags(0x0800_0000); // CREATE_NO_WINDOW
}
let out = match command.output() {
Ok(out) => out,
Err(e) => return State::Off(e.to_string()),
};
match serde_json::from_slice::<Value>(&out.stdout) {
Ok(v) if v.is_object() => parse_status(&v),
_ => {
let err = String::from_utf8_lossy(&out.stderr);
let line = err.lines().find(|l| !l.trim().is_empty()).unwrap_or("");
State::Off(line.trim().to_string())
}
}
}
pub fn parse_status(v: &Value) -> State {
let backend = v["BackendState"].as_str().unwrap_or_default();
match backend {
"Running" => {
let me = &v["Self"];
let ips = me["TailscaleIPs"]
.as_array()
.map(|a| {
a.iter()
.filter_map(|ip| ip.as_str().map(str::to_string))
.collect()
})
.unwrap_or_default();
let tailnet = &v["CurrentTailnet"];
State::Running(Node {
id: me["ID"].as_str().unwrap_or_default().to_string(),
host: me["HostName"].as_str().unwrap_or_default().to_string(),
dns_name: me["DNSName"]
.as_str()
.unwrap_or_default()
.trim_end_matches('.')
.to_string(),
ips,
tailnet: tailnet["Name"].as_str().unwrap_or_default().to_string(),
magic_dns: tailnet["MagicDNSEnabled"].as_bool().unwrap_or(false),
})
}
"NeedsLogin" | "NeedsMachineAuth" | "NoState" => State::SignedOut {
auth_url: v["AuthURL"]
.as_str()
.filter(|u| u.starts_with("https://"))
.map(str::to_string),
},
"Stopped" => State::Off("Tailscale is switched off.".into()),
other => State::Off(format!("Tailscale is {}.", other.to_lowercase())),
}
}
/// Opens the Tailscale app, where the player signs in (macOS and Windows; Linux has no app).
pub fn open_app() -> Result<(), String> {
let result = if cfg!(target_os = "macos") {
Command::new("open").args(["-a", "Tailscale"]).status()
} else if cfg!(windows) {
Command::new(r"C:\Program Files\Tailscale\tailscale-ipn.exe").status()
} else {
return Err(format!(
"Run `{LINUX_LOGIN}` in a terminal and open the link it prints."
));
};
match result {
Ok(s) if s.success() => Ok(()),
Ok(_) | Err(_) => {
Err("Tailscale's app didn't open: start it from your applications.".into())
}
}
}
// ----- The API --------------------------------------------------------------------------------------
/// An invitation to use this computer, as the API lists it.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Invite {
pub id: String,
pub url: String,
pub email: Option<String>,
pub created: String,
pub accepted: bool,
/// Who accepted it (their login name).
pub accepted_by: Option<String>,
}
impl Invite {
fn parse(v: &Value) -> Self {
Self {
id: match &v["id"] {
Value::String(s) => s.clone(),
other => other.to_string(),
},
url: v["inviteUrl"].as_str().unwrap_or_default().to_string(),
email: v["email"]
.as_str()
.filter(|e| !e.is_empty())
.map(str::to_string),
created: v["created"].as_str().unwrap_or_default().to_string(),
accepted: v["accepted"].as_bool().unwrap_or(false),
accepted_by: v["acceptedBy"]["loginName"].as_str().map(str::to_string),
}
}
}
/// Tailscale's API with the player's access token.
pub struct Api {
base: String,
token: String,
}
impl Api {
pub fn new(token: &str) -> Self {
let base = std::env::var("HORIZON_TAILSCALE_API")
.unwrap_or_else(|_| "https://api.tailscale.com".into());
Self::at(&base, token)
}
/// The API at another address (a stand-in in tests).
pub fn at(base: &str, token: &str) -> Self {
Self {
base: base.trim_end_matches('/').to_string(),
token: token.trim().to_string(),
}
}
fn send(
&self,
method: &str,
path: &str,
body: Option<&Value>,
if_match: Option<&str>,
) -> Result<(Value, Option<String>), String> {
let agent = ureq::AgentBuilder::new()
.timeout(Duration::from_secs(20))
.build();
let mut request = agent
.request(method, &format!("{}/api/v2{path}", self.base))
.set("Authorization", &format!("Bearer {}", self.token))
.set("Accept", "application/json");
if let Some(etag) = if_match {
request = request.set("If-Match", etag);
}
let response = match body {
Some(b) => request.send_json(b.clone()),
None => request.call(),
};
match response {
Ok(r) => {
let etag = r.header("ETag").map(str::to_string);
let text = r.into_string().map_err(|e| e.to_string())?;
let v = if text.trim().is_empty() {
Value::Null
} else {
serde_json::from_str(&text).map_err(|e| e.to_string())?
};
Ok((v, etag))
}
Err(ureq::Error::Status(code, r)) => {
let v: Value = r.into_json().unwrap_or(Value::Null);
let said = v["message"].as_str().unwrap_or_default();
Err(match code {
401 => "Tailscale didn't accept the access token: it may have expired. Make a \
new one and paste it again."
.to_string(),
403 => format!(
"This access token may not do that{}. Make one as an admin of your tailnet.",
if said.is_empty() {
String::new()
} else {
format!(" ({said})")
}
),
412 => "The access controls changed meanwhile: look again before changing them."
.to_string(),
_ if !said.is_empty() => format!("Tailscale says: {said}"),
_ => format!("Tailscale answered {code}."),
})
}
Err(e) => Err(format!("Tailscale's API didn't answer: {e}")),
}
}
/// The invitations to this computer.
pub fn invites(&self, device: &str) -> Result<Vec<Invite>, String> {
let (v, _) = self.send(
"GET",
&format!("/device/{device}/device-invites"),
None,
None,
)?;
Ok(v.as_array()
.map(|a| a.iter().map(Invite::parse).collect())
.unwrap_or_default())
}
/// A new invitation to this computer only: a link to send, or sent by email by Tailscale. One
/// friend each (not reusable), never as an exit node.
pub fn invite(&self, device: &str, email: Option<&str>) -> Result<Invite, String> {
let mut one = json!({ "multiUse": false, "allowExitNode": false });
if let Some(email) = email.filter(|e| !e.trim().is_empty()) {
one["email"] = json!(email.trim());
}
let (v, _) = self.send(
"POST",
&format!("/device/{device}/device-invites"),
Some(&json!([one])),
None,
)?;
v.as_array()
.and_then(|a| a.first())
.map(Invite::parse)
.ok_or_else(|| "Tailscale made no invitation.".to_string())
}
/// Withdraws an invitation (an accepted one stops sharing the computer with that friend).
pub fn revoke(&self, invite: &str) -> Result<(), String> {
self.send("DELETE", &format!("/device-invites/{invite}"), None, None)
.map(|_| ())
}
/// The tailnet's access controls (as JSON) and their version.
pub fn policy(&self) -> Result<(Value, Option<String>), String> {
self.send("GET", "/tailnet/-/acl", None, None)
}
/// Checks new access controls without applying them.
pub fn validate(&self, policy: &Value) -> Result<(), String> {
let (v, _) = self.send("POST", "/tailnet/-/acl/validate", Some(policy), None)?;
match v["message"].as_str() {
Some(m) if !m.is_empty() => Err(format!("Tailscale wouldn't take the change: {m}")),
_ => Ok(()),
}
}
/// Applies new access controls, only if nobody changed them since `etag`.
pub fn set_policy(&self, policy: &Value, etag: Option<&str>) -> Result<(), String> {
self.send("POST", "/tailnet/-/acl", Some(policy), etag)
.map(|_| ())
}
}
// ----- Limiting what shared friends reach -------------------------------------------------------------
/// What the tailnet's access controls let shared friends reach.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Reach {
/// Tailscale's default: everyone, shared friends included, reaches every port of this computer.
Everything,
/// Shared friends reach only the server's port (the rule this launcher adds).
ServerOnly,
/// Access controls someone wrote: the launcher doesn't touch them.
Custom,
}
const MEMBERS: [&str; 2] = ["autogroup:member", "autogroup:tagged"];
const SHARED: &str = "autogroup:shared";
fn is_star(v: &Value) -> bool {
v.as_array().is_some_and(|a| a.len() == 1 && a[0] == "*")
}
fn is_members(v: &Value) -> bool {
v.as_array().is_some_and(|a| {
a.len() == MEMBERS.len() && MEMBERS.iter().all(|m| a.iter().any(|x| x == m))
})
}
/// The allow-all rule, in either of its forms: `acls` (`src`/`dst` or the older `users`/`ports`) or
/// `grants`.
fn allow_all(rule: &Value, grant: bool) -> bool {
if grant {
is_star(&rule["src"]) && is_star(&rule["dst"]) && is_star(&rule["ip"])
} else {
rule["action"] == "accept"
&& (is_star(&rule["src"]) || is_star(&rule["users"]))
&& [&rule["dst"], &rule["ports"]]
.iter()
.any(|d| d.as_array().is_some_and(|a| a.len() == 1 && a[0] == "*:*"))
}
}
fn members_all(rule: &Value, grant: bool) -> bool {
if grant {
is_members(&rule["src"]) && is_star(&rule["dst"]) && is_star(&rule["ip"])
} else {
rule["action"] == "accept"
&& is_members(&rule["src"])
&& rule["dst"]
.as_array()
.is_some_and(|a| a.len() == 1 && a[0] == "*:*")
}
}
fn shared_rule(ip: &str, port: u16, grant: bool) -> Value {
if grant {
json!({ "src": [SHARED], "dst": [ip], "ip": [format!("tcp:{port}")] })
} else {
json!({ "action": "accept", "src": [SHARED], "dst": [format!("{ip}:{port}")] })
}
}
/// The rules (`acls` or `grants`) when the policy is one of the two shapes this launcher knows: the
/// default allow-all alone, or that limited by `limit_shared` (with only shared rules beside it).
fn known(policy: &Value) -> Option<(&'static str, &Vec<Value>)> {
let acls = policy["acls"].as_array().filter(|a| !a.is_empty());
let grants = policy["grants"].as_array().filter(|a| !a.is_empty());
match (acls, grants) {
(Some(a), None) => Some(("acls", a)),
(None, Some(g)) => Some(("grants", g)),
_ => None,
}
}
/// What shared friends reach under `policy`, for this computer's address and the server's port.
pub fn reach(policy: &Value, ip: &str, port: u16) -> Reach {
let Some((key, rules)) = known(policy) else {
return Reach::Custom;
};
let grant = key == "grants";
if rules.len() == 1 && allow_all(&rules[0], grant) {
return Reach::Everything;
}
let ours = shared_rule(ip, port, grant);
if rules.len() == 2 && members_all(&rules[0], grant) && rules[1] == ours {
return Reach::ServerOnly;
}
Reach::Custom
}
/// The default policy changed so shared friends reach only `ip:port`, everyone else as before; None
/// when the policy isn't the default (then the player adds `snippet` by hand).
pub fn limit_shared(policy: &Value, ip: &str, port: u16) -> Option<Value> {
if reach(policy, ip, port) != Reach::Everything {
return None;
}
let (key, _) = known(policy)?;
let grant = key == "grants";
let members = if grant {
json!({ "src": MEMBERS, "dst": ["*"], "ip": ["*"] })
} else {
json!({ "action": "accept", "src": MEMBERS, "dst": ["*:*"] })
};
let mut new = policy.clone();
new[key] = json!([members, shared_rule(ip, port, grant)]);
Some(new)
}
/// Undoes `limit_shared`: Tailscale's default rule back. None if the policy isn't the limited one.
pub fn unlimit_shared(policy: &Value, ip: &str, port: u16) -> Option<Value> {
if reach(policy, ip, port) != Reach::ServerOnly {
return None;
}
let (key, _) = known(policy)?;
let mut new = policy.clone();
new[key] = if key == "grants" {
json!([{ "src": ["*"], "dst": ["*"], "ip": ["*"] }])
} else {
json!([{ "action": "accept", "src": ["*"], "dst": ["*:*"] }])
};
Some(new)
}
/// The two rules to put in access controls someone wrote, by hand in the admin console.
pub fn snippet(ip: &str, port: u16) -> String {
format!(
"// Friends this computer is shared with reach only the Minecraft server.\n\
// Replace \"*\" with these two in the rule that lets everyone reach everything:\n\
// \"src\": [\"autogroup:member\", \"autogroup:tagged\"]\n\
{{\"action\": \"accept\", \"src\": [\"autogroup:shared\"], \"dst\": [\"{ip}:{port}\"]}},"
)
}
#[cfg(test)]
mod tests;
+270
View File
@@ -0,0 +1,270 @@
use super::*;
use std::io::{BufRead, BufReader, Read, Write};
use std::sync::{Arc, Mutex};
fn running() -> Value {
json!({
"BackendState": "Running",
"AuthURL": "",
"Self": {
"ID": "nAbCdEf1CNTRL",
"HostName": "ludos-mac",
"DNSName": "ludos-mac.tail1234.ts.net.",
"TailscaleIPs": ["100.101.102.103", "fd7a:115c:a1e0::1"]
},
"CurrentTailnet": { "Name": "ludo@example.com", "MagicDNSSuffix": "tail1234.ts.net", "MagicDNSEnabled": true }
})
}
#[test]
fn status_reads_as_the_cli_writes_it() {
let State::Running(node) = parse_status(&running()) else {
panic!("not running");
};
assert_eq!(node.id, "nAbCdEf1CNTRL");
assert_eq!(node.dns_name, "ludos-mac.tail1234.ts.net");
assert_eq!(node.ipv4(), Some("100.101.102.103"));
assert_eq!(node.address(25565), "ludos-mac.tail1234.ts.net");
assert_eq!(
Node {
magic_dns: false,
..node.clone()
}
.address(25570),
"100.101.102.103:25570"
);
assert_eq!(
parse_status(
&json!({ "BackendState": "NeedsLogin", "AuthURL": "https://login.tailscale.com/a/abc" })
),
State::SignedOut {
auth_url: Some("https://login.tailscale.com/a/abc".into())
}
);
assert_eq!(
parse_status(&json!({ "BackendState": "NeedsLogin", "AuthURL": "" })),
State::SignedOut { auth_url: None }
);
assert!(matches!(
parse_status(&json!({ "BackendState": "Stopped" })),
State::Off(_)
));
assert_eq!(status_of(None), State::Missing);
}
/// The CLI is only ever asked `status --json`: a stand-in that refuses anything else.
#[cfg(unix)]
#[test]
fn status_asks_the_cli_read_only() {
use std::os::unix::fs::PermissionsExt;
let dir = std::env::temp_dir().join(format!("horizon-tailscale-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let json = dir.join("status.json");
std::fs::write(&json, running().to_string()).unwrap();
let fake = dir.join("tailscale");
std::fs::write(
&fake,
format!(
"#!/bin/sh\n[ \"$*\" = \"status --json\" ] || {{ echo \"unexpected: $*\" >&2; exit 9; }}\ncat '{}'\n",
json.display()
),
)
.unwrap();
std::fs::set_permissions(&fake, std::fs::Permissions::from_mode(0o755)).unwrap();
assert!(matches!(status_of(Some(fake.clone())), State::Running(n) if n.host == "ludos-mac"));
// A stopped daemon: no JSON, a line on stderr.
std::fs::write(
&fake,
"#!/bin/sh\necho 'failed to connect to local tailscaled; it doesn'\\''t appear to be running' >&2\nexit 1\n",
)
.unwrap();
assert!(matches!(status_of(Some(fake)), State::Off(why) if why.contains("tailscaled")));
std::fs::remove_dir_all(&dir).unwrap();
}
fn default_acls() -> Value {
json!({
"tagOwners": { "tag:server": ["autogroup:admin"] },
"acls": [{ "action": "accept", "src": ["*"], "dst": ["*:*"] }],
"ssh": [{ "action": "check", "src": ["autogroup:member"], "dst": ["autogroup:self"], "users": ["autogroup:nonroot", "root"] }]
})
}
#[test]
fn the_default_policy_is_limited_for_shared_friends_only() {
let ip = "100.101.102.103";
let policy = default_acls();
assert_eq!(reach(&policy, ip, 25565), Reach::Everything);
let limited = limit_shared(&policy, ip, 25565).unwrap();
assert_eq!(
limited["acls"],
json!([
{ "action": "accept", "src": ["autogroup:member", "autogroup:tagged"], "dst": ["*:*"] },
{ "action": "accept", "src": ["autogroup:shared"], "dst": ["100.101.102.103:25565"] }
])
);
// Everything else stays.
assert_eq!(limited["ssh"], policy["ssh"]);
assert_eq!(limited["tagOwners"], policy["tagOwners"]);
assert_eq!(reach(&limited, ip, 25565), Reach::ServerOnly);
// Another port: not the launcher's rule for this server.
assert_eq!(reach(&limited, ip, 25566), Reach::Custom);
assert!(limit_shared(&limited, ip, 25565).is_none());
// Undone, it's Tailscale's default again.
let undone = unlimit_shared(&limited, ip, 25565).unwrap();
assert_eq!(reach(&undone, ip, 25565), Reach::Everything);
assert_eq!(undone["ssh"], policy["ssh"]);
// The older form of the default rule.
let old = json!({ "acls": [{ "action": "accept", "users": ["*"], "ports": ["*:*"] }] });
assert_eq!(reach(&old, ip, 25565), Reach::Everything);
assert!(limit_shared(&old, ip, 25565).is_some());
// Grants.
let grants = json!({ "grants": [{ "src": ["*"], "dst": ["*"], "ip": ["*"] }] });
let limited = limit_shared(&grants, ip, 25570).unwrap();
assert_eq!(
limited["grants"][1],
json!({ "src": ["autogroup:shared"], "dst": [ip], "ip": ["tcp:25570"] })
);
assert_eq!(reach(&limited, ip, 25570), Reach::ServerOnly);
assert!(unlimit_shared(&limited, ip, 25570).is_some());
// Someone's own rules: never rewritten.
let custom = json!({ "acls": [
{ "action": "accept", "src": ["group:family"], "dst": ["*:*"] },
{ "action": "accept", "src": ["*"], "dst": ["*:*"] }
] });
assert_eq!(reach(&custom, ip, 25565), Reach::Custom);
assert!(limit_shared(&custom, ip, 25565).is_none());
assert!(unlimit_shared(&custom, ip, 25565).is_none());
let both = json!({ "acls": [{ "action": "accept", "src": ["*"], "dst": ["*:*"] }], "grants": [{ "src": ["*"], "dst": ["*"], "ip": ["*"] }] });
assert_eq!(reach(&both, ip, 25565), Reach::Custom);
assert!(snippet(ip, 25565).contains("\"dst\": [\"100.101.102.103:25565\"]"));
}
/// What the stand-in API saw: request line, headers, body.
type Seen = Arc<Mutex<Vec<(String, String, String)>>>;
/// A stand-in for api.tailscale.com answering each request with the next reply (status, ETag, body).
fn stand_in(replies: Vec<(u16, Option<&'static str>, String)>) -> (String, Seen) {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let base = format!("http://{}", listener.local_addr().unwrap());
let seen: Seen = Arc::default();
let log = seen.clone();
std::thread::spawn(move || {
for (code, etag, body) in replies {
let (stream, _) = listener.accept().unwrap();
let mut reader = BufReader::new(stream.try_clone().unwrap());
let mut first = String::new();
reader.read_line(&mut first).unwrap();
let mut head = String::new();
loop {
let mut line = String::new();
reader.read_line(&mut line).unwrap();
if line == "\r\n" || line.is_empty() {
break;
}
head.push_str(&line);
}
let length = head
.lines()
.find_map(|l| {
l.to_ascii_lowercase()
.strip_prefix("content-length:")
.map(|n| n.trim().parse::<usize>().unwrap())
})
.unwrap_or(0);
let mut sent = vec![0; length];
reader.read_exact(&mut sent).unwrap();
log.lock().unwrap().push((
first.trim().to_string(),
head,
String::from_utf8(sent).unwrap(),
));
let reason = match code {
200 => "OK",
401 => "Unauthorized",
_ => "Error",
};
let etag = etag.map(|e| format!("ETag: {e}\r\n")).unwrap_or_default();
let mut stream = stream;
write!(
stream,
"HTTP/1.1 {code} {reason}\r\nContent-Type: application/json\r\n{etag}Content-Length: {}\r\nConnection: close\r\n\r\n{body}",
body.len()
)
.unwrap();
}
});
(base, seen)
}
#[test]
fn invites_and_access_controls_go_through_the_api() {
let invite = json!([{
"id": "12345", "created": "2026-10-19T12:00:00Z", "inviteUrl": "https://login.tailscale.com/admin/invite/code1",
"accepted": false
}]);
let accepted = json!([{
"id": 12346, "created": "2026-10-18T12:00:00Z", "inviteUrl": "https://login.tailscale.com/admin/invite/code2",
"accepted": true, "acceptedBy": { "id": 1, "loginName": "friend@example.com" }
}]);
let (base, seen) = stand_in(vec![
(200, None, invite.to_string()),
(200, None, accepted.to_string()),
(200, None, String::new()),
(200, Some("\"e1\""), default_acls().to_string()),
(200, None, "{}".into()),
(200, None, "{}".into()),
(
401,
None,
json!({ "message": "API token invalid" }).to_string(),
),
]);
let api = Api::at(&base, " tskey-api-test\n");
let made = api.invite("nAbC", None).unwrap();
assert_eq!(made.url, "https://login.tailscale.com/admin/invite/code1");
assert!(!made.accepted);
let list = api.invites("nAbC").unwrap();
assert_eq!(list[0].id, "12346");
assert_eq!(list[0].accepted_by.as_deref(), Some("friend@example.com"));
api.revoke("12346").unwrap();
let (policy, etag) = api.policy().unwrap();
assert_eq!(etag.as_deref(), Some("\"e1\""));
let limited = limit_shared(&policy, "100.101.102.103", 25565).unwrap();
api.validate(&limited).unwrap();
api.set_policy(&limited, etag.as_deref()).unwrap();
assert!(api.invites("nAbC").unwrap_err().contains("access token"));
let seen = seen.lock().unwrap();
let lines: Vec<&str> = seen.iter().map(|(l, _, _)| l.as_str()).collect();
assert_eq!(
lines,
[
"POST /api/v2/device/nAbC/device-invites HTTP/1.1",
"GET /api/v2/device/nAbC/device-invites HTTP/1.1",
"DELETE /api/v2/device-invites/12346 HTTP/1.1",
"GET /api/v2/tailnet/-/acl HTTP/1.1",
"POST /api/v2/tailnet/-/acl/validate HTTP/1.1",
"POST /api/v2/tailnet/-/acl HTTP/1.1",
"GET /api/v2/device/nAbC/device-invites HTTP/1.1",
]
);
for (_, head, _) in seen.iter() {
assert!(
head.to_ascii_lowercase()
.contains("authorization: bearer tskey-api-test\r\n"),
"{head}"
);
}
// One friend per invitation, never as an exit node.
let body: Value = serde_json::from_str(&seen[0].2).unwrap();
assert_eq!(body, json!([{ "multiUse": false, "allowExitNode": false }]));
// The change applies only to the version read.
assert!(seen[5].1.to_ascii_lowercase().contains("if-match: \"e1\""));
let sent: Value = serde_json::from_str(&seen[5].2).unwrap();
assert_eq!(sent, limited);
}
+44
View File
@@ -83,3 +83,47 @@ impl Theme {
}
}
}
/// How strongly a selected suggestion in a text field is tinted with the accent.
pub const SELECTION: f32 = 0.22;
#[cfg(test)]
mod tests {
use super::{tokens, SELECTION};
/// `top` at `alpha` over `under`, as the GPU blends them.
fn blend(top: u32, under: u32, alpha: f32) -> u32 {
let channel = |shift: u32| {
let (a, b) = ((top >> shift & 0xff) as f32, (under >> shift & 0xff) as f32);
((a * alpha + b * (1. - alpha)).round() as u32) << shift
};
channel(16) | channel(8) | channel(0)
}
/// What the Servers page draws besides text (whose colours horizon-theme's own test holds to
/// WCAG AA on every background): the keyboard focus ring, the borders of a focused or refused
/// field (WCAG 1.4.11 asks 3:1 for those), and text over a selected suggestion.
#[test]
fn marks_and_selections_read_clearly() {
for p in [tokens::LIGHT, tokens::DARK] {
let theme = if p.dark { "dark" } else { "light" };
for (name, bg) in [
("page", p.page),
("plate_soft", p.plate_soft),
("plate", p.plate),
("surface", p.surface),
] {
let ring = tokens::contrast(p.accent, bg);
assert!(ring >= 3.0, "{theme} focus ring on {name}: {ring:.2}");
let refused = tokens::contrast(p.danger, bg);
assert!(
refused >= 3.0,
"{theme} refused field on {name}: {refused:.2}"
);
}
let selection = blend(p.accent, p.surface, SELECTION);
let ratio = tokens::contrast(p.ink, selection);
assert!(ratio >= 4.5, "{theme} text on a selection: {ratio:.2}");
}
}
}