-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
161 lines (146 loc) · 5.08 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
use std::{
collections::HashMap,
ffi::CString,
fs::{self, File},
io::{Read, Write},
path::{Path, PathBuf},
process::Command,
};
use anyhow::{Context, Result};
use clap::Clap;
use json;
use rand::{rngs::OsRng, RngCore};
use rocket::{config::Environment, response::NamedFile, Config, Data, State};
use zenroom::zencode_exec;
struct ZexecConfig {
logs_dir: String,
}
const BUFFER_LIMIT: u64 = 2 * 1024 * 1024; // 2 Mb
#[derive(Clap)]
#[clap(version = "0.1", author = "Danilo Spinella <oss@danyspin97.org>")]
struct Opts {
#[clap(short, long, default_value = "127.0.0.1")]
address: String,
#[clap(short, long, default_value = "/etc/zexec")]
contracts_dir: String,
#[clap(short, long, default_value = "/var/lib/zexec/logs")]
logs_dir: String,
#[clap(short, long, default_value = "9856")]
port: u16,
}
fn get_random_name() -> String {
let mut data = [0u8; 16];
OsRng.fill_bytes(&mut data);
data.iter().map(|byte| format!("{:x}", byte)).collect()
}
fn get_contracts(contract_dir: &str) -> Result<HashMap<String, (CString, CString)>> {
fs::read_dir(contract_dir)
.with_context(|| format!("unable to read directory {}", contract_dir))?
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<PathBuf>, _>>()?
.iter()
.filter(|path| {
if let Some(ext) = Path::new(path).extension() {
ext == "zen"
} else {
false
}
})
.map(|path| -> Result<(String, (CString, CString))> {
let contract = Path::new(path)
.with_extension("")
.file_name()
.with_context(|| format!("unable to get filename for file {:?}", path))?
.to_str()
.with_context(|| format!("unable to convert `{:?}` to String", path))?
.to_string();
Ok((
contract,
(
CString::new(
fs::read(path)
.with_context(|| format!("unable to read file {:?}", path))?,
)?,
CString::new(
fs::read(Path::new(path).with_extension("keys")).with_context(|| {
format!("unable to read path {:?}", path.with_extension("keys"))
})?,
)?,
),
))
})
.collect::<Result<HashMap<String, (CString, CString)>>>()
}
#[post("/contracts/<contract>", format = "json", data = "<msg>")]
fn contracts(
config: State<ZexecConfig>,
contracts: State<HashMap<String, (CString, CString)>>,
contract: String,
msg: Data,
) -> Result<Option<String>> {
if let Some((contract, keys)) = &contracts.get(&contract) {
let mut buf = String::new();
msg.open()
.take(BUFFER_LIMIT)
.read_to_string(&mut buf)
.context("unable to read from POST data")?;
let (res, success) = zencode_exec(
contract.clone(),
CString::new("")?,
CString::new(buf)?,
keys.clone(),
);
if success {
let filename = get_random_name();
let mut log =
File::create(Path::new(&config.logs_dir).join(&filename)).with_context(|| {
format!(
"unable to create file {:?}",
Path::new(&config.logs_dir).join(&filename)
)
})?;
let command = json::parse(&res.output)
.with_context(|| format!("unable to parse JSON '{}'", res.output))?["command"]
.as_str()
.with_context(|| "unable to find command in JSON value")?
.to_owned();
let output = Command::new(&command)
.output()
.with_context(|| format!("unable to execute command {}", command))?;
log.write(&output.stdout)
.with_context(|| "unable to write command output to log")?;
Ok(Some(filename))
} else {
Ok(Some(res.logs))
}
} else {
Ok(None)
}
}
#[get("/logs/<file..>")]
fn logs(file: PathBuf, config: State<ZexecConfig>) -> Option<NamedFile> {
NamedFile::open(Path::new(&config.logs_dir).join(file)).ok()
}
fn main() -> Result<()> {
let opts: Opts = Opts::parse();
let mut data = [0u8; 32];
OsRng.fill_bytes(&mut data);
let secret_key = base64::encode(data);
let config = Config::build(Environment::Production)
.address(opts.address)
.port(opts.port)
.secret_key(secret_key)
.finalize()?;
let logs_dir = opts.logs_dir;
std::fs::create_dir_all(&logs_dir)
.with_context(|| format!("unable to create logs directory {:?}", &logs_dir))?;
rocket::custom(config)
.mount("/", routes![contracts, logs])
.manage(get_contracts(&opts.contracts_dir)?)
.manage(ZexecConfig { logs_dir })
.launch();
Ok(())
}