Skip to content

Commit

Permalink
Merge pull request #104 from genonullfree/cleanup-imports
Browse files Browse the repository at this point in the history
Cleanup imports
  • Loading branch information
genonullfree authored Feb 21, 2023
2 parents d42e03a + 0c01371 commit c03a660
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 177 deletions.
50 changes: 16 additions & 34 deletions Cargo.lock

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

10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "teleporter"
version = "0.10.3"
version = "0.10.4"
authors = ["geno nullfree <[email protected]>"]
license = "BSD-3-Clause"
description = "A small utility to send files quickly from point A to point B"
Expand All @@ -14,6 +14,14 @@ rust-version = "1.64.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
name = "teleporter"
path = "src/lib.rs"

[[bin]]
name = "teleporter"
path = "src/main.rs"

[dependencies]
clap = { version = "4.0", features = ["derive"] }
byteorder = "1.4"
Expand Down
8 changes: 4 additions & 4 deletions src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::errors::TeleportError;
use crate::teleport::TeleportEnc;
use crate::{Error, ErrorKind};
use aes_gcm::aead::Aead;
use aes_gcm::{Aes256Gcm, KeyInit};
use generic_array::GenericArray;
Expand All @@ -13,7 +13,7 @@ pub fn genkey(ctx: &mut TeleportEnc) -> EphemeralSecret {
secret
}

pub fn decrypt(key: &[u8; 32], nonce: Vec<u8>, data: Vec<u8>) -> Result<Vec<u8>, Error> {
pub fn decrypt(key: &[u8; 32], nonce: Vec<u8>, data: Vec<u8>) -> Result<Vec<u8>, TeleportError> {
let key = GenericArray::from_slice(key);
let cipher = Aes256Gcm::new(key);
let gen_nonce = GenericArray::from_slice(&nonce);
Expand All @@ -25,12 +25,12 @@ pub fn decrypt(key: &[u8; 32], nonce: Vec<u8>, data: Vec<u8>) -> Result<Vec<u8>,
Ok(plaintext.to_vec())
}

pub fn encrypt(key: &[u8; 32], nonce: Vec<u8>, input: Vec<u8>) -> Result<Vec<u8>, Error> {
pub fn encrypt(key: &[u8; 32], nonce: Vec<u8>, input: Vec<u8>) -> Result<Vec<u8>, TeleportError> {
let key = GenericArray::from_slice(key);
let cipher = Aes256Gcm::new(key);
let gen_nonce = GenericArray::from_slice(&nonce);
match cipher.encrypt(gen_nonce, input.as_ref()) {
Ok(s) => Ok(s),
Err(_) => Err(Error::new(ErrorKind::InvalidData, "Encryption failed")),
Err(_) => Err(TeleportError::EncryptionFailure),
}
}
21 changes: 21 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,25 @@ pub enum TeleportError {

#[error("Invalid file name")]
InvalidFileName,

#[error("Error reading protcool header")]
InvalidHeaderRead,

#[error("Not enough data for IV")]
InvalidIV,

#[error("Data is not the expected length")]
InvalidLength,

#[error("Not enough data for public key")]
InvalidPubKey,

#[error("Unknown TeleportStatus code - update Teleporter?")]
InvalidStatusCode,

#[error("Cannot deserialize delta data")]
InvalidDelta,

#[error("Encryption failed")]
EncryptionFailure,
}
71 changes: 71 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use clap::Parser;
use std::path::PathBuf;

pub mod errors;
pub mod listen;
pub mod send;

mod crypto;
mod teleport;
mod utils;

pub const PROTOCOL: u64 = 0x54524f50454c4554;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Clone, Debug, Parser, PartialEq, Eq)]
pub struct SendOpt {
/// List of filepaths to files that will be teleported
#[arg(short, long, num_args = ..)]
input: Vec<PathBuf>,

/// Destination teleporter host
#[arg(short, long, default_value = "localhost")]
dest: String,

/// Destination teleporter port
#[arg(short, long, default_value = "9001")]
port: u16,

/// Overwrite remote file
#[arg(short, long)]
overwrite: bool,

/// Recurse into directories on send
#[arg(short, long)]
recursive: bool,

/// Encrypt the file transfer using ECDH key-exchange and random keys
#[arg(short, long)]
encrypt: bool,

/// Disable delta transfer (overwrite will transfer entire file)
#[arg(short, long)]
no_delta: bool,

/// Keep path info (recreate directory path on remote server)
#[arg(short, long)]
keep_path: bool,

/// Backup the destination file to a ".bak" extension if it exists and is being overwritten (consecutive runs will replace the *.bak file)
#[arg(short, long)]
backup: bool,

/// If the destination file exists, append a ".1" (or next available number) to the filename instead of overwriting
#[arg(short, long)]
filename_append: bool,
}

#[derive(Clone, Debug, Parser, PartialEq, Eq)]
pub struct ListenOpt {
/// Allow absolute and relative file paths for transfers (server only) [WARNING: potentially dangerous option, use at your own risk!]
#[arg(long)]
allow_dangerous_filepath: bool,

/// Require encryption for incoming connections to the server
#[arg(short, long)]
must_encrypt: bool,

/// Port to listen on
#[arg(short, long, default_value = "9001")]
port: u16,
}
19 changes: 14 additions & 5 deletions src/listen.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
use crate::teleport::*;
use crate::teleport::{TeleportFeatures, TeleportStatus};
use crate::teleport::{TeleportInit, TeleportInitAck};
use crate::*;
use crate::errors::TeleportError;
use crate::teleport::{TeleportAction, TeleportEnc, TeleportFeatures, TeleportStatus};
use crate::teleport::{TeleportData, TeleportInit, TeleportInitAck};
use crate::ListenOpt;
use crate::VERSION;
use crate::{crypto, utils};
use semver::Version;
use std::fs;
use std::fs::OpenOptions;
use std::fs::{File, OpenOptions};
use std::io;
use std::io::{Seek, SeekFrom, Write};
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, TcpListener, TcpStream};
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::sync::{Arc, Mutex, MutexGuard};
use std::thread;
use std::time::Instant;

/// Server function sets up a listening socket for any incoming connnections
pub fn run(opt: ListenOpt) -> Result<(), TeleportError> {
Expand Down
Loading

0 comments on commit c03a660

Please sign in to comment.