feat: added sandbox.args and sandbox.mounts.symlink

This commit is contained in:
Observer KRypt0n_ 2023-04-17 15:27:00 +02:00
parent 5d4e541db4
commit 3505a2ef55
No known key found for this signature in database
GPG key ID: 844DA47BA25FE1E2
4 changed files with 51 additions and 4 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "anime-launcher-sdk" name = "anime-launcher-sdk"
version = "1.0.4" version = "1.0.5"
authors = ["Nikita Podvirnyy <suimin.tu.mu.ga.mi@gmail.com>"] authors = ["Nikita Podvirnyy <suimin.tu.mu.ga.mi@gmail.com>"]
license = "GPL-3.0" license = "GPL-3.0"
readme = "README.md" readme = "README.md"

View file

@ -16,6 +16,9 @@ pub struct Sandbox {
/// Spoof original hostname. Default is `None` /// Spoof original hostname. Default is `None`
pub hostname: Option<String>, pub hostname: Option<String>,
/// Append additional bwrap arguments. Default is `None`
pub args: Option<String>,
/// List of paths to which tmpfs will be mounted. Default is empty /// List of paths to which tmpfs will be mounted. Default is empty
pub private: Vec<String>, pub private: Vec<String>,
@ -30,6 +33,7 @@ impl Default for Sandbox {
enabled: false, enabled: false,
isolate_home: true, isolate_home: true,
hostname: None, hostname: None,
args: None,
private: vec![], private: vec![],
mounts: Mounts::default() mounts: Mounts::default()
} }
@ -65,6 +69,20 @@ impl From<&JsonValue> for Sandbox {
None => default.hostname 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") { private: match value.get("private") {
Some(value) => match value.as_array() { Some(value) => match value.as_array() {
Some(values) => { Some(values) => {
@ -108,6 +126,7 @@ impl Sandbox {
/// | `game_dir` | `/tmp/sandbox/game` | bind | false | /// | `game_dir` | `/tmp/sandbox/game` | bind | false |
/// | <mounts/read_only> | <mounts/read_only> | read-only bind | true | /// | <mounts/read_only> | <mounts/read_only> | read-only bind | true |
/// | <mounts/binds> | <mounts/binds> | bind | true | /// | <mounts/binds> | <mounts/binds> | bind | true |
/// | <mounts/symlinks> | <mounts/symlinks> | symlink | true |
pub fn get_command(&self, wine_dir: impl AsRef<str>, prefix_dir: impl AsRef<str>, game_dir: impl AsRef<str>) -> String { pub fn get_command(&self, wine_dir: impl AsRef<str>, prefix_dir: impl AsRef<str>, game_dir: impl AsRef<str>) -> String {
let mut command = String::from("bwrap --ro-bind / /"); let mut command = String::from("bwrap --ro-bind / /");
@ -142,16 +161,23 @@ impl Sandbox {
command += &format!(" --bind '{}' '{}'", from.trim(), to.trim()); 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/wine", wine_dir.as_ref());
command += &format!(" --bind '{}' /tmp/sandbox/prefix", prefix_dir.as_ref()); command += &format!(" --bind '{}' /tmp/sandbox/prefix", prefix_dir.as_ref());
command += &format!(" --bind '{}' /tmp/sandbox/game", game_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(" --die-with-parent");
command.push_str(" --unshare-all"); command.push_str(" --unshare-all");
command.push_str(" --share-net"); command.push_str(" --share-net");
if let Some(args) = &self.args {
command.push_str(args.trim());
}
command command
} }
} }

View file

@ -9,7 +9,10 @@ pub struct Mounts {
pub read_only: HashMap<String, String>, pub read_only: HashMap<String, String>,
/// Bind original directory into the sandbox with writing access /// Bind original directory into the sandbox with writing access
pub bind: HashMap<String, String> pub bind: HashMap<String, String>,
/// Symlink original files into sandbox with writing access
pub symlinks: HashMap<String, String>
} }
impl From<&JsonValue> for Mounts { impl From<&JsonValue> for Mounts {
@ -51,6 +54,24 @@ impl From<&JsonValue> for Mounts {
None => default.bind None => default.bind
}, },
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
} }
} }
} }

View file

@ -185,7 +185,7 @@ pub fn run() -> anyhow::Result<()> {
.replace(folders.game.to_str().unwrap(), sandboxed_folders.game.to_str().unwrap()) .replace(folders.game.to_str().unwrap(), sandboxed_folders.game.to_str().unwrap())
.replace(folders.temp.to_str().unwrap(), sandboxed_folders.temp.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; folders = sandboxed_folders;
} }