bwbox/lib/sandbox.nim

50 lines
1.2 KiB
Nim
Raw Normal View History

2021-05-18 22:10:35 +02:00
import os
2021-06-19 16:33:47 +02:00
import args
import utils
import modes
2021-05-18 22:10:35 +02:00
import bwrap
import config
import options
2021-06-19 16:33:47 +02:00
proc sandboxExec*(mode: Modes, args: Args) =
var call = BwrapCall()
var userConfig = none(Config)
2021-05-18 22:10:35 +02:00
2021-06-19 16:33:47 +02:00
let hostname = args.name.get("sandbox")
let profilePath = getProfilePath(args, mode)
2021-05-18 22:10:35 +02:00
2021-06-19 16:33:47 +02:00
if args.name.isSome:
let name = args.name.unsafeGet
let sandboxPath = getSandboxPath(name)
let sandboxFiles = sandboxPath.joinPath("files")
let configPath = sandboxPath.joinPath("config.json")
2021-05-18 22:10:35 +02:00
2021-06-19 16:33:47 +02:00
if fileExists(configPath):
userConfig = some(loadConfig(configPath))
2021-05-18 22:10:35 +02:00
2021-06-19 16:33:47 +02:00
createDir(sandboxFiles)
call.addArg("--bind", sandboxFiles, getHomeDir())
2021-05-18 22:10:35 +02:00
2021-06-19 16:33:47 +02:00
var profile = loadConfig(profilePath)
profile.extendConfig()
2021-05-18 22:10:35 +02:00
call
2021-06-19 16:33:47 +02:00
.addMount("--dev-bind", "/dev/null")
.addArg("--tmpfs", "/tmp")
2021-05-18 22:10:35 +02:00
.addArg("--proc", "/proc")
.addArg("--unshare-all")
.addArg("--share-net")
.addArg("--die-with-parent")
2021-06-19 16:33:47 +02:00
.applyConfig(profile)
2021-05-18 22:10:35 +02:00
if mode == Modes.Shell:
call
.addMount("--bind", getCurrentDir())
.addArg("--chdir", getCurrentDir())
2021-06-19 16:33:47 +02:00
.addArg("--hostname", hostname)
2021-06-19 16:33:47 +02:00
if userConfig.isSome:
call.applyConfig(userConfig.unsafeGet)
2021-05-18 22:10:35 +02:00
2021-06-19 16:33:47 +02:00
call.addArg(args.getCmd).exec()