Skip to content

Commit

Permalink
Merge pull request #64 from genonullfree/feature/file-transport-to-lo…
Browse files Browse the repository at this point in the history
…cation

Add remote file location set
  • Loading branch information
genonullfree authored Nov 22, 2021
2 parents 14e1b43 + 3f38867 commit 174c2ce
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "teleporter"
version = "0.6.0"
version = "0.6.1"
authors = ["geno nullfree <[email protected]>"]
license = "BSD-3-Clause"
description = "A small utility to send files quickly from point A to point B"
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,17 @@ To start a teleporter in client (sending) mode, run:
./teleporter -d <destination IP> -i <file> [[file2] [file3] ...]
```

Teleporter will transfer files with their name information as well as their file permissions. Any file path information will be lost. All the received files will be written out in the CWD where the server side was started.
Teleporter will transfer files with their name information as well as their file permissions. Any file path information will be lost unless the `-k` option is enabled. All the received files will be written out in the CWD where the server side was started unless the server was started with the `--allow-dangerous-filepath` option.

## Rename / Copy-To

Teleporter can now set remote file locations, or file renaming, via the `:` operator. Similar to how `Docker` allows quick mounting of directory locations, Teleporter will first attempt to open a file by the full given path, if that file does not exist, it will see if there are any colons (`:`) in the filename. If present, it will split the filepath and attempt to open on the first portion of the name. If that succeeds, Teleporter assumes this is a file rename / copy-to. Teleporter will also need the `-k` option, to keep filepath information. Otherwise only the file name will be changed.

For example, given the following command:
```bash
./teleporter -i ~/Downloads/ubuntu-20.04.3-live-server-arm64.iso:/tmp/ubuntu.iso -k
```
(and assuming the server was started with `--allow-dangerous-filepath`), Teleporter will first attempt to open `~/Downloads/ubuntu-20.04.3-live-server-arm64.iso:/tmp/ubuntu.iso`, if that fails, it will attempt to split the path on `:` and open `~/Downloads/ubuntu-20.04.3-live-server-arm64.iso`. If that succeeds, then it knows it is a rename / copy-to operation and will set the destination filepath to be the second part of the string: `/tmp/ubuntu.iso`. On the server, it will only receive the file for `/tmp/ubuntu.iso`. If the `-k` argument was ommitted, the server would just receive the original file renamed as `ubuntu.iso`.

# Installation

Expand Down
58 changes: 55 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ use crate::utils::print_updates;
use crate::*;
use std::path::Path;

#[derive(Debug)]
struct Replace {
orig: Vec<String>,
new: Vec<String>,
}

fn get_file_list(opt: &Opt) -> Vec<String> {
let mut files = Vec::<String>::new();

Expand All @@ -18,7 +24,7 @@ fn get_file_list(opt: &Opt) -> Vec<String> {
}
};
files.append(&mut tmp);
} else if item.is_file() {
} else if item.exists() && item.is_file() {
files.push(item.to_str().unwrap().to_string());
}
}
Expand Down Expand Up @@ -52,10 +58,51 @@ fn scope_dir(dir: &Path) -> Result<Vec<String>, Error> {
Ok(files)
}

fn find_replacements(opt: &mut Opt) -> Replace {
let mut rep = Replace {
orig: Vec::<String>::new(),
new: Vec::<String>::new(),
};

let mut orig: String;
let mut new: String;
let mut poppers = Vec::<usize>::new();

for (idx, item) in opt.input.iter().enumerate() {
if File::open(&item).is_ok() {
continue;
}

let path = item.to_str().unwrap();
if path.contains(&":") {
let mut split = path.split(':');
orig = split.next().unwrap().to_string();
new = split.next().unwrap().to_string();

if File::open(&orig).is_ok() {
rep.orig.push(orig.clone());
rep.new.push(new.clone());
poppers.push(idx);
}
}
}

while !poppers.is_empty() {
let idx = poppers.pop().unwrap();
opt.input.remove(idx);
opt.input
.insert(idx, PathBuf::from(&rep.orig[poppers.len()]));
}

rep
}

/// Client function sends filename and file data for each filepath
pub fn run(opt: Opt) -> Result<(), Error> {
pub fn run(mut opt: Opt) -> Result<(), Error> {
println!("Teleporter Client {}", VERSION);

let rep = find_replacements(&mut opt);

let files = get_file_list(&opt);

if files.is_empty() {
Expand All @@ -71,6 +118,11 @@ pub fn run(opt: Opt) -> Result<(), Error> {

let filepath = item;
let mut filename = filepath.clone().to_string();
for (idx, item) in rep.orig.iter().enumerate() {
if item.contains(&filepath.to_string()) {
filename = rep.new[idx].clone();
}
}

// Validate file
let file = match File::open(&filepath) {
Expand All @@ -91,7 +143,7 @@ pub fn run(opt: Opt) -> Result<(), Error> {

// Remove all path info if !opt.keep_path
if !opt.keep_path {
filename = Path::new(item)
filename = Path::new(&filename)
.file_name()
.unwrap()
.to_str()
Expand Down
2 changes: 2 additions & 0 deletions src/teleport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ mod tests {
let mut test = TeleportInitAck::new(TeleportStatus::Proceed);
let feat = TeleportFeatures::NewFile as u32 | TeleportFeatures::Overwrite as u32;
test.features = Some(feat);
test.version = [0, 6, 0];
let out = test.serialize();

assert_eq!(out, TESTINITACK);
Expand All @@ -704,6 +705,7 @@ mod tests {
let mut test = TeleportInitAck::new(TeleportStatus::Proceed);
let feat = TeleportFeatures::NewFile as u32 | TeleportFeatures::Overwrite as u32;
test.features = Some(feat);
test.version = [0, 6, 0];

let mut t = TeleportInitAck::new(TeleportStatus::Proceed);
t.deserialize(TESTINITACK).unwrap();
Expand Down

0 comments on commit 174c2ce

Please sign in to comment.