Skip to content

Commit

Permalink
Replace old upload route
Browse files Browse the repository at this point in the history
  • Loading branch information
jkcoxson committed Apr 21, 2022
1 parent 0fa1079 commit 52e7015
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
108 changes: 107 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
// jkcoxson

use backend::Backend;
use bytes::BufMut;
use futures::TryStreamExt;
use log::{info, warn};
use plist_plus::Plist;
use serde_json::Value;
use std::{net::SocketAddr, str::FromStr, sync::Arc};
use tokio::sync::Mutex;
use warp::{filters::BoxedFilter, http::Uri, path::FullPath, redirect, Filter, Rejection, Reply};
use warp::{
filters::BoxedFilter,
http::Uri,
multipart::{FormData, Part},
path::FullPath,
redirect, Filter, Rejection, Reply,
};

mod backend;
mod client;
Expand All @@ -24,6 +32,7 @@ async fn main() {
let static_dir = config.static_path.clone();
let current_dir = std::env::current_dir().expect("failed to read current directory");
let backend = Arc::new(Mutex::new(backend::Backend::load(&config)));
let upload_backend = backend.clone();
let potential_backend = backend.clone();
let potential_follow_up_backend = backend.clone();
let status_backend = backend.clone();
Expand All @@ -44,6 +53,13 @@ async fn main() {
))
});

// Upload route
let upload_route = warp::path("upload")
.and(warp::post())
.and(warp::multipart::form().max_length(5_000_000))
.and(warp::filters::addr::remote())
.and_then(move |form, addr| upload_file(form, addr, upload_backend.clone()));

// Potential route
let potential_route = warp::path("potential")
.and(warp::get())
Expand Down Expand Up @@ -84,6 +100,7 @@ async fn main() {
let routes = root_redirect()
.or(warp::fs::dir(current_dir.join(static_dir)))
.or(status_route)
.or(upload_route)
.or(potential_route)
.or(potential_follow_up_route)
.or(list_apps_route)
Expand Down Expand Up @@ -121,6 +138,95 @@ async fn version_route() -> Result<impl Reply, Rejection> {
Ok("0.1.2")
}

async fn upload_file(
form: FormData,
address: Option<SocketAddr>,
backend: Arc<Mutex<Backend>>,
) -> Result<impl Reply, Rejection> {
let lock = backend.lock().await;
let parts: Vec<Part> = match form.try_collect().await {
Ok(parts) => parts,
Err(_) => return Ok(packets::upload_response(false, "Form error")),
};

for p in parts {
if p.name() == "file" {
let value = match p
.stream()
.try_fold(Vec::new(), |mut vec, data| {
vec.put(data);
async move { Ok(vec) }
})
.await
{
Ok(value) => value,
Err(_) => return Ok(packets::upload_response(false, "File error")),
};

// Get string from value
let value = match String::from_utf8(value) {
Ok(value) => value,
Err(_) => {
return Ok(packets::upload_response(false, "Unable to read file"));
}
};
// Attempt to parse it as an Apple Plist
let plist: Plist = Plist::from_xml(value.clone()).unwrap();
let udid = match plist.dict_get_item("UDID") {
Ok(s) => match s.get_string_val() {
Ok(s) => s,
Err(_) => {
return Ok(packets::upload_response(
false,
"Unable to read UDID from Plist",
));
}
},
_ => {
return Ok(packets::upload_response(false, "Invalid pairing file!"));
}
};
let address = match address {
Some(address) => address,
None => {
return Ok(packets::upload_response(false, "No address provided"));
}
};
let plist: Plist = Plist::from_xml(value).unwrap();
// Save the plist to the plist storage directory
match lock.write_pairing_file(plist.to_string(), &udid) {
Ok(_) => {}
Err(_) => {
return Ok(packets::upload_response(
false,
"Unable to save pairing file",
));
}
}
drop(lock);
// Make sure that the client is valid before adding it to the backend
match backend::Backend::test_new_client(&address.ip().to_string(), &udid).await {
Ok(_) => {}
Err(_) => {
return Ok(packets::upload_response(
false,
"Device did not respond to pairing test",
));
}
}
let mut lock = backend.lock().await;
match lock.register_client(address.ip().to_string(), udid.clone()) {
Ok(_) => {}
Err(_) => {
return Ok(packets::upload_response(false, "Client already registered"));
}
}
return Ok(packets::upload_response(true, ""));
}
}
return Ok(packets::upload_response(false, "No file found"));
}

async fn potential_pair(
addr: Option<SocketAddr>,
backend: Arc<Mutex<Backend>>,
Expand Down
3 changes: 1 addition & 2 deletions src/packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ pub fn list_apps_response(
success: bool,
message: &str,
list: serde_json::Value,
mut prefered_list: serde_json::Value,
prefered_list: serde_json::Value,
) -> String {
let mut packet: serde_json::Value = serde_json::Value::Object(serde_json::Map::new());
prefered_list["More"] = serde_json::Value::String("List all apps".to_string());
packet["success"] = serde_json::Value::Bool(success);
packet["message"] = serde_json::Value::String(message.to_string());
packet["list"] = list;
Expand Down

0 comments on commit 52e7015

Please sign in to comment.