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-01-27 13:46:55 +00:00
|
|
|
use std::collections::HashMap;
|
2021-12-13 19:23:25 +00:00
|
|
|
#[allow(unused)]
|
2021-12-20 09:52:09 +00:00
|
|
|
use std::io::{self, BufReader, Write};
|
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
|
|
|
|
2021-12-13 19:23:25 +00:00
|
|
|
#[allow(unused)]
|
2022-01-07 14:20:17 +00:00
|
|
|
fn publicise() {}
|
|
|
|
|
|
|
|
#[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")]
|
|
|
|
prop: Option<Prop>,
|
|
|
|
#[yaserde(prefix = "d")]
|
|
|
|
status: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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")]
|
|
|
|
get_last_modified: Option<String>,
|
|
|
|
#[yaserde(prefix = "oc")]
|
|
|
|
permissions: Option<String>,
|
|
|
|
#[yaserde(prefix = "d", rename = "resourcetype")]
|
|
|
|
resource_type: Option<ResourceType>,
|
|
|
|
#[yaserde(prefix = "d", rename = "getetag")]
|
|
|
|
get_etag: Option<String>,
|
|
|
|
#[yaserde(prefix = "d", rename = "getcontentlength")]
|
|
|
|
get_content_length: Option<u32>,
|
|
|
|
#[yaserde(prefix = "d", rename = "getcontenttype")]
|
|
|
|
get_content_type: Option<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")]
|
|
|
|
collection: Option<String>,
|
|
|
|
}
|
|
|
|
|
2022-01-27 13:46:55 +00:00
|
|
|
async fn get_folder_contents(url_tail: &String) -> Result<String, Box<dyn std::error::Error>> {
|
2021-12-16 03:59:53 +00:00
|
|
|
debug!("Entering: get_folder_contents()");
|
2022-01-17 00:24:39 +00:00
|
|
|
let password = get_password().unwrap().trim().to_string();
|
|
|
|
debug!("Received password: {}[END]", password);
|
2021-12-13 06:20:45 +00:00
|
|
|
let root_url = "https://theadamcooper.com";
|
|
|
|
let method = reqwest::Method::from_bytes(b"PROPFIND").unwrap();
|
|
|
|
let client = reqwest::Client::new();
|
2021-12-16 03:59:53 +00:00
|
|
|
let url = format!("{}{}", root_url, url_tail);
|
|
|
|
debug!("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?;
|
2021-12-16 13:53:25 +00:00
|
|
|
debug!("{:?}", response_text);
|
|
|
|
Ok(response_text)
|
2021-12-13 06:20:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 13:58:15 +00:00
|
|
|
#[tokio::main]
|
2022-02-15 14:31:15 +00:00
|
|
|
async fn traverse(mut result: Multistatus) {
|
2022-01-17 00:24:39 +00:00
|
|
|
debug!("Entering: traverse()");
|
|
|
|
// 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
|
|
|
let mut already_visited: bool;
|
|
|
|
// Initialize the hashmap of visited items (by etag?)
|
|
|
|
let mut visited_items = HashMap::new();
|
2022-02-16 14:17:43 +00:00
|
|
|
/*
|
|
|
|
match result.response[0].propstat[0].prop {
|
|
|
|
// match result.response.get(0).unwrap().propstat.get(0).unwrap().prop {
|
2022-02-14 02:01:33 +00:00
|
|
|
Some(prop) => match prop.get_etag {
|
2022-01-27 13:46:55 +00:00
|
|
|
Some(etag) => {
|
|
|
|
visited_items.insert(etag, true);
|
|
|
|
()
|
2022-01-17 00:24:39 +00:00
|
|
|
}
|
2022-01-27 13:46:55 +00:00
|
|
|
_ => current_index += 1,
|
2022-01-17 00:24:39 +00:00
|
|
|
},
|
2022-01-27 13:46:55 +00:00
|
|
|
_ => current_index += 1,
|
2022-01-17 00:24:39 +00:00
|
|
|
}
|
2022-02-16 14:17:43 +00:00
|
|
|
*/
|
|
|
|
if let Some(prop) = &result.response[0].propstat[0].prop {
|
|
|
|
if let Some(etag) = &prop.get_etag {
|
|
|
|
visited_items.insert(etag, true);
|
|
|
|
} else {
|
|
|
|
current_index += 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
current_index += 1;
|
|
|
|
}
|
|
|
|
|
2022-01-17 00:24:39 +00:00
|
|
|
// Depth first traversal
|
2022-02-16 14:17:43 +00:00
|
|
|
while current_index < &result.response.len() {
|
2022-02-14 02:01:33 +00:00
|
|
|
match &result.response[current_index].propstat[0].prop {
|
2022-01-17 00:24:39 +00:00
|
|
|
Some(prop) => {
|
2022-02-15 14:31:15 +00:00
|
|
|
match prop.get_etag {
|
2022-01-17 00:24:39 +00:00
|
|
|
Some(etag) => {
|
2022-02-14 02:01:33 +00:00
|
|
|
// If result.response[current_index] has not been visited
|
2022-02-15 14:31:15 +00:00
|
|
|
if !visited_items.contains_key(&etag) {
|
2022-01-27 13:46:55 +00:00
|
|
|
// If item is a collection
|
2022-02-15 14:31:15 +00:00
|
|
|
match prop.resource_type {
|
2022-01-27 13:46:55 +00:00
|
|
|
Some(resource_type) => {
|
|
|
|
if !resource_type.collection.is_empty() {
|
|
|
|
// Get the contents XML
|
|
|
|
let folder_contents: String = get_folder_contents(
|
2022-02-14 02:01:33 +00:00
|
|
|
&result.response[current_index].href,
|
2022-01-27 13:46:55 +00:00
|
|
|
)
|
2022-01-24 13:58:15 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-01-27 13:46:55 +00:00
|
|
|
debug!("{:?}", folder_contents);
|
|
|
|
// Parse the contents XML into Multistatus
|
2022-02-14 02:01:33 +00:00
|
|
|
let mut new_result: Multistatus = from_str(&String::from(folder_contents)).unwrap();
|
|
|
|
// Append the NextcloudResponse vector to result.response
|
2022-02-15 14:31:15 +00:00
|
|
|
result.response.append(&mut new_result.response);
|
2022-01-27 13:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => current_index += 1,
|
2022-01-24 13:58:15 +00:00
|
|
|
}
|
2022-01-17 00:24:39 +00:00
|
|
|
}
|
2022-01-27 13:46:55 +00:00
|
|
|
// else
|
|
|
|
// if item is not public, publicise it.
|
|
|
|
// Add item to visited items hashmap
|
|
|
|
// Increment current_index by 1
|
2022-01-17 00:24:39 +00:00
|
|
|
()
|
2022-01-27 13:46:55 +00:00
|
|
|
}
|
2022-01-17 00:24:39 +00:00
|
|
|
_ => println!("something's happening"),
|
|
|
|
}
|
2022-01-27 13:46:55 +00:00
|
|
|
}
|
2022-01-17 00:24:39 +00:00
|
|
|
_ => println!("something's happening"),
|
|
|
|
}
|
2022-01-21 13:53:35 +00:00
|
|
|
current_index += 1;
|
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-27 13:46:55 +00:00
|
|
|
// use yaserde::de::from_str;
|
2022-01-07 14:20:17 +00:00
|
|
|
|
|
|
|
env_logger::Builder::from_env(Env::default().default_filter_or("trace"))
|
|
|
|
.target(Target::Stdout)
|
|
|
|
.init();
|
2021-12-13 19:23:25 +00:00
|
|
|
println!("Publicise it!");
|
2021-12-16 03:59:53 +00:00
|
|
|
|
2022-01-17 00:24:39 +00:00
|
|
|
let folder_contents: String =
|
2022-01-24 13:58:15 +00:00
|
|
|
// get_folder_contents("/nextcloud/remote.php/dav/files/adam/test_public/2019_test_public")
|
2022-01-27 13:46:55 +00:00
|
|
|
get_folder_contents(&String::from("/nextcloud/remote.php/dav/files/adam/test_public"))
|
2022-01-07 14:20:17 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-12-16 03:59:53 +00:00
|
|
|
debug!("{:?}", folder_contents);
|
2021-12-20 09:52:09 +00:00
|
|
|
|
2022-01-27 13:46:55 +00:00
|
|
|
let mut result: Multistatus = from_str(&folder_contents).unwrap();
|
2022-01-07 14:20:17 +00:00
|
|
|
println!("{:?}", result);
|
2022-01-17 00:24:39 +00:00
|
|
|
|
2022-02-15 14:31:15 +00:00
|
|
|
traverse(result);
|
2021-12-16 03:59:53 +00:00
|
|
|
Ok(())
|
2021-12-11 20:55:06 +00:00
|
|
|
}
|