aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 287f43fa4e2b17b2b105aa249db0753ff09ced40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::{
    env::var,
    fs::File,
    io::Cursor,
    time::{Duration, UNIX_EPOCH},
};

use anyhow::Result;
use log::error;
use rpgpie::{
    certificate::Certificate,
    message::{SignatureMode, encrypt},
    policy::Seipd,
};
use tar::Builder;
use teloxide::{Bot, prelude::Requester, types::InputFile};
use tokio::time::sleep;

const MAX_FILE_SIZE: usize = 50000000;

#[tokio::main]
async fn main() -> Result<()> {
    env_logger::init();
    let chat_id = var("CHAT_ID").unwrap();
    let locations: Vec<String> = var("LOCATIONS")
        .unwrap()
        .trim()
        .split(':')
        .map(|s| s.to_string())
        .collect();

    let tar_data = archive(&locations)?;
    let encrypted_tar = encrypt_data("pub.txt", tar_data)?;

    let bot = Bot::from_env();
    let documents: Vec<InputFile> = encrypted_tar
        .chunks(MAX_FILE_SIZE)
        .enumerate()
        .map(|(part, data)| InputFile::memory(data.to_vec()).file_name(format!("part_{:06}", part)))
        .collect();

    bot.send_message(
        chat_id.clone(),
        format!("{}", UNIX_EPOCH.elapsed()?.as_secs().to_string()),
    )
    .await?;

    for x in documents {
        loop {
            match bot.send_document(chat_id.clone(), x.clone()).await {
                Ok(_) => break,
                Err(teloxide::RequestError::RetryAfter(n)) => {
                    error!("Awaiting {} seconds", n.seconds());
                    sleep(Duration::from_secs(n.seconds() as u64)).await
                }
                Err(err) => {
                    error!("{}\nAwaiting 8 seconds", err);
                    sleep(Duration::from_secs(8)).await
                }
            }
        }
    }

    Ok(())
}

fn archive(location: &[String]) -> Result<Vec<u8>> {
    let mut b = Builder::new(Vec::new());
    for loc in location {
        b.append_dir_all(loc, loc)?;
    }
    Ok(b.into_inner()?)
}

fn encrypt_data(key_file: &str, message: Vec<u8>) -> Result<Vec<u8>> {
    let rec = Certificate::load(&mut File::open(key_file)?)?;
    let mut plaintext = Cursor::new(message);
    let mut output = Cursor::new(Vec::new());
    encrypt(
        Some(Seipd::SEIPD2),
        rec,
        Vec::new(),
        Vec::new(),
        &Vec::new(),
        &mut plaintext,
        SignatureMode::Binary,
        &mut output,
        false,
    )?;
    Ok(output.into_inner())
}