publicise-rs/src/main.rs

289 lines
9.8 KiB
Rust
Raw Normal View History

2022-03-18 13:42:23 +00:00
extern crate xdg;
2021-12-13 19:23:25 +00:00
#[allow(unused)]
2022-01-07 14:20:17 +00:00
use env_logger::{Env, Target};
2021-12-13 19:23:25 +00:00
#[allow(unused)]
2022-01-07 14:20:17 +00:00
use log::{debug, error, info, log_enabled, warn};
2021-12-13 06:20:45 +00:00
use reqwest;
2022-03-18 13:42:23 +00:00
use serde::Deserialize;
use std::collections::HashMap;
use std::fs;
2022-03-07 14:41:57 +00:00
use std::io::{self, Write};
#[allow(unused)]
use std::process::Command;
use std::str;
2022-03-18 13:42:23 +00:00
use toml;
2022-03-07 14:41:57 +00:00
use url::Url;
2021-12-13 19:23:25 +00:00
#[allow(unused)]
2022-01-07 14:20:17 +00:00
use yaserde_derive::{YaDeserialize, YaSerialize};
use yaserde::de::from_str;
2021-12-13 06:20:45 +00:00
#[derive(Debug, Deserialize)]
2022-03-18 13:42:23 +00:00
pub struct Config {
credentials: Credentials,
paths: NextcloudPaths,
}
#[derive(Debug, Deserialize)]
2022-03-18 13:42:23 +00:00
pub struct Credentials {
username: String,
password: Option<String>,
password_script: Option<String>,
2022-03-18 13:42:23 +00:00
}
#[derive(Debug, Deserialize)]
2022-03-18 13:42:23 +00:00
pub struct NextcloudPaths {
root: String,
2022-03-18 13:42:23 +00:00
target: String,
}
2022-01-07 14:20:17 +00:00
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(
rename = "multistatus",
prefix = "d",
namespace = "d: DAV:",
namespace = "nc: http://nextcloud.org/ns",
namespace = "oc: http://owncloud.org/ns",
namespace = "s: http://sabredav.org/ns"
2022-01-07 14:20:17 +00:00
)]
pub struct Multistatus {
#[yaserde(prefix = "d")]
response: Vec<NextcloudResponse>,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(rename = "response", prefix = "d", namespace = "d: DAV:")]
pub struct NextcloudResponse {
#[yaserde(prefix = "d")]
// href: Option<String>,
href: String,
2022-01-07 14:20:17 +00:00
#[yaserde(prefix = "d")]
propstat: Vec<Propstat>,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(rename = "propstat", prefix = "d", namespace = "d: DAV:")]
pub struct Propstat {
#[yaserde(prefix = "d")]
2022-02-20 06:25:34 +00:00
prop: Prop,
2022-01-07 14:20:17 +00:00
#[yaserde(prefix = "d")]
2022-02-20 06:25:34 +00:00
status: String,
2022-01-07 14:20:17 +00:00
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(
rename = "prop",
namespace = "d: DAV:",
namespace = "oc: http://owncloud.org/ns"
)]
2022-01-07 14:20:17 +00:00
pub struct Prop {
#[yaserde(prefix = "d", rename = "getlastmodified")]
2022-02-20 06:25:34 +00:00
get_last_modified: String,
2022-01-07 14:20:17 +00:00
#[yaserde(prefix = "oc")]
2022-02-20 06:25:34 +00:00
permissions: String,
2022-01-07 14:20:17 +00:00
#[yaserde(prefix = "d", rename = "resourcetype")]
2022-02-20 06:25:34 +00:00
resource_type: ResourceType,
2022-01-07 14:20:17 +00:00
#[yaserde(prefix = "d", rename = "getetag")]
2022-02-20 06:25:34 +00:00
get_etag: String,
2022-01-07 14:20:17 +00:00
#[yaserde(prefix = "d", rename = "getcontentlength")]
2022-02-20 06:25:34 +00:00
get_content_length: u32,
2022-01-07 14:20:17 +00:00
#[yaserde(prefix = "d", rename = "getcontenttype")]
2022-02-20 06:25:34 +00:00
get_content_type: String,
2022-01-08 21:51:20 +00:00
#[yaserde(prefix = "oc", rename = "share-types")]
share_types: Vec<ShareType>,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(rename = "share-types", namespace = "oc: http://owncloud.org/ns")]
pub struct ShareType {
#[yaserde(rename = "share-type", prefix = "oc")]
share_type: u8,
2021-12-13 06:20:45 +00:00
}
2022-01-07 14:20:17 +00:00
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(rename = "resourcetype", namespace = "d: DAV:")]
pub struct ResourceType {
#[yaserde(prefix = "d")]
collection: Vec<Collection>,
}
#[derive(Default, PartialEq, Debug, YaDeserialize)]
#[yaserde(rename = "collection", namespace = "d: DAV:")]
pub struct Collection {
#[yaserde(prefix = "d")]
2022-02-20 06:25:34 +00:00
collection: String,
2022-01-07 14:20:17 +00:00
}
2022-03-18 13:42:23 +00:00
fn init() -> Result<Config, Box<dyn std::error::Error>> {
let xdg_dirs = xdg::BaseDirectories::with_prefix("publicise-rs").unwrap();
let config_path = xdg_dirs.find_config_file("config.toml").unwrap();
let config_file_contents = fs::read_to_string(config_path).unwrap();
let config: Config = toml::from_str(&config_file_contents).unwrap();
return Ok(config);
2022-03-18 13:42:23 +00:00
}
#[allow(unused)]
async fn get_folder_contents(url_tail: &str, config: &Config) -> Result<String, Box<dyn std::error::Error>> {
debug!("[get_folder_contents] Entering function...");
let root_url = Url::parse(&config.paths.root)?;
2021-12-13 06:20:45 +00:00
let method = reqwest::Method::from_bytes(b"PROPFIND").unwrap();
let client = reqwest::Client::new();
let url = root_url.join(url_tail)?;
debug!("[get_folder_contents] url: {}", url);
2022-01-07 14:20:17 +00:00
let body = String::from(
r#"<?xml version="1.0" encoding="UTF-8"?>
2021-12-16 03:59:53 +00:00
<d:propfind xmlns:d="DAV:">
<d:prop xmlns:oc="http://owncloud.org/ns">
<d:getlastmodified/>
<d:getcontentlength/>
<d:getcontenttype/>
<oc:permissions/>
<d:resourcetype/>
<d:getetag/>
<oc:share-types/>
</d:prop>
2022-01-07 14:20:17 +00:00
</d:propfind>"#,
);
let response_text = client
.request(method, url)
.basic_auth("adam", config.credentials.password.as_ref())
2021-12-16 03:59:53 +00:00
.body(body)
2021-12-13 06:20:45 +00:00
.send()
2021-12-16 13:53:25 +00:00
.await?
.text()
2021-12-13 06:20:45 +00:00
.await?;
debug!("[get_folder_contents] response_text: {:?}", response_text);
2021-12-16 13:53:25 +00:00
Ok(response_text)
2021-12-13 06:20:45 +00:00
}
#[allow(unused)]
async fn publicise_it(path: &str, password: &str) -> Result<bool, Box<dyn std::error::Error>> {
debug!("[publicise_it] Entering function...");
let url = "https://cloud.theadamcooper.com/ocs/v2.php/apps/files_sharing/api/v1/shares";
let method = reqwest::Method::POST;
let client = reqwest::Client::new();
let params = [("path", path), ("shareType", "3")];
debug!("[publicise_it] url: {}", url);
debug!("[publicise_it] params: {:?}", params);
let response_text = client
.request(method, url)
.basic_auth("adam", Some(password))
.header("OCS-APIRequest", "true")
.form(&params)
.send()
.await?
.text()
.await?;
debug!("[publicise_it] response_text: {:?}", response_text);
Ok(true)
}
#[allow(unused)]
async fn traverse(mut result: Multistatus, config: &Config) -> Result<bool, Box<dyn std::error::Error>> {
debug!("[traverse] Entering function...");
// Initialize the indexed "pointer"
2022-01-21 13:53:35 +00:00
let mut current_index: usize = 0;
// Initialize the hashmap of visited items (by etag?)
let mut visited_items = HashMap::new();
2022-03-07 14:41:57 +00:00
let href_key = (&mut result.response[current_index].href).clone();
visited_items.insert(href_key, true);
2022-02-20 06:25:34 +00:00
current_index += 1;
// Depth first traversal
2022-02-16 14:30:46 +00:00
while current_index < (&mut result.response).len() {
debug!("[traverse] current_index: {:?}", current_index);
debug!("[traverse] current href: {}", &result.response[current_index].href);
2022-03-07 14:41:57 +00:00
let href_key = (&mut result.response[current_index].href).clone();
2022-02-20 06:25:34 +00:00
// If result.response[current_index] has not been visited
2022-03-07 14:41:57 +00:00
if !visited_items.contains_key(&href_key) {
debug!("[traverse] Fresh item...");
2022-02-26 23:51:01 +00:00
// if it's a collection
2022-02-20 06:25:34 +00:00
if !(&mut result.response[current_index].propstat[0].prop.resource_type.collection).is_empty() {
debug!("[traverse] Collection...");
2022-02-20 06:25:34 +00:00
// Get the contents XML
let folder_contents: String = get_folder_contents(
&result.response[current_index].href, // change to mutable borrow if necessary
config,
2022-02-20 06:25:34 +00:00
)
.await
.unwrap();
// Parse the contents XML into Multistatus
let mut new_result: Multistatus = from_str(&String::from(folder_contents)).unwrap();
debug!("[traverse] Parsed:\n{:?}", new_result);
2022-02-20 06:25:34 +00:00
// Append the NextcloudResponse vector to result.response
result.response.append(&mut new_result.response);
debug!("[traverse] new vector length: {}", &result.response.len());
2022-02-26 23:51:01 +00:00
} else {
debug!("[traverse] Node...");
2022-02-26 23:51:01 +00:00
// else it's a node. if it's not public, publicise it.
if !(&mut result.response[current_index].propstat[0].prop.share_types).contains(&ShareType{ share_type: 3 }) {
debug!("[traverse] it's not public");
// Search for username and lop.
let username = "adam";
let username_seg_string = format!("/{}/", username);
let username_seg = username_seg_string.as_str();
let index = &result.response[current_index].href.find(username_seg).unwrap_or(0);
let new_index = index + username.len() + 2;
let new_href = &result.response[current_index].href[new_index..];
publicise_it(new_href, config.credentials.password.as_ref().unwrap()).await.unwrap();
} else {
debug!("[traverse] it's already public");
2022-02-26 23:51:01 +00:00
}
}
2022-03-07 14:41:57 +00:00
visited_items.insert(href_key, true);
debug!("[traverse] visited items: {:?}", visited_items);
} else {
debug!("[traverse] Already-visited item.");
}
2022-02-20 06:25:34 +00:00
current_index += 1;
}
Ok(true)
}
#[allow(unused)]
2021-12-13 19:23:25 +00:00
fn get_password() -> Result<String, std::io::Error> {
2021-12-13 06:20:45 +00:00
print!("Nextcloud password: ");
io::stdout().flush().unwrap();
let mut buffer = String::new();
let stdin = io::stdin();
match stdin.read_line(&mut buffer) {
2021-12-13 19:23:25 +00:00
Ok(_) => Ok(buffer),
Err(error) => Err(error),
}
}
2022-01-07 14:20:17 +00:00
#[allow(unused)]
2021-12-20 09:52:09 +00:00
fn indent(size: usize) -> String {
const INDENT: &'static str = " ";
2022-01-07 14:20:17 +00:00
(0..size)
.map(|_| INDENT)
.fold(String::with_capacity(size * INDENT.len()), |r, s| r + s)
2021-12-20 09:52:09 +00:00
}
2021-12-16 03:59:53 +00:00
#[tokio::main]
2021-12-16 13:53:25 +00:00
async fn main() -> std::io::Result<()> {
2022-01-07 14:20:17 +00:00
2022-03-18 13:42:23 +00:00
env_logger::Builder::from_env(Env::default().default_filter_or("debug"))
2022-01-07 14:20:17 +00:00
.target(Target::Stdout)
.init();
println!("Publicise it!\n\n");
2021-12-16 03:59:53 +00:00
let mut config = init().unwrap();
if config.credentials.password == None {
let output = Command::new(&config.credentials.password_script.as_ref().unwrap()).output().unwrap();
config.credentials.password = Some(String::from_utf8(output.stdout).unwrap());
}
debug!("{:?}", &config);
let full_path = &(String::from("/remote.php/dav/files/") + &config.credentials.username + "/" + &config.paths.target);
let folder_contents: String =
get_folder_contents(full_path, &config)
2022-01-07 14:20:17 +00:00
.await
.unwrap();
2021-12-20 09:52:09 +00:00
2022-03-07 14:41:57 +00:00
let result: Multistatus = from_str(&folder_contents).unwrap();
debug!("{:?}", result);
let _ = traverse(result, &config).await.unwrap();
2021-12-16 03:59:53 +00:00
Ok(())
2021-12-11 20:55:06 +00:00
}