From 3505a2ef55fc89fb09d7a75849b6ce38c713973a Mon Sep 17 00:00:00 2001 From: Observer KRypt0n_ Date: Mon, 17 Apr 2023 15:27:00 +0200 Subject: [PATCH] feat: added `sandbox.args` and `sandbox.mounts.symlink` --- Cargo.toml | 2 +- src/config/schema_blanks/sandbox/mod.rs | 28 +++++++++++++++++++++- src/config/schema_blanks/sandbox/mounts.rs | 23 +++++++++++++++++- src/genshin/game.rs | 2 +- 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6e24997..9d1f26e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "anime-launcher-sdk" -version = "1.0.4" +version = "1.0.5" authors = ["Nikita Podvirnyy "] license = "GPL-3.0" readme = "README.md" diff --git a/src/config/schema_blanks/sandbox/mod.rs b/src/config/schema_blanks/sandbox/mod.rs index d80e4af..0990d17 100644 --- a/src/config/schema_blanks/sandbox/mod.rs +++ b/src/config/schema_blanks/sandbox/mod.rs @@ -16,6 +16,9 @@ pub struct Sandbox { /// Spoof original hostname. Default is `None` pub hostname: Option, + /// Append additional bwrap arguments. Default is `None` + pub args: Option, + /// List of paths to which tmpfs will be mounted. Default is empty pub private: Vec, @@ -30,6 +33,7 @@ impl Default for Sandbox { enabled: false, isolate_home: true, hostname: None, + args: None, private: vec![], mounts: Mounts::default() } @@ -65,6 +69,20 @@ impl From<&JsonValue> for Sandbox { None => default.hostname }, + args: match value.get("args") { + Some(value) => { + if value.is_null() { + None + } else { + match value.as_str() { + Some(value) => Some(value.to_string()), + None => default.args + } + } + }, + None => default.args + }, + private: match value.get("private") { Some(value) => match value.as_array() { Some(values) => { @@ -108,6 +126,7 @@ impl Sandbox { /// | `game_dir` | `/tmp/sandbox/game` | bind | false | /// | | | read-only bind | true | /// | | | bind | true | + /// | | | symlink | true | pub fn get_command(&self, wine_dir: impl AsRef, prefix_dir: impl AsRef, game_dir: impl AsRef) -> String { let mut command = String::from("bwrap --ro-bind / /"); @@ -142,16 +161,23 @@ impl Sandbox { command += &format!(" --bind '{}' '{}'", from.trim(), to.trim()); } + for (from, to) in &self.mounts.symlinks { + command += &format!(" --symlink '{}' '{}'", from.trim(), to.trim()); + } + command += &format!(" --bind '{}' /tmp/sandbox/wine", wine_dir.as_ref()); command += &format!(" --bind '{}' /tmp/sandbox/prefix", prefix_dir.as_ref()); command += &format!(" --bind '{}' /tmp/sandbox/game", game_dir.as_ref()); - command.push_str(" --chdir /"); command.push_str(" --die-with-parent"); command.push_str(" --unshare-all"); command.push_str(" --share-net"); + if let Some(args) = &self.args { + command.push_str(args.trim()); + } + command } } diff --git a/src/config/schema_blanks/sandbox/mounts.rs b/src/config/schema_blanks/sandbox/mounts.rs index b9986ce..b74c6b3 100644 --- a/src/config/schema_blanks/sandbox/mounts.rs +++ b/src/config/schema_blanks/sandbox/mounts.rs @@ -9,7 +9,10 @@ pub struct Mounts { pub read_only: HashMap, /// Bind original directory into the sandbox with writing access - pub bind: HashMap + pub bind: HashMap, + + /// Symlink original files into sandbox with writing access + pub symlinks: HashMap } impl From<&JsonValue> for Mounts { @@ -51,6 +54,24 @@ impl From<&JsonValue> for Mounts { None => default.bind }, None => default.bind + }, + + symlinks: match value.get("symlinks") { + Some(value) => match value.as_object() { + Some(values) => { + let mut vars = HashMap::new(); + + for (name, value) in values { + if let Some(value) = value.as_str() { + vars.insert(name.clone(), value.to_string()); + } + } + + vars + }, + None => default.symlinks + }, + None => default.symlinks } } } diff --git a/src/genshin/game.rs b/src/genshin/game.rs index 45356ef..8fc89e7 100644 --- a/src/genshin/game.rs +++ b/src/genshin/game.rs @@ -185,7 +185,7 @@ pub fn run() -> anyhow::Result<()> { .replace(folders.game.to_str().unwrap(), sandboxed_folders.game.to_str().unwrap()) .replace(folders.temp.to_str().unwrap(), sandboxed_folders.temp.to_str().unwrap()); - bash_command = format!("{bwrap} -- {bash_command}"); + bash_command = format!("{bwrap} --chdir /tmp/sandbox/game -- {bash_command}"); folders = sandboxed_folders; }