Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swapped targets (hopefully works) #2

Merged
merged 3 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ jobs:
- name: Install i586-unknown-linux-gnu toolchain
run: rustup target add i586-unknown-linux-gnu
- name: Build for i586-unknown-linux-gnu
run: cargo build --verbose --release --target i586-unknown-linux-gnu
run: cargo build --verbose --release --target i586-unknown-linux-gnu --no-default-features --features minreq
- name: Copy i586-unknown-linux-gnu artifact
run: cp target/i586-unknown-linux-gnu/release/discord-backup-util target/github-release/discord-backup-util.i586-unknown-linux-gnu

- name: Install i586-unknown-linux-musl toolchain
run: rustup target add i586-unknown-linux-musl
- name: Build for i586-unknown-linux-musl
run: cargo build --verbose --release --target i586-unknown-linux-musl
- name: Copy i586-unknown-linux-musl artifact
run: cp target/i586-unknown-linux-musl/release/discord-backup-util target/github-release/discord-backup-util.i586-unknown-linux-musl
- name: Install i686-unknown-linux-gnu toolchain
run: rustup target add i686-unknown-linux-gnu
- name: Build for i686-unknown-linux-gnu
run: cargo build --verbose --release --target i686-unknown-linux-gnu
- name: Copy i686-unknown-linux-gnu artifact
run: cp target/i686-unknown-linux-gnu/release/discord-backup-util target/github-release/discord-backup-util.i686-unknown-linux-gnu

- uses: colathro/[email protected]
id: crate-version
Expand Down
89 changes: 88 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "discord-backup-util"
version = "0.1.1"
version = "0.2.0"
authors = ["buj"]
license = "AGPL3-or-later"
description = "A tiny tool to backup stuff to Discord"
Expand All @@ -11,9 +11,15 @@ keywords = ["cli", "discord"]
categories = ["command-line-utilities"]
edition = "2021"

[features]
default = ["ureq"]
minreq = ["dep:minreq"]
ureq = ["dep:ureq"]

[dependencies]
minreq = { version = "2.12.0", features = ["https-bundled-probe"], optional = true }
rand = "0.8.5"
serde = { version = "1.0.208", features = ["derive"] }
serde_json = "1.0.125"
ureq = "2.10.1"
ureq = { version = "2.10.1", optional = true }
zip = { version = "2.2.0", features = ["aes", "aes-crypto", "deflate", "deflate-zlib", "deflate64"], default-features = false }
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ A tool that backs up whatever you need to Discord.
- Setup a cron job/systemd service to start `discord-backup-util` on boot.
- Password-protect the artifacts (they are being uploaded to Discord of all places after all).
- Rethink your life choices of why are you backing up your infrastructure to Discord.

## Building for 32-bit platforms

We support building `discord-backup-util` down to i586, although build might fail due
to some C packages failing to compile.

If build fails due to dependencies, add `--no-default-features --features minreq` to command line
(This may take longer to compile as for `minreq` we use bundled OpenSSL instead of RusTLS) (Not all
targets can be fixed this way).
98 changes: 73 additions & 25 deletions src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ use std::{num::NonZeroU64, ops::Add, time::Duration};
use rand::Rng;
use serde::Deserialize;

use crate::log::Logger;

#[derive(Default)]
pub struct MessageBuilder(Message);
impl MessageBuilder {
pub fn content(mut self, content: impl Into<String>) -> Self {
self.0.content.replace(content.into());
self
}
pub fn content_maybe(mut self, content: Option<impl Into<String>>) -> Self {
self.0.content = content.map(|x| x.into());
self
}

pub fn file(mut self, name: impl Into<String>, file: Vec<u8>) -> Self {
self.0.files.push((name.into(), file));
self
Expand All @@ -28,7 +25,7 @@ pub struct Message {
pub files: Vec<(String, Vec<u8>)>,
}
impl Message {
pub fn edit(&mut self, hook: &Webhook, text: impl Into<String>) {
pub fn edit<L: Logger>(&mut self, hook: &Webhook, text: impl Into<String>, logger: &mut L) {
let text: String = text.into();

let Some(id) = self.id else {
Expand All @@ -38,24 +35,43 @@ impl Message {
let body = format!("{{\"content\":{text:?}}}");
self.content.replace(text);

#[allow(clippy::blocks_in_conditions)]
loop {
if ureq::patch(&format!("{}/messages/{id}", hook.0))
.set("Content-Type", "application/json")
.set("Content-Length", &body.len().to_string())
.send_string(&body)
.is_ok()
{
if {
#[cfg(feature = "minreq")]
{
let body = body.clone();
minreq::patch(format!("{}/messages/{id}", hook.0))
.with_header("Content-Type", "application/json")
.with_header("Content-Length", body.len().to_string())
.with_body(body)
.send()
.is_ok()
}
#[cfg(feature = "ureq")]
{
ureq::patch(&format!("{}/messages/{id}", hook.0))
.set("Content-Type", "application/json")
.set("Content-Length", &body.len().to_string())
.send_string(&body)
.is_ok()
}
} {
break;
}

logger.info("Failed to edit message, retrying in 10 seconds..");
std::thread::sleep(Duration::from_secs(10));
}
}

pub fn reply(
pub fn reply<L: Logger>(
&self,
hook: &Webhook,
message: impl Fn(MessageBuilder) -> MessageBuilder,
logger: &mut L,
) -> Message {
hook.send(message)
hook.send(message, logger)
}
}

Expand All @@ -74,7 +90,11 @@ impl Webhook {
/// Send a message.
///
/// Will try indefinitely until success.
pub fn send(&self, message: impl Fn(MessageBuilder) -> MessageBuilder) -> Message {
pub fn send<L: Logger>(
&self,
message: impl Fn(MessageBuilder) -> MessageBuilder,
logger: &mut L,
) -> Message {
let mut message: Message = message(Default::default()).0;

let mut bodies: Vec<Vec<u8>> = vec![];
Expand Down Expand Up @@ -127,17 +147,43 @@ impl Webhook {
body[ptr..ptr + header.len()].copy_from_slice(&header);
}

#[allow(clippy::blocks_in_conditions)]
loop {
match ureq::post(&format!("{}?wait=true", self.0))
.set(
"Content-Type",
&format!("multipart/form-data; boundary={boundary}"),
)
.set("Content-Length", &body.len().to_string())
.send_bytes(&body)
{
match {
#[cfg(feature = "minreq")]
{
let body = body.clone();
minreq::post(format!("{}?wait=true", self.0))
.with_header(
"Content-Type",
format!("multipart/form-data; boundary={boundary}"),
)
.with_header("Content-Length", body.len().to_string())
.with_body(body)
.send()
}
#[cfg(feature = "ureq")]
{
ureq::post(&format!("{}?wait=true", self.0))
.set(
"Content-Type",
&format!("multipart/form-data; boundary={boundary}"),
)
.set("Content-Length", &body.len().to_string())
.send_bytes(&body)
}
} {
Ok(x) => {
let parsed: ApiMessage = match serde_json::from_reader(x.into_reader()) {
let parsed: ApiMessage = match serde_json::from_reader({
#[cfg(feature = "ureq")]
{
x.into_reader()
}
#[cfg(feature = "minreq")]
{
std::io::Cursor::new(x.as_bytes())
}
}) {
Ok(x) => x,
Err(why) => {
println!("Failed to parse message, retrying in 5 minutes...\n\n{why}");
Expand All @@ -151,7 +197,9 @@ impl Webhook {
break message;
}
Err(why) => {
println!("Error sending request: {why}, retrying in 1 minute...");
logger.error(&format!(
"Error sending request: {why}, retrying in 1 minute..."
));
std::thread::sleep(Duration::from_secs(60));
}
}
Expand Down
Loading