add nothing flag and add default paste server to upload to
This commit is contained in:
parent
676cda3109
commit
bc64746424
64
src/main.rs
64
src/main.rs
@ -11,17 +11,17 @@ use std::{io::Read, path::PathBuf};
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, about, version, long_about = None)]
|
||||
struct Cli {
|
||||
#[arg(short, long, help = "choose privatebin instance to use")]
|
||||
#[arg(long, help = "choose privatebin instance to use")]
|
||||
url: Option<String>, // used
|
||||
#[arg(short, long)]
|
||||
expire: Option<String>, // used
|
||||
#[arg(short, long)]
|
||||
open_discussion: Option<bool>, // used
|
||||
#[arg(long)]
|
||||
burn_after_reading: Option<bool>, // used
|
||||
#[arg(short, long, action=ArgAction::SetFalse, help = "Disable gzip compression")]
|
||||
gzip: Option<bool>, // used
|
||||
#[arg(short, long, value_enum)]
|
||||
open_discussion: bool, // used
|
||||
#[arg(long)]
|
||||
burn_after_reading: bool, // used
|
||||
#[arg(long, action=ArgAction::SetFalse, help = "Disable gzip compression")]
|
||||
gzip: bool, // used
|
||||
#[arg(long, value_enum)]
|
||||
formatter: Option<PasteFormat>, // used
|
||||
#[arg(short, long)]
|
||||
password: Option<String>, // used
|
||||
@ -29,6 +29,8 @@ struct Cli {
|
||||
filename: Option<String>, // used
|
||||
#[arg(short, long)]
|
||||
attachment: Option<PathBuf>, // used
|
||||
#[arg(long)]
|
||||
nothing: bool // used
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Serialize, Clone, ValueEnum)]
|
||||
@ -65,6 +67,13 @@ fn main() {
|
||||
|
||||
//println!("{}", input);
|
||||
|
||||
// check if no input and no attachment and do nothing
|
||||
// if nothing flag isn't set
|
||||
if input==String::new() && cli.attachment == None && cli.nothing == false {
|
||||
println!("would have sent nothing. if you want to send nothing use the --nothing flag");
|
||||
return;
|
||||
}
|
||||
|
||||
let pasteurl = create_paste(cli, input);
|
||||
println!("{}", pasteurl);
|
||||
}
|
||||
@ -134,8 +143,8 @@ fn create_paste(cfg: Cli, input: String) -> String {
|
||||
rng.fill_bytes(&mut nonce);
|
||||
|
||||
let compression = match cfg.gzip {
|
||||
Some(true) => "zlib",
|
||||
_ => "none",
|
||||
true => "zlib",
|
||||
false => "none",
|
||||
};
|
||||
|
||||
let mut post_body = serde_json::json!({
|
||||
@ -152,8 +161,8 @@ fn create_paste(cfg: Cli, input: String) -> String {
|
||||
compression
|
||||
],
|
||||
cfg.formatter.unwrap_or(PasteFormat::Plaintext),
|
||||
cfg.open_discussion.unwrap_or_default() as u8,
|
||||
cfg.burn_after_reading.unwrap_or_default() as u8
|
||||
cfg.open_discussion as u8,
|
||||
cfg.burn_after_reading as u8
|
||||
],
|
||||
"ct": "",
|
||||
"meta": {
|
||||
@ -169,7 +178,7 @@ fn create_paste(cfg: Cli, input: String) -> String {
|
||||
let cipher = Cipher::new(key);
|
||||
|
||||
let paste_blob = match cfg.gzip {
|
||||
Some(true) => miniz_oxide::deflate::compress_to_vec(
|
||||
true => miniz_oxide::deflate::compress_to_vec(
|
||||
serde_json::to_string(&paste_data).unwrap().as_bytes(),
|
||||
10,
|
||||
),
|
||||
@ -193,7 +202,7 @@ fn create_paste(cfg: Cli, input: String) -> String {
|
||||
//println!("b58: {}", post_key);
|
||||
|
||||
// check if url is set and upload to that url
|
||||
let mut paste_id = String::new();
|
||||
let paste_id;
|
||||
if let Some(url) = &cfg.url {
|
||||
//println!("url: {}", url);
|
||||
let client = reqwest::blocking::Client::builder().build().unwrap();
|
||||
@ -219,10 +228,35 @@ fn create_paste(cfg: Cli, input: String) -> String {
|
||||
return String::new();
|
||||
}
|
||||
};
|
||||
}
|
||||
} else {
|
||||
let url = "https://privatebin.net/";
|
||||
let client = reqwest::blocking::Client::builder().build().unwrap();
|
||||
let mut request = client.request(Method::POST, url);
|
||||
request = request.header("X-Requested-With", "JSONHttpRequest");
|
||||
let res = request
|
||||
.body::<String>(serde_json::to_string(&post_body).unwrap())
|
||||
.send()
|
||||
.unwrap();
|
||||
|
||||
let rsv: serde_json::Value = match res.json() {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
println!("Error: {}", e);
|
||||
return String::new();
|
||||
}
|
||||
};
|
||||
|
||||
paste_id = match rsv.get("id") {
|
||||
Some(v) => v.as_str().unwrap().to_string(),
|
||||
None => {
|
||||
println!("Error: {}", rsv.get("message").unwrap());
|
||||
return String::new();
|
||||
}
|
||||
};
|
||||
};
|
||||
format!(
|
||||
"{}?{}#{}",
|
||||
cfg.url.unwrap_or("https://EXAMPLE.com/".to_string()),
|
||||
cfg.url.unwrap_or("https://privatebin.net/".to_string()),
|
||||
paste_id,
|
||||
post_key
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user