relay: sender: receiver: add custom errortypes
This commit is contained in:
parent
9c11836101
commit
79c9741522
8 changed files with 135 additions and 68 deletions
81
Cargo.lock
generated
81
Cargo.lock
generated
|
|
@ -91,7 +91,7 @@ checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.58",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -216,6 +216,12 @@ version = "3.16.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.6.0"
|
||||
|
|
@ -235,6 +241,7 @@ dependencies = [
|
|||
"hyper",
|
||||
"hyper-util",
|
||||
"lazy_static",
|
||||
"local-ip-address",
|
||||
"log",
|
||||
"rand",
|
||||
"reqwest",
|
||||
|
|
@ -304,7 +311,7 @@ dependencies = [
|
|||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.58",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -344,6 +351,12 @@ dependencies = [
|
|||
"clap 2.34.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2"
|
||||
|
||||
[[package]]
|
||||
name = "encoding_rs"
|
||||
version = "0.8.34"
|
||||
|
|
@ -725,6 +738,18 @@ version = "0.4.13"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c"
|
||||
|
||||
[[package]]
|
||||
name = "local-ip-address"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "136ef34e18462b17bf39a7826f8f3bbc223341f8e83822beb8b77db9a3d49696"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"neli",
|
||||
"thiserror",
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.11"
|
||||
|
|
@ -807,6 +832,31 @@ dependencies = [
|
|||
"tempfile",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "neli"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1100229e06604150b3becd61a4965d5c70f3be1759544ea7274166f4be41ef43"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"libc",
|
||||
"log",
|
||||
"neli-proc-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "neli-proc-macros"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c168194d373b1e134786274020dae7fc5513d565ea2ebb9bc9ff17ffb69106d4"
|
||||
dependencies = [
|
||||
"either",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"serde",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nonempty"
|
||||
version = "0.7.0"
|
||||
|
|
@ -871,7 +921,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.58",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -944,7 +994,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.58",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1202,7 +1252,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.58",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1293,6 +1343,17 @@ version = "0.11.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.109"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.58"
|
||||
|
|
@ -1375,7 +1436,7 @@ checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.58",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1430,7 +1491,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.58",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1530,7 +1591,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.58",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1687,7 +1748,7 @@ dependencies = [
|
|||
"once_cell",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.58",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
|
|
@ -1721,7 +1782,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"syn 2.0.58",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -25,3 +25,4 @@ dotenv = { version = "0.15.0", features = ["clap"] }
|
|||
tower-http = {version = "0.5.2", features = ["fs"]}
|
||||
axum-client-ip = "0.6.0"
|
||||
lazy_static = "1.4.0"
|
||||
local-ip-address = "0.6.1"
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ impl Args {
|
|||
match &self.command {
|
||||
Some(Commands::Send { relay, file }) => {
|
||||
let _ = match sender::send_info(
|
||||
relay.as_deref().unwrap_or("http://0.0.0.0:1323"),
|
||||
relay.as_deref().unwrap_or("http://0.0.0.0:8080"),
|
||||
file.as_deref().unwrap_or("test.txt"),
|
||||
)
|
||||
.await
|
||||
|
|
@ -101,7 +101,7 @@ impl Args {
|
|||
name,
|
||||
}) => {
|
||||
let response = receiver::download_info(
|
||||
relay.as_deref().unwrap_or("http://0.0.0.0:1323"),
|
||||
relay.as_deref().unwrap_or("http://0.0.0.0:8080"),
|
||||
name.as_deref().unwrap_or("None"),
|
||||
)
|
||||
.await;
|
||||
|
|
|
|||
52
src/error/mod.rs
Normal file
52
src/error/mod.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
use std::error::Error;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TransferNotCreatedError {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl TransferNotCreatedError {
|
||||
pub fn new(msg: &str) -> Self {
|
||||
Self {
|
||||
message: msg.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for TransferNotCreatedError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for TransferNotCreatedError {
|
||||
fn description(&self) -> &str {
|
||||
&self.message
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TransferNotFoundError {
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl TransferNotFoundError {
|
||||
pub fn new(msg: &str) -> Self {
|
||||
Self {
|
||||
message: msg.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for TransferNotFoundError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for TransferNotFoundError {
|
||||
fn description(&self) -> &str {
|
||||
&self.message
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
mod cli;
|
||||
mod error;
|
||||
mod http_client;
|
||||
mod receiver;
|
||||
mod relay;
|
||||
|
|
|
|||
|
|
@ -1,38 +1,12 @@
|
|||
use crate::error::TransferNotFoundError;
|
||||
use crate::transfer_info::transfer_info::TransferInfoRequest;
|
||||
use reqwest::Client;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::fs::File;
|
||||
use std::io::copy;
|
||||
use tracing::{debug, error, info};
|
||||
|
||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct TransferNotFoundError {
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl TransferNotFoundError {
|
||||
fn new(msg: &str) -> Self {
|
||||
Self {
|
||||
message: msg.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for TransferNotFoundError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for TransferNotFoundError {
|
||||
fn description(&self) -> &str {
|
||||
&self.message
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn download_info(relay: &str, filename: &str) -> Result<TransferInfoRequest> {
|
||||
match reqwest::get(format!("{}/download/{}", relay.to_string(), filename)).await {
|
||||
Ok(resp) => {
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@ struct AppState {
|
|||
}
|
||||
|
||||
pub async fn start_server(port: Option<&i32>, listen_addr: Option<&String>) {
|
||||
// env_logger::init();
|
||||
info!("Server starting...");
|
||||
let shared_state = AppState {
|
||||
data: Arc::new(Mutex::new(Vec::new())),
|
||||
|
|
|
|||
|
|
@ -1,42 +1,21 @@
|
|||
use crate::error::TransferNotCreatedError;
|
||||
use crate::transfer_info::transfer_info::TransferInfoRequest;
|
||||
use log::{debug, error};
|
||||
use local_ip_address;
|
||||
use reqwest::{Client, StatusCode};
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use tracing::{debug, error, info};
|
||||
|
||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct TransferNotCreatedError {
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl TransferNotCreatedError {
|
||||
fn new(msg: &str) -> Self {
|
||||
Self {
|
||||
message: msg.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for TransferNotCreatedError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for TransferNotCreatedError {
|
||||
fn description(&self) -> &str {
|
||||
&self.message
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_info(relay: &str, file: &str) -> Result<String> {
|
||||
let sender_ip = local_ip_address::local_ip().unwrap();
|
||||
debug!("local ip is: {}", sender_ip);
|
||||
let ip_str = sender_ip.to_owned().to_string();
|
||||
debug!("Send Request to: {:?}", relay.to_string());
|
||||
let mut map = HashMap::new();
|
||||
map.insert("keyword", "test");
|
||||
map.insert("files", file);
|
||||
map.insert("ip", ip_str.as_str());
|
||||
|
||||
let client = Client::new();
|
||||
let res = client
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue