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;
|
2022-01-27 13:46:55 +00:00
|
|
|
use std::collections::HashMap;
|
2022-03-07 14:41:57 +00:00
|
|
|
use std::io::{self, Write};
|
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};
|
2022-01-27 13:46:55 +00:00
|
|
|
use yaserde::de::from_str;
|
2021-12-13 06:20:45 +00:00
|
|
|
|
2022-03-18 13:42:23 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Config {
|
|
|
|
credentials: Credentials,
|
|
|
|
paths: NextcloudPaths,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Credentials {
|
|
|
|
username: String,
|
|
|
|
password: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct NextcloudPaths {
|
|
|
|
root: Url,
|
|
|
|
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",
|
2022-01-27 13:46:55 +00:00
|
|
|
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")]
|
2022-01-27 13:46:55 +00:00
|
|
|
// 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)]
|
2022-01-27 13:46:55 +00:00
|
|
|
#[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_file = xdg_dirs.find_config_file("config.toml").unwrap();
|
|
|
|
return Error{};
|
|
|
|
}
|
|
|
|
|
2022-03-06 23:45:16 +00:00
|
|
|
async fn get_folder_contents(url_tail: &str, password: &str) -> Result<String, Box<dyn std::error::Error>> {
|
2022-03-07 08:38:27 +00:00
|
|
|
debug!("[get_folder_contents] Entering function...");
|
2022-03-10 19:55:28 +00:00
|
|
|
let root_url = Url::parse("https://cloud.theadamcooper.com")?;
|
2021-12-13 06:20:45 +00:00
|
|
|
let method = reqwest::Method::from_bytes(b"PROPFIND").unwrap();
|
|
|
|
let client = reqwest::Client::new();
|
2022-03-06 23:45:16 +00:00
|
|
|
let url = root_url.join(url_tail)?;
|
2022-03-07 08:38:27 +00:00
|
|
|
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)
|
2021-12-13 06:20:45 +00:00
|
|
|
.basic_auth("adam", Some(password))
|
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?;
|
2022-03-07 08:38:27 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-06 23:45:16 +00:00
|
|
|
async fn publicise_it(path: &str, password: &str) -> Result<bool, Box<dyn std::error::Error>> {
|
2022-03-07 08:38:27 +00:00
|
|
|
debug!("[publicise_it] Entering function...");
|
2022-03-10 19:55:28 +00:00
|
|
|
let url = "https://cloud.theadamcooper.com/ocs/v2.php/apps/files_sharing/api/v1/shares";
|
2022-02-27 08:13:49 +00:00
|
|
|
let method = reqwest::Method::POST;
|
|
|
|
let client = reqwest::Client::new();
|
2022-03-06 23:45:16 +00:00
|
|
|
let params = [("path", path), ("shareType", "3")];
|
2022-03-07 08:38:27 +00:00
|
|
|
debug!("[publicise_it] url: {}", url);
|
|
|
|
debug!("[publicise_it] params: {:?}", params);
|
2022-02-27 08:13:49 +00:00
|
|
|
let response_text = client
|
|
|
|
.request(method, url)
|
|
|
|
.basic_auth("adam", Some(password))
|
|
|
|
.header("OCS-APIRequest", "true")
|
|
|
|
.form(¶ms)
|
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.text()
|
|
|
|
.await?;
|
2022-03-07 08:38:27 +00:00
|
|
|
debug!("[publicise_it] response_text: {:?}", response_text);
|
2022-02-27 08:13:49 +00:00
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
2022-03-06 23:45:16 +00:00
|
|
|
async fn traverse(mut result: Multistatus, password: &str) -> Result<bool, Box<dyn std::error::Error>> {
|
2022-03-07 08:38:27 +00:00
|
|
|
debug!("[traverse] Entering function...");
|
2022-01-17 00:24:39 +00:00
|
|
|
// Initialize the indexed "pointer"
|
2022-01-21 13:53:35 +00:00
|
|
|
let mut current_index: usize = 0;
|
2022-01-17 00:24:39 +00:00
|
|
|
// 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;
|
2022-02-16 14:17:43 +00:00
|
|
|
|
2022-01-17 00:24:39 +00:00
|
|
|
// Depth first traversal
|
2022-02-16 14:30:46 +00:00
|
|
|
while current_index < (&mut result.response).len() {
|
2022-03-07 08:38:27 +00:00
|
|
|
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) {
|
2022-03-07 08:38:27 +00:00
|
|
|
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() {
|
2022-03-07 08:38:27 +00:00
|
|
|
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
|
2022-02-27 08:13:49 +00:00
|
|
|
password,
|
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();
|
2022-03-07 08:38:27 +00:00
|
|
|
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);
|
2022-03-07 08:38:27 +00:00
|
|
|
debug!("[traverse] new vector length: {}", &result.response.len());
|
2022-02-26 23:51:01 +00:00
|
|
|
} else {
|
2022-03-07 08:38:27 +00:00
|
|
|
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 }) {
|
2022-03-07 08:38:27 +00:00
|
|
|
debug!("[traverse] it's not public");
|
2022-03-06 23:45:16 +00:00
|
|
|
// 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, password).await.unwrap();
|
|
|
|
} else {
|
2022-03-07 08:38:27 +00:00
|
|
|
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);
|
2022-03-07 08:38:27 +00:00
|
|
|
debug!("[traverse] visited items: {:?}", visited_items);
|
|
|
|
} else {
|
|
|
|
debug!("[traverse] Already-visited item.");
|
2022-01-17 00:24:39 +00:00
|
|
|
}
|
2022-02-20 06:25:34 +00:00
|
|
|
current_index += 1;
|
2022-01-17 00:24:39 +00:00
|
|
|
}
|
2022-02-28 04:14:28 +00:00
|
|
|
Ok(true)
|
2022-01-17 00:24:39 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2022-03-06 23:45:16 +00:00
|
|
|
println!("Publicise it!\n\n");
|
2021-12-16 03:59:53 +00:00
|
|
|
|
2022-03-18 13:42:23 +00:00
|
|
|
let config = init().unwrap();
|
2022-02-27 08:13:49 +00:00
|
|
|
let password: String = get_password().unwrap().trim().to_string();
|
2022-01-17 00:24:39 +00:00
|
|
|
let folder_contents: String =
|
2022-03-10 19:55:28 +00:00
|
|
|
get_folder_contents("/remote.php/dav/files/adam/test_public", &password)
|
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();
|
2022-03-07 08:38:27 +00:00
|
|
|
debug!("{:?}", result);
|
2022-01-17 00:24:39 +00:00
|
|
|
|
2022-03-06 23:45:16 +00:00
|
|
|
let _ = traverse(result, &password).await.unwrap();
|
2021-12-16 03:59:53 +00:00
|
|
|
Ok(())
|
2021-12-11 20:55:06 +00:00
|
|
|
}
|