summaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/crypto.rs18
-rw-r--r--src/bin/main.rs127
2 files changed, 145 insertions, 0 deletions
diff --git a/src/bin/crypto.rs b/src/bin/crypto.rs
new file mode 100644
index 0000000..e059663
--- /dev/null
+++ b/src/bin/crypto.rs
@@ -0,0 +1,18 @@
+extern crate crypto;
+
+use crypto::bcrypt_pbkdf::bcrypt_pbkdf;
+
+pub fn encode(pw: &str) -> Vec<u8> {
+ let salt = "hello";
+ let mut out = vec!(0; 32);
+ let encrypted = bcrypt_pbkdf(
+ pw.as_bytes(), salt.as_bytes(),
+ 100, &mut out);
+ out
+}
+
+pub fn main() {
+ let pw = "123";
+ let out = encode(pw);
+ println!("{}: {:?}", pw, out);
+}
diff --git a/src/bin/main.rs b/src/bin/main.rs
new file mode 100644
index 0000000..36463a6
--- /dev/null
+++ b/src/bin/main.rs
@@ -0,0 +1,127 @@
+// CSRF protection
+// https://github.com/heartsucker/iron-csrf
+#[macro_use]
+extern crate iron;
+#[macro_use]
+extern crate log;
+#[macro_use]
+extern crate router;
+extern crate env_logger;
+extern crate systemhttp;
+extern crate iron_sessionstorage;
+extern crate staticfile;
+extern crate regex;
+
+use iron_sessionstorage::traits::*;
+use iron_sessionstorage::SessionStorage;
+use iron_sessionstorage::backends::SignedCookieBackend;
+use iron::status;
+use iron::modifiers::Header;
+use iron::headers::ContentType;
+use iron::{Iron, Request, IronResult, Response, Chain};
+use router::Router;
+use systemhttp::systemd::unit;
+use systemhttp::systemd::journal;
+use systemhttp::render;
+use staticfile::Static;
+use regex::Regex;
+
+struct Aaa(String);
+
+impl iron_sessionstorage::Value for Aaa {
+ fn get_key() -> &'static str {
+ "aaa"
+ }
+ fn into_raw(self) -> String {
+ self.0
+ }
+ fn from_raw(v: String) -> Option<Self> {
+ Some(Aaa(v))
+ }
+}
+
+fn overview(r: &mut Request) -> IronResult<Response> {
+ let mut _value = match try!(r.session().get::<Aaa>()) {
+ Some(aaa) => aaa,
+ None => Aaa("".to_owned()),
+ };
+
+ let name = r.extensions
+ .get::<Router>()
+ .unwrap()
+ .find("name")
+ .unwrap_or("World").to_owned();
+
+ let units = unit::get_units("*").unwrap();
+
+ let sections = ["service", "timer", "socket", "target", "slice", "mount",
+ "path"];
+ let units_by_section = sections.iter().map(|&s| {
+ (s.to_owned(),
+ units.iter().filter(|&u| &u.type_ == s).collect::<Vec<&unit::Unit>>())
+ }).collect::<Vec<_>>();
+
+ // let res = Ok(Response::with((status::Ok,
+ // Header(ContentType::html()),
+ // render_message(&format!("Hello, {} ({})",
+ // name,
+ // session_value.0),
+ // &units))));
+
+ // info!("Updating session value. Current value: {}", session_value.0);
+ // session_value.0.push('a');
+ // try!(r.session().set(session_value));
+ Ok(Response::with((status::Ok,
+ Header(ContentType::html()),
+ render::system_status(&units_by_section))))
+}
+
+fn journal(r: &mut Request) -> IronResult<Response> {
+ let unit = iexpect!(r.extensions
+ .get::<Router>()
+ .unwrap()
+ .find("unit"), status::BadRequest);
+ let re = Regex::new(r"[-_\w\d]*").unwrap();
+ if !re.is_match(unit) {
+ return Ok(Response::with(
+ (status::BadRequest,
+ format!("Unit ({}) does not match {}",
+ unit, re))));
+ }
+ Ok(Response::with((status::Ok,
+ itry!(journal::get_log(unit, 100)))))
+}
+
+fn unit_status(r: &mut Request) -> IronResult<Response> {
+ let unit_name = iexpect!(r.extensions
+ .get::<Router>()
+ .unwrap()
+ .find("unit"), status::BadRequest);
+ let re = Regex::new(r"[-_\w\d]*").unwrap();
+ if !re.is_match(unit_name) {
+ return Ok(Response::with(
+ (status::BadRequest,
+ format!("Unit ({}) does not match {}",
+ unit_name, re))));
+ }
+ let ref unit = itry!(unit::get_units(unit_name))[0];
+ let log = itry!(journal::get_log(unit_name, 15));
+ Ok(Response::with((status::Ok,
+ Header(ContentType::html()),
+ render::unit_status(&unit, &log))))
+}
+
+fn main() {
+ env_logger::init().unwrap();
+ let secret = b"secret2".to_vec();
+ let router = router!(
+ root: get "/" => overview,
+ details: get "/status/:unit" => unit_status,
+ journal: get "/journal/:unit" => journal,
+ css: get "/static/main.css" => Static::new(""),
+ );
+ let mut chain = Chain::new(router);
+ chain.link_around(SessionStorage::new(SignedCookieBackend::new(secret)));
+ let _server = Iron::new(chain).http(":::8080").unwrap();
+ println!("On 8080");
+}