ed6af00ff8
- packaging/server/install.sh (Linux, macOS) and install.ps1 (Windows) install the latest release (or --version) from GitHub, check the SHA-256 GitHub publishes for it, and keep releases side by side: <dir>/current points at the one in use, so an update or a rollback is one link, and the game files prepared from the owner's Minecraft stay in <dir>/game-files across updates. - packaging/server/Dockerfile builds the server and the engine; the prepared game files are mounted read-only, and the first start writes server.toml from HORIZON_EULA, HORIZON_WORLD, HORIZON_SEED, HORIZON_PORT, HORIZON_WHITELIST, HORIZON_PLAYERS and HORIZON_OP (`init` reads them too). - `horizon-server service install|uninstall|print` writes and loads a systemd unit (user, or --system), a launchd agent, or a Windows task at logon. It runs the installer's launcher, so updates apply on the next restart. - Releases build horizon-server and ship it beside horizon-ctl and horizon-prepare; PLAY.txt says how to run a server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
309 lines
11 KiB
Rust
309 lines
11 KiB
Rust
//! `horizon-server init`: the first run (docs/server.md, section 3). It asks about the EULA, the world, the
|
|
//! port and who may join, imports an existing `server.properties`, finds the game, and writes `server.toml`.
|
|
//! Every question has a flag, so it also runs unattended (Docker, scripts).
|
|
|
|
use crate::{config, config::Config, engine};
|
|
use horizon_server_files::{
|
|
eula,
|
|
java_properties::now_millis,
|
|
user_lists::{NameAndId, OpEntry, OpList, WhiteEntry, WhiteList},
|
|
};
|
|
use std::{
|
|
io::{BufRead, IsTerminal, Write},
|
|
path::Path,
|
|
};
|
|
|
|
/// The answers given as flags; what is missing is asked, or defaulted with `yes`.
|
|
#[derive(Default)]
|
|
pub struct Answers {
|
|
pub eula: bool,
|
|
pub world: Option<String>,
|
|
pub seed: Option<String>,
|
|
pub port: Option<u16>,
|
|
pub whitelist: Option<bool>,
|
|
pub players: Vec<String>,
|
|
pub op: Option<String>,
|
|
/// Take the default for every question not answered by a flag.
|
|
pub yes: bool,
|
|
}
|
|
|
|
impl Answers {
|
|
/// The answers in the environment, for containers: `HORIZON_EULA=true`, `HORIZON_WORLD`,
|
|
/// `HORIZON_SEED`, `HORIZON_PORT`, `HORIZON_WHITELIST=true|false`, `HORIZON_PLAYERS` (names separated by
|
|
/// commas) and `HORIZON_OP`. Flags given on the command line win.
|
|
pub fn from_env() -> Result<Self, String> {
|
|
let var = |name: &str| std::env::var(name).ok().filter(|v| !v.is_empty());
|
|
let flag = |name: &str| var(name).map(|v| v.eq_ignore_ascii_case("true"));
|
|
Ok(Self {
|
|
eula: flag("HORIZON_EULA").unwrap_or(false),
|
|
world: var("HORIZON_WORLD"),
|
|
seed: var("HORIZON_SEED"),
|
|
port: var("HORIZON_PORT")
|
|
.map(|p| {
|
|
p.parse()
|
|
.map_err(|_| format!("HORIZON_PORT is {p:?}: use a port number"))
|
|
})
|
|
.transpose()?,
|
|
whitelist: flag("HORIZON_WHITELIST"),
|
|
players: var("HORIZON_PLAYERS")
|
|
.map(|p| {
|
|
p.split([',', ' '])
|
|
.filter(|n| !n.is_empty())
|
|
.map(str::to_owned)
|
|
.collect()
|
|
})
|
|
.unwrap_or_default(),
|
|
op: var("HORIZON_OP"),
|
|
yes: false,
|
|
})
|
|
}
|
|
}
|
|
|
|
struct Prompt {
|
|
interactive: bool,
|
|
}
|
|
|
|
impl Prompt {
|
|
fn ask(&self, question: &str, default: &str) -> String {
|
|
if !self.interactive {
|
|
return default.to_owned();
|
|
}
|
|
if default.is_empty() {
|
|
print!("{question}: ");
|
|
} else {
|
|
print!("{question} [{default}]: ");
|
|
}
|
|
let _ = std::io::stdout().flush();
|
|
let mut line = String::new();
|
|
if std::io::stdin().lock().read_line(&mut line).unwrap_or(0) == 0 {
|
|
return default.to_owned();
|
|
}
|
|
match line.trim() {
|
|
"" => default.to_owned(),
|
|
answer => answer.to_owned(),
|
|
}
|
|
}
|
|
fn yes(&self, question: &str, default: bool) -> bool {
|
|
let answer = self.ask(
|
|
&format!("{question} ({})", if default { "Y/n" } else { "y/N" }),
|
|
"",
|
|
);
|
|
match answer.to_ascii_lowercase().as_str() {
|
|
"" => default,
|
|
a => a.starts_with('y'),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn run(dir: &Path, answers: Answers) -> Result<(), String> {
|
|
std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?;
|
|
let toml_path = dir.join(config::FILE);
|
|
if toml_path.exists() {
|
|
return Err(format!(
|
|
"{} already exists: this server is set up. Edit it, or start the server with `horizon-server run`",
|
|
toml_path.display()
|
|
));
|
|
}
|
|
let prompt = Prompt {
|
|
interactive: !answers.yes && std::io::stdin().is_terminal(),
|
|
};
|
|
|
|
// An existing vanilla server folder: its settings become server.toml.
|
|
let properties = dir.join(config::PROPERTIES);
|
|
let mut cfg = if properties.exists() {
|
|
let source = config::read_properties(&properties)?;
|
|
let cfg = Config::import(&source)?;
|
|
let kept = dir.join("server.properties.imported");
|
|
std::fs::copy(&properties, &kept).map_err(|e| e.to_string())?;
|
|
println!(
|
|
"Imported server.properties into server.toml (the original is kept as {}).",
|
|
kept.display()
|
|
);
|
|
cfg
|
|
} else {
|
|
Config::default()
|
|
};
|
|
|
|
// 1. The EULA: only a yes writes eula=true.
|
|
let eula_path = dir.join("eula.txt");
|
|
let agreed = eula::agreed(&eula_path).unwrap_or(false);
|
|
if !agreed {
|
|
println!(
|
|
"Minecraft's End User License Agreement applies to running a server: {}",
|
|
eula::EULA_LINK
|
|
);
|
|
let yes = answers.eula || (prompt.interactive && prompt.yes("Do you agree to it?", false));
|
|
if !yes {
|
|
let _ = std::fs::write(&eula_path, eula::file(now_millis(), false));
|
|
return Err(
|
|
"the server can't run until you agree to the EULA: run init again and answer yes, \
|
|
or pass --eula"
|
|
.into(),
|
|
);
|
|
}
|
|
std::fs::write(&eula_path, eula::file(now_millis(), true)).map_err(|e| e.to_string())?;
|
|
}
|
|
|
|
// 2. The world: an existing one keeps its seed.
|
|
cfg.world.name = answers
|
|
.world
|
|
.unwrap_or_else(|| prompt.ask("World name", &cfg.world.name));
|
|
if dir.join(&cfg.world.name).join("level.dat").exists() {
|
|
println!("Using the existing world in {}.", cfg.world.name);
|
|
} else {
|
|
cfg.world.seed = answers
|
|
.seed
|
|
.unwrap_or_else(|| prompt.ask("Seed (empty for a random one)", &cfg.world.seed));
|
|
}
|
|
|
|
// 3. The port, checked free now rather than at the first start.
|
|
cfg.server.port = match answers.port {
|
|
Some(port) => port,
|
|
None => prompt
|
|
.ask("Port", &cfg.server.port.to_string())
|
|
.parse()
|
|
.map_err(|_| "the port must be a number from 1 to 65535".to_owned())?,
|
|
};
|
|
if std::net::TcpListener::bind(("0.0.0.0", cfg.server.port)).is_err() {
|
|
println!(
|
|
"Port {} is in use right now: stop what uses it, or change server.port in server.toml.",
|
|
cfg.server.port
|
|
);
|
|
}
|
|
|
|
// 4. Who may join.
|
|
cfg.server.whitelist = answers.whitelist.unwrap_or_else(|| {
|
|
prompt.yes(
|
|
"Only let in players you list (whitelist)?",
|
|
cfg.server.whitelist,
|
|
)
|
|
});
|
|
let mut players = answers.players;
|
|
if players.is_empty() && cfg.server.whitelist {
|
|
players = prompt
|
|
.ask("Players to let in (names, separated by commas)", "")
|
|
.split([',', ' '])
|
|
.filter(|n| !n.is_empty())
|
|
.map(str::to_owned)
|
|
.collect();
|
|
}
|
|
let op = answers.op.or_else(|| {
|
|
let first = players.first()?;
|
|
prompt
|
|
.yes(&format!("Make {first} an operator?"), true)
|
|
.then(|| first.clone())
|
|
});
|
|
add_players(dir, &players, op.as_deref())?;
|
|
|
|
// 5. The game and its files.
|
|
match engine::program(&cfg, dir) {
|
|
Ok(program) => {
|
|
println!("Game: {}", program.display());
|
|
match engine::resources(&cfg, dir, Some(&program)) {
|
|
Ok(resources) => println!("Game files: {}", resources.display()),
|
|
Err(e) => println!("{e}"),
|
|
}
|
|
}
|
|
Err(e) => println!("{e}"),
|
|
}
|
|
|
|
std::fs::write(&toml_path, config::render(&cfg)).map_err(|e| e.to_string())?;
|
|
println!(
|
|
"Wrote {}. Start the server with: horizon-server run --dir {}",
|
|
toml_path.display(),
|
|
dir.display()
|
|
);
|
|
if cfg.server.online_mode {
|
|
println!(
|
|
"Note: Horizon's server doesn't check Mojang accounts yet (online mode), so it runs offline: \
|
|
keep the whitelist on, or play on a LAN or a tailnet."
|
|
);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Adds players to the whitelist (and one as an operator) in vanilla's files, with offline UUIDs as the
|
|
/// server runs offline.
|
|
fn add_players(dir: &Path, players: &[String], op: Option<&str>) -> Result<(), String> {
|
|
if !players.is_empty() {
|
|
let mut white = WhiteList::new(dir.join("whitelist.json"));
|
|
let _ = white.load();
|
|
for name in players {
|
|
white.add(WhiteEntry {
|
|
user: NameAndId::offline(name),
|
|
});
|
|
}
|
|
println!("Whitelisted: {}", players.join(", "));
|
|
}
|
|
if let Some(name) = op {
|
|
let mut ops = OpList::new(dir.join("ops.json"));
|
|
let _ = ops.load();
|
|
ops.add(OpEntry {
|
|
user: NameAndId::offline(name),
|
|
level: 4,
|
|
bypasses_player_limit: false,
|
|
});
|
|
println!("Operator: {name}");
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn unattended_init_writes_everything() {
|
|
let dir = std::env::temp_dir().join(format!("horizon-server-init-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
let answers = Answers {
|
|
eula: true,
|
|
world: Some("survival".into()),
|
|
seed: Some("12345".into()),
|
|
port: Some(25599),
|
|
whitelist: Some(true),
|
|
players: vec!["Alex".into(), "Steve".into()],
|
|
op: Some("Alex".into()),
|
|
yes: true,
|
|
};
|
|
run(&dir, answers).unwrap();
|
|
let cfg = Config::load(&dir).unwrap();
|
|
assert_eq!(
|
|
(
|
|
cfg.world.name.as_str(),
|
|
cfg.world.seed.as_str(),
|
|
cfg.server.port
|
|
),
|
|
("survival", "12345", 25599)
|
|
);
|
|
assert!(eula::agreed(&dir.join("eula.txt")).unwrap());
|
|
let white = std::fs::read_to_string(dir.join("whitelist.json")).unwrap();
|
|
assert!(
|
|
white.contains("\"Alex\"") && white.contains("\"Steve\""),
|
|
"{white}"
|
|
);
|
|
let ops = std::fs::read_to_string(dir.join("ops.json")).unwrap();
|
|
assert!(
|
|
ops.contains("\"Alex\"") && ops.contains("\"level\": 4"),
|
|
"{ops}"
|
|
);
|
|
// A second init refuses to overwrite the configuration.
|
|
assert!(run(&dir, Answers::default()).is_err());
|
|
std::fs::remove_dir_all(&dir).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn no_eula_no_server() {
|
|
let dir = std::env::temp_dir().join(format!("horizon-server-eula-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
let answers = Answers {
|
|
yes: true,
|
|
..Answers::default()
|
|
};
|
|
assert!(run(&dir, answers).unwrap_err().contains("EULA"));
|
|
assert!(!dir.join(config::FILE).exists());
|
|
assert!(!eula::agreed(&dir.join("eula.txt")).unwrap());
|
|
std::fs::remove_dir_all(&dir).unwrap();
|
|
}
|
|
}
|