82 Commits

Author SHA1 Message Date
ChristianVisintin
c9948162b9 lol, for some reason cargo-aur renames my crate into termscp-bin. WHY OMG WHY. 2020-12-21 14:42:26 +01:00
ChristianVisintin
4e8e0d1b41 Fixed that f aur action 2020-12-21 14:33:00 +01:00
ChristianVisintin
95b7a773d7 aur runs on 2020-12-21 14:24:25 +01:00
ChristianVisintin
07c924d6a0 0.2.0 2020-12-21 14:21:04 +01:00
ChristianVisintin
50efebdd63 Fixed deploy script 0.2.0 2020-12-21 14:07:43 +01:00
ChristianVisintin
50b8f3cd71 Improved abs paths in host 2020-12-21 13:59:03 +01:00
ChristianVisintin
093dc4f33d Fixed test 2020-12-21 13:43:05 +01:00
ChristianVisintin
963ef88ae5 Arch package (0.2.0) 2020-12-21 12:22:13 +01:00
ChristianVisintin
03e63e11ac 0.2.0 2020-12-21 12:06:44 +01:00
ChristianVisintin
9f478227a9 0.2.0 readme 2020-12-21 12:02:45 +01:00
ChristianVisintin
1d112b3f32 Arch publish workflow and build with docker 2020-12-21 11:48:41 +01:00
ChristianVisintin
1a5bd394b6 Optimizations 2020-12-21 11:12:49 +01:00
ChristianVisintin
3901ed54c6 Copy feature in ui; new keybinding <C> 2020-12-21 11:11:29 +01:00
ChristianVisintin
08728bf55e Copy method (host/transfer) 2020-12-21 10:49:31 +01:00
ChristianVisintin
3220d00b14 archlinux dist 2020-12-21 10:27:33 +01:00
ChristianVisintin
eb12da0308 Utils into multiple files 2020-12-20 15:36:48 +01:00
ChristianVisintin
77545ec87d Text editor documentation 2020-12-20 15:22:52 +01:00
ChristianVisintin
3f6f03af33 Text editor input and session 2020-12-20 15:05:22 +01:00
ChristianVisintin
47f9b39630 Sha256 in utils 2020-12-20 14:49:32 +01:00
ChristianVisintin
9171e0789f Moved recv_file and send_file to an independent method (fix) 2020-12-20 14:47:37 +01:00
ChristianVisintin
5eabaf8ac2 Moved recv_file and send_file to an independent method 2020-12-20 12:33:19 +01:00
ChristianVisintin
b8ad1e7feb edit file method 2020-12-19 21:49:53 +01:00
ChristianVisintin
4d7ea1cdb4 Context methods 2020-12-19 21:47:03 +01:00
ChristianVisintin
021f860ca6 context enter_alternate_screen leave_alternate_screen clear_screen 2020-12-19 21:39:31 +01:00
ChristianVisintin
d0774fd7ed FsEntry::is_file 2020-12-19 21:38:56 +01:00
ChristianVisintin
5632ac6f0b Optimizing log code 2020-12-19 21:12:30 +01:00
ChristianVisintin
7a115e5dc3 wrkdir as member of FileExplorer 2020-12-19 21:10:00 +01:00
ChristianVisintin
d95cda3dfc Optimizing log code 2020-12-19 21:05:56 +01:00
ChristianVisintin
dd9f54acae Working on text-editor 2020-12-19 21:04:55 +01:00
ChristianVisintin
6bc2bcb89e Include set_permissions in UNIX/macos/linux only 2020-12-18 20:58:57 +01:00
ChristianVisintin
9c2e751e11 Rounded borders; collapsed bookmarks borders 2020-12-18 20:54:34 +01:00
ChristianVisintin
2a52a19552 Display transfer speed 2020-12-18 17:06:17 +01:00
ChristianVisintin
1b99d63c47 Added one more space to size in FsEntry::fmt, since sometimes size has 4 digits 2020-12-18 16:42:06 +01:00
ChristianVisintin
33683bc8ce SFTP speed issue marked as solved 2020-12-18 16:16:23 +01:00
ChristianVisintin
61b4a3b76e Log how long it took to download/upload a file 2020-12-18 16:14:23 +01:00
ChristianVisintin
386db6278b Updated github uri 2020-12-18 14:47:48 +01:00
ChristianVisintin
47664b98ad Updated github uri 2020-12-18 14:47:32 +01:00
ChristianVisintin
898b57943b Improved test coverage 2020-12-18 14:40:37 +01:00
ChristianVisintin
d37cc4f796 test name elide in fs fmt 2020-12-18 12:27:37 +01:00
ChristianVisintin
b3fed60d12 Fixed fmt explorer windows 2020-12-18 12:06:54 +01:00
ChristianVisintin
a3d1db3fa2 Fs test fmt 2020-12-18 11:56:08 +01:00
ChristianVisintin
0a79fb3687 Scp: when username was not provided, it didn't fallback to current username 2020-12-18 11:40:51 +01:00
ChristianVisintin
900d9ac3c6 Apply file mode of file downloaded from remote 2020-12-18 11:31:51 +01:00
ChristianVisintin
50b523f9f4 codecov.yml 2020-12-18 10:49:41 +01:00
ChristianVisintin
0759651ee4 codecov 2020-12-18 10:47:37 +01:00
ChristianVisintin
daedee3f66 Documentation about bookmarks 2020-12-16 21:09:59 +01:00
ChristianVisintin
61c58e227e Merge branch 'bookmarks' into 0.2.0 2020-12-16 20:42:47 +01:00
ChristianVisintin
3cd9fc407e Bookmarks layout OK 2020-12-16 20:41:42 +01:00
ChristianVisintin
dd6e2be75d Panic if bookmark name is empty 2020-12-16 20:39:35 +01:00
ChristianVisintin
38e015efe4 removed args from SaveBookmark 2020-12-16 20:34:36 +01:00
ChristianVisintin
335bfc8460 Added 'save password' tab to auth activity when saving bookmarks 2020-12-16 17:01:11 +01:00
ChristianVisintin
344bf8604f Fixed crash in auth_activity 2020-12-16 16:51:21 +01:00
ChristianVisintin
562a1b3ae8 Clippy optimizations 2020-12-16 16:35:11 +01:00
ChristianVisintin
65c541ff2a bookmarks client OK 2020-12-16 16:32:50 +01:00
ChristianVisintin
14ddba022f Implemented BookmarkClient in AuthActivity 2020-12-16 16:01:29 +01:00
ChristianVisintin
10df5abae2 Added Option<String> password to bookmarks 2020-12-16 16:00:43 +01:00
ChristianVisintin
443789698b Copy trait for FileTransferProtocol 2020-12-16 15:57:46 +01:00
ChristianVisintin
52df9bc73b Macro use for magic-crypt 2020-12-16 15:47:02 +01:00
ChristianVisintin
8cb3637954 Magic-crypt 2020-12-16 15:46:46 +01:00
ChristianVisintin
ee55d1fd31 Bookmarks client 2020-12-16 12:09:34 +01:00
ChristianVisintin
dcc289153f Working on bookmarksClient 2020-12-15 22:17:19 +01:00
ChristianVisintin
940d8d94e5 Moving bookmarks from ui to system 2020-12-15 20:48:43 +01:00
ChristianVisintin
c9f1086408 Merge branch 'bookmarks' into 0.2.0 2020-12-15 16:45:34 +01:00
ChristianVisintin
ff4f35e5f5 Clippy ok 2020-12-15 16:31:21 +01:00
ChristianVisintin
b865fed7e9 Removed bookmarks from upcoming features (since implemented) 2020-12-15 16:23:09 +01:00
ChristianVisintin
274c5e309b Set password as InputField after loading bookmark 2020-12-15 16:17:56 +01:00
ChristianVisintin
e7d53a7d00 Help page in auth activity 2020-12-15 15:00:21 +01:00
ChristianVisintin
99117e067e Bookmarks layouts 2020-12-15 14:46:59 +01:00
ChristianVisintin
88e27ee8fe New popup handlers 2020-12-15 14:18:16 +01:00
ChristianVisintin
d3fe546264 Callbacks and handler for Bookmarks events 2020-12-15 14:15:35 +01:00
ChristianVisintin
982b3ec8d0 Delete recents/bookmarks 2020-12-15 14:03:50 +01:00
ChristianVisintin
b8cf2bea77 load bookmarks 2020-12-15 13:55:41 +01:00
ChristianVisintin
a511cd4ac3 Read one input event at a time 2020-12-15 13:44:51 +01:00
ChristianVisintin
0bab9a77a2 Working on bookmarks 2020-12-15 12:28:06 +01:00
ChristianVisintin
1f9b616de7 Bookmarks in auth activity (logic) 2020-12-15 12:22:47 +01:00
ChristianVisintin
71593e3ea7 PartialEq for bookmark 2020-12-15 12:18:35 +01:00
ChristianVisintin
db7ee624e3 Bookmark default 2020-12-15 12:03:31 +01:00
ChristianVisintin
0e7527fb3f file_exists as pub 2020-12-15 11:36:07 +01:00
ChristianVisintin
50fcba63c4 Bookmarks serializer and data types 2020-12-15 11:13:29 +01:00
ChristianVisintin
8046f82214 AuthActivity refactoring 2020-12-15 09:21:52 +01:00
ChristianVisintin
c33045b4b8 Refactoring auth activity... 2020-12-15 09:14:08 +01:00
ChristianVisintin
e273192f19 Working on 0.2.0 2020-12-13 10:27:21 +01:00
51 changed files with 5617 additions and 1442 deletions

21
.github/workflows/aur-pub.yml vendored Normal file
View File

@@ -0,0 +1,21 @@
name: aur-pub
on:
push:
tags:
- "*"
jobs:
aur-publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Publish AUR package
uses: KSXGitHub/github-actions-deploy-aur@v2.2.3
with:
pkgname: termscp
pkgbuild: ./dist/pkgs/arch/PKGBUILD
commit_username: ${{ secrets.AUR_USERNAME }}
commit_email: ${{ secrets.AUR_EMAIL }}
ssh_private_key: ${{ secrets.AUR_KEY }}
commit_message: Update AUR package
ssh_keyscan_types: rsa,dsa,ecdsa,ed25519

View File

@@ -7,14 +7,28 @@ env:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Clippy
run: cargo clippy
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rustfmt, clippy
- uses: actions-rs/cargo@v1
with:
command: test
args: --all-features --no-fail-fast
env:
CARGO_INCREMENTAL: "0"
RUSTFLAGS: "-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests"
RUSTDOCFLAGS: "-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests"
- name: Clippy
run: cargo clippy
- name: Coverage with grcov
uses: actions-rs/grcov@v0.1
- name: Upload to codecov.io
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}

View File

@@ -7,14 +7,13 @@ env:
jobs:
build:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Clippy
run: cargo clippy
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Clippy
run: cargo clippy

View File

@@ -7,14 +7,13 @@ env:
jobs:
build:
runs-on: windows-2019
steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Clippy
run: cargo clippy
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Clippy
run: cargo clippy

2
.gitignore vendored
View File

@@ -14,5 +14,7 @@
# End of https://www.gitignore.io/api/rust
# Distributions
*.rpm
*.deb
dist/pkgs/arch/*.tar.gz

View File

@@ -1,6 +1,7 @@
# Changelog
- [Changelog](#changelog)
- [0.2.0](#020)
- [0.1.3](#013)
- [0.1.2](#012)
- [0.1.1](#011)
@@ -8,6 +9,36 @@
---
## 0.2.0
Released on 21/12/2020
> The Bookmarks Update
- **Bookmarks**
- Bookmarks and recent connections are now displayed in the home page
- Bookmarks are saved at
- Linux: `/home/alice/.config/termscp/bookmarks.toml`
- Windows: `C:\Users\Alice\AppData\Roaming\termscp\bookmarks.toml`
- MacOS: `/Users/Alice/Library/Application Support/termscp/bookmarks.toml`
- **Text Editor**
- Added text editor feature to explorer view
- Added `o` to keybindings to open a text file
- Keybindings:
- `C`: Copy file/directory
- `O`: Open text file in editor
- Enhancements:
- User interface
- Collpased borders to make everything more *aesthetic*
- Rounded input field boards
- File explorer:
- Log how long it took to upload/download a file and the transfer speed
- Display in progress bar the transfer speed (bytes/seconds)
- Bugfix:
- File mode of file on remote is now reported on local file after being downloaded (unix, linux, macos only)
- Scp: when username was not provided, it didn't fallback to current username
- Explorer: fixed UID format in Windows
## 0.1.3
Released on 14/12/2020

427
Cargo.lock generated
View File

@@ -1,5 +1,15 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "aes-soft"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be14c7498ea50828a38d0e24a765ed2effe92a705885b57d029cd67d45744072"
dependencies = [
"cipher",
"opaque-debug",
]
[[package]]
name = "aho-corasick"
version = "0.7.15"
@@ -9,24 +19,116 @@ dependencies = [
"memchr",
]
[[package]]
name = "arrayref"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544"
[[package]]
name = "arrayvec"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
[[package]]
name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "base64"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "blake2b_simd"
version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afa748e348ad3be8263be728124b24a24f268266f6f5d58af9d75f6a40b5c587"
dependencies = [
"arrayref",
"arrayvec",
"constant_time_eq",
]
[[package]]
name = "block-buffer"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d70f2a8c3126a2aec089e0aebcd945607e1155bfb5b89682eddf43c3ce386718"
dependencies = [
"block-padding 0.1.5",
"byte-tools 0.2.0",
"generic-array 0.11.1",
]
[[package]]
name = "block-buffer"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
dependencies = [
"generic-array 0.14.4",
]
[[package]]
name = "block-modes"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57a0e8073e8baa88212fb5823574c02ebccb395136ba9a164ab89379ec6072f0"
dependencies = [
"block-padding 0.2.1",
"cipher",
]
[[package]]
name = "block-padding"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5"
dependencies = [
"byte-tools 0.3.1",
]
[[package]]
name = "block-padding"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae"
[[package]]
name = "bumpalo"
version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820"
[[package]]
name = "byte-tools"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40"
[[package]]
name = "byte-tools"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7"
[[package]]
name = "byteorder"
version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
[[package]]
name = "bytesize"
version = "1.0.1"
@@ -70,6 +172,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "cipher"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12f8e7987cbd042a63249497f41aed09f8e65add917ea6566effbc56578d6801"
dependencies = [
"generic-array 0.14.4",
]
[[package]]
name = "cloudabi"
version = "0.0.3"
@@ -79,6 +190,21 @@ dependencies = [
"bitflags",
]
[[package]]
name = "constant_time_eq"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
[[package]]
name = "content_inspector"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7bda66e858c683005a53a9a60c69a4aca7eeaa45d124526e389f7aec8e62f38"
dependencies = [
"memchr",
]
[[package]]
name = "core-foundation"
version = "0.9.1"
@@ -95,6 +221,32 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b"
[[package]]
name = "cpuid-bool"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634"
[[package]]
name = "crc-any"
version = "2.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3784befdf9469f4d51c69ef0b774f6a99de6bcc655285f746f16e0dd63d9007"
dependencies = [
"debug-helper",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02d96d1e189ef58269ebe5b97953da3274d83a93af647c2ddd6f9dab28cedb8d"
dependencies = [
"autocfg",
"cfg-if 1.0.0",
"lazy_static",
]
[[package]]
name = "crossterm"
version = "0.18.2"
@@ -120,6 +272,77 @@ dependencies = [
"winapi",
]
[[package]]
name = "data-encoding"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "993a608597367c6377b258c25d7120740f00ed23a2252b729b1932dd7866f908"
[[package]]
name = "debug-helper"
version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8a5bb894f24f42c247f19b25928a88e31867c0f84552c05df41a9dd527435e"
[[package]]
name = "des"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b24e7c748888aa2fa8bce21d8c64a52efc810663285315ac7476f7197a982fae"
dependencies = [
"byteorder",
"cipher",
"opaque-debug",
]
[[package]]
name = "digest"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03b072242a8cbaf9c145665af9d250c59af3b958f83ed6824e13533cf76d5b90"
dependencies = [
"generic-array 0.9.0",
]
[[package]]
name = "digest"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
dependencies = [
"generic-array 0.14.4",
]
[[package]]
name = "dirs"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "142995ed02755914747cc6ca76fc7e4583cd18578746716d0508ea6ed558b9ff"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]]
name = "edit"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "323032447eba6f5aca88b46d6e7815151c16c53e4128569420c09d7840db3bfc"
dependencies = [
"tempfile",
"which",
]
[[package]]
name = "foreign-types"
version = "0.3.2"
@@ -147,6 +370,34 @@ dependencies = [
"regex",
]
[[package]]
name = "generic-array"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d"
dependencies = [
"typenum",
]
[[package]]
name = "generic-array"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8107dafa78c80c848b71b60133954b4a58609a3a1a5f9af037ecc7f67280f369"
dependencies = [
"typenum",
]
[[package]]
name = "generic-array"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getopts"
version = "0.2.21"
@@ -261,12 +512,41 @@ dependencies = [
"cfg-if 0.1.10",
]
[[package]]
name = "magic-crypt"
version = "3.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a01cf5086c27e3daff2a06886ab2fc44fe4fdec7d2df7a82e5329483011bfd7"
dependencies = [
"aes-soft",
"base64",
"block-modes",
"crc-any",
"des",
"digest 0.7.6",
"digest 0.9.0",
"md-5",
"sha2",
"tiger-digest",
]
[[package]]
name = "match_cfg"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4"
[[package]]
name = "md-5"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b5a279bb9607f9f53c22d496eade00d138d1bdcccd07d74650387cf94942a15"
dependencies = [
"block-buffer 0.9.0",
"digest 0.9.0",
"opaque-debug",
]
[[package]]
name = "memchr"
version = "2.3.4"
@@ -342,6 +622,18 @@ dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0"
[[package]]
name = "opaque-debug"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "openssl"
version = "0.10.30"
@@ -501,6 +793,17 @@ version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "redox_users"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d"
dependencies = [
"getrandom",
"redox_syscall",
"rust-argon2",
]
[[package]]
name = "regex"
version = "1.4.2"
@@ -528,6 +831,21 @@ dependencies = [
"winapi",
]
[[package]]
name = "ring"
version = "0.16.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "024a1e66fea74c66c66624ee5622a7ff0e4b73a13b4f5c326ddb50c708944226"
dependencies = [
"cc",
"libc",
"once_cell",
"spin",
"untrusted",
"web-sys",
"winapi",
]
[[package]]
name = "rpassword"
version = "5.0.0"
@@ -538,6 +856,18 @@ dependencies = [
"winapi",
]
[[package]]
name = "rust-argon2"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b18820d944b33caa75a71378964ac46f58517c92b6ae5f762636247c09e78fb"
dependencies = [
"base64",
"blake2b_simd",
"constant_time_eq",
"crossbeam-utils",
]
[[package]]
name = "schannel"
version = "0.1.19"
@@ -577,6 +907,39 @@ dependencies = [
"libc",
]
[[package]]
name = "serde"
version = "1.0.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c84d3526699cd55261af4b941e4e725444df67aa4f9e6a3564f18030d12672df"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "sha2"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e7aab86fe2149bad8c507606bdb3f4ef5e7b2380eb92350f56122cca72a42a8"
dependencies = [
"block-buffer 0.9.0",
"cfg-if 1.0.0",
"cpuid-bool",
"digest 0.9.0",
"opaque-debug",
]
[[package]]
name = "signal-hook"
version = "0.1.16"
@@ -621,6 +984,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "spin"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "ssh2"
version = "0.9.0"
@@ -660,20 +1029,29 @@ dependencies = [
[[package]]
name = "termscp"
version = "0.1.3"
version = "0.2.0"
dependencies = [
"bytesize",
"chrono",
"content_inspector",
"crossterm",
"data-encoding",
"dirs",
"edit",
"ftp4",
"getopts",
"hostname",
"lazy_static",
"magic-crypt",
"rand",
"regex",
"ring",
"rpassword",
"serde",
"ssh2",
"tempfile",
"textwrap",
"toml",
"tui",
"unicode-width",
"users",
@@ -699,6 +1077,17 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "tiger-digest"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68067e91b4b9bb2e1ce3dc55077c984bbe2fa2be65308264dab403c165257545"
dependencies = [
"block-buffer 0.5.1",
"byte-tools 0.2.0",
"digest 0.7.6",
]
[[package]]
name = "time"
version = "0.1.44"
@@ -710,6 +1099,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "toml"
version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75cf45bb0bef80604d001caaec0d09da99611b3c0fd39d3080468875cdb65645"
dependencies = [
"serde",
]
[[package]]
name = "tui"
version = "0.13.0"
@@ -723,6 +1121,12 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "typenum"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
[[package]]
name = "unicode-segmentation"
version = "1.7.1"
@@ -741,6 +1145,12 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "untrusted"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
[[package]]
name = "users"
version = "0.11.0"
@@ -757,6 +1167,12 @@ version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c"
[[package]]
name = "version_check"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
@@ -833,6 +1249,15 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "which"
version = "3.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724"
dependencies = [
"libc",
]
[[package]]
name = "whoami"
version = "1.0.0"

View File

@@ -1,41 +1,48 @@
[package]
name = "termscp"
version = "0.1.3"
version = "0.2.0"
authors = ["Christian Visintin"]
edition = "2018"
license = "GPL-3.0"
keywords = ["scp-client", "sftp-client", "ftp-client", "winscp", "command-line-utility"]
categories = ["command-line-utilities"]
description = "TermSCP is a SCP/SFTP/FTPS client for command line with an integrated UI to explore the remote file system. Basically WinSCP on a terminal."
homepage = "https://github.com/ChristianVisintin/TermSCP"
repository = "https://github.com/ChristianVisintin/TermSCP"
homepage = "https://github.com/ChristianVisintin/termscp"
repository = "https://github.com/ChristianVisintin/termscp"
documentation = "https://docs.rs/termscp"
readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bytesize = "1.0.1"
chrono = "0.4.19"
content_inspector = "0.2.4"
crossterm = "0.18.2"
dirs = "3.0.1"
edit = "0.1.2"
ftp4 = { version = "^4.0.1", features = ["secure"] }
getopts = "0.2.21"
ssh2 = "0.9.0"
tui = { version = "0.13.0", features = ["crossterm"], default-features = false }
whoami = "1.0.0"
rpassword = "5.0.0"
unicode-width = "0.1.7"
chrono = "0.4.19"
bytesize = "1.0.1"
textwrap = "0.13.0"
regex = "1.4.2"
lazy_static = "1.4.0"
hostname = "0.3.1"
lazy_static = "1.4.0"
magic-crypt = "3.1.6"
rand = "0.7.3"
regex = "1.4.2"
rpassword = "5.0.0"
serde = { version = "1.0.118", features = ["derive"] }
ssh2 = "0.9.0"
tempfile = "3.1.0"
textwrap = "0.13.0"
toml = "0.5.7"
tui = { version = "0.13.0", features = ["crossterm"], default-features = false }
unicode-width = "0.1.7"
whoami = "1.0.0"
ring = "0.16.19"
data-encoding = "2.3.1"
[target.'cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))'.dependencies]
users = "0.11.0"
[dev-dependencies]
tempfile = "3"
#[patch.crates-io]
#ftp = { git = "https://github.com/ChristianVisintin/rust-ftp" }

154
README.md
View File

@@ -1,12 +1,12 @@
# TermSCP
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) [![Stars](https://img.shields.io/github/stars/ChristianVisintin/TermSCP.svg)](https://github.com/ChristianVisintin/TermSCP) [![Issues](https://img.shields.io/github/issues/ChristianVisintin/TermSCP.svg)](https://github.com/ChristianVisintin/TermSCP/issues) [![Downloads](https://img.shields.io/crates/d/termscp.svg)](https://crates.io/crates/termscp) [![Crates.io](https://img.shields.io/badge/crates.io-v0.1.3-orange.svg)](https://crates.io/crates/termscp) [![Docs](https://docs.rs/termscp/badge.svg)](https://docs.rs/termscp)
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) [![Stars](https://img.shields.io/github/stars/ChristianVisintin/TermSCP.svg)](https://github.com/ChristianVisintin/TermSCP) [![Issues](https://img.shields.io/github/issues/ChristianVisintin/TermSCP.svg)](https://github.com/ChristianVisintin/TermSCP/issues) [![Downloads](https://img.shields.io/crates/d/termscp.svg)](https://crates.io/crates/termscp) [![Crates.io](https://img.shields.io/badge/crates.io-v0.2.0-orange.svg)](https://crates.io/crates/termscp) [![Docs](https://docs.rs/termscp/badge.svg)](https://docs.rs/termscp)
[![Build](https://github.com/ChristianVisintin/TermSCP/workflows/Linux/badge.svg)](https://github.com/ChristianVisintin/TermSCP/actions) [![Build](https://github.com/ChristianVisintin/TermSCP/workflows/MacOS/badge.svg)](https://github.com/ChristianVisintin/TermSCP/actions) [![Build](https://github.com/ChristianVisintin/TermSCP/workflows/Windows/badge.svg)](https://github.com/ChristianVisintin/TermSCP/actions)
[![Build](https://github.com/ChristianVisintin/TermSCP/workflows/Linux/badge.svg)](https://github.com/ChristianVisintin/TermSCP/actions) [![Build](https://github.com/ChristianVisintin/TermSCP/workflows/MacOS/badge.svg)](https://github.com/ChristianVisintin/TermSCP/actions) [![Build](https://github.com/ChristianVisintin/TermSCP/workflows/Windows/badge.svg)](https://github.com/ChristianVisintin/TermSCP/actions) [![codecov](https://codecov.io/gh/ChristianVisintin/termscp/branch/main/graph/badge.svg?token=au67l7nQah)](https://codecov.io/gh/ChristianVisintin/termscp)
~ Basically, WinSCP on a terminal ~
Developed by Christian Visintin
Current version: 0.1.3 (13/12/2020)
Current version: 0.2.0 (21/12/2020)
---
@@ -18,11 +18,16 @@ Current version: 0.1.3 (13/12/2020)
- [Cargo 🦀](#cargo-)
- [Deb package 📦](#deb-package-)
- [RPM package 📦](#rpm-package-)
- [AUR Package 🔼](#aur-package-)
- [Chocolatey 🍫](#chocolatey-)
- [Brew 🍻](#brew-)
- [Usage ❓](#usage-)
- [Address argument](#address-argument)
- [How Password can be provided](#how-password-can-be-provided)
- [Address argument 🌎](#address-argument-)
- [How Password can be provided 🔐](#how-password-can-be-provided-)
- [Bookmarks ⭐](#bookmarks-)
- [Are my passwords Safe 😈](#are-my-passwords-safe-)
- [Text Editor ✏](#text-editor-)
- [How do I configure the text editor 🦥](#how-do-i-configure-the-text-editor-)
- [Keybindings ⌨](#keybindings-)
- [Documentation 📚](#documentation-)
- [Known issues 🧻](#known-issues-)
@@ -54,6 +59,8 @@ It happens quite often to me, when using SCP at work to forget the path of a fil
- SCP
- FTP and FTPS
- Practical user interface to explore and operate on the remote and on the local machine file system
- Bookmarks and recent connections can be saved to access quickly to your favourite hosts
- Supports text editors to view and edit text files
- Compatible with Windows, Linux, BSD and MacOS
- Written in Rust
- Easy to extend with new file transfers protocols
@@ -74,8 +81,8 @@ cargo install termscp
### Deb package 📦
Get `deb` package from [HERE](https://github.com/ChristianVisintin/TermSCP/releases/download/latest/termscp_0.1.3_amd64.deb)
or run `wget https://github.com/ChristianVisintin/TermSCP/releases/download/latest/termscp_0.1.3_amd64.deb`
Get `deb` package from [HERE](https://github.com/ChristianVisintin/TermSCP/releases/download/latest/termscp_0.2.0_amd64.deb)
or run `wget https://github.com/ChristianVisintin/TermSCP/releases/download/latest/termscp_0.2.0_amd64.deb`
then install through dpkg:
@@ -87,8 +94,8 @@ gdebi termscp_*.deb
### RPM package 📦
Get `rpm` package from [HERE](https://github.com/ChristianVisintin/TermSCP/releases/download/latest/termscp-0.1.3-1.x86_64.rpm)
or run `wget https://github.com/ChristianVisintin/TermSCP/releases/download/latest/termscp-0.1.3-1.x86_64.rpm`
Get `rpm` package from [HERE](https://github.com/ChristianVisintin/TermSCP/releases/download/latest/termscp-0.2.0-1.x86_64.rpm)
or run `wget https://github.com/ChristianVisintin/TermSCP/releases/download/latest/termscp-0.2.0-1.x86_64.rpm`
then install through rpm:
@@ -96,6 +103,14 @@ then install through rpm:
rpm -U termscp_*.rpm
```
### AUR Package 🔼
On Arch Linux based distribution, you can install termscp using for example [yay](https://github.com/Jguer/yay), which I recommend to install AUR packages.
```sh
yay -S termscp
```
### Chocolatey 🍫
You can install TermSCP on Windows using [chocolatey](https://chocolatey.org/)
@@ -106,7 +121,7 @@ Start PowerShell as administrator and run
choco install termscp
```
Alternatively you can download the ZIP file from [HERE](https://github.com/ChristianVisintin/TermSCP/releases/download/latest/termscp.0.1.3.nupkg)
Alternatively you can download the ZIP file from [HERE](https://github.com/ChristianVisintin/TermSCP/releases/download/latest/termscp.0.2.0.nupkg)
and then with PowerShell started with administrator previleges, run:
@@ -139,7 +154,7 @@ TermSCP can be started in two different mode, if no extra arguments is provided,
Alternatively, the user can provide an address as argument to skip the authentication form and starting directly the connection to the remote server.
### Address argument
### Address argument 🌎
The address argument has the following syntax:
@@ -167,7 +182,7 @@ Let's see some example of this particular syntax, since it's very comfortable an
termscp scp://omar@192.168.1.31:4022
```
#### How Password can be provided
#### How Password can be provided 🔐
You have probably noticed, that, when providing the address as argument, there's no way to provide the password.
Password can be basically provided through 3 ways when address argument is provided:
@@ -178,32 +193,81 @@ Password can be basically provided through 3 ways when address argument is provi
---
## Bookmarks ⭐
In TermSCP it is possible to save favourites hosts, which can be then loaded quickly from the main layout of termscp.
TermSCP will also save the last 16 hosts you connected to.
This feature allows you to load all the parameters required to connect to a certain remote, simply selecting the bookmark in the tab under the authentication form.
Bookmarks will be saved, if possible at:
- `$HOME/.config/termscp/` on Linux
- `FOLDERID_RoamingAppData\termscp\` on Windows
- `$HOME/Library/Application Support/termscp` on MacOs
For bookmarks only (this won't apply to recent hosts) it is also possible to save the password used to authenticate. The password is not saved by default and must be specified through the prompt when saving a new Bookmark.
> I was very undecided about storing passwords in termscp. The reason? Saving a password on your computer might give access to a hacker to any server you've registered. But I must admit by myself that for many machines typing the password everytime is really boring, also many times I have to work with machines in LAN, which wouldn't provide any advantage to an attacker, So I came out with a good compromise for passwords.
I warmly suggest you to follow these guidelines in order to decide whether you should or you shouldn't save passwords:
- **DON'T** save passwords for machines which are exposed on the internet, save passwords only for machines in LAN
- Make sure your machine is protected by attackers. If possible encrypt your disk and don't leave your PC unlocked while you're away.
- Preferably, save passwords only when a compromising of the target machine wouldn't be a problem.
To create a bookmark, just fulfill the authentication form and then input `CTRL+S`; you'll then be asked to give a name to your bookmark, and tadah, the bookmark has been created.
If you go to [gallery](#gallery-), there is a GIF showing how bookmarks work 💪.
### Are my passwords Safe 😈
Well, kinda.
As said before, bookmarks are saved in your configuration directory along with passwords. Passwords are obviously not plain text, they are encrypted with **AES-128**. Does this make them safe? Well, no, the key used to encrypt your passwords is generated at the first launch of termscp and stored on your drive. So it's still possible to retrieve the key to decrypt passwords. Luckily, the location of the key guarantees your key can't be read by users different from yours, but yeah, I still wouldn't save the password for a server exposed on the internet 😉.
---
## Text Editor ✏
TermSCP has, as you might have noticed, many features, one of these is the possibility to view and edit text file. It doesn't matter if the file is located on the local host or on the remote host, termscp provides the possibility to open a file in your favourite text editor.
In case the file is located on remote host, the file will be first downloaded into your temporary file directory and then, **only** if changes were made to the file, re-uploaded to the remote host. TermSCP checks if you made changes to the file calculating the digest of the file using `sha256`.
Just a reminder: **you can edit only textual file**; binary files are not supported.
### How do I configure the text editor 🦥
Text editor is automatically found using this [awesome crate](https://github.com/milkey-mouse/edit), if you want to change the text editor it has chosen for you, just set the `EDITOR` variable in your environment.
> This mechanism will probably change in 0.3.0, since I'm going to introduce the possibility to configure directly in termscp's settings.
---
## Keybindings ⌨
| Key | Command |
|---------------|-------------------------------------------------------|
| `<ESC>` | Disconnect from remote; return to authentication page |
| `<TAB>` | Switch between log tab and explorer |
| `<BACKSPACE>` | Go to previous directory in stack |
| `<RIGHT>` | Move to remote explorer tab |
| `<LEFT>` | Move to local explorer tab |
| `<UP>` | Move up in selected list |
| `<DOWN>` | Move down in selected list |
| `<PGUP>` | Move up in selected list by 8 rows |
| `<PGDOWN>` | Move down in selected list by 8 rows |
| `<ENTER>` | Enter directory |
| `<SPACE>` | Upload / download selected file |
| `<D>` | Make directory |
| `<E>` | Delete file (Same as `CANC`) |
| `<G>` | Go to supplied path |
| `<H>` | Show help |
| `<I>` | Show info about selected file or directory |
| `<L>` | Reload current directory's content |
| `<Q>` | Quit TermSCP |
| `<R>` | Rename file |
| `<U>` | Go to parent directory |
| `<DEL>` | Delete file |
| `<CTRL+C>` | Abort file transfer process |
| Key | Command | Reminder |
|---------------|-------------------------------------------------------|-----------|
| `<ESC>` | Disconnect from remote; return to authentication page | |
| `<TAB>` | Switch between log tab and explorer | |
| `<BACKSPACE>` | Go to previous directory in stack | |
| `<RIGHT>` | Move to remote explorer tab | |
| `<LEFT>` | Move to local explorer tab | |
| `<UP>` | Move up in selected list | |
| `<DOWN>` | Move down in selected list | |
| `<PGUP>` | Move up in selected list by 8 rows | |
| `<PGDOWN>` | Move down in selected list by 8 rows | |
| `<ENTER>` | Enter directory | |
| `<SPACE>` | Upload / download selected file | |
| `<C>` | Copy file/directory | Copy |
| `<D>` | Make directory | Directory |
| `<E>` | Delete file (Same as `CANC`) | Erase |
| `<G>` | Go to supplied path | Go to |
| `<H>` | Show help | Help |
| `<I>` | Show info about selected file or directory | Info |
| `<L>` | Reload current directory's content | List |
| `<O>` | Edit file; see [Text editor](#text-editor-) | Open |
| `<Q>` | Quit TermSCP | Quit |
| `<R>` | Rename file | Rename |
| `<U>` | Go to parent directory | Upper |
| `<DEL>` | Delete file | |
| `<CTRL+C>` | Abort file transfer process | |
---
@@ -218,16 +282,15 @@ The developer documentation can be found on Rust Docs at <https://docs.rs/termsc
- Ftp:
- Time in explorer is `1 Jan 1970`, but shouldn't be: that's because chrono can't parse date in a different locale. So if your server has a locale different from the one on your machine, it won't be able to parse the date.
- Some servers don't work: yes, some kind of ftp server don't work correctly, sometimes it won't display any files in the directories, some other times uploading files will fail. Up to date, `vsftpd` is the only one server which I saw working correctly with TermSCP. Am I going to solve this? I'd like to, but it's not my fault at all. Unfortunately [rust-ftp](https://github.com/mattnenterprise/rust-ftp) is an abandoned project (up to 2020), indeed I had to patch many stuff by myself. I'll try to solve these issues, but it will take a long time.
- Sftp:
- sftp is much slower than scp: Okay this is an annoying issue, and again: not my fault. It seems there is an issue with [ssh2-rs](https://github.com/alexcrichton/ssh2-rs) library. If you want to stay up to date with the status of this issue, subscribe to [this issue](https://github.com/alexcrichton/ssh2-rs/issues/206)
- `NoSuchFileOrDirectory` on connect: let me guess, you're running on WSL and you've installed termscp through cargo. I know about this issue and it's a glitch of WSL I guess. Don't worry about it, just move the termscp executable into another PATH location, such as `/usr/bin`.
---
## Upcoming Features 🧪
- **Bookmarks and recents**: possibility to save favourites and recent connections to user data, to connect quickly from main menu
- **Text viewer**: possibility to open and read file both on remote and on local host; this will also support syntax highlighting.
- **SSH Key storage**: termscp 0.3.0 will (finally) support the SSH key storage. From the configuration interface, you will be able to add SSH keys to the termscp's storage as you do indeed with other similiar clients.
- **User customizations**: termscp 0.3.0 will support some user customizations, such as the possibility to setup the text editor directly from termscp and the default communication protocol. Everything will be configurable directly from the termscp user interface.
- **Find command in explorer**: possibility to search for files in explorers.
---
@@ -249,6 +312,7 @@ TermSCP is powered by these aweseome projects:
- [bytesize](https://github.com/hyunsik/bytesize)
- [crossterm](https://github.com/crossterm-rs/crossterm)
- [edit](https://github.com/milkey-mouse/edit)
- [rpassword](https://github.com/conradkleinespel/rpassword)
- [ssh2-rs](https://github.com/alexcrichton/ssh2-rs)
- [textwrap](https://github.com/mgeisler/textwrap)
@@ -259,8 +323,18 @@ TermSCP is powered by these aweseome projects:
## Gallery 🎬
> Termscp Home
![Auth](assets/images/auth.gif)
> Bookmarks
![Bookmarks](assets/images/bookmarks.gif)
> Text editor
![TextEditor](assets/images/text-editor.gif)
---
## License 📃

Binary file not shown.

Before

Width:  |  Height:  |  Size: 210 KiB

After

Width:  |  Height:  |  Size: 314 KiB

BIN
assets/images/bookmarks.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 292 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB

5
codecov.yml Normal file
View File

@@ -0,0 +1,5 @@
ignore:
- src/main.rs
- src/lib.rs
- src/activity_manager.rs
- src/ui/

25
dist/build/README.md vendored Normal file
View File

@@ -0,0 +1,25 @@
# Build with Docker
- [Build with Docker](#build-with-docker)
- [Prerequisites](#prerequisites)
- [Build](#build)
---
## Prerequisites
- Docker
## Build
1. Build x86_64
this will build termscp for:
- Linux x86_64 Deb packages
- Linux x86_64 RPM packages
- Windows x86_64 MSVC packages
```sh
```

26
dist/build/deploy.sh vendored
View File

@@ -7,14 +7,32 @@ fi
VERSION=$1
# Create pkgs directory
cd ..
PKGS_DIR=$(pwd)/pkgs
cd -
mkdir -p ${PKGS_DIR}/
# Build x86_64
cd x86_64/
docker build --tag termscp-${VERSION}-x86_64 .
# Get pkgs
# Create container and get deb, rpm
cd -
# Create container
mkdir -p ${PKGS_DIR}/deb/
mkdir -p ${PKGS_DIR}/rpm/
CONTAINER_NAME=$(docker create termscp-${VERSION}-x86_64 termscp-${VERSION}-x86_64)
docker cp ${CONTAINER_NAME}:/usr/src/TermSCP/target/debian/termscp_${VERSION}_amd64.deb .
docker cp ${CONTAINER_NAME}:/usr/src/TermSCP/target/release/rpmbuild/RPMS/x86_64/termscp-${VERSION}-1.x86_64.rpm .
docker cp ${CONTAINER_NAME}:/usr/src/termscp/target/debian/termscp_${VERSION}_amd64.deb ${PKGS_DIR}/deb/
docker cp ${CONTAINER_NAME}:/usr/src/termscp/target/release/rpmbuild/RPMS/x86_64/termscp-${VERSION}-1.x86_64.rpm ${PKGS_DIR}/rpm/
# Build x86_64_archlinux
cd x86_64_archlinux/
docker build --tag termscp-${VERSION}-x86_64_archlinux .
# Create container and get AUR pkg
cd -
mkdir -p ${PKGS_DIR}/arch/
CONTAINER_NAME=$(docker create termscp-${VERSION}-x86_64_archlinux termscp-${VERSION}-x86_64_archlinux)
docker cp ${CONTAINER_NAME}:/usr/src/termscp/termscp-${VERSION}-x86_64.tar.gz ${PKGS_DIR}/arch/
docker cp ${CONTAINER_NAME}:/usr/src/termscp/PKGBUILD ${PKGS_DIR}/arch/
docker cp ${CONTAINER_NAME}:/usr/src/termscp/.SRCINFO ${PKGS_DIR}/arch/
# Replace termscp-bin with termscp in PKGBUILD
sed -i 's/termscp-bin/termscp/g' ${PKGS_DIR}/arch/PKGBUILD
exit $?

View File

@@ -6,9 +6,9 @@ RUN rustup target add x86_64-unknown-linux-gnu
# Install dependencies
RUN apt update && apt install -y rpm
# Clone repository
RUN git clone https://github.com/ChristianVisintin/TermSCP.git
RUN git clone https://github.com/ChristianVisintin/termscp.git
# Set workdir to termscp
WORKDIR /usr/src/TermSCP/
WORKDIR /usr/src/termscp/
# Install cargo RPM/Deb
RUN cargo install cargo-deb cargo-rpm
# Build for x86_64

33
dist/build/x86_64_archlinux/Dockerfile vendored Normal file
View File

@@ -0,0 +1,33 @@
FROM archlinux/archlinux:latest as builder
WORKDIR /usr/src/
# Install dependencies
RUN pacman -Syu --noconfirm \
git \
gcc \
openssl \
pkg-config \
sudo
# Create build user
RUN useradd build -m && \
passwd -d build && \
mkdir -p termscp && \
chown -R build.build termscp/
# Install rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > /tmp/rust.sh && \
chmod +x /tmp/rust.sh && \
/tmp/rust.sh -y
# Clone repository
RUN git clone https://github.com/ChristianVisintin/termscp.git
# Set workdir to termscp
WORKDIR /usr/src/termscp/
# Install cargo arxch
RUN source $HOME/.cargo/env && cargo install cargo-aur
# Build for x86_64
RUN source $HOME/.cargo/env && cargo build --release
# Build pkgs
RUN source $HOME/.cargo/env && cargo aur
# Create SRCINFO
RUN chown -R build.build ../termscp/ && sudo -u build bash -c 'makepkg --printsrcinfo > .SRCINFO'
CMD ["sh"]

14
dist/pkgs/arch/.SRCINFO vendored Normal file
View File

@@ -0,0 +1,14 @@
pkgbase = termscp-bin
pkgdesc = TermSCP is a SCP/SFTP/FTPS client for command line with an integrated UI to explore the remote file system. Basically WinSCP on a terminal.
pkgver = 0.2.0
pkgrel = 1
url = https://github.com/ChristianVisintin/termscp
arch = x86_64
license = GPL-3.0
provides = termscp
options = strip
source = https://github.com/ChristianVisintin/termscp/releases/download/v0.2.0/termscp-0.2.0-x86_64.tar.gz
sha256sums = b40f5223e74514c4a9855831da7b5c4a79d415822ec2840ccae98278a2176d01
pkgname = termscp-bin

16
dist/pkgs/arch/PKGBUILD vendored Normal file
View File

@@ -0,0 +1,16 @@
# Maintainer: Christian Visintin
pkgname=termscp
pkgver=0.2.0
pkgrel=1
pkgdesc="TermSCP is a SCP/SFTP/FTPS client for command line with an integrated UI to explore the remote file system. Basically WinSCP on a terminal."
url="https://github.com/ChristianVisintin/termscp"
license=("GPL-3.0")
arch=("x86_64")
provides=("termscp")
options=("strip")
source=("https://github.com/ChristianVisintin/termscp/releases/download/v$pkgver/termscp-$pkgver-x86_64.tar.gz")
sha256sums=("b40f5223e74514c4a9855831da7b5c4a79d415822ec2840ccae98278a2176d01")
package() {
install -Dm755 termscp -t "$pkgdir/usr/bin/"
}

View File

@@ -160,7 +160,7 @@ impl ActivityManager {
0 => None,
_ => Some(activity.password.clone()),
},
protocol: activity.protocol.clone(),
protocol: activity.protocol,
});
break;
}

197
src/bookmarks/mod.rs Normal file
View File

@@ -0,0 +1,197 @@
//! ## Bookmarks
//!
//! `bookmarks` is the module which provides data types and de/serializer for bookmarks
/*
*
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
pub mod serializer;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Deserialize, Serialize, std::fmt::Debug)]
/// ## UserHosts
///
/// UserHosts contains all the hosts saved by the user in the data storage
/// It contains both `Bookmark`
pub struct UserHosts {
pub bookmarks: HashMap<String, Bookmark>,
pub recents: HashMap<String, Bookmark>,
}
#[derive(Deserialize, Serialize, std::fmt::Debug, PartialEq)]
/// ## Bookmark
///
/// Bookmark describes a single bookmark entry in the user hosts storage
pub struct Bookmark {
pub address: String,
pub port: u16,
pub protocol: String,
pub username: String,
pub password: Option<String>, // Password is optional; base64, aes-128 encrypted password
}
// Errors
/// ## SerializerError
///
/// Contains the error for serializer/deserializer
#[derive(std::fmt::Debug)]
pub struct SerializerError {
kind: SerializerErrorKind,
msg: Option<String>,
}
/// ## SerializerErrorKind
///
/// Describes the kind of error for the serializer/deserializer
#[derive(std::fmt::Debug, PartialEq)]
pub enum SerializerErrorKind {
IoError,
SerializationError,
SyntaxError,
}
impl Default for UserHosts {
fn default() -> Self {
UserHosts {
bookmarks: HashMap::new(),
recents: HashMap::new(),
}
}
}
impl SerializerError {
/// ### new
///
/// Instantiate a new `SerializerError`
pub fn new(kind: SerializerErrorKind) -> SerializerError {
SerializerError { kind, msg: None }
}
/// ### new_ex
///
/// Instantiates a new `SerializerError` with description message
pub fn new_ex(kind: SerializerErrorKind, msg: String) -> SerializerError {
let mut err: SerializerError = SerializerError::new(kind);
err.msg = Some(msg);
err
}
}
impl std::fmt::Display for SerializerError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let err: String = match &self.kind {
SerializerErrorKind::IoError => String::from("IO error"),
SerializerErrorKind::SerializationError => String::from("Serialization error"),
SerializerErrorKind::SyntaxError => String::from("Syntax error"),
};
match &self.msg {
Some(msg) => write!(f, "{} ({})", err, msg),
None => write!(f, "{}", err),
}
}
}
// Tests
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bookmarks_bookmark_new() {
let bookmark: Bookmark = Bookmark {
address: String::from("192.168.1.1"),
port: 22,
protocol: String::from("SFTP"),
username: String::from("root"),
password: Some(String::from("password")),
};
let recent: Bookmark = Bookmark {
address: String::from("192.168.1.2"),
port: 22,
protocol: String::from("SCP"),
username: String::from("admin"),
password: Some(String::from("password")),
};
let mut bookmarks: HashMap<String, Bookmark> = HashMap::with_capacity(1);
bookmarks.insert(String::from("test"), bookmark);
let mut recents: HashMap<String, Bookmark> = HashMap::with_capacity(1);
recents.insert(String::from("ISO20201218T181432"), recent);
let hosts: UserHosts = UserHosts {
bookmarks: bookmarks,
recents: recents,
};
// Verify
let bookmark: &Bookmark = hosts.bookmarks.get(&String::from("test")).unwrap();
assert_eq!(bookmark.address, String::from("192.168.1.1"));
assert_eq!(bookmark.port, 22);
assert_eq!(bookmark.protocol, String::from("SFTP"));
assert_eq!(bookmark.username, String::from("root"));
assert_eq!(
*bookmark.password.as_ref().unwrap(),
String::from("password")
);
let bookmark: &Bookmark = hosts
.recents
.get(&String::from("ISO20201218T181432"))
.unwrap();
assert_eq!(bookmark.address, String::from("192.168.1.2"));
assert_eq!(bookmark.port, 22);
assert_eq!(bookmark.protocol, String::from("SCP"));
assert_eq!(bookmark.username, String::from("admin"));
assert_eq!(
*bookmark.password.as_ref().unwrap(),
String::from("password")
);
}
#[test]
fn test_bookmarks_bookmark_errors() {
let error: SerializerError = SerializerError::new(SerializerErrorKind::SyntaxError);
assert_eq!(error.kind, SerializerErrorKind::SyntaxError);
assert!(error.msg.is_none());
assert_eq!(format!("{}", error), String::from("Syntax error"));
let error: SerializerError =
SerializerError::new_ex(SerializerErrorKind::SyntaxError, String::from("bad syntax"));
assert_eq!(error.kind, SerializerErrorKind::SyntaxError);
assert!(error.msg.is_some());
assert_eq!(
format!("{}", error),
String::from("Syntax error (bad syntax)")
);
// Fmt
assert_eq!(
format!("{}", SerializerError::new(SerializerErrorKind::IoError)),
String::from("IO error")
);
assert_eq!(
format!(
"{}",
SerializerError::new(SerializerErrorKind::SerializationError)
),
String::from("Serialization error")
);
}
}

220
src/bookmarks/serializer.rs Normal file
View File

@@ -0,0 +1,220 @@
//! ## Serializer
//!
//! `serializer` is the module which provides the serializer/deserializer for bookmarks
/*
*
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
use super::{SerializerError, SerializerErrorKind, UserHosts};
use std::io::{Read, Write};
pub struct BookmarkSerializer {}
impl BookmarkSerializer {
/// ### serialize
///
/// Serialize `UserHosts` into TOML and write content to writable
pub fn serialize(
&self,
mut writable: Box<dyn Write>,
hosts: &UserHosts,
) -> Result<(), SerializerError> {
// Serialize content
let data: String = match toml::ser::to_string(hosts) {
Ok(dt) => dt,
Err(err) => {
return Err(SerializerError::new_ex(
SerializerErrorKind::SerializationError,
err.to_string(),
))
}
};
// Write file
match writable.write_all(data.as_bytes()) {
Ok(_) => Ok(()),
Err(err) => Err(SerializerError::new_ex(
SerializerErrorKind::IoError,
err.to_string(),
)),
}
}
/// ### deserialize
///
/// Read data from readable and deserialize its content as TOML
pub fn deserialize(&self, mut readable: Box<dyn Read>) -> Result<UserHosts, SerializerError> {
// Read file content
let mut data: String = String::new();
if let Err(err) = readable.read_to_string(&mut data) {
return Err(SerializerError::new_ex(
SerializerErrorKind::IoError,
err.to_string(),
));
}
// Deserialize
match toml::de::from_str(data.as_str()) {
Ok(hosts) => Ok(hosts),
Err(err) => Err(SerializerError::new_ex(
SerializerErrorKind::SyntaxError,
err.to_string(),
)),
}
}
}
// Tests
#[cfg(test)]
mod tests {
use super::super::Bookmark;
use super::*;
use std::collections::HashMap;
use std::io::{Seek, SeekFrom};
#[test]
fn test_bookmarks_serializer_deserialize_ok() {
let toml_file: tempfile::NamedTempFile = create_good_toml();
toml_file.as_file().sync_all().unwrap();
toml_file.as_file().seek(SeekFrom::Start(0)).unwrap();
// Parse
let deserializer: BookmarkSerializer = BookmarkSerializer {};
let hosts = deserializer.deserialize(Box::new(toml_file));
assert!(hosts.is_ok());
let hosts: UserHosts = hosts.ok().unwrap();
// Verify hosts
// Verify recents
assert_eq!(hosts.recents.len(), 1);
let host: &Bookmark = hosts.recents.get("ISO20201215T094000Z").unwrap();
assert_eq!(host.address, String::from("172.16.104.10"));
assert_eq!(host.port, 22);
assert_eq!(host.protocol, String::from("SCP"));
assert_eq!(host.username, String::from("root"));
assert_eq!(host.password, None);
// Verify bookmarks
assert_eq!(hosts.bookmarks.len(), 3);
let host: &Bookmark = hosts.bookmarks.get("raspberrypi2").unwrap();
assert_eq!(host.address, String::from("192.168.1.31"));
assert_eq!(host.port, 22);
assert_eq!(host.protocol, String::from("SFTP"));
assert_eq!(host.username, String::from("root"));
assert_eq!(*host.password.as_ref().unwrap(), String::from("mypassword"));
let host: &Bookmark = hosts.bookmarks.get("msi-estrem").unwrap();
assert_eq!(host.address, String::from("192.168.1.30"));
assert_eq!(host.port, 22);
assert_eq!(host.protocol, String::from("SFTP"));
assert_eq!(host.username, String::from("cvisintin"));
assert_eq!(*host.password.as_ref().unwrap(), String::from("mysecret"));
let host: &Bookmark = hosts.bookmarks.get("aws-server-prod1").unwrap();
assert_eq!(host.address, String::from("51.23.67.12"));
assert_eq!(host.port, 21);
assert_eq!(host.protocol, String::from("FTPS"));
assert_eq!(host.username, String::from("aws001"));
assert_eq!(host.password, None);
}
#[test]
fn test_bookmarks_serializer_deserialize_nok() {
let toml_file: tempfile::NamedTempFile = create_bad_toml();
toml_file.as_file().sync_all().unwrap();
toml_file.as_file().seek(SeekFrom::Start(0)).unwrap();
// Parse
let deserializer: BookmarkSerializer = BookmarkSerializer {};
assert!(deserializer.deserialize(Box::new(toml_file)).is_err());
}
#[test]
fn test_bookmarks_serializer_serialize() {
let mut bookmarks: HashMap<String, Bookmark> = HashMap::with_capacity(2);
// Push two samples
bookmarks.insert(
String::from("raspberrypi2"),
Bookmark {
address: String::from("192.168.1.31"),
port: 22,
protocol: String::from("SFTP"),
username: String::from("root"),
password: None,
},
);
bookmarks.insert(
String::from("msi-estrem"),
Bookmark {
address: String::from("192.168.1.30"),
port: 4022,
protocol: String::from("SFTP"),
username: String::from("cvisintin"),
password: Some(String::from("password")),
},
);
let mut recents: HashMap<String, Bookmark> = HashMap::with_capacity(1);
recents.insert(
String::from("ISO20201215T094000Z"),
Bookmark {
address: String::from("192.168.1.254"),
port: 3022,
protocol: String::from("SCP"),
username: String::from("omar"),
password: Some(String::from("aaa")),
},
);
let tmpfile: tempfile::NamedTempFile = tempfile::NamedTempFile::new().unwrap();
// Serialize
let deserializer: BookmarkSerializer = BookmarkSerializer {};
let hosts: UserHosts = UserHosts { bookmarks, recents };
assert!(deserializer.serialize(Box::new(tmpfile), &hosts).is_ok());
}
fn create_good_toml() -> tempfile::NamedTempFile {
// Write
let mut tmpfile: tempfile::NamedTempFile = tempfile::NamedTempFile::new().unwrap();
let file_content: &str = r#"
[bookmarks]
raspberrypi2 = { address = "192.168.1.31", port = 22, protocol = "SFTP", username = "root", password = "mypassword" }
msi-estrem = { address = "192.168.1.30", port = 22, protocol = "SFTP", username = "cvisintin", password = "mysecret" }
aws-server-prod1 = { address = "51.23.67.12", port = 21, protocol = "FTPS", username = "aws001" }
[recents]
ISO20201215T094000Z = { address = "172.16.104.10", port = 22, protocol = "SCP", username = "root" }
"#;
tmpfile.write_all(file_content.as_bytes()).unwrap();
//write!(tmpfile, "[bookmarks]\nraspberrypi2 = {{ address = \"192.168.1.31\", port = 22, protocol = \"SFTP\", username = \"root\" }}\nmsi-estrem = {{ address = \"192.168.1.30\", port = 22, protocol = \"SFTP\", username = \"cvisintin\" }}\naws-server-prod1 = {{ address = \"51.23.67.12\", port = 21, protocol = \"FTPS\", username = \"aws001\" }}\n\n[recents]\nISO20201215T094000Z = {{ address = \"172.16.104.10\", port = 22, protocol = \"SCP\", username = \"root\" }}\n");
tmpfile
}
fn create_bad_toml() -> tempfile::NamedTempFile {
// Write
let mut tmpfile: tempfile::NamedTempFile = tempfile::NamedTempFile::new().unwrap();
let file_content: &str = r#"
[bookmarks]
raspberrypi2 = { address = "192.168.1.31", port = 22, protocol = "SFTP", username = "root"}
msi-estrem = { address = "192.168.1.30", port = 22, protocol = "SFTP" }
aws-server-prod1 = { address = "51.23.67.12", port = 21, protocol = "FTPS", username = "aws001" }
[recents]
ISO20201215T094000Z = { address = "172.16.104.10", protocol = "SCP", username = "root", port = 22 }
"#;
tmpfile.write_all(file_content.as_bytes()).unwrap();
tmpfile
}
}

View File

@@ -30,7 +30,7 @@ extern crate regex;
use super::{FileTransfer, FileTransferError, FileTransferErrorType};
use crate::fs::{FsDirectory, FsEntry, FsFile};
use crate::utils::lstime_to_systime;
use crate::utils::parser::parse_lstime;
// Includes
use ftp4::native_tls::TlsConnector;
@@ -142,7 +142,7 @@ impl FtpFileTransfer {
(owner_pex, group_pex, others_pex)
};
// Parse mtime and convert to SystemTime
let mtime: SystemTime = match lstime_to_systime(
let mtime: SystemTime = match parse_lstime(
metadata.get(7).unwrap().as_str(),
"%b %d %Y",
"%b %d %H:%M",
@@ -344,6 +344,16 @@ impl FileTransfer for FtpFileTransfer {
}
}
/// ### copy
///
/// Copy file to destination
fn copy(&mut self, _src: &FsEntry, _dst: &Path) -> Result<(), FileTransferError> {
// FTP doesn't support file copy
Err(FileTransferError::new(
FileTransferErrorType::UnsupportedFeature,
))
}
/// ### list_dir
///
/// List directory entries
@@ -796,6 +806,35 @@ mod tests {
assert!(ftp.disconnect().is_ok());
}
#[test]
fn test_filetransfer_sftp_copy() {
let mut ftp: FtpFileTransfer = FtpFileTransfer::new(false);
// Connect
assert!(ftp
.connect(String::from("speedtest.tele2.net"), 21, None, None)
.is_ok());
// Pwd
assert_eq!(ftp.pwd().ok().unwrap(), PathBuf::from("/"));
// Copy
let file: FsFile = FsFile {
name: String::from("readme.txt"),
abs_path: PathBuf::from("/readme.txt"),
last_change_time: SystemTime::UNIX_EPOCH,
last_access_time: SystemTime::UNIX_EPOCH,
creation_time: SystemTime::UNIX_EPOCH,
size: 0,
ftype: Some(String::from("txt")), // File type
readonly: true,
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: Some((6, 4, 4)), // UNIX only
};
assert!(ftp
.copy(&FsEntry::File(file), &Path::new("/tmp/dest.txt"))
.is_err());
}
/* NOTE: they don't work
#[test]
fn test_filetransfer_ftp_list_dir() {
@@ -866,4 +905,31 @@ mod tests {
// Disconnect
assert!(ftp.disconnect().is_ok());
}*/
#[test]
fn test_filetransfer_ftp_uninitialized() {
let file: FsFile = FsFile {
name: String::from("omar.txt"),
abs_path: PathBuf::from("/omar.txt"),
last_change_time: SystemTime::UNIX_EPOCH,
last_access_time: SystemTime::UNIX_EPOCH,
creation_time: SystemTime::UNIX_EPOCH,
size: 0,
ftype: Some(String::from("txt")), // File type
readonly: true,
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: Some((6, 4, 4)), // UNIX only
};
let mut ftp: FtpFileTransfer = FtpFileTransfer::new(false);
assert!(ftp.change_dir(Path::new("/tmp")).is_err());
assert!(ftp.disconnect().is_err());
assert!(ftp.list_dir(Path::new("/tmp")).is_err());
assert!(ftp.mkdir(Path::new("/tmp")).is_err());
assert!(ftp.pwd().is_err());
assert!(ftp.stat(Path::new("/tmp")).is_err());
assert!(ftp.recv_file(&file).is_err());
assert!(ftp.send_file(&file, Path::new("/tmp/omar.txt")).is_err());
}
}

View File

@@ -37,7 +37,7 @@ pub mod sftp_transfer;
///
/// This enum defines the different transfer protocol available in TermSCP
#[derive(std::cmp::PartialEq, std::fmt::Debug, std::clone::Clone)]
#[derive(PartialEq, std::fmt::Debug, std::clone::Clone, Copy)]
pub enum FileTransferProtocol {
Sftp,
Scp,
@@ -78,10 +78,7 @@ impl FileTransferError {
///
/// Instantiates a new FileTransferError
pub fn new(code: FileTransferErrorType) -> FileTransferError {
FileTransferError {
code,
msg: None,
}
FileTransferError { code, msg: None }
}
/// ### new_ex
@@ -102,7 +99,7 @@ impl std::fmt::Display for FileTransferError {
FileTransferErrorType::ConnectionError => String::from("Connection error"),
FileTransferErrorType::DirStatFailed => String::from("Could not stat directory"),
FileTransferErrorType::FileCreateDenied => String::from("Failed to create file"),
FileTransferErrorType::IoErr(err) => format!("IO Error: {}", err),
FileTransferErrorType::IoErr(err) => format!("IO error: {}", err),
FileTransferErrorType::NoSuchFileOrDirectory => {
String::from("No such file or directory")
}
@@ -160,6 +157,11 @@ pub trait FileTransfer {
fn change_dir(&mut self, dir: &Path) -> Result<PathBuf, FileTransferError>;
/// ### copy
///
/// Copy file to destination
fn copy(&mut self, src: &FsEntry, dst: &Path) -> Result<(), FileTransferError>;
/// ### list_dir
///
/// List directory entries
@@ -193,7 +195,11 @@ pub trait FileTransfer {
/// File name is referred to the name of the file as it will be saved
/// Data contains the file data
/// Returns file and its size
fn send_file(&mut self, local: &FsFile, file_name: &Path) -> Result<Box<dyn Write>, FileTransferError>;
fn send_file(
&mut self,
local: &FsFile,
file_name: &Path,
) -> Result<Box<dyn Write>, FileTransferError>;
/// ### recv_file
///
@@ -219,3 +225,113 @@ pub trait FileTransfer {
/// You must call this method each time you want to finalize the read of the remote file.
fn on_recv(&mut self, readable: Box<dyn Read>) -> Result<(), FileTransferError>;
}
// Tests
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_filetransfer_mod_protocol() {
assert_eq!(
FileTransferProtocol::Ftp(true),
FileTransferProtocol::Ftp(true)
);
assert_eq!(
FileTransferProtocol::Ftp(false),
FileTransferProtocol::Ftp(false)
);
}
#[test]
fn test_filetransfer_mod_error() {
let err: FileTransferError = FileTransferError::new_ex(
FileTransferErrorType::IoErr(std::io::Error::from(std::io::ErrorKind::AddrInUse)),
String::from("non va una mazza"),
);
assert_eq!(*err.msg.as_ref().unwrap(), String::from("non va una mazza"));
assert_eq!(
format!("{}", err),
String::from("IO error: address in use (non va una mazza)")
);
assert_eq!(
format!(
"{}",
FileTransferError::new(FileTransferErrorType::AuthenticationFailed)
),
String::from("Authentication failed")
);
assert_eq!(
format!(
"{}",
FileTransferError::new(FileTransferErrorType::BadAddress)
),
String::from("Bad address syntax")
);
assert_eq!(
format!(
"{}",
FileTransferError::new(FileTransferErrorType::ConnectionError)
),
String::from("Connection error")
);
assert_eq!(
format!(
"{}",
FileTransferError::new(FileTransferErrorType::DirStatFailed)
),
String::from("Could not stat directory")
);
assert_eq!(
format!(
"{}",
FileTransferError::new(FileTransferErrorType::FileCreateDenied)
),
String::from("Failed to create file")
);
assert_eq!(
format!(
"{}",
FileTransferError::new(FileTransferErrorType::NoSuchFileOrDirectory)
),
String::from("No such file or directory")
);
assert_eq!(
format!(
"{}",
FileTransferError::new(FileTransferErrorType::PexError)
),
String::from("Not enough permissions")
);
assert_eq!(
format!(
"{}",
FileTransferError::new(FileTransferErrorType::ProtocolError)
),
String::from("Protocol error")
);
assert_eq!(
format!(
"{}",
FileTransferError::new(FileTransferErrorType::SslError)
),
String::from("SSL error")
);
assert_eq!(
format!(
"{}",
FileTransferError::new(FileTransferErrorType::UninitializedSession)
),
String::from("Uninitialized session")
);
assert_eq!(
format!(
"{}",
FileTransferError::new(FileTransferErrorType::UnsupportedFeature)
),
String::from("Unsupported feature")
);
}
}

View File

@@ -30,7 +30,7 @@ extern crate ssh2;
// Locals
use super::{FileTransfer, FileTransferError, FileTransferErrorType};
use crate::fs::{FsDirectory, FsEntry, FsFile};
use crate::utils::lstime_to_systime;
use crate::utils::parser::parse_lstime;
// Includes
use regex::Regex;
@@ -151,7 +151,7 @@ impl ScpFileTransfer {
(owner_pex, group_pex, others_pex)
};
// Parse mtime and convert to SystemTime
let mtime: SystemTime = match lstime_to_systime(
let mtime: SystemTime = match parse_lstime(
metadata.get(7).unwrap().as_str(),
"%b %d %Y",
"%b %d %H:%M",
@@ -185,7 +185,7 @@ impl ScpFileTransfer {
Some(p) => match self.stat(p.as_path()) {
Ok(e) => Some(Box::new(e)),
Err(_) => None, // Ignore errors
}
},
};
// Check if file_name is '.' or '..'
if file_name.as_str() == "." || file_name.as_str() == ".." {
@@ -468,6 +468,47 @@ impl FileTransfer for ScpFileTransfer {
}
}
/// ### copy
///
/// Copy file to destination
fn copy(&mut self, src: &FsEntry, dst: &Path) -> Result<(), FileTransferError> {
match self.is_connected() {
true => {
// Run `cp -rf`
let p: PathBuf = self.wrkdir.clone();
match self.perform_shell_cmd_with_path(
p.as_path(),
format!(
"cp -rf \"{}\" \"{}\"; echo $?",
src.get_abs_path().display(),
dst.display()
)
.as_str(),
) {
Ok(output) =>
// Check if output is 0
{
match output.as_str().trim() == "0" {
true => Ok(()), // File copied
false => Err(FileTransferError::new_ex(
// Could not copy file
FileTransferErrorType::FileCreateDenied,
format!("\"{}\"", dst.display()),
)),
}
}
Err(err) => Err(FileTransferError::new_ex(
FileTransferErrorType::ProtocolError,
format!("{}", err),
)),
}
}
false => Err(FileTransferError::new(
FileTransferErrorType::UninitializedSession,
)),
}
}
/// ### list_dir
///
/// List directory entries
@@ -1029,4 +1070,31 @@ mod tests {
assert!(client.disconnect().is_ok());
}
*/
#[test]
fn test_filetransfer_scp_uninitialized() {
let file: FsFile = FsFile {
name: String::from("omar.txt"),
abs_path: PathBuf::from("/omar.txt"),
last_change_time: SystemTime::UNIX_EPOCH,
last_access_time: SystemTime::UNIX_EPOCH,
creation_time: SystemTime::UNIX_EPOCH,
size: 0,
ftype: Some(String::from("txt")), // File type
readonly: true,
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: Some((6, 4, 4)), // UNIX only
};
let mut scp: ScpFileTransfer = ScpFileTransfer::new();
assert!(scp.change_dir(Path::new("/tmp")).is_err());
assert!(scp.disconnect().is_err());
assert!(scp.list_dir(Path::new("/tmp")).is_err());
assert!(scp.mkdir(Path::new("/tmp")).is_err());
assert!(scp.pwd().is_err());
assert!(scp.stat(Path::new("/tmp")).is_err());
assert!(scp.recv_file(&file).is_err());
assert!(scp.send_file(&file, Path::new("/tmp/omar.txt")).is_err());
}
}

View File

@@ -348,6 +348,16 @@ impl FileTransfer for SftpFileTransfer {
}
}
/// ### copy
///
/// Copy file to destination
fn copy(&mut self, _src: &FsEntry, _dst: &Path) -> Result<(), FileTransferError> {
// SFTP doesn't support file copy
Err(FileTransferError::new(
FileTransferErrorType::UnsupportedFeature,
))
}
/// ### list_dir
///
/// List directory entries
@@ -701,6 +711,41 @@ mod tests {
assert!(client.disconnect().is_ok());
}
#[test]
fn test_filetransfer_sftp_copy() {
let mut client: SftpFileTransfer = SftpFileTransfer::new();
assert!(client
.connect(
String::from("test.rebex.net"),
22,
Some(String::from("demo")),
Some(String::from("password"))
)
.is_ok());
// Check session and sftp
assert!(client.session.is_some());
assert!(client.sftp.is_some());
assert_eq!(client.wrkdir, PathBuf::from("/"));
// Copy
let file: FsFile = FsFile {
name: String::from("readme.txt"),
abs_path: PathBuf::from("/readme.txt"),
last_change_time: SystemTime::UNIX_EPOCH,
last_access_time: SystemTime::UNIX_EPOCH,
creation_time: SystemTime::UNIX_EPOCH,
size: 0,
ftype: Some(String::from("txt")), // File type
readonly: true,
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: Some((6, 4, 4)), // UNIX only
};
assert!(client
.copy(&FsEntry::File(file), &Path::new("/tmp/dest.txt"))
.is_err());
}
#[test]
fn test_filetransfer_sftp_cwd_error() {
let mut client: SftpFileTransfer = SftpFileTransfer::new();
@@ -859,4 +904,31 @@ mod tests {
assert!(client.disconnect().is_ok());
}
*/
#[test]
fn test_filetransfer_sftp_uninitialized() {
let file: FsFile = FsFile {
name: String::from("omar.txt"),
abs_path: PathBuf::from("/omar.txt"),
last_change_time: SystemTime::UNIX_EPOCH,
last_access_time: SystemTime::UNIX_EPOCH,
creation_time: SystemTime::UNIX_EPOCH,
size: 0,
ftype: Some(String::from("txt")), // File type
readonly: true,
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: Some((6, 4, 4)), // UNIX only
};
let mut sftp: SftpFileTransfer = SftpFileTransfer::new();
assert!(sftp.change_dir(Path::new("/tmp")).is_err());
assert!(sftp.disconnect().is_err());
assert!(sftp.list_dir(Path::new("/tmp")).is_err());
assert!(sftp.mkdir(Path::new("/tmp")).is_err());
assert!(sftp.pwd().is_err());
assert!(sftp.stat(Path::new("/tmp")).is_err());
assert!(sftp.recv_file(&file).is_err());
assert!(sftp.send_file(&file, Path::new("/tmp/omar.txt")).is_err());
}
}

View File

@@ -27,7 +27,7 @@ extern crate bytesize;
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
extern crate users;
use crate::utils::{fmt_pex, time_to_str};
use crate::utils::fmt::{fmt_pex, fmt_time};
use bytesize::ByteSize;
use std::path::PathBuf;
@@ -201,6 +201,13 @@ impl FsEntry {
matches!(self, FsEntry::Directory(_))
}
/// ### is_file
///
/// Returns whether a FsEntry is a File
pub fn is_file(&self) -> bool {
matches!(self, FsEntry::File(_))
}
/// ### get_realfile
///
/// Return the real file pointed by a `FsEntry`
@@ -244,12 +251,12 @@ impl std::fmt::Display for FsEntry {
Some(user) => user.name().to_string_lossy().to_string(),
None => uid.to_string(),
},
None => String::from("0"),
None => 0.to_string(),
};
#[cfg(target_os = "windows")]
let username: usize = match self.get_user() {
Some(uid) => uid as usize,
None => 0,
let username: String = match self.get_user() {
Some(uid) => uid.to_string(),
None => 0.to_string(),
};
// Get group
/*
@@ -264,7 +271,7 @@ impl std::fmt::Display for FsEntry {
// Get byte size
let size: ByteSize = ByteSize(self.get_size() as u64);
// Get date
let datetime: String = time_to_str(self.get_last_change_time(), "%b %d %Y %H:%M");
let datetime: String = fmt_time(self.get_last_change_time(), "%b %d %Y %H:%M");
// Set file name (or elide if too long)
let name: String = self.get_name();
let name: String = match name.len() >= 24 {
@@ -273,7 +280,7 @@ impl std::fmt::Display for FsEntry {
};
write!(
f,
"{:24}\t{:12}\t{:12}\t{:9}\t{:17}",
"{:24}\t{:12}\t{:12}\t{:10}\t{:17}",
name, mode, username, size, datetime
)
}
@@ -310,6 +317,7 @@ mod tests {
assert_eq!(entry.get_group(), Some(0));
assert_eq!(entry.is_symlink(), false);
assert_eq!(entry.is_dir(), true);
assert_eq!(entry.is_file(), false);
assert_eq!(entry.get_unix_pex(), Some((7, 5, 5)));
}
@@ -342,6 +350,7 @@ mod tests {
assert_eq!(entry.get_unix_pex(), Some((6, 4, 4)));
assert_eq!(entry.is_symlink(), false);
assert_eq!(entry.is_dir(), false);
assert_eq!(entry.is_file(), true);
}
#[test]
@@ -435,4 +444,194 @@ mod tests {
PathBuf::from("/home/cvisintin/projects")
);
}
#[test]
fn test_fs_fmt_file() {
let t: SystemTime = SystemTime::now();
let entry: FsEntry = FsEntry::File(FsFile {
name: String::from("bar.txt"),
abs_path: PathBuf::from("/bar.txt"),
last_change_time: t,
last_access_time: t,
creation_time: t,
size: 8192,
readonly: false,
ftype: Some(String::from("txt")),
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: Some((6, 4, 4)), // UNIX only
});
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
assert_eq!(
format!("{}", entry),
format!(
"bar.txt \t-rw-r--r-- \troot \t8.2 KB \t{}",
fmt_time(t, "%b %d %Y %H:%M")
)
);
#[cfg(target_os = "windows")]
assert_eq!(
format!("{}", entry),
format!(
"bar.txt \t-rw-r--r-- \t0 \t8.2 KB \t{}",
fmt_time(t, "%b %d %Y %H:%M")
)
);
// Elide name
let entry: FsEntry = FsEntry::File(FsFile {
name: String::from("piroparoporoperoperupupu.txt"),
abs_path: PathBuf::from("/bar.txt"),
last_change_time: t,
last_access_time: t,
creation_time: t,
size: 8192,
readonly: false,
ftype: Some(String::from("txt")),
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: Some((6, 4, 4)), // UNIX only
});
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
assert_eq!(
format!("{}", entry),
format!(
"piroparoporoperoperu... \t-rw-r--r-- \troot \t8.2 KB \t{}",
fmt_time(t, "%b %d %Y %H:%M")
)
);
#[cfg(target_os = "windows")]
assert_eq!(
format!("{}", entry),
format!(
"piroparoporoperoperu... \t-rw-r--r-- \t0 \t8.2 KB \t{}",
fmt_time(t, "%b %d %Y %H:%M")
)
);
// No pex
let entry: FsEntry = FsEntry::File(FsFile {
name: String::from("bar.txt"),
abs_path: PathBuf::from("/bar.txt"),
last_change_time: t,
last_access_time: t,
creation_time: t,
size: 8192,
readonly: false,
ftype: Some(String::from("txt")),
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: None, // UNIX only
});
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
assert_eq!(
format!("{}", entry),
format!(
"bar.txt \t-????????? \troot \t8.2 KB \t{}",
fmt_time(t, "%b %d %Y %H:%M")
)
);
#[cfg(target_os = "windows")]
assert_eq!(
format!("{}", entry),
format!(
"bar.txt \t-????????? \t0 \t8.2 KB \t{}",
fmt_time(t, "%b %d %Y %H:%M")
)
);
// No user
let entry: FsEntry = FsEntry::File(FsFile {
name: String::from("bar.txt"),
abs_path: PathBuf::from("/bar.txt"),
last_change_time: t,
last_access_time: t,
creation_time: t,
size: 8192,
readonly: false,
ftype: Some(String::from("txt")),
symlink: None, // UNIX only
user: None, // UNIX only
group: Some(0), // UNIX only
unix_pex: None, // UNIX only
});
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
assert_eq!(
format!("{}", entry),
format!(
"bar.txt \t-????????? \t0 \t8.2 KB \t{}",
fmt_time(t, "%b %d %Y %H:%M")
)
);
#[cfg(target_os = "windows")]
assert_eq!(
format!("{}", entry),
format!(
"bar.txt \t-????????? \t0 \t8.2 KB \t{}",
fmt_time(t, "%b %d %Y %H:%M")
)
);
}
#[test]
fn test_fs_fmt_dir() {
let t_now: SystemTime = SystemTime::now();
let entry: FsEntry = FsEntry::Directory(FsDirectory {
name: String::from("projects"),
abs_path: PathBuf::from("/home/cvisintin/projects"),
last_change_time: t_now,
last_access_time: t_now,
creation_time: t_now,
readonly: false,
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: Some((7, 5, 5)), // UNIX only
});
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
assert_eq!(
format!("{}", entry),
format!(
"projects \tdrwxr-xr-x \troot \t4.1 KB \t{}",
fmt_time(t_now, "%b %d %Y %H:%M")
)
);
#[cfg(target_os = "windows")]
assert_eq!(
format!("{}", entry),
format!(
"projects \tdrwxr-xr-x \t0 \t4.1 KB \t{}",
fmt_time(t_now, "%b %d %Y %H:%M")
)
);
// No pex, no user
let entry: FsEntry = FsEntry::Directory(FsDirectory {
name: String::from("projects"),
abs_path: PathBuf::from("/home/cvisintin/projects"),
last_change_time: t_now,
last_access_time: t_now,
creation_time: t_now,
readonly: false,
symlink: None, // UNIX only
user: None, // UNIX only
group: Some(0), // UNIX only
unix_pex: None, // UNIX only
});
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
assert_eq!(
format!("{}", entry),
format!(
"projects \td????????? \t0 \t4.1 KB \t{}",
fmt_time(t_now, "%b %d %Y %H:%M")
)
);
#[cfg(target_os = "windows")]
assert_eq!(
format!("{}", entry),
format!(
"projects \td????????? \t0 \t4.1 KB \t{}",
fmt_time(t_now, "%b %d %Y %H:%M")
)
);
}
}

View File

@@ -28,7 +28,9 @@ use std::path::{Path, PathBuf};
use std::time::SystemTime;
// Metadata ext
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
use std::os::unix::fs::MetadataExt;
use std::fs::set_permissions;
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
use std::os::unix::fs::{MetadataExt, PermissionsExt};
// Locals
use crate::fs::{FsDirectory, FsEntry, FsFile};
@@ -74,7 +76,7 @@ impl std::fmt::Display for HostError {
HostErrorType::NoSuchFileOrDirectory => "No such file or directory",
HostErrorType::ReadonlyFile => "File is readonly",
HostErrorType::DirNotAccessible => "Could not access directory",
HostErrorType::FileNotAccessible => "Could not access directory",
HostErrorType::FileNotAccessible => "Could not access file",
HostErrorType::FileAlreadyExists => "File already exists",
HostErrorType::CouldNotCreateFile => "Could not create file",
HostErrorType::DeleteFailed => "Could not delete file",
@@ -134,7 +136,8 @@ impl Localhost {
/// ### change_wrkdir
///
/// Change working directory with the new provided directory
pub fn change_wrkdir(&mut self, new_dir: PathBuf) -> Result<PathBuf, HostError> {
pub fn change_wrkdir(&mut self, new_dir: &Path) -> Result<PathBuf, HostError> {
let new_dir: PathBuf = self.to_abs_path(new_dir);
// Check whether directory exists
if !self.file_exists(new_dir.as_path()) {
return Err(HostError::new(HostErrorType::NoSuchFileOrDirectory, None));
@@ -166,14 +169,7 @@ impl Localhost {
/// Extended option version of makedir.
/// ignex: don't report error if directory already exists
pub fn mkdir_ex(&mut self, dir_name: &Path, ignex: bool) -> Result<(), HostError> {
let dir_path: PathBuf = match dir_name.is_absolute() {
true => PathBuf::from(dir_name),
false => {
let mut dir_path: PathBuf = self.wrkdir.clone();
dir_path.push(dir_name);
dir_path
}
};
let dir_path: PathBuf = self.to_abs_path(dir_name);
// If dir already exists, return Error
if dir_path.exists() {
match ignex {
@@ -185,10 +181,7 @@ impl Localhost {
Ok(_) => {
// Update dir
if dir_name.is_relative() {
self.files = match self.scan_dir(self.wrkdir.as_path()) {
Ok(f) => f,
Err(err) => return Err(err),
};
self.files = self.scan_dir(self.wrkdir.as_path())?;
}
Ok(())
}
@@ -210,10 +203,7 @@ impl Localhost {
match std::fs::remove_dir_all(dir.abs_path.as_path()) {
Ok(_) => {
// Update dir
self.files = match self.scan_dir(self.wrkdir.as_path()) {
Ok(f) => f,
Err(err) => return Err(err),
};
self.files = self.scan_dir(self.wrkdir.as_path())?;
Ok(())
}
Err(err) => Err(HostError::new(HostErrorType::DeleteFailed, Some(err))),
@@ -228,10 +218,7 @@ impl Localhost {
match std::fs::remove_file(file.abs_path.as_path()) {
Ok(_) => {
// Update dir
self.files = match self.scan_dir(self.wrkdir.as_path()) {
Ok(f) => f,
Err(err) => return Err(err),
};
self.files = self.scan_dir(self.wrkdir.as_path())?;
Ok(())
}
Err(err) => Err(HostError::new(HostErrorType::DeleteFailed, Some(err))),
@@ -248,22 +235,85 @@ impl Localhost {
match std::fs::rename(abs_path.as_path(), dst_path) {
Ok(_) => {
// Scan dir
self.files = match self.scan_dir(self.wrkdir.as_path()) {
Ok(f) => f,
Err(err) => return Err(err),
};
self.files = self.scan_dir(self.wrkdir.as_path())?;
Ok(())
}
Err(err) => Err(HostError::new(HostErrorType::CouldNotCreateFile, Some(err))),
}
}
/// ### copy
///
/// Copy file to destination path
pub fn copy(&mut self, entry: &FsEntry, dst: &Path) -> Result<(), HostError> {
// Get absolute path of dest
let dst: PathBuf = self.to_abs_path(dst);
// Match entry
match entry {
FsEntry::File(file) => {
// Copy file
// If destination path is a directory, push file name
let dst: PathBuf = match dst.as_path().is_dir() {
true => {
let mut p: PathBuf = dst.clone();
p.push(file.name.as_str());
p
}
false => dst.clone(),
};
// Copy entry path to dst path
if let Err(err) = std::fs::copy(file.abs_path.as_path(), dst.as_path()) {
return Err(HostError::new(HostErrorType::CouldNotCreateFile, Some(err)));
}
}
FsEntry::Directory(dir) => {
// If destination path doesn't exist, create destination
if !dst.exists() {
self.mkdir(dst.as_path())?;
}
// Scan dir
let dir_files: Vec<FsEntry> = self.scan_dir(dir.abs_path.as_path())?;
// Iterate files
for dir_entry in dir_files.iter() {
// Calculate dst
let mut sub_dst: PathBuf = dst.clone();
sub_dst.push(dir_entry.get_name());
// Call function recursively
self.copy(dir_entry, sub_dst.as_path())?;
}
}
}
// Reload directory if dst is pwd
match dst.is_dir() {
true => {
if dst == self.pwd().as_path() {
self.files = self.scan_dir(self.wrkdir.as_path())?;
} else if let Some(parent) = dst.parent() {
// If parent is pwd, scan directory
if parent == self.pwd().as_path() {
self.files = self.scan_dir(self.wrkdir.as_path())?;
}
}
}
false => {
if let Some(parent) = dst.parent() {
// If parent is pwd, scan directory
if parent == self.pwd().as_path() {
self.files = self.scan_dir(self.wrkdir.as_path())?;
}
}
}
}
Ok(())
}
/// ### stat
///
/// Stat file and create a FsEntry
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
pub fn stat(&self, path: &Path) -> Result<FsEntry, HostError> {
let attr: Metadata = match fs::metadata(path) {
let path: PathBuf = self.to_abs_path(path);
let attr: Metadata = match fs::metadata(path.as_path()) {
Ok(metadata) => metadata,
Err(err) => return Err(HostError::new(HostErrorType::FileNotAccessible, Some(err))),
};
@@ -272,12 +322,12 @@ impl Localhost {
Ok(match path.is_dir() {
true => FsEntry::Directory(FsDirectory {
name: file_name,
abs_path: PathBuf::from(path),
abs_path: path.clone(),
last_change_time: attr.modified().unwrap_or(SystemTime::UNIX_EPOCH),
last_access_time: attr.accessed().unwrap_or(SystemTime::UNIX_EPOCH),
creation_time: attr.created().unwrap_or(SystemTime::UNIX_EPOCH),
readonly: attr.permissions().readonly(),
symlink: match fs::read_link(path) {
symlink: match fs::read_link(path.as_path()) {
Ok(p) => match self.stat(p.as_path()) {
Ok(entry) => Some(Box::new(entry)),
Err(_) => None,
@@ -296,14 +346,14 @@ impl Localhost {
};
FsEntry::File(FsFile {
name: file_name,
abs_path: PathBuf::from(path),
abs_path: path.clone(),
last_change_time: attr.modified().unwrap_or(SystemTime::UNIX_EPOCH),
last_access_time: attr.accessed().unwrap_or(SystemTime::UNIX_EPOCH),
creation_time: attr.created().unwrap_or(SystemTime::UNIX_EPOCH),
readonly: attr.permissions().readonly(),
size: attr.len() as usize,
ftype: extension,
symlink: match fs::read_link(path) {
symlink: match fs::read_link(path.as_path()) {
Ok(p) => match self.stat(p.as_path()) {
Ok(entry) => Some(Box::new(entry)),
Err(_) => None,
@@ -324,7 +374,8 @@ impl Localhost {
#[cfg(target_os = "windows")]
#[cfg(not(tarpaulin_include))]
pub fn stat(&self, path: &Path) -> Result<FsEntry, HostError> {
let attr: Metadata = match fs::metadata(path) {
let path: PathBuf = self.to_abs_path(path);
let attr: Metadata = match fs::metadata(path.as_path()) {
Ok(metadata) => metadata,
Err(err) => return Err(HostError::new(HostErrorType::FileNotAccessible, Some(err))),
};
@@ -333,12 +384,12 @@ impl Localhost {
Ok(match path.is_dir() {
true => FsEntry::Directory(FsDirectory {
name: file_name,
abs_path: PathBuf::from(path),
abs_path: path.clone(),
last_change_time: attr.modified().unwrap_or(SystemTime::UNIX_EPOCH),
last_access_time: attr.accessed().unwrap_or(SystemTime::UNIX_EPOCH),
creation_time: attr.created().unwrap_or(SystemTime::UNIX_EPOCH),
readonly: attr.permissions().readonly(),
symlink: match fs::read_link(path) {
symlink: match fs::read_link(path.as_path()) {
Ok(p) => match self.stat(p.as_path()) {
Ok(entry) => Some(Box::new(entry)),
Err(_) => None, // Ignore errors
@@ -357,14 +408,14 @@ impl Localhost {
};
FsEntry::File(FsFile {
name: file_name,
abs_path: PathBuf::from(path),
abs_path: path.clone(),
last_change_time: attr.modified().unwrap_or(SystemTime::UNIX_EPOCH),
last_access_time: attr.accessed().unwrap_or(SystemTime::UNIX_EPOCH),
creation_time: attr.created().unwrap_or(SystemTime::UNIX_EPOCH),
readonly: attr.permissions().readonly(),
size: attr.len() as usize,
ftype: extension,
symlink: match fs::read_link(path) {
symlink: match fs::read_link(path.as_path()) {
Ok(p) => match self.stat(p.as_path()) {
Ok(entry) => Some(Box::new(entry)),
Err(_) => None,
@@ -379,18 +430,39 @@ impl Localhost {
})
}
/// ### chmod
///
/// Change file mode to file, according to UNIX permissions
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
pub fn chmod(&self, path: &Path, pex: (u8, u8, u8)) -> Result<(), HostError> {
let path: PathBuf = self.to_abs_path(path);
// Get metadta
match fs::metadata(path.as_path()) {
Ok(metadata) => {
let mut mpex = metadata.permissions();
mpex.set_mode(self.mode_to_u32(pex));
match set_permissions(path.as_path(), mpex) {
Ok(_) => Ok(()),
Err(err) => Err(HostError::new(HostErrorType::FileNotAccessible, Some(err))),
}
}
Err(err) => Err(HostError::new(HostErrorType::FileNotAccessible, Some(err))),
}
}
/// ### open_file_read
///
/// Open file for read
pub fn open_file_read(&self, file: &Path) -> Result<File, HostError> {
if !self.file_exists(file) {
let file: PathBuf = self.to_abs_path(file);
if !self.file_exists(file.as_path()) {
return Err(HostError::new(HostErrorType::NoSuchFileOrDirectory, None));
}
match OpenOptions::new()
.create(false)
.read(true)
.write(false)
.open(file)
.open(file.as_path())
{
Ok(f) => Ok(f),
Err(err) => Err(HostError::new(HostErrorType::FileNotAccessible, Some(err))),
@@ -401,14 +473,15 @@ impl Localhost {
///
/// Open file for write
pub fn open_file_write(&self, file: &Path) -> Result<File, HostError> {
let file: PathBuf = self.to_abs_path(file);
match OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(file)
.open(file.as_path())
{
Ok(f) => Ok(f),
Err(err) => match self.file_exists(file) {
Err(err) => match self.file_exists(file.as_path()) {
true => Err(HostError::new(HostErrorType::ReadonlyFile, Some(err))),
false => Err(HostError::new(HostErrorType::FileNotAccessible, Some(err))),
},
@@ -418,7 +491,7 @@ impl Localhost {
/// ### file_exists
///
/// Returns whether provided file path exists
fn file_exists(&self, path: &Path) -> bool {
pub fn file_exists(&self, path: &Path) -> bool {
path.exists()
}
@@ -452,6 +525,29 @@ impl Localhost {
let others: u8 = (mode & 0x7) as u8;
(user, group, others)
}
/// mode_to_u32
///
/// Convert owner,group,others to u32
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
fn mode_to_u32(&self, mode: (u8, u8, u8)) -> u32 {
((mode.0 as u32) << 6) + ((mode.1 as u32) << 3) + mode.2 as u32
}
/// ### to_abs_path
///
/// Convert path to absolute path
fn to_abs_path(&self, p: &Path) -> PathBuf {
// Convert to abs path
match p.is_relative() {
true => {
let mut path: PathBuf = self.wrkdir.clone();
path.push(p);
path
}
false => PathBuf::from(p),
}
}
}
#[cfg(test)]
@@ -534,7 +630,7 @@ mod tests {
fn test_host_localhost_change_dir() {
let mut host: Localhost = Localhost::new(PathBuf::from("/dev")).ok().unwrap();
let new_dir: PathBuf = PathBuf::from("/dev");
assert!(host.change_wrkdir(new_dir.clone()).is_ok());
assert!(host.change_wrkdir(new_dir.as_path()).is_ok());
// Verify new files
// Scan dir
let entries = std::fs::read_dir(PathBuf::from(new_dir).as_path()).unwrap();
@@ -551,7 +647,7 @@ mod tests {
fn test_host_localhost_change_dir_failed() {
let mut host: Localhost = Localhost::new(PathBuf::from("/bin")).ok().unwrap();
let new_dir: PathBuf = PathBuf::from("/omar/gabber/123/456");
assert!(host.change_wrkdir(new_dir.clone()).is_ok());
assert!(host.change_wrkdir(new_dir.as_path()).is_ok());
}
#[test]
@@ -720,6 +816,171 @@ mod tests {
.is_err());
}
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
#[test]
fn test_host_chmod() {
let tmpdir: tempfile::TempDir = tempfile::TempDir::new().unwrap();
let file: tempfile::NamedTempFile = create_sample_file();
let host: Localhost = Localhost::new(PathBuf::from(tmpdir.path())).ok().unwrap();
// mode_to_u32
assert_eq!(host.mode_to_u32((6, 4, 4)), 0o644);
assert_eq!(host.mode_to_u32((7, 7, 5)), 0o775);
// Chmod to file
assert!(host.chmod(file.path(), (7, 7, 5)).is_ok());
// Chmod to dir
assert!(host.chmod(tmpdir.path(), (7, 5, 0)).is_ok());
// Error
assert!(host
.chmod(Path::new("/tmp/krgiogoiegj/kwrgnoerig"), (7, 7, 7))
.is_err());
}
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
#[test]
fn test_host_copy_file_absolute() {
let tmpdir: tempfile::TempDir = tempfile::TempDir::new().unwrap();
// Create file in tmpdir
let mut file1_path: PathBuf = PathBuf::from(tmpdir.path());
file1_path.push("foo.txt");
// Write file 1
let mut file1: File = File::create(file1_path.as_path()).ok().unwrap();
assert!(file1.write_all(b"Hello world!\n").is_ok());
// Get file 2 path
let mut file2_path: PathBuf = PathBuf::from(tmpdir.path());
file2_path.push("bar.txt");
// Create host
let mut host: Localhost = Localhost::new(PathBuf::from(tmpdir.path())).ok().unwrap();
let file1_entry: FsEntry = host.files.get(0).unwrap().clone();
assert_eq!(file1_entry.get_name(), String::from("foo.txt"));
// Copy
assert!(host.copy(&file1_entry, file2_path.as_path()).is_ok());
// Verify host has two files
assert_eq!(host.files.len(), 2);
}
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
#[test]
fn test_host_copy_file_relative() {
let tmpdir: tempfile::TempDir = tempfile::TempDir::new().unwrap();
// Create file in tmpdir
let mut file1_path: PathBuf = PathBuf::from(tmpdir.path());
file1_path.push("foo.txt");
// Write file 1
let mut file1: File = File::create(file1_path.as_path()).ok().unwrap();
assert!(file1.write_all(b"Hello world!\n").is_ok());
// Get file 2 path
let file2_path: PathBuf = PathBuf::from("bar.txt");
// Create host
let mut host: Localhost = Localhost::new(PathBuf::from(tmpdir.path())).ok().unwrap();
let file1_entry: FsEntry = host.files.get(0).unwrap().clone();
assert_eq!(file1_entry.get_name(), String::from("foo.txt"));
// Copy
assert!(host.copy(&file1_entry, file2_path.as_path()).is_ok());
// Verify host has two files
assert_eq!(host.files.len(), 2);
}
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
#[test]
fn test_host_copy_directory_absolute() {
let tmpdir: tempfile::TempDir = tempfile::TempDir::new().unwrap();
// Create directory in tmpdir
let mut dir_src: PathBuf = PathBuf::from(tmpdir.path());
dir_src.push("test_dir/");
assert!(std::fs::create_dir(dir_src.as_path()).is_ok());
// Create file in src dir
let mut file1_path: PathBuf = dir_src.clone();
file1_path.push("foo.txt");
// Write file 1
let mut file1: File = File::create(file1_path.as_path()).ok().unwrap();
assert!(file1.write_all(b"Hello world!\n").is_ok());
// Copy dir src to dir ddest
let mut dir_dest: PathBuf = PathBuf::from(tmpdir.path());
dir_dest.push("test_dest_dir/");
// Create host
let mut host: Localhost = Localhost::new(PathBuf::from(tmpdir.path())).ok().unwrap();
let dir_src_entry: FsEntry = host.files.get(0).unwrap().clone();
assert_eq!(dir_src_entry.get_name(), String::from("test_dir"));
// Copy
assert!(host.copy(&dir_src_entry, dir_dest.as_path()).is_ok());
// Verify host has two files
assert_eq!(host.files.len(), 2);
// Verify dir_dest contains foo.txt
let mut test_file_path: PathBuf = dir_dest.clone();
test_file_path.push("foo.txt");
assert!(host.stat(test_file_path.as_path()).is_ok());
}
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
#[test]
fn test_host_copy_directory_relative() {
let tmpdir: tempfile::TempDir = tempfile::TempDir::new().unwrap();
// Create directory in tmpdir
let mut dir_src: PathBuf = PathBuf::from(tmpdir.path());
dir_src.push("test_dir/");
assert!(std::fs::create_dir(dir_src.as_path()).is_ok());
// Create file in src dir
let mut file1_path: PathBuf = dir_src.clone();
file1_path.push("foo.txt");
// Write file 1
let mut file1: File = File::create(file1_path.as_path()).ok().unwrap();
assert!(file1.write_all(b"Hello world!\n").is_ok());
// Copy dir src to dir ddest
let dir_dest: PathBuf = PathBuf::from("test_dest_dir/");
// Create host
let mut host: Localhost = Localhost::new(PathBuf::from(tmpdir.path())).ok().unwrap();
let dir_src_entry: FsEntry = host.files.get(0).unwrap().clone();
assert_eq!(dir_src_entry.get_name(), String::from("test_dir"));
// Copy
assert!(host.copy(&dir_src_entry, dir_dest.as_path()).is_ok());
// Verify host has two files
assert_eq!(host.files.len(), 2);
// Verify dir_dest contains foo.txt
let mut test_file_path: PathBuf = dir_dest.clone();
test_file_path.push("foo.txt");
println!("{:?}", host.scan_dir(tmpdir.path()).ok().unwrap());
assert!(host.stat(test_file_path.as_path()).is_ok());
}
#[test]
fn test_host_fmt_error() {
let err: HostError = HostError::new(
HostErrorType::CouldNotCreateFile,
Some(std::io::Error::from(std::io::ErrorKind::AddrInUse)),
);
assert_eq!(
format!("{}", err),
String::from("Could not create file: address in use")
);
assert_eq!(
format!("{}", HostError::new(HostErrorType::DeleteFailed, None)),
String::from("Could not delete file")
);
assert_eq!(
format!("{}", HostError::new(HostErrorType::DirNotAccessible, None)),
String::from("Could not access directory")
);
assert_eq!(
format!(
"{}",
HostError::new(HostErrorType::NoSuchFileOrDirectory, None)
),
String::from("No such file or directory")
);
assert_eq!(
format!("{}", HostError::new(HostErrorType::ReadonlyFile, None)),
String::from("File is readonly")
);
assert_eq!(
format!("{}", HostError::new(HostErrorType::FileNotAccessible, None)),
String::from("Could not access file")
);
assert_eq!(
format!("{}", HostError::new(HostErrorType::FileAlreadyExists, None)),
String::from("File already exists")
);
}
/// ### create_sample_file
///
/// Create a sample file

View File

@@ -19,11 +19,16 @@
*
*/
#[macro_use] extern crate lazy_static;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate magic_crypt;
pub mod activity_manager;
pub mod bookmarks;
pub mod filetransfer;
pub mod fs;
pub mod host;
pub mod system;
pub mod ui;
pub mod utils;

View File

@@ -26,6 +26,8 @@ const TERMSCP_AUTHORS: &str = env!("CARGO_PKG_AUTHORS");
extern crate getopts;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate magic_crypt;
extern crate rpassword;
// External libs
@@ -36,9 +38,11 @@ use std::time::Duration;
// Include
mod activity_manager;
mod bookmarks;
mod filetransfer;
mod fs;
mod host;
mod system;
mod ui;
mod utils;
@@ -53,7 +57,7 @@ use filetransfer::FileTransferProtocol;
fn print_usage(opts: Options) {
let brief = String::from("Usage: termscp [options]... [protocol://user@address:port]");
print!("{}", opts.usage(&brief));
println!("\nPlease, report issues to <https://github.com/ChristianVisintin/TermSCP>");
println!("\nPlease, report issues to <https://github.com/ChristianVisintin/termscp>");
}
fn main() {
@@ -115,7 +119,7 @@ fn main() {
let extra_args: Vec<String> = matches.free;
if let Some(remote) = extra_args.get(0) {
// Parse address
match utils::parse_remote_opt(remote) {
match utils::parser::parse_remote_opt(remote) {
Ok((addr, portn, proto, user)) => {
// Set params
address = Some(addr);

View File

@@ -0,0 +1,681 @@
//! ## BookmarksClient
//!
//! `bookmarks_client` is the module which provides an API between the Bookmarks module and the system
/*
*
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Deps
extern crate magic_crypt;
extern crate rand;
// Local
use crate::bookmarks::serializer::BookmarkSerializer;
use crate::bookmarks::{Bookmark, SerializerError, SerializerErrorKind, UserHosts};
use crate::filetransfer::FileTransferProtocol;
use crate::utils::fmt::fmt_time;
// Ext
use magic_crypt::MagicCryptTrait;
use rand::{distributions::Alphanumeric, Rng};
use std::fs::{OpenOptions, Permissions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::time::SystemTime;
/// ## BookmarksClient
///
/// BookmarksClient provides a layer between the host system and the bookmarks module
pub struct BookmarksClient {
hosts: UserHosts,
bookmarks_file: PathBuf,
key: String,
recents_size: usize,
}
impl BookmarksClient {
/// ### BookmarksClient
///
/// Instantiates a new BookmarksClient
/// Bookmarks file path must be provided
/// Key file must be provided
pub fn new(
bookmarks_file: &Path,
key_file: &Path,
recents_size: usize,
) -> Result<BookmarksClient, SerializerError> {
// Create default hosts
let default_hosts: UserHosts = Default::default();
// If key file doesn't exist, create key, otherwise read it
let key: String = match key_file.exists() {
true => match BookmarksClient::load_key(key_file) {
Ok(key) => key,
Err(err) => return Err(err),
},
false => match BookmarksClient::generate_key(key_file) {
Ok(key) => key,
Err(err) => return Err(err),
},
};
let mut client: BookmarksClient = BookmarksClient {
hosts: default_hosts,
bookmarks_file: PathBuf::from(bookmarks_file),
key,
recents_size,
};
// If bookmark file doesn't exist, initialize it
if !bookmarks_file.exists() {
if let Err(err) = client.write_bookmarks() {
return Err(err);
}
} else {
// Load bookmarks from file
if let Err(err) = client.read_bookmarks() {
return Err(err);
}
}
// Load key
Ok(client)
}
/// ### iter_bookmarks
///
/// Iterate over bookmarks keys
pub fn iter_bookmarks(&self) -> Box<dyn Iterator<Item = &String> + '_> {
Box::new(self.hosts.bookmarks.keys())
}
/// ### get_bookmark
///
/// Get bookmark associated to key
pub fn get_bookmark(
&self,
key: &str,
) -> Option<(String, u16, FileTransferProtocol, String, Option<String>)> {
let entry: &Bookmark = self.hosts.bookmarks.get(key)?;
Some((
entry.address.clone(),
entry.port,
match entry.protocol.to_ascii_uppercase().as_str() {
"FTP" => FileTransferProtocol::Ftp(false),
"FTPS" => FileTransferProtocol::Ftp(true),
"SCP" => FileTransferProtocol::Scp,
_ => FileTransferProtocol::Sftp,
},
entry.username.clone(),
match &entry.password {
// Decrypted password if Some; if decryption fails return None
Some(pwd) => match self.decrypt_str(pwd.as_str()) {
Ok(decrypted_pwd) => Some(decrypted_pwd),
Err(_) => None,
},
None => None,
},
))
}
/// ### add_recent
///
/// Add a new recent to bookmarks
pub fn add_bookmark(
&mut self,
name: String,
addr: String,
port: u16,
protocol: FileTransferProtocol,
username: String,
password: Option<String>,
) {
if name.is_empty() {
panic!("Bookmark name can't be empty");
}
// Make bookmark
let host: Bookmark = self.make_bookmark(addr, port, protocol, username, password);
self.hosts.bookmarks.insert(name, host);
}
/// ### del_bookmark
///
/// Delete entry from bookmarks
pub fn del_bookmark(&mut self, name: &str) {
let _ = self.hosts.bookmarks.remove(name);
}
/// ### iter_recents
///
/// Iterate over recents keys
pub fn iter_recents(&self) -> Box<dyn Iterator<Item = &String> + '_> {
Box::new(self.hosts.recents.keys())
}
/// ### get_recent
///
/// Get recent associated to key
pub fn get_recent(&self, key: &str) -> Option<(String, u16, FileTransferProtocol, String)> {
// NOTE: password is not decrypted; recents will never have password
let entry: &Bookmark = self.hosts.recents.get(key)?;
Some((
entry.address.clone(),
entry.port,
match entry.protocol.to_ascii_uppercase().as_str() {
"FTP" => FileTransferProtocol::Ftp(false),
"FTPS" => FileTransferProtocol::Ftp(true),
"SCP" => FileTransferProtocol::Scp,
_ => FileTransferProtocol::Sftp,
},
entry.username.clone(),
))
}
/// ### add_recent
///
/// Add a new recent to bookmarks
pub fn add_recent(
&mut self,
addr: String,
port: u16,
protocol: FileTransferProtocol,
username: String,
) {
// Make bookmark
let host: Bookmark = self.make_bookmark(addr, port, protocol, username, None);
// Check if duplicated
for recent_host in self.hosts.recents.values() {
if *recent_host == host {
// Don't save duplicates
return;
}
}
// If hosts size is bigger than self.recents_size; pop last
if self.hosts.recents.len() >= self.recents_size {
// Get keys
let mut keys: Vec<String> = Vec::with_capacity(self.hosts.recents.len());
for key in self.hosts.recents.keys() {
keys.push(key.clone());
}
// Sort keys; NOTE: most recent is the last element
keys.sort();
// Delete keys starting from the last one
for key in keys.iter() {
let _ = self.hosts.recents.remove(key);
// If length is < self.recents_size; break
if self.hosts.recents.len() < self.recents_size {
break;
}
}
}
let name: String = fmt_time(SystemTime::now(), "ISO%Y%m%dT%H%M%S");
self.hosts.recents.insert(name, host);
}
/// ### del_recent
///
/// Delete entry from recents
pub fn del_recent(&mut self, name: &str) {
let _ = self.hosts.recents.remove(name);
}
/// ### write_bookmarks
///
/// Write bookmarks to file
pub fn write_bookmarks(&self) -> Result<(), SerializerError> {
// Open file
match OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(self.bookmarks_file.as_path())
{
Ok(writer) => {
let serializer: BookmarkSerializer = BookmarkSerializer {};
serializer.serialize(Box::new(writer), &self.hosts)
}
Err(err) => Err(SerializerError::new_ex(
SerializerErrorKind::IoError,
err.to_string(),
)),
}
}
/// ### read_bookmarks
///
/// Read bookmarks from file
fn read_bookmarks(&mut self) -> Result<(), SerializerError> {
// Open bookmarks file for read
match OpenOptions::new()
.read(true)
.open(self.bookmarks_file.as_path())
{
Ok(reader) => {
// Deserialize
let deserializer: BookmarkSerializer = BookmarkSerializer {};
match deserializer.deserialize(Box::new(reader)) {
Ok(hosts) => {
self.hosts = hosts;
Ok(())
}
Err(err) => Err(err),
}
}
Err(err) => Err(SerializerError::new_ex(
SerializerErrorKind::IoError,
err.to_string(),
)),
}
}
/// ### generate_key
///
/// Generate a new AES key and write it to key file
fn generate_key(key_file: &Path) -> Result<String, SerializerError> {
// Generate 256 bytes (2048 bits) key
let key: String = rand::thread_rng()
.sample_iter(Alphanumeric)
.take(256)
.collect::<String>();
// Write file
match OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(key_file)
{
Ok(mut file) => {
// Write key to file
if let Err(err) = file.write_all(key.as_bytes()) {
return Err(SerializerError::new_ex(
SerializerErrorKind::IoError,
err.to_string(),
));
}
// Set file to readonly
let mut permissions: Permissions = file.metadata().unwrap().permissions();
permissions.set_readonly(true);
let _ = file.set_permissions(permissions);
Ok(key)
}
Err(err) => Err(SerializerError::new_ex(
SerializerErrorKind::IoError,
err.to_string(),
)),
}
}
/// ### make_bookmark
///
/// Make bookmark from credentials
fn make_bookmark(
&self,
addr: String,
port: u16,
protocol: FileTransferProtocol,
username: String,
password: Option<String>,
) -> Bookmark {
Bookmark {
address: addr,
port,
username,
protocol: match protocol {
FileTransferProtocol::Ftp(secure) => match secure {
true => String::from("FTPS"),
false => String::from("FTP"),
},
FileTransferProtocol::Scp => String::from("SCP"),
FileTransferProtocol::Sftp => String::from("SFTP"),
},
password: match password {
Some(p) => Some(self.encrypt_str(p.as_str())), // Encrypt password if provided
None => None,
},
}
}
/// ### load_key
///
/// Load key from key_file
fn load_key(key_file: &Path) -> Result<String, SerializerError> {
match OpenOptions::new().read(true).open(key_file) {
Ok(mut file) => {
let mut key: String = String::with_capacity(256);
match file.read_to_string(&mut key) {
Ok(_) => Ok(key),
Err(err) => Err(SerializerError::new_ex(
SerializerErrorKind::IoError,
err.to_string(),
)),
}
}
Err(err) => Err(SerializerError::new_ex(
SerializerErrorKind::IoError,
err.to_string(),
)),
}
}
/// ### encrypt_str
///
/// Encrypt provided string using AES-128. Encrypted buffer is then converted to BASE64
fn encrypt_str(&self, txt: &str) -> String {
let crypter = new_magic_crypt!(self.key.clone(), 128);
crypter.encrypt_str_to_base64(txt.to_string())
}
/// ### decrypt_str
///
/// Decrypt provided string using AES-128
fn decrypt_str(&self, secret: &str) -> Result<String, SerializerError> {
let crypter = new_magic_crypt!(self.key.clone(), 128);
match crypter.decrypt_base64_to_string(secret.to_string()) {
Ok(txt) => Ok(txt),
Err(err) => Err(SerializerError::new_ex(
SerializerErrorKind::SyntaxError,
err.to_string(),
)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::thread::sleep;
use std::time::Duration;
#[test]
fn test_system_bookmarks_new() {
let tmp_dir: tempfile::TempDir = create_tmp_dir();
let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
// Initialize a new bookmarks client
let client: BookmarksClient =
BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16).unwrap();
// Verify client
assert_eq!(client.hosts.bookmarks.len(), 0);
assert_eq!(client.hosts.recents.len(), 0);
assert!(client.key.len() > 0);
assert_eq!(client.bookmarks_file, cfg_path);
assert_eq!(client.recents_size, 16);
}
#[test]
fn test_system_bookmarks_new_err() {
assert!(BookmarksClient::new(
Path::new("/tmp/oifoif/omar"),
Path::new("/tmp/efnnu/omar"),
16
)
.is_err());
let tmp_dir: tempfile::TempDir = create_tmp_dir();
let (cfg_path, _): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
assert!(
BookmarksClient::new(cfg_path.as_path(), Path::new("/tmp/efnnu/omar"), 16).is_err()
);
}
#[test]
fn test_system_bookmarks_new_from_existing() {
let tmp_dir: tempfile::TempDir = create_tmp_dir();
let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
// Initialize a new bookmarks client
let mut client: BookmarksClient =
BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16).unwrap();
// Add some bookmarks
client.add_bookmark(
String::from("raspberry"),
String::from("192.168.1.31"),
22,
FileTransferProtocol::Sftp,
String::from("pi"),
Some(String::from("mypassword")),
);
client.add_recent(
String::from("192.168.1.31"),
22,
FileTransferProtocol::Sftp,
String::from("pi"),
);
let recent_key: String = String::from(client.iter_recents().next().unwrap());
assert!(client.write_bookmarks().is_ok());
let key: String = client.key.clone();
// Re-initialize a client
let client: BookmarksClient =
BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16).unwrap();
// Verify it loaded parameters correctly
assert_eq!(client.key, key);
let bookmark: (String, u16, FileTransferProtocol, String, Option<String>) =
client.get_bookmark(&String::from("raspberry")).unwrap();
assert_eq!(bookmark.0, String::from("192.168.1.31"));
assert_eq!(bookmark.1, 22);
assert_eq!(bookmark.2, FileTransferProtocol::Sftp);
assert_eq!(bookmark.3, String::from("pi"));
assert_eq!(*bookmark.4.as_ref().unwrap(), String::from("mypassword"));
let bookmark: (String, u16, FileTransferProtocol, String) =
client.get_recent(&recent_key).unwrap();
assert_eq!(bookmark.0, String::from("192.168.1.31"));
assert_eq!(bookmark.1, 22);
assert_eq!(bookmark.2, FileTransferProtocol::Sftp);
assert_eq!(bookmark.3, String::from("pi"));
}
#[test]
fn test_system_bookmarks_manipulate_bookmarks() {
let tmp_dir: tempfile::TempDir = create_tmp_dir();
let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
// Initialize a new bookmarks client
let mut client: BookmarksClient =
BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16).unwrap();
// Add bookmark
client.add_bookmark(
String::from("raspberry"),
String::from("192.168.1.31"),
22,
FileTransferProtocol::Sftp,
String::from("pi"),
Some(String::from("mypassword")),
);
client.add_bookmark(
String::from("raspberry2"),
String::from("192.168.1.32"),
22,
FileTransferProtocol::Sftp,
String::from("pi"),
Some(String::from("mypassword2")),
);
// Iter
assert_eq!(client.iter_bookmarks().count(), 2);
// Get bookmark
let bookmark: (String, u16, FileTransferProtocol, String, Option<String>) =
client.get_bookmark(&String::from("raspberry")).unwrap();
assert_eq!(bookmark.0, String::from("192.168.1.31"));
assert_eq!(bookmark.1, 22);
assert_eq!(bookmark.2, FileTransferProtocol::Sftp);
assert_eq!(bookmark.3, String::from("pi"));
assert_eq!(*bookmark.4.as_ref().unwrap(), String::from("mypassword"));
// Write bookmarks
assert!(client.write_bookmarks().is_ok());
// Delete bookmark
client.del_bookmark(&String::from("raspberry"));
// Get unexisting bookmark
assert!(client.get_bookmark(&String::from("raspberry")).is_none());
// Write bookmarks
assert!(client.write_bookmarks().is_ok());
}
#[test]
#[should_panic]
fn test_system_bookmarks_bad_bookmark_name() {
let tmp_dir: tempfile::TempDir = create_tmp_dir();
let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
// Initialize a new bookmarks client
let mut client: BookmarksClient =
BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16).unwrap();
// Add bookmark
client.add_bookmark(
String::from(""),
String::from("192.168.1.31"),
22,
FileTransferProtocol::Sftp,
String::from("pi"),
Some(String::from("mypassword")),
);
}
#[test]
fn test_system_bookmarks_manipulate_recents() {
let tmp_dir: tempfile::TempDir = create_tmp_dir();
let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
// Initialize a new bookmarks client
let mut client: BookmarksClient =
BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16).unwrap();
// Add bookmark
client.add_recent(
String::from("192.168.1.31"),
22,
FileTransferProtocol::Sftp,
String::from("pi"),
);
// Iter
assert_eq!(client.iter_recents().count(), 1);
let key: String = String::from(client.iter_recents().next().unwrap());
// Get bookmark
let bookmark: (String, u16, FileTransferProtocol, String) =
client.get_recent(&key).unwrap();
assert_eq!(bookmark.0, String::from("192.168.1.31"));
assert_eq!(bookmark.1, 22);
assert_eq!(bookmark.2, FileTransferProtocol::Sftp);
assert_eq!(bookmark.3, String::from("pi"));
// Write bookmarks
assert!(client.write_bookmarks().is_ok());
// Delete bookmark
client.del_recent(&key);
// Get unexisting bookmark
assert!(client.get_bookmark(&key).is_none());
// Write bookmarks
assert!(client.write_bookmarks().is_ok());
}
#[test]
fn test_system_bookmarks_dup_recent() {
let tmp_dir: tempfile::TempDir = create_tmp_dir();
let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
// Initialize a new bookmarks client
let mut client: BookmarksClient =
BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16).unwrap();
// Add bookmark
client.add_recent(
String::from("192.168.1.31"),
22,
FileTransferProtocol::Sftp,
String::from("pi"),
);
client.add_recent(
String::from("192.168.1.31"),
22,
FileTransferProtocol::Sftp,
String::from("pi"),
);
// There should be only one recent
assert_eq!(client.iter_recents().count(), 1);
}
#[test]
fn test_system_bookmarks_recents_more_than_limit() {
let tmp_dir: tempfile::TempDir = create_tmp_dir();
let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
// Initialize a new bookmarks client
let mut client: BookmarksClient =
BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 2).unwrap();
// Add recent, wait 1 second for each one (cause the name depends on time)
// 1
client.add_recent(
String::from("192.168.1.1"),
22,
FileTransferProtocol::Sftp,
String::from("pi"),
);
sleep(Duration::from_secs(1));
// 2
client.add_recent(
String::from("192.168.1.2"),
22,
FileTransferProtocol::Sftp,
String::from("pi"),
);
sleep(Duration::from_secs(1));
// 3
client.add_recent(
String::from("192.168.1.3"),
22,
FileTransferProtocol::Sftp,
String::from("pi"),
);
// Limit is 2
assert_eq!(client.iter_recents().count(), 2);
// Check that 192.168.1.1 has been removed
let key: String = client.iter_recents().nth(0).unwrap().to_string();
assert!(matches!(
client.hosts.recents.get(&key).unwrap().address.as_str(),
"192.168.1.2" | "192.168.1.3"
));
let key: String = client.iter_recents().nth(1).unwrap().to_string();
assert!(matches!(
client.hosts.recents.get(&key).unwrap().address.as_str(),
"192.168.1.2" | "192.168.1.3"
));
}
#[test]
#[should_panic]
fn test_system_bookmarks_add_bookmark_empty() {
let tmp_dir: tempfile::TempDir = create_tmp_dir();
let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
// Initialize a new bookmarks client
let mut client: BookmarksClient =
BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16).unwrap();
// Add bookmark
client.add_bookmark(
String::from(""),
String::from("192.168.1.31"),
22,
FileTransferProtocol::Sftp,
String::from("pi"),
Some(String::from("mypassword")),
);
}
/// ### get_paths
///
/// Get paths for configuration and key for bookmarks
fn get_paths(dir: &Path) -> (PathBuf, PathBuf) {
let mut k: PathBuf = PathBuf::from(dir);
let mut c: PathBuf = k.clone();
k.push("bookmarks.key");
c.push("bookmarks.toml");
(c, k)
}
/// ### create_tmp_dir
///
/// Create temporary directory
fn create_tmp_dir() -> tempfile::TempDir {
tempfile::TempDir::new().ok().unwrap()
}
}

95
src/system/environment.rs Normal file
View File

@@ -0,0 +1,95 @@
//! ## Environment
//!
//! `environment` is the module which provides Path and values for the system environment
/*
*
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Deps
extern crate dirs;
// Ext
use std::path::PathBuf;
/// ### get_config_dir
///
/// Get termscp configuration directory path.
/// Returns None, if it's not possible to get it
pub fn init_config_dir() -> Result<Option<PathBuf>, String> {
// Get file
lazy_static! {
static ref CONF_DIR: Option<PathBuf> = dirs::config_dir();
}
if CONF_DIR.is_some() {
// Get path of bookmarks
let mut p: PathBuf = CONF_DIR.as_ref().unwrap().clone();
// Append termscp dir
p.push("termscp/");
// If directory doesn't exist, create it
match p.exists() {
true => Ok(Some(p)),
false => match std::fs::create_dir(p.as_path()) {
Ok(_) => Ok(Some(p)),
Err(err) => Err(err.to_string()),
},
}
} else {
Ok(None)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::{File, OpenOptions};
use std::io::Write;
#[test]
fn test_system_environment_get_config_dir() {
// Create and get conf_dir
let conf_dir: PathBuf = init_config_dir().ok().unwrap().unwrap();
// Remove dir
assert!(std::fs::remove_dir_all(conf_dir.as_path()).is_ok());
}
#[test]
fn test_system_environment_get_config_dir_err() {
let mut conf_dir: PathBuf = dirs::config_dir().unwrap();
conf_dir.push("termscp");
// Create file
let mut f: File = OpenOptions::new()
.create(true)
.write(true)
.open(conf_dir.as_path())
.ok()
.unwrap();
// Write
assert!(writeln!(f, "Hello world!").is_ok());
// Drop file
drop(f);
// Get config dir (will fail)
assert!(init_config_dir().is_err());
// Remove file
assert!(std::fs::remove_file(conf_dir.as_path()).is_ok());
}
}

28
src/system/mod.rs Normal file
View File

@@ -0,0 +1,28 @@
//! ## System
//!
//! `system` is the module which contains functions and data types related to current system
/*
*
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
// modules
pub mod bookmarks_client;
pub mod environment;

View File

@@ -1,546 +0,0 @@
//! ## AuthActivity
//!
//! `auth_activity` is the module which implements the authentication activity
/*
*
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Dependencies
extern crate crossterm;
extern crate tui;
extern crate unicode_width;
// locals
use super::{Activity, Context};
use crate::filetransfer::FileTransferProtocol;
use crate::utils::align_text_center;
// Includes
use crossterm::event::Event as InputEvent;
use crossterm::event::KeyCode;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use tui::{
layout::{Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::{Span, Spans, Text},
widgets::{Block, Borders, Clear, Paragraph, Tabs},
};
use unicode_width::UnicodeWidthStr;
/// ### InputField
///
/// InputField describes the current input field to edit
#[derive(std::cmp::PartialEq)]
enum InputField {
Address,
Port,
Protocol,
Username,
Password,
}
/// ### InputMode
///
/// InputMode describes the current input mode
/// Each input mode handle the input events in a different way
#[derive(std::cmp::PartialEq)]
enum InputMode {
Text,
Popup,
}
/// ### AuthActivity
///
/// AuthActivity is the data holder for the authentication activity
pub struct AuthActivity {
pub address: String,
pub port: String,
pub protocol: FileTransferProtocol,
pub username: String,
pub password: String,
pub submit: bool, // becomes true after user has submitted fields
pub quit: bool, // Becomes true if user has pressed esc
context: Option<Context>,
selected_field: InputField,
input_mode: InputMode,
popup_message: Option<String>,
password_placeholder: String,
redraw: bool, // Should ui actually be redrawned?
}
impl Default for AuthActivity {
fn default() -> Self {
Self::new()
}
}
impl AuthActivity {
/// ### new
///
/// Instantiates a new AuthActivity
pub fn new() -> AuthActivity {
AuthActivity {
address: String::new(),
port: String::from("22"),
protocol: FileTransferProtocol::Sftp,
username: String::new(),
password: String::new(),
submit: false,
quit: false,
context: None,
selected_field: InputField::Address,
input_mode: InputMode::Text,
popup_message: None,
password_placeholder: String::new(),
redraw: true, // True at startup
}
}
/// ### set_input_mode
///
/// Update input mode based on current parameters
fn select_input_mode(&mut self) -> InputMode {
if self.popup_message.is_some() {
return InputMode::Popup;
}
// Default to text
InputMode::Text
}
/// ### handle_input_event
///
/// Handle input event, based on current input mode
fn handle_input_event(&mut self, ev: &InputEvent) {
match self.input_mode {
InputMode::Text => self.handle_input_event_mode_text(ev),
InputMode::Popup => self.handle_input_event_mode_popup(ev),
}
}
/// ### handle_input_event_mode_text
///
/// Handler for input event when in textmode
fn handle_input_event_mode_text(&mut self, ev: &InputEvent) {
if let InputEvent::Key(key) = ev {
match key.code {
KeyCode::Esc => {
self.quit = true;
}
KeyCode::Enter => {
// Handle submit
// Check form
// Check address
if self.address.is_empty() {
self.popup_message = Some(String::from("Invalid address"));
return;
}
// Check port
// Convert port to number
match self.port.parse::<usize>() {
Ok(val) => {
if val > 65535 {
self.popup_message =
Some(String::from("Specified port must be in range 0-65535"));
return;
}
}
Err(_) => {
self.popup_message =
Some(String::from("Specified port is not a number"));
return;
}
}
// Check username
//if self.username.len() == 0 {
// self.popup_message = Some(String::from("Invalid username"));
// return;
//}
// Everything OK, set enter
self.submit = true;
self.popup_message =
Some(format!("Connecting to {}:{}...", self.address, self.port));
}
KeyCode::Backspace => {
// Pop last char
match self.selected_field {
InputField::Address => {
let _ = self.address.pop();
}
InputField::Password => {
let _ = self.password.pop();
}
InputField::Username => {
let _ = self.username.pop();
}
InputField::Port => {
let _ = self.port.pop();
}
_ => { /* Nothing to do */ }
};
}
KeyCode::Up => {
// Move item up
self.selected_field = match self.selected_field {
InputField::Address => InputField::Password, // End of list (wrap)
InputField::Port => InputField::Address,
InputField::Protocol => InputField::Port,
InputField::Username => InputField::Protocol,
InputField::Password => InputField::Username,
}
}
KeyCode::Down | KeyCode::Tab => {
// Move item down
self.selected_field = match self.selected_field {
InputField::Address => InputField::Port,
InputField::Port => InputField::Protocol,
InputField::Protocol => InputField::Username,
InputField::Username => InputField::Password,
InputField::Password => InputField::Address, // End of list (wrap)
}
}
KeyCode::Char(ch) => {
match self.selected_field {
InputField::Address => self.address.push(ch),
InputField::Password => self.password.push(ch),
InputField::Username => self.username.push(ch),
InputField::Port => {
// Value must be numeric
if ch.is_numeric() {
self.port.push(ch);
}
}
_ => { /* Nothing to do */ }
}
}
KeyCode::Left => {
// If current field is Protocol handle event... (move element left)
if self.selected_field == InputField::Protocol {
self.protocol = match self.protocol {
FileTransferProtocol::Sftp => FileTransferProtocol::Ftp(true), // End of list (wrap)
FileTransferProtocol::Scp => FileTransferProtocol::Sftp,
FileTransferProtocol::Ftp(ftps) => match ftps {
false => FileTransferProtocol::Scp,
true => FileTransferProtocol::Ftp(false),
},
};
}
}
KeyCode::Right => {
// If current field is Protocol handle event... ( move element right )
if self.selected_field == InputField::Protocol {
self.protocol = match self.protocol {
FileTransferProtocol::Sftp => FileTransferProtocol::Scp,
FileTransferProtocol::Scp => FileTransferProtocol::Ftp(false),
FileTransferProtocol::Ftp(ftps) => match ftps {
false => FileTransferProtocol::Ftp(true),
true => FileTransferProtocol::Sftp, // End of list (wrap)
},
};
}
}
_ => { /* Nothing to do */ }
}
}
}
/// ### handle_input_event_mode_text
///
/// Handler for input event when in popup mode
fn handle_input_event_mode_popup(&mut self, ev: &InputEvent) {
// Only enter should be allowed here
if let InputEvent::Key(key) = ev {
if let KeyCode::Enter = key.code {
self.popup_message = None; // Hide popup
}
}
}
/// ### draw_remote_address
///
/// Draw remote address block
fn draw_remote_address(&self) -> Paragraph {
Paragraph::new(self.address.as_ref())
.style(match self.selected_field {
InputField::Address => Style::default().fg(Color::Yellow),
_ => Style::default(),
})
.block(
Block::default()
.borders(Borders::ALL)
.title("Remote address"),
)
}
/// ### draw_remote_port
///
/// Draw remote port block
fn draw_remote_port(&self) -> Paragraph {
Paragraph::new(self.port.as_ref())
.style(match self.selected_field {
InputField::Port => Style::default().fg(Color::Cyan),
_ => Style::default(),
})
.block(Block::default().borders(Borders::ALL).title("Remote port"))
}
/// ### draw_protocol_select
///
/// Draw protocol select
fn draw_protocol_select(&self) -> Tabs {
let protocols: Vec<Spans> = vec![
Spans::from("SFTP"),
Spans::from("SCP"),
Spans::from("FTP"),
Spans::from("FTPS"),
];
let index: usize = match self.protocol {
FileTransferProtocol::Sftp => 0,
FileTransferProtocol::Scp => 1,
FileTransferProtocol::Ftp(ftps) => match ftps {
false => 2,
true => 3,
},
};
Tabs::new(protocols)
.block(Block::default().borders(Borders::ALL).title("Protocol"))
.select(index)
.style(match self.selected_field {
InputField::Protocol => Style::default().fg(Color::Green),
_ => Style::default(),
})
.highlight_style(
Style::default()
.add_modifier(Modifier::BOLD)
.bg(Color::Green)
.fg(Color::Black),
)
}
/// ### draw_protocol_username
///
/// Draw username block
fn draw_protocol_username(&self) -> Paragraph {
Paragraph::new(self.username.as_ref())
.style(match self.selected_field {
InputField::Username => Style::default().fg(Color::Magenta),
_ => Style::default(),
})
.block(Block::default().borders(Borders::ALL).title("Username"))
}
/// ### draw_protocol_password
///
/// Draw password block
fn draw_protocol_password(&mut self) -> Paragraph {
// Create password secret
self.password_placeholder = (0..self.password.width()).map(|_| "*").collect::<String>();
Paragraph::new(self.password_placeholder.as_ref())
.style(match self.selected_field {
InputField::Password => Style::default().fg(Color::LightBlue),
_ => Style::default(),
})
.block(Block::default().borders(Borders::ALL).title("Password"))
}
/// ### draw_header
///
/// Draw header
fn draw_header(&self) -> Paragraph {
Paragraph::new(" _____ ____ ____ ____ \n|_ _|__ _ __ _ __ ___ / ___| / ___| _ \\ \n | |/ _ \\ '__| '_ ` _ \\\\___ \\| | | |_) |\n | | __/ | | | | | | |___) | |___| __/ \n |_|\\___|_| |_| |_| |_|____/ \\____|_| \n")
.style(Style::default().fg(Color::White).add_modifier(Modifier::BOLD))
}
/// ### draw_footer
///
/// Draw authentication page footer
fn draw_footer(&self) -> Paragraph {
// Write header
let (footer, h_style) = (
vec![
Span::raw("Press "),
Span::styled("<ESC>", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to exit, "),
Span::styled("<UP,DOWN>", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to change input field, "),
Span::styled("<ENTER>", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to submit form"),
],
Style::default().add_modifier(Modifier::BOLD),
);
let mut footer_text = Text::from(Spans::from(footer));
footer_text.patch_style(h_style);
Paragraph::new(footer_text)
}
/// ### draw_popup
///
/// Draw popup block
fn draw_popup(&self, r: Rect) -> (Paragraph, Rect) {
let popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage(30), // Offset top
Constraint::Percentage(10), // Actual height
Constraint::Percentage(60), // Offset bottom
]
.as_ref(),
)
.split(r);
let area: Rect = Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Percentage((80) / 2),
Constraint::Percentage(20),
Constraint::Percentage((80) / 2),
]
.as_ref(),
)
.split(popup_layout[1])[1];
let popup: Paragraph = Paragraph::new(align_text_center(
self.popup_message.as_ref().unwrap().as_ref(),
area.width,
))
.style(Style::default().fg(Color::Red))
.block(Block::default().borders(Borders::ALL).title("Alert"));
(popup, area)
}
}
impl Activity for AuthActivity {
/// ### on_create
///
/// `on_create` is the function which must be called to initialize the activity.
/// `on_create` must initialize all the data structures used by the activity
/// Context is taken from activity manager and will be released only when activity is destroyed
fn on_create(&mut self, context: Context) {
// Set context
self.context = Some(context);
// Clear terminal
let _ = self.context.as_mut().unwrap().terminal.clear();
// Put raw mode on enabled
let _ = enable_raw_mode();
}
/// ### on_draw
///
/// `on_draw` is the function which draws the graphical interface.
/// This function must be called at each tick to refresh the interface
fn on_draw(&mut self) {
// Context must be something
if self.context.is_none() {
return;
}
// Start catching Input Events
if let Ok(input_events) = self.context.as_ref().unwrap().input_hnd.fetch_events() {
if !input_events.is_empty() {
self.redraw = true; // Set redraw to true if there is at least one event
}
// Iterate over input events
for event in input_events.iter() {
self.handle_input_event(event);
}
}
// Redraw if necessary
if self.redraw {
// Determine input mode
self.input_mode = self.select_input_mode();
// draw interface
let mut ctx: Context = self.context.take().unwrap();
let _ = ctx.terminal.draw(|f| {
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(2)
.constraints(
[
Constraint::Length(5),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
]
.as_ref(),
)
.split(f.size());
// Draw header
f.render_widget(self.draw_header(), chunks[0]);
// Draw input fields
f.render_widget(self.draw_remote_address(), chunks[1]);
f.render_widget(self.draw_remote_port(), chunks[2]);
f.render_widget(self.draw_protocol_select(), chunks[3]);
f.render_widget(self.draw_protocol_username(), chunks[4]);
f.render_widget(self.draw_protocol_password(), chunks[5]);
// Draw footer
f.render_widget(self.draw_footer(), chunks[6]);
if self.popup_message.is_some() {
let (popup, popup_area): (Paragraph, Rect) = self.draw_popup(f.size());
f.render_widget(Clear, popup_area); //this clears out the background
f.render_widget(popup, popup_area);
}
// Set cursor
match self.selected_field {
InputField::Address => f.set_cursor(
chunks[1].x + self.address.width() as u16 + 1,
chunks[1].y + 1,
),
InputField::Port => {
f.set_cursor(chunks[2].x + self.port.width() as u16 + 1, chunks[2].y + 1)
}
InputField::Username => f.set_cursor(
chunks[4].x + self.username.width() as u16 + 1,
chunks[4].y + 1,
),
InputField::Password => f.set_cursor(
chunks[5].x + self.password_placeholder.width() as u16 + 1,
chunks[5].y + 1,
),
_ => {}
}
});
// Reset ctx
self.context = Some(ctx);
// Set redraw to false
self.redraw = false;
}
}
/// ### on_destroy
///
/// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity.
/// This function must be called once before terminating the activity.
/// This function finally releases the context
fn on_destroy(&mut self) -> Option<Context> {
// Disable raw mode
let _ = disable_raw_mode();
self.context.as_ref()?;
// Clear terminal and return
match self.context.take() {
Some(mut ctx) => {
let _ = ctx.terminal.clear();
Some(ctx)
}
None => None,
}
}
}

View File

@@ -0,0 +1,258 @@
//! ## AuthActivity
//!
//! `auth_activity` is the module which implements the authentication activity
/*
*
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Dependencies
extern crate dirs;
// Locals
use super::{AuthActivity, Color, DialogYesNoOption, InputMode, PopupType};
use crate::system::bookmarks_client::BookmarksClient;
use crate::system::environment;
// Ext
use std::path::PathBuf;
impl AuthActivity {
/// ### del_bookmark
///
/// Delete bookmark
pub(super) fn del_bookmark(&mut self, idx: usize) {
if let Some(bookmarks_cli) = self.bookmarks_client.as_mut() {
// Iterate over kyes
let mut name: Option<String> = None;
for (i, key) in bookmarks_cli.iter_bookmarks().enumerate() {
if i == idx {
name = Some(key.clone());
break;
}
}
if let Some(name) = name {
bookmarks_cli.del_bookmark(&name);
// Write bookmarks
self.write_bookmarks();
}
}
}
/// ### load_bookmark
///
/// Load selected bookmark (at index) to input fields
pub(super) fn load_bookmark(&mut self, idx: usize) {
if let Some(bookmarks_cli) = self.bookmarks_client.as_ref() {
// Iterate over bookmarks
for (i, key) in bookmarks_cli.iter_bookmarks().enumerate() {
if i == idx {
if let Some(bookmark) = bookmarks_cli.get_bookmark(&key) {
// Load parameters
self.address = bookmark.0;
self.port = bookmark.1.to_string();
self.protocol = bookmark.2;
self.username = bookmark.3;
if let Some(password) = bookmark.4 {
self.password = password;
}
}
// Break
break;
}
}
}
}
/// ### save_bookmark
///
/// Save current input fields as a bookmark
pub(super) fn save_bookmark(&mut self, name: String) {
// Check port
let port: u16 = match self.port.parse::<usize>() {
Ok(val) => {
if val > 65535 {
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
String::from("Specified port must be in range 0-65535"),
));
return;
}
val as u16
}
Err(_) => {
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
String::from("Specified port is not a number"),
));
return;
}
};
if let Some(bookmarks_cli) = self.bookmarks_client.as_mut() {
// Check if password must be saved
let password: Option<String> = match self.choice_opt {
DialogYesNoOption::Yes => Some(self.password.clone()),
DialogYesNoOption::No => None,
};
bookmarks_cli.add_bookmark(
name,
self.address.clone(),
port,
self.protocol,
self.username.clone(),
password,
);
// Save bookmarks
self.write_bookmarks();
}
}
/// ### del_recent
///
/// Delete recent
pub(super) fn del_recent(&mut self, idx: usize) {
if let Some(client) = self.bookmarks_client.as_mut() {
// Iterate over kyes
let mut name: Option<String> = None;
for (i, key) in client.iter_recents().enumerate() {
if i == idx {
name = Some(key.clone());
break;
}
}
if let Some(name) = name {
client.del_recent(&name);
// Save bookmarks
self.write_bookmarks();
}
}
}
/// ### load_recent
///
/// Load selected recent (at index) to input fields
pub(super) fn load_recent(&mut self, idx: usize) {
if let Some(client) = self.bookmarks_client.as_ref() {
// Iterate over bookmarks
for (i, key) in client.iter_recents().enumerate() {
if i == idx {
if let Some(bookmark) = client.get_recent(key) {
// Load parameters
self.address = bookmark.0;
self.port = bookmark.1.to_string();
self.protocol = bookmark.2;
self.username = bookmark.3;
// Break
break;
}
}
}
}
}
/// ### save_recent
///
/// Save current input fields as a "recent"
pub(super) fn save_recent(&mut self) {
// Check port
let port: u16 = match self.port.parse::<usize>() {
Ok(val) => {
if val > 65535 {
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
String::from("Specified port must be in range 0-65535"),
));
return;
}
val as u16
}
Err(_) => {
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
String::from("Specified port is not a number"),
));
return;
}
};
if let Some(bookmarks_cli) = self.bookmarks_client.as_mut() {
bookmarks_cli.add_recent(
self.address.clone(),
port,
self.protocol,
self.username.clone(),
);
// Save bookmarks
self.write_bookmarks();
}
}
/// ### write_bookmarks
///
/// Write bookmarks to file
fn write_bookmarks(&mut self) {
if let Some(bookmarks_cli) = self.bookmarks_client.as_ref() {
if let Err(err) = bookmarks_cli.write_bookmarks() {
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
format!("Could not write bookmarks: {}", err),
));
}
}
}
/// ### init_bookmarks_client
///
/// Initialize bookmarks client
pub(super) fn init_bookmarks_client(&mut self) {
// Get config dir
match environment::init_config_dir() {
Ok(path) => {
// If some configure client, otherwise do nothing; don't bother users telling them that bookmarks are not supported on their system.
if let Some(path) = path {
// Prepare paths
let mut bookmarks_file: PathBuf = path.clone();
bookmarks_file.push("bookmarks.toml");
let mut key_file: PathBuf = path;
key_file.push(".bookmarks.key"); // key file is hidden
// Initialize client
match BookmarksClient::new(bookmarks_file.as_path(), key_file.as_path(), 16) {
Ok(cli) => self.bookmarks_client = Some(cli),
Err(err) => {
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
format!(
"Could not initialize bookmarks (at \"{}\", \"{}\"): {}",
bookmarks_file.display(),
key_file.display(),
err
),
))
}
}
}
}
Err(err) => {
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
format!("Could not initialize configuration directory: {}", err),
))
}
}
}
}

View File

@@ -0,0 +1,60 @@
//! ## AuthActivity
//!
//! `auth_activity` is the module which implements the authentication activity
/*
*
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
use super::{AuthActivity, InputForm};
impl AuthActivity {
/// ### callback_nothing_to_do
///
/// Self titled
pub(super) fn callback_nothing_to_do(&mut self) {}
/// ### callback_quit
///
/// Self titled
pub(super) fn callback_quit(&mut self) {
self.quit = true;
}
/// ### callback_del_bookmark
///
/// Callback which deletes recent or bookmark based on current form
pub(super) fn callback_del_bookmark(&mut self) {
match self.input_form {
InputForm::Bookmarks => self.del_bookmark(self.bookmarks_idx),
InputForm::Recents => self.del_recent(self.recents_idx),
_ => { /* Nothing to do */ }
}
}
/// ### callback_save_bookmark
///
/// Callback used to save bookmark with name
pub(super) fn callback_save_bookmark(&mut self, input: String) {
if !input.is_empty() {
self.save_bookmark(input);
}
}
}

View File

@@ -0,0 +1,486 @@
//! ## AuthActivity
//!
//! `auth_activity` is the module which implements the authentication activity
/*
*
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
use super::{
AuthActivity, DialogCallback, DialogYesNoOption, FileTransferProtocol, InputEvent, InputField,
InputForm, InputMode, PopupType,
};
use crossterm::event::{KeyCode, KeyModifiers};
use tui::style::Color;
impl AuthActivity {
/// ### handle_input_event
///
/// Handle input event, based on current input mode
pub(super) fn handle_input_event(&mut self, ev: &InputEvent) {
let popup: Option<PopupType> = match &self.input_mode {
InputMode::Popup(ptype) => Some(ptype.clone()),
_ => None,
};
match self.input_mode {
InputMode::Form => self.handle_input_event_mode_form(ev),
InputMode::Popup(_) => {
if let Some(ptype) = popup {
self.handle_input_event_mode_popup(ev, ptype)
}
}
}
}
/// ### handle_input_event_mode_form
///
/// Handler for input event when in form mode
pub(super) fn handle_input_event_mode_form(&mut self, ev: &InputEvent) {
match self.input_form {
InputForm::AuthCredentials => self.handle_input_event_mode_form_auth(ev),
InputForm::Bookmarks => self.handle_input_event_mode_form_bookmarks(ev),
InputForm::Recents => self.handle_input_event_mode_form_recents(ev),
}
}
/// ### handle_input_event_mode_form_auth
///
/// Handle input event when input mode is Form and Tab is Auth
pub(super) fn handle_input_event_mode_form_auth(&mut self, ev: &InputEvent) {
if let InputEvent::Key(key) = ev {
match key.code {
KeyCode::Esc => {
// Show quit dialog
self.input_mode = InputMode::Popup(PopupType::YesNo(
String::from("Are you sure you want to quit termscp?"),
AuthActivity::callback_quit,
AuthActivity::callback_nothing_to_do,
));
}
KeyCode::Tab => self.input_form = InputForm::Bookmarks, // Move to bookmarks
KeyCode::Enter => {
// Handle submit
// Check form
// Check address
if self.address.is_empty() {
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
String::from("Invalid address"),
));
return;
}
// Check port
// Convert port to number
match self.port.parse::<usize>() {
Ok(val) => {
if val > 65535 {
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
String::from("Specified port must be in range 0-65535"),
));
return;
}
}
Err(_) => {
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
String::from("Specified port is not a number"),
));
return;
}
}
// Save recent
self.save_recent();
// Everything OK, set enter
self.submit = true;
}
KeyCode::Backspace => {
// Pop last char
match self.selected_field {
InputField::Address => {
let _ = self.address.pop();
}
InputField::Password => {
let _ = self.password.pop();
}
InputField::Username => {
let _ = self.username.pop();
}
InputField::Port => {
let _ = self.port.pop();
}
_ => { /* Nothing to do */ }
};
}
KeyCode::Up => {
// Move item up
self.selected_field = match self.selected_field {
InputField::Address => InputField::Password, // End of list (wrap)
InputField::Port => InputField::Address,
InputField::Protocol => InputField::Port,
InputField::Username => InputField::Protocol,
InputField::Password => InputField::Username,
}
}
KeyCode::Down => {
// Move item down
self.selected_field = match self.selected_field {
InputField::Address => InputField::Port,
InputField::Port => InputField::Protocol,
InputField::Protocol => InputField::Username,
InputField::Username => InputField::Password,
InputField::Password => InputField::Address, // End of list (wrap)
}
}
KeyCode::Char(ch) => {
// Check if Ctrl is enabled
if key.modifiers.intersects(KeyModifiers::CONTROL) {
// If 'S', save bookmark as...
match ch {
'H' | 'h' => {
// Show help
self.input_mode = InputMode::Popup(PopupType::Help);
}
'S' | 's' => {
// Default choice option to no
self.choice_opt = DialogYesNoOption::No;
// Save bookmark as...
self.input_mode = InputMode::Popup(PopupType::SaveBookmark);
}
_ => { /* Nothing to do */ }
}
} else {
match self.selected_field {
InputField::Address => self.address.push(ch),
InputField::Password => self.password.push(ch),
InputField::Username => self.username.push(ch),
InputField::Port => {
// Value must be numeric
if ch.is_numeric() {
self.port.push(ch);
}
}
_ => { /* Nothing to do */ }
}
}
}
KeyCode::Left => {
// If current field is Protocol handle event... (move element left)
if self.selected_field == InputField::Protocol {
self.protocol = match self.protocol {
FileTransferProtocol::Sftp => FileTransferProtocol::Ftp(true), // End of list (wrap)
FileTransferProtocol::Scp => FileTransferProtocol::Sftp,
FileTransferProtocol::Ftp(ftps) => match ftps {
false => FileTransferProtocol::Scp,
true => FileTransferProtocol::Ftp(false),
},
};
}
}
KeyCode::Right => {
// If current field is Protocol handle event... ( move element right )
if self.selected_field == InputField::Protocol {
self.protocol = match self.protocol {
FileTransferProtocol::Sftp => FileTransferProtocol::Scp,
FileTransferProtocol::Scp => FileTransferProtocol::Ftp(false),
FileTransferProtocol::Ftp(ftps) => match ftps {
false => FileTransferProtocol::Ftp(true),
true => FileTransferProtocol::Sftp, // End of list (wrap)
},
};
}
}
_ => { /* Nothing to do */ }
}
}
}
/// ### handle_input_event_mode_form_bookmarks
///
/// Handle input event when input mode is Form and Tab is Bookmarks
pub(super) fn handle_input_event_mode_form_bookmarks(&mut self, ev: &InputEvent) {
if let InputEvent::Key(key) = ev {
match key.code {
KeyCode::Esc => {
// Show quit dialog
self.input_mode = InputMode::Popup(PopupType::YesNo(
String::from("Are you sure you want to quit termscp?"),
AuthActivity::callback_quit,
AuthActivity::callback_nothing_to_do,
));
}
KeyCode::Tab => self.input_form = InputForm::AuthCredentials, // Move to Auth credentials
KeyCode::Right => self.input_form = InputForm::Recents, // Move to recents
KeyCode::Up => {
// Move bookmarks index up
if self.bookmarks_idx > 0 {
self.bookmarks_idx -= 1;
} else if let Some(bookmarks_cli) = &self.bookmarks_client {
// Put to last index (wrap)
self.bookmarks_idx = bookmarks_cli.iter_bookmarks().count() - 1;
}
}
KeyCode::Down => {
if let Some(bookmarks_cli) = &self.bookmarks_client {
let size: usize = bookmarks_cli.iter_bookmarks().count();
// Check if can move down
if self.bookmarks_idx + 1 >= size {
// Move bookmarks index down
self.bookmarks_idx = 0;
} else {
// Set index to first element (wrap)
self.bookmarks_idx += 1;
}
}
}
KeyCode::Delete => {
// Ask if user wants to delete bookmark
self.input_mode = InputMode::Popup(PopupType::YesNo(
String::from("Are you sure you want to delete the selected bookmark?"),
AuthActivity::callback_del_bookmark,
AuthActivity::callback_nothing_to_do,
));
}
KeyCode::Enter => {
// Load bookmark
self.load_bookmark(self.bookmarks_idx);
// Set input form to Auth
self.input_form = InputForm::AuthCredentials;
// Set input field to password (very comfy)
self.selected_field = InputField::Password;
}
KeyCode::Char(ch) => match ch {
'E' | 'e' => {
// Ask if user wants to delete bookmark; NOTE: same as <DEL>
self.input_mode = InputMode::Popup(PopupType::YesNo(
String::from("Are you sure you want to delete the selected bookmark?"),
AuthActivity::callback_del_bookmark,
AuthActivity::callback_nothing_to_do,
));
}
'H' | 'h' => {
// Show help
self.input_mode = InputMode::Popup(PopupType::Help);
}
'S' | 's' => {
// Default choice option to no
self.choice_opt = DialogYesNoOption::No;
// Save bookmark as...
self.input_mode = InputMode::Popup(PopupType::SaveBookmark);
}
_ => { /* Nothing to do */ }
},
_ => { /* Nothing to do */ }
}
}
}
/// ### handle_input_event_mode_form_recents
///
/// Handle input event when input mode is Form and Tab is Recents
pub(super) fn handle_input_event_mode_form_recents(&mut self, ev: &InputEvent) {
if let InputEvent::Key(key) = ev {
match key.code {
KeyCode::Esc => {
// Show quit dialog
self.input_mode = InputMode::Popup(PopupType::YesNo(
String::from("Are you sure you want to quit termscp?"),
AuthActivity::callback_quit,
AuthActivity::callback_nothing_to_do,
));
}
KeyCode::Tab => self.input_form = InputForm::AuthCredentials, // Move to Auth credentials
KeyCode::Left => self.input_form = InputForm::Bookmarks, // Move to bookmarks
KeyCode::Up => {
// Move bookmarks index up
if self.recents_idx > 0 {
self.recents_idx -= 1;
} else if let Some(bookmarks_cli) = &self.bookmarks_client {
// Put to last index (wrap)
self.recents_idx = bookmarks_cli.iter_recents().count() - 1;
}
}
KeyCode::Down => {
if let Some(bookmarks_cli) = &self.bookmarks_client {
let size: usize = bookmarks_cli.iter_recents().count();
// Check if can move down
if self.recents_idx + 1 >= size {
// Move bookmarks index down
self.recents_idx = 0;
} else {
// Set index to first element (wrap)
self.recents_idx += 1;
}
}
}
KeyCode::Delete => {
// Ask if user wants to delete bookmark
self.input_mode = InputMode::Popup(PopupType::YesNo(
String::from("Are you sure you want to delete the selected host?"),
AuthActivity::callback_del_bookmark,
AuthActivity::callback_nothing_to_do,
));
}
KeyCode::Enter => {
// Load bookmark
self.load_recent(self.recents_idx);
// Set input form to Auth
self.input_form = InputForm::AuthCredentials;
// Set input field to password (very comfy)
self.selected_field = InputField::Password;
}
KeyCode::Char(ch) => match ch {
'E' | 'e' => {
// Ask if user wants to delete bookmark; NOTE: same as <DEL>
self.input_mode = InputMode::Popup(PopupType::YesNo(
String::from("Are you sure you want to delete the selected host?"),
AuthActivity::callback_del_bookmark,
AuthActivity::callback_nothing_to_do,
));
}
'H' | 'h' => {
// Show help
self.input_mode = InputMode::Popup(PopupType::Help);
}
'S' | 's' => {
// Default choice option to no
self.choice_opt = DialogYesNoOption::No;
// Save bookmark as...
self.input_mode = InputMode::Popup(PopupType::SaveBookmark);
}
_ => { /* Nothing to do */ }
},
_ => { /* Nothing to do */ }
}
}
}
/// ### handle_input_event_mode_text
///
/// Handler for input event when in popup mode
pub(super) fn handle_input_event_mode_popup(&mut self, ev: &InputEvent, ptype: PopupType) {
match ptype {
PopupType::Alert(_, _) => self.handle_input_event_mode_popup_alert(ev),
PopupType::Help => self.handle_input_event_mode_popup_help(ev),
PopupType::SaveBookmark => self.handle_input_event_mode_popup_save_bookmark(ev),
PopupType::YesNo(_, yes_cb, no_cb) => {
self.handle_input_event_mode_popup_yesno(ev, yes_cb, no_cb)
}
}
}
/// ### handle_input_event_mode_popup_alert
///
/// Handle input event when the input mode is popup, and popup type is alert
pub(super) fn handle_input_event_mode_popup_alert(&mut self, ev: &InputEvent) {
// Only enter should be allowed here
if let InputEvent::Key(key) = ev {
if let KeyCode::Enter = key.code {
self.input_mode = InputMode::Form; // Hide popup
}
}
}
/// ### handle_input_event_mode_popup_help
///
/// Input event handler for popup help
pub(super) fn handle_input_event_mode_popup_help(&mut self, ev: &InputEvent) {
// If enter, close popup
if let InputEvent::Key(key) = ev {
match key.code {
KeyCode::Enter | KeyCode::Esc => {
// Set input mode back to form
self.input_mode = InputMode::Form;
}
_ => { /* Nothing to do */ }
}
}
}
/// ### handle_input_event_mode_popup_save_bookmark
///
/// Input event handler for SaveBookmark popup
pub(super) fn handle_input_event_mode_popup_save_bookmark(&mut self, ev: &InputEvent) {
// If enter, close popup, otherwise push chars to input
if let InputEvent::Key(key) = ev {
match key.code {
KeyCode::Esc => {
// Abort input
// Clear current input text
self.input_txt.clear();
// Set mode back to form
self.input_mode = InputMode::Form;
// Reset choice option to yes
self.choice_opt = DialogYesNoOption::Yes;
}
KeyCode::Enter => {
// Submit
let input_text: String = self.input_txt.clone();
// Clear current input text
self.input_txt.clear();
// Set mode back to form BEFORE CALLBACKS!!! Callback can then overwrite this, clever uh?
self.input_mode = InputMode::Form;
// Call cb
self.callback_save_bookmark(input_text);
// Reset choice option to yes
self.choice_opt = DialogYesNoOption::Yes;
}
KeyCode::Left => self.choice_opt = DialogYesNoOption::Yes, // Move yes/no with arrows
KeyCode::Right => self.choice_opt = DialogYesNoOption::No, // Move yes/no with arrows
KeyCode::Char(ch) => self.input_txt.push(ch),
KeyCode::Backspace => {
let _ = self.input_txt.pop();
}
_ => { /* Nothing to do */ }
}
}
}
/// ### handle_input_event_mode_popup_yesno
///
/// Input event handler for popup alert
pub(super) fn handle_input_event_mode_popup_yesno(
&mut self,
ev: &InputEvent,
yes_cb: DialogCallback,
no_cb: DialogCallback,
) {
// If enter, close popup, otherwise move dialog option
if let InputEvent::Key(key) = ev {
match key.code {
KeyCode::Enter => {
// @! Set input mode to Form BEFORE CALLBACKS!!! Callback can then overwrite this, clever uh?
self.input_mode = InputMode::Form;
// Check if user selected yes or not
match self.choice_opt {
DialogYesNoOption::No => no_cb(self),
DialogYesNoOption::Yes => yes_cb(self),
}
// Reset choice option to yes
self.choice_opt = DialogYesNoOption::Yes;
}
KeyCode::Right => self.choice_opt = DialogYesNoOption::No, // Set to NO
KeyCode::Left => self.choice_opt = DialogYesNoOption::Yes, // Set to YES
_ => { /* Nothing to do */ }
}
}
}
}

View File

@@ -0,0 +1,629 @@
//! ## AuthActivity
//!
//! `auth_activity` is the module which implements the authentication activity
/*
*
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
use super::{
AuthActivity, Context, DialogYesNoOption, FileTransferProtocol, InputField, InputForm,
InputMode, PopupType,
};
use crate::utils::fmt::align_text_center;
use tui::{
layout::{Constraint, Corner, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::{Span, Spans, Text},
widgets::{Block, BorderType, Borders, Clear, List, ListItem, ListState, Paragraph, Tabs},
};
use unicode_width::UnicodeWidthStr;
impl AuthActivity {
/// ### draw
///
/// Draw UI
pub(super) fn draw(&mut self) {
let mut ctx: Context = self.context.take().unwrap();
let _ = ctx.terminal.draw(|f| {
// Prepare chunks
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.constraints(
[
Constraint::Percentage(70), // Auth Form
Constraint::Percentage(30), // Bookmarks
]
.as_ref(),
)
.split(f.size());
// Create explorer chunks
let auth_chunks = Layout::default()
.constraints(
[
Constraint::Length(5),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
]
.as_ref(),
)
.direction(Direction::Vertical)
.split(chunks[0]);
// Create bookmark chunks
let bookmark_chunks = Layout::default()
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.direction(Direction::Horizontal)
.split(chunks[1]);
// Draw header
f.render_widget(self.draw_header(), auth_chunks[0]);
// Draw input fields
f.render_widget(self.draw_remote_address(), auth_chunks[1]);
f.render_widget(self.draw_remote_port(), auth_chunks[2]);
f.render_widget(self.draw_protocol_select(), auth_chunks[3]);
f.render_widget(self.draw_protocol_username(), auth_chunks[4]);
f.render_widget(self.draw_protocol_password(), auth_chunks[5]);
// Draw footer
f.render_widget(self.draw_footer(), auth_chunks[6]);
// Set cursor
if let InputForm::AuthCredentials = self.input_form {
match self.selected_field {
InputField::Address => f.set_cursor(
auth_chunks[1].x + self.address.width() as u16 + 1,
auth_chunks[1].y + 1,
),
InputField::Port => f.set_cursor(
auth_chunks[2].x + self.port.width() as u16 + 1,
auth_chunks[2].y + 1,
),
InputField::Username => f.set_cursor(
auth_chunks[4].x + self.username.width() as u16 + 1,
auth_chunks[4].y + 1,
),
InputField::Password => f.set_cursor(
auth_chunks[5].x + self.password_placeholder.width() as u16 + 1,
auth_chunks[5].y + 1,
),
_ => {}
}
}
// Draw bookmarks
if let Some(tab) = self.draw_bookmarks_tab() {
let mut bookmarks_state: ListState = ListState::default();
bookmarks_state.select(Some(self.bookmarks_idx));
f.render_stateful_widget(tab, bookmark_chunks[0], &mut bookmarks_state);
}
if let Some(tab) = self.draw_recents_tab() {
let mut recents_state: ListState = ListState::default();
recents_state.select(Some(self.recents_idx));
f.render_stateful_widget(tab, bookmark_chunks[1], &mut recents_state);
}
// Draw popup
if let InputMode::Popup(popup) = &self.input_mode {
// Calculate popup size
let (width, height): (u16, u16) = match popup {
PopupType::Alert(_, _) => (50, 10),
PopupType::Help => (50, 70),
PopupType::SaveBookmark => (20, 20),
PopupType::YesNo(_, _, _) => (30, 10),
};
let popup_area: Rect = self.draw_popup_area(f.size(), width, height);
f.render_widget(Clear, popup_area); //this clears out the background
match popup {
PopupType::Alert(color, txt) => f.render_widget(
self.draw_popup_alert(*color, txt.clone(), popup_area.width),
popup_area,
),
PopupType::Help => f.render_widget(self.draw_popup_help(), popup_area),
PopupType::SaveBookmark => {
let popup_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Length(3), // Input form
Constraint::Length(2), // Yes/No
]
.as_ref(),
)
.split(popup_area);
let (input, yes_no): (Paragraph, Tabs) = self.draw_popup_save_bookmark();
// Render parts
f.render_widget(input, popup_chunks[0]);
f.render_widget(yes_no, popup_chunks[1]);
// Set cursor
f.set_cursor(
popup_chunks[0].x + self.input_txt.width() as u16 + 1,
popup_chunks[0].y + 1,
)
}
PopupType::YesNo(txt, _, _) => {
f.render_widget(self.draw_popup_yesno(txt.clone()), popup_area)
}
}
}
});
self.context = Some(ctx);
}
/// ### draw_remote_address
///
/// Draw remote address block
fn draw_remote_address(&self) -> Paragraph {
Paragraph::new(self.address.as_ref())
.style(match self.selected_field {
InputField::Address => Style::default().fg(Color::Yellow),
_ => Style::default(),
})
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.title("Remote address"),
)
}
/// ### draw_remote_port
///
/// Draw remote port block
fn draw_remote_port(&self) -> Paragraph {
Paragraph::new(self.port.as_ref())
.style(match self.selected_field {
InputField::Port => Style::default().fg(Color::Cyan),
_ => Style::default(),
})
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.title("Remote port"),
)
}
/// ### draw_protocol_select
///
/// Draw protocol select
fn draw_protocol_select(&self) -> Tabs {
let protocols: Vec<Spans> = vec![
Spans::from("SFTP"),
Spans::from("SCP"),
Spans::from("FTP"),
Spans::from("FTPS"),
];
let index: usize = match self.protocol {
FileTransferProtocol::Sftp => 0,
FileTransferProtocol::Scp => 1,
FileTransferProtocol::Ftp(ftps) => match ftps {
false => 2,
true => 3,
},
};
Tabs::new(protocols)
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.title("Protocol"),
)
.select(index)
.style(match self.selected_field {
InputField::Protocol => Style::default().fg(Color::Green),
_ => Style::default(),
})
.highlight_style(
Style::default()
.add_modifier(Modifier::BOLD)
.bg(Color::Green)
.fg(Color::Black),
)
}
/// ### draw_protocol_username
///
/// Draw username block
fn draw_protocol_username(&self) -> Paragraph {
Paragraph::new(self.username.as_ref())
.style(match self.selected_field {
InputField::Username => Style::default().fg(Color::Magenta),
_ => Style::default(),
})
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.title("Username"),
)
}
/// ### draw_protocol_password
///
/// Draw password block
fn draw_protocol_password(&mut self) -> Paragraph {
// Create password secret
self.password_placeholder = (0..self.password.width()).map(|_| "*").collect::<String>();
Paragraph::new(self.password_placeholder.as_ref())
.style(match self.selected_field {
InputField::Password => Style::default().fg(Color::LightBlue),
_ => Style::default(),
})
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.title("Password"),
)
}
/// ### draw_header
///
/// Draw header
fn draw_header(&self) -> Paragraph {
Paragraph::new(" _____ ____ ____ ____ \n|_ _|__ _ __ _ __ ___ / ___| / ___| _ \\ \n | |/ _ \\ '__| '_ ` _ \\\\___ \\| | | |_) |\n | | __/ | | | | | | |___) | |___| __/ \n |_|\\___|_| |_| |_| |_|____/ \\____|_| \n")
.style(Style::default().fg(Color::White).add_modifier(Modifier::BOLD))
}
/// ### draw_footer
///
/// Draw authentication page footer
fn draw_footer(&self) -> Paragraph {
// Write header
let (footer, h_style) = (
vec![
Span::raw("Press "),
Span::styled("<CTRL+H>", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to show keybindings"),
],
Style::default().add_modifier(Modifier::BOLD),
);
let mut footer_text = Text::from(Spans::from(footer));
footer_text.patch_style(h_style);
Paragraph::new(footer_text)
}
/// ### draw_local_explorer
///
/// Draw local explorer list
pub(super) fn draw_bookmarks_tab(&self) -> Option<List> {
self.bookmarks_client.as_ref()?;
let hosts: Vec<ListItem> = self
.bookmarks_client
.as_ref()
.unwrap()
.iter_bookmarks()
.map(|key: &String| {
let entry: (String, u16, FileTransferProtocol, String, _) = self
.bookmarks_client
.as_ref()
.unwrap()
.get_bookmark(key)
.unwrap();
ListItem::new(Span::from(format!(
"{} ({}://{}@{}:{})",
key,
AuthActivity::protocol_to_str(entry.2),
entry.3,
entry.0,
entry.1
)))
})
.collect();
// Get colors to use; highlight element inverting fg/bg only when tab is active
let (fg, bg): (Color, Color) = match self.input_form {
InputForm::Bookmarks => (Color::Black, Color::LightGreen),
_ => (Color::Reset, Color::Reset),
};
Some(
List::new(hosts)
.block(
Block::default()
.borders(Borders::ALL)
.border_style(match self.input_form {
InputForm::Bookmarks => Style::default().fg(Color::LightGreen),
_ => Style::default(),
})
.title("Bookmarks"),
)
.start_corner(Corner::TopLeft)
.highlight_style(Style::default().fg(fg).bg(bg).add_modifier(Modifier::BOLD)),
)
}
/// ### draw_local_explorer
///
/// Draw local explorer list
pub(super) fn draw_recents_tab(&self) -> Option<List> {
self.bookmarks_client.as_ref()?;
let hosts: Vec<ListItem> = self
.bookmarks_client
.as_ref()
.unwrap()
.iter_recents()
.map(|key: &String| {
let entry: (String, u16, FileTransferProtocol, String) = self
.bookmarks_client
.as_ref()
.unwrap()
.get_recent(key)
.unwrap();
ListItem::new(Span::from(format!(
"{}://{}@{}:{}",
AuthActivity::protocol_to_str(entry.2),
entry.3,
entry.0,
entry.1
)))
})
.collect();
// Get colors to use; highlight element inverting fg/bg only when tab is active
let (fg, bg): (Color, Color) = match self.input_form {
InputForm::Recents => (Color::Black, Color::LightBlue),
_ => (Color::Reset, Color::Reset),
};
Some(
List::new(hosts)
.block(
Block::default()
.borders(Borders::TOP | Borders::BOTTOM | Borders::RIGHT)
.border_style(match self.input_form {
InputForm::Recents => Style::default().fg(Color::LightBlue),
_ => Style::default(),
})
.title("Recent connections"),
)
.start_corner(Corner::TopLeft)
.highlight_style(Style::default().fg(fg).bg(bg).add_modifier(Modifier::BOLD)),
)
}
/// ### draw_popup_area
///
/// Draw popup area
fn draw_popup_area(&self, area: Rect, width: u16, height: u16) -> Rect {
let popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage((100 - height) / 2),
Constraint::Percentage(height),
Constraint::Percentage((100 - height) / 2),
]
.as_ref(),
)
.split(area);
Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Percentage((100 - width) / 2),
Constraint::Percentage(width),
Constraint::Percentage((100 - width) / 2),
]
.as_ref(),
)
.split(popup_layout[1])[1]
}
/// ### draw_popup_alert
///
/// Draw alert popup
fn draw_popup_alert(&self, color: Color, text: String, width: u16) -> List {
// Wraps texts
let message_rows = textwrap::wrap(text.as_str(), width as usize);
let mut lines: Vec<ListItem> = Vec::new();
for msg in message_rows.iter() {
lines.push(ListItem::new(Spans::from(align_text_center(msg, width))));
}
List::new(lines)
.block(
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(color))
.border_type(BorderType::Rounded)
.title("Alert"),
)
.start_corner(Corner::TopLeft)
.style(Style::default().fg(color))
}
/// ### draw_popup_input
///
/// Draw input popup
pub(super) fn draw_popup_save_bookmark(&self) -> (Paragraph, Tabs) {
let input: Paragraph = Paragraph::new(self.input_txt.as_ref())
.style(Style::default().fg(Color::White))
.block(
Block::default()
.borders(Borders::TOP | Borders::RIGHT | Borders::LEFT)
.border_type(BorderType::Rounded)
.title("Save bookmark as..."),
);
let choices: Vec<Spans> = vec![Spans::from("Yes"), Spans::from("No")];
let index: usize = match self.choice_opt {
DialogYesNoOption::Yes => 0,
DialogYesNoOption::No => 1,
};
let tabs: Tabs = Tabs::new(choices)
.block(
Block::default()
.borders(Borders::BOTTOM | Borders::RIGHT | Borders::LEFT)
.border_type(BorderType::Rounded)
.title("Save password?"),
)
.select(index)
.style(Style::default())
.highlight_style(
Style::default()
.add_modifier(Modifier::BOLD)
.fg(Color::LightRed),
);
(input, tabs)
}
/// ### draw_popup_yesno
///
/// Draw yes/no select popup
pub(super) fn draw_popup_yesno(&self, text: String) -> Tabs {
let choices: Vec<Spans> = vec![Spans::from("Yes"), Spans::from("No")];
let index: usize = match self.choice_opt {
DialogYesNoOption::Yes => 0,
DialogYesNoOption::No => 1,
};
Tabs::new(choices)
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.title(text),
)
.select(index)
.style(Style::default())
.highlight_style(
Style::default()
.add_modifier(Modifier::BOLD)
.fg(Color::Yellow),
)
}
/// ### draw_footer
///
/// Draw authentication page footer
pub(super) fn draw_popup_help(&self) -> List {
// Write header
let cmds: Vec<ListItem> = vec![
ListItem::new(Spans::from(vec![
Span::styled(
"<ESC>",
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
),
Span::raw(" "),
Span::raw("Quit TermSCP"),
])),
ListItem::new(Spans::from(vec![
Span::styled(
"<TAB>",
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
),
Span::raw(" "),
Span::raw("Switch input form and bookmarks"),
])),
ListItem::new(Spans::from(vec![
Span::styled(
"<RIGHT/LEFT>",
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
),
Span::raw(" "),
Span::raw("Change bookmark tab"),
])),
ListItem::new(Spans::from(vec![
Span::styled(
"<UP/DOWN>",
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
),
Span::raw(" "),
Span::raw("Move up/down in current tab"),
])),
ListItem::new(Spans::from(vec![
Span::styled(
"<ENTER>",
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
),
Span::raw(" "),
Span::raw("Submit"),
])),
ListItem::new(Spans::from(vec![
Span::styled(
"<DEL>",
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
),
Span::raw(" "),
Span::raw("Delete bookmark"),
])),
ListItem::new(Spans::from(vec![
Span::styled(
"<E>",
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
),
Span::raw(" "),
Span::raw("Delete selected bookmark"),
])),
ListItem::new(Spans::from(vec![
Span::styled(
"<CTRL+H>",
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
),
Span::raw(" "),
Span::raw("Show help"),
])),
ListItem::new(Spans::from(vec![
Span::styled(
"<CTRL+S>",
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
),
Span::raw(" "),
Span::raw("Save bookmark"),
])),
];
List::new(cmds)
.block(
Block::default()
.borders(Borders::ALL)
.border_style(Style::default())
.border_type(BorderType::Rounded)
.title("Help"),
)
.start_corner(Corner::TopLeft)
}
/// ### protocol_to_str
///
/// Convert protocol to str for layouts
fn protocol_to_str(proto: FileTransferProtocol) -> &'static str {
match proto {
FileTransferProtocol::Ftp(secure) => match secure {
true => "ftps",
false => "ftp",
},
FileTransferProtocol::Scp => "scp",
FileTransferProtocol::Sftp => "sftp",
}
}
}

View File

@@ -0,0 +1,224 @@
//! ## AuthActivity
//!
//! `auth_activity` is the module which implements the authentication activity
/*
*
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Sub modules
mod bookmarks;
mod callbacks;
mod input;
mod layout;
// Dependencies
extern crate crossterm;
extern crate tui;
extern crate unicode_width;
// locals
use super::{Activity, Context};
use crate::filetransfer::FileTransferProtocol;
use crate::system::bookmarks_client::BookmarksClient;
// Includes
use crossterm::event::Event as InputEvent;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use tui::style::Color;
// Types
type DialogCallback = fn(&mut AuthActivity);
/// ### InputField
///
/// InputField describes the current input field to edit
#[derive(std::cmp::PartialEq)]
enum InputField {
Address,
Port,
Protocol,
Username,
Password,
}
/// ### DialogYesNoOption
///
/// Current yes/no dialog option
#[derive(std::cmp::PartialEq, Clone)]
enum DialogYesNoOption {
Yes,
No,
}
/// ### PopupType
///
/// PopupType describes the type of the popup displayed
#[derive(Clone)]
enum PopupType {
Alert(Color, String), // Show a message displaying text with the provided color
Help, // Help page
SaveBookmark,
YesNo(String, DialogCallback, DialogCallback), // Yes, no callback
}
/// ### InputMode
///
/// InputMode describes the current input mode
/// Each input mode handle the input events in a different way
enum InputMode {
Form,
Popup(PopupType),
}
#[derive(std::cmp::PartialEq)]
/// ### InputForm
///
/// InputForm describes the selected input form
enum InputForm {
AuthCredentials,
Bookmarks,
Recents,
}
/// ### AuthActivity
///
/// AuthActivity is the data holder for the authentication activity
pub struct AuthActivity {
pub address: String,
pub port: String,
pub protocol: FileTransferProtocol,
pub username: String,
pub password: String,
pub submit: bool, // becomes true after user has submitted fields
pub quit: bool, // Becomes true if user has pressed esc
context: Option<Context>,
bookmarks_client: Option<BookmarksClient>,
selected_field: InputField, // Selected field in AuthCredentials Form
input_mode: InputMode,
input_form: InputForm,
password_placeholder: String,
redraw: bool, // Should ui actually be redrawned?
input_txt: String, // Input text
choice_opt: DialogYesNoOption, // Dialog popup selected option
bookmarks_idx: usize, // Index of selected bookmark
recents_idx: usize, // Index of selected recent
}
impl Default for AuthActivity {
fn default() -> Self {
Self::new()
}
}
impl AuthActivity {
/// ### new
///
/// Instantiates a new AuthActivity
pub fn new() -> AuthActivity {
AuthActivity {
address: String::new(),
port: String::from("22"),
protocol: FileTransferProtocol::Sftp,
username: String::new(),
password: String::new(),
submit: false,
quit: false,
context: None,
bookmarks_client: None,
selected_field: InputField::Address,
input_mode: InputMode::Form,
input_form: InputForm::AuthCredentials,
password_placeholder: String::new(),
redraw: true, // True at startup
input_txt: String::new(),
choice_opt: DialogYesNoOption::Yes,
bookmarks_idx: 0,
recents_idx: 0,
}
}
}
impl Activity for AuthActivity {
/// ### on_create
///
/// `on_create` is the function which must be called to initialize the activity.
/// `on_create` must initialize all the data structures used by the activity
/// Context is taken from activity manager and will be released only when activity is destroyed
fn on_create(&mut self, context: Context) {
// Set context
self.context = Some(context);
// Clear terminal
self.context.as_mut().unwrap().clear_screen();
// Put raw mode on enabled
let _ = enable_raw_mode();
self.input_mode = InputMode::Form;
// Init bookmarks client
if self.bookmarks_client.is_none() {
self.init_bookmarks_client();
}
}
/// ### on_draw
///
/// `on_draw` is the function which draws the graphical interface.
/// This function must be called at each tick to refresh the interface
fn on_draw(&mut self) {
// Context must be something
if self.context.is_none() {
return;
}
// Read one event
if let Ok(event) = self.context.as_ref().unwrap().input_hnd.read_event() {
if let Some(event) = event {
// Set redraw to true
self.redraw = true;
// Handle event
self.handle_input_event(&event);
}
}
// Redraw if necessary
if self.redraw {
// Draw
self.draw();
// Set redraw to false
self.redraw = false;
}
}
/// ### on_destroy
///
/// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity.
/// This function must be called once before terminating the activity.
/// This function finally releases the context
fn on_destroy(&mut self) -> Option<Context> {
// Disable raw mode
let _ = disable_raw_mode();
self.context.as_ref()?;
// Clear terminal and return
match self.context.take() {
Some(mut ctx) => {
ctx.clear_screen();
Some(ctx)
}
None => None,
}
}
}

View File

@@ -19,10 +19,9 @@
*
*/
use super::{FileExplorerTab, FileTransferActivity, FsEntry, InputMode, LogLevel, PopupType};
use super::{FileExplorerTab, FileTransferActivity, FsEntry, LogLevel};
use std::path::PathBuf;
use tui::style::Color;
impl FileTransferActivity {
/// ### callback_nothing_to_do
@@ -40,7 +39,7 @@ impl FileTransferActivity {
// If path is relative, concat pwd
let abs_dir_path: PathBuf = match dir_path.is_relative() {
true => {
let mut d: PathBuf = self.context.as_ref().unwrap().local.pwd();
let mut d: PathBuf = self.local.wrkdir.clone();
d.push(dir_path);
d
}
@@ -51,19 +50,11 @@ impl FileTransferActivity {
FileExplorerTab::Remote => {
// If path is relative, concat pwd
let abs_dir_path: PathBuf = match dir_path.is_relative() {
true => match self.client.pwd() {
Ok(mut wkrdir) => {
wkrdir.push(dir_path);
wkrdir
}
Err(err) => {
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
format!("Could not retrieve current directory: {}", err),
));
return;
}
},
true => {
let mut wrkdir: PathBuf = self.remote.wrkdir.clone();
wrkdir.push(dir_path);
wrkdir
}
false => dir_path,
};
self.remote_changedir(abs_dir_path.as_path(), true);
@@ -71,6 +62,77 @@ impl FileTransferActivity {
}
}
/// ### callback_copy
///
/// Callback for COPY command (both from local and remote)
pub(super) fn callback_copy(&mut self, input: String) {
let dest_path: PathBuf = PathBuf::from(input);
match self.tab {
FileExplorerTab::Local => {
// Get selected entry
if self.local.files.get(self.local.index).is_some() {
let entry: FsEntry = self.local.files.get(self.local.index).unwrap().clone();
if let Some(ctx) = self.context.as_mut() {
match ctx.local.copy(&entry, dest_path.as_path()) {
Ok(_) => {
self.log(
LogLevel::Info,
format!(
"Copied \"{}\" to \"{}\"",
entry.get_abs_path().display(),
dest_path.display()
)
.as_str(),
);
// Reload entries
let wrkdir: PathBuf = self.local.wrkdir.clone();
self.local_scan(wrkdir.as_path());
}
Err(err) => self.log_and_alert(
LogLevel::Error,
format!(
"Could not copy \"{}\" to \"{}\": {}",
entry.get_abs_path().display(),
dest_path.display(),
err
),
),
}
}
}
}
FileExplorerTab::Remote => {
// Get selected entry
if self.remote.files.get(self.remote.index).is_some() {
let entry: FsEntry = self.remote.files.get(self.remote.index).unwrap().clone();
match self.client.as_mut().copy(&entry, dest_path.as_path()) {
Ok(_) => {
self.log(
LogLevel::Info,
format!(
"Copied \"{}\" to \"{}\"",
entry.get_abs_path().display(),
dest_path.display()
)
.as_str(),
);
self.reload_remote_dir();
}
Err(err) => self.log_and_alert(
LogLevel::Error,
format!(
"Could not copy \"{}\" to \"{}\": {}",
entry.get_abs_path().display(),
dest_path.display(),
err
),
),
}
}
}
}
}
/// ### callback_mkdir
///
/// Callback for MKDIR command (supports both local and remote)
@@ -90,24 +152,24 @@ impl FileTransferActivity {
LogLevel::Info,
format!("Created directory \"{}\"", input).as_ref(),
);
let wrkdir: PathBuf = self.context.as_ref().unwrap().local.pwd();
let wrkdir: PathBuf = self.local.wrkdir.clone();
self.local_scan(wrkdir.as_path());
}
Err(err) => {
// Report err
self.log(
self.log_and_alert(
LogLevel::Error,
format!("Could not create directory \"{}\": {}", input, err).as_ref(),
);
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
format!("Could not create directory \"{}\": {}", input, err),
));
);
}
}
}
FileExplorerTab::Remote => {
match self.client.mkdir(PathBuf::from(input.as_str()).as_path()) {
match self
.client
.as_mut()
.mkdir(PathBuf::from(input.as_str()).as_path())
{
Ok(_) => {
// Reload files
self.log(
@@ -118,14 +180,10 @@ impl FileTransferActivity {
}
Err(err) => {
// Report err
self.log(
self.log_and_alert(
LogLevel::Error,
format!("Could not create directory \"{}\": {}", input, err).as_ref(),
);
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
format!("Could not create directory \"{}\": {}", input, err),
));
);
}
}
}
@@ -141,7 +199,7 @@ impl FileTransferActivity {
let mut dst_path: PathBuf = PathBuf::from(input);
// Check if path is relative
if dst_path.as_path().is_relative() {
let mut wrkdir: PathBuf = self.context.as_ref().unwrap().local.pwd();
let mut wrkdir: PathBuf = self.local.wrkdir.clone();
wrkdir.push(dst_path);
dst_path = wrkdir;
}
@@ -158,7 +216,8 @@ impl FileTransferActivity {
{
Ok(_) => {
// Reload files
self.local_scan(self.context.as_ref().unwrap().local.pwd().as_path());
let path: PathBuf = self.local.wrkdir.clone();
self.local_scan(path.as_path());
// Log
self.log(
LogLevel::Info,
@@ -171,19 +230,14 @@ impl FileTransferActivity {
);
}
Err(err) => {
self.log(
self.log_and_alert(
LogLevel::Error,
format!(
"Could not rename file \"{}\": {}",
full_path.display(),
err
)
.as_ref(),
),
);
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
format!("Could not rename file: {}", err),
))
}
}
}
@@ -194,12 +248,11 @@ impl FileTransferActivity {
let full_path: PathBuf = entry.get_abs_path();
// Rename file or directory and report status as popup
let dst_path: PathBuf = PathBuf::from(input);
match self.client.rename(entry, dst_path.as_path()) {
match self.client.as_mut().rename(entry, dst_path.as_path()) {
Ok(_) => {
// Reload files
if let Ok(path) = self.client.pwd() {
self.remote_scan(path.as_path());
}
let path: PathBuf = self.remote.wrkdir.clone();
self.remote_scan(path.as_path());
// Log
self.log(
LogLevel::Info,
@@ -212,19 +265,14 @@ impl FileTransferActivity {
);
}
Err(err) => {
self.log(
self.log_and_alert(
LogLevel::Error,
format!(
"Could not rename file \"{}\": {}",
full_path.display(),
err
)
.as_ref(),
),
);
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
format!("Could not rename file: {}", err),
))
}
}
}
@@ -246,7 +294,8 @@ impl FileTransferActivity {
match self.context.as_mut().unwrap().local.remove(entry) {
Ok(_) => {
// Reload files
self.local_scan(self.context.as_ref().unwrap().local.pwd().as_path());
let p: PathBuf = self.local.wrkdir.clone();
self.local_scan(p.as_path());
// Log
self.log(
LogLevel::Info,
@@ -254,19 +303,14 @@ impl FileTransferActivity {
);
}
Err(err) => {
self.log(
self.log_and_alert(
LogLevel::Error,
format!(
"Could not delete file \"{}\": {}",
full_path.display(),
err
)
.as_ref(),
),
);
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
format!("Could not delete file: {}", err),
))
}
}
}
@@ -285,19 +329,14 @@ impl FileTransferActivity {
);
}
Err(err) => {
self.log(
self.log_and_alert(
LogLevel::Error,
format!(
"Could not delete file \"{}\": {}",
full_path.display(),
err
)
.as_ref(),
),
);
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
format!("Could not delete file: {}", err),
))
}
}
}
@@ -313,20 +352,7 @@ impl FileTransferActivity {
match self.tab {
FileExplorerTab::Local => {
// Get pwd
let wrkdir: PathBuf = match self.client.pwd() {
Ok(p) => p,
Err(err) => {
self.log(
LogLevel::Error,
format!("Could not get current remote path: {}", err).as_ref(),
);
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
format!("Could not get current remote path: {}", err),
));
return;
}
};
let wrkdir: PathBuf = self.remote.wrkdir.clone();
// Get file and clone (due to mutable / immutable stuff...)
if self.local.files.get(self.local.index).is_some() {
let file: FsEntry = self.local.files.get(self.local.index).unwrap().clone();
@@ -339,11 +365,8 @@ impl FileTransferActivity {
if self.remote.files.get(self.remote.index).is_some() {
let file: FsEntry = self.remote.files.get(self.remote.index).unwrap().clone();
// Call upload; pass realfile, keep link name
self.filetransfer_recv(
&file.get_realfile(),
self.context.as_ref().unwrap().local.pwd().as_path(),
Some(input),
);
let wrkdir: PathBuf = self.local.wrkdir.clone();
self.filetransfer_recv(&file.get_realfile(), wrkdir.as_path(), Some(input));
}
}
}

View File

@@ -19,6 +19,8 @@
*
*/
extern crate tempfile;
use super::{
DialogCallback, DialogYesNoOption, FileExplorerTab, FileTransferActivity, FsEntry, InputEvent,
InputField, InputMode, LogLevel, OnInputSubmitCallback, PopupType,
@@ -26,7 +28,6 @@ use super::{
use crossterm::event::{KeyCode, KeyModifiers};
use std::path::PathBuf;
use tui::style::Color;
impl FileTransferActivity {
/// ### read_input_event
@@ -176,9 +177,19 @@ impl FileTransferActivity {
}
}
KeyCode::Char(ch) => match ch {
'q' | 'Q' => {
// Create quit prompt dialog
self.input_mode = self.create_quit_popup();
'c' | 'C' => {
// Copy
self.input_mode = InputMode::Popup(PopupType::Input(
String::from("Insert destination name"),
FileTransferActivity::callback_copy,
));
}
'd' | 'D' => {
// Make directory
self.input_mode = InputMode::Popup(PopupType::Input(
String::from("Insert directory name"),
FileTransferActivity::callback_mkdir,
));
}
'e' | 'E' => {
// Get file at index
@@ -204,13 +215,6 @@ impl FileTransferActivity {
FileTransferActivity::callback_change_directory,
));
}
'd' | 'D' => {
// Make directory
self.input_mode = InputMode::Popup(PopupType::Input(
String::from("Insert directory name"),
FileTransferActivity::callback_mkdir,
));
}
'h' | 'H' => {
// Show help
self.input_mode = InputMode::Popup(PopupType::Help);
@@ -221,9 +225,41 @@ impl FileTransferActivity {
}
'l' | 'L' => {
// Reload file entries
let pwd: PathBuf = self.context.as_ref().unwrap().local.pwd();
let pwd: PathBuf = self.local.wrkdir.clone();
self.local_scan(pwd.as_path());
}
'o' | 'O' => {
// Edit local file
if self.local.files.get(self.local.index).is_some() {
// Clone entry due to mutable stuff...
let fsentry: FsEntry =
self.local.files.get(self.local.index).unwrap().clone();
// Check if file
if fsentry.is_file() {
self.log(
LogLevel::Info,
format!(
"Opening file \"{}\"...",
fsentry.get_abs_path().display()
)
.as_str(),
);
// Edit file
match self.edit_local_file(fsentry.get_abs_path().as_path()) {
Ok(_) => {
// Reload directory
let pwd: PathBuf = self.local.wrkdir.clone();
self.local_scan(pwd.as_path());
}
Err(err) => self.log_and_alert(LogLevel::Error, err),
}
}
}
}
'q' | 'Q' => {
// Create quit prompt dialog
self.input_mode = self.create_quit_popup();
}
'r' | 'R' => {
// Rename
self.input_mode = InputMode::Popup(PopupType::Input(
@@ -242,27 +278,14 @@ impl FileTransferActivity {
'u' | 'U' => {
// Go to parent directory
// Get pwd
let path: PathBuf = self.context.as_ref().unwrap().local.pwd();
let path: PathBuf = self.local.wrkdir.clone();
if let Some(parent) = path.as_path().parent() {
self.local_changedir(parent, true);
}
}
' ' => {
// Get pwd
let wrkdir: PathBuf = match self.client.pwd() {
Ok(p) => p,
Err(err) => {
self.log(
LogLevel::Error,
format!("Could not get current remote path: {}", err).as_ref(),
);
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
format!("Could not get current remote path: {}", err),
));
return;
}
};
let wrkdir: PathBuf = self.remote.wrkdir.clone();
// Get file and clone (due to mutable / immutable stuff...)
if self.local.files.get(self.local.index).is_some() {
let file: FsEntry =
@@ -373,6 +396,20 @@ impl FileTransferActivity {
}
}
KeyCode::Char(ch) => match ch {
'c' | 'C' => {
// Copy
self.input_mode = InputMode::Popup(PopupType::Input(
String::from("Insert destination name"),
FileTransferActivity::callback_copy,
));
}
'd' | 'D' => {
// Make directory
self.input_mode = InputMode::Popup(PopupType::Input(
String::from("Insert directory name"),
FileTransferActivity::callback_mkdir,
));
}
'e' | 'E' => {
// Get file at index
if let Some(entry) = self.remote.files.get(self.remote.index) {
@@ -389,13 +426,6 @@ impl FileTransferActivity {
))
}
}
'd' | 'D' => {
// Make directory
self.input_mode = InputMode::Popup(PopupType::Input(
String::from("Insert directory name"),
FileTransferActivity::callback_mkdir,
));
}
'g' | 'G' => {
// Goto
// Show input popup
@@ -416,6 +446,33 @@ impl FileTransferActivity {
// Reload file entries
self.reload_remote_dir();
}
'o' | 'O' => {
// Edit remote file
if self.remote.files.get(self.remote.index).is_some() {
// Clone entry due to mutable stuff...
let fsentry: FsEntry =
self.remote.files.get(self.remote.index).unwrap().clone();
// Check if file
if let FsEntry::File(file) = fsentry {
self.log(
LogLevel::Info,
format!("Opening file \"{}\"...", file.abs_path.display())
.as_str(),
);
// Edit file
match self.edit_remote_file(&file) {
Ok(_) => {
// Reload directory
let pwd: PathBuf = self.remote.wrkdir.clone();
self.remote_scan(pwd.as_path());
}
Err(err) => self.log_and_alert(LogLevel::Error, err),
}
// Put input mode back to normal
self.input_mode = InputMode::Explorer;
}
}
}
'q' | 'Q' => {
// Create quit prompt dialog
self.input_mode = self.create_quit_popup();
@@ -436,20 +493,11 @@ impl FileTransferActivity {
));
}
'u' | 'U' => {
// Go to parent directory
// Get pwd
match self.client.pwd() {
Ok(path) => {
if let Some(parent) = path.as_path().parent() {
self.remote_changedir(parent, true);
}
}
Err(err) => {
self.input_mode = InputMode::Popup(PopupType::Alert(
Color::Red,
format!("Could not change working directory: {}", err),
))
}
let path: PathBuf = self.remote.wrkdir.clone();
// Go to parent directory
if let Some(parent) = path.as_path().parent() {
self.remote_changedir(parent, true);
}
}
' ' => {
@@ -459,9 +507,10 @@ impl FileTransferActivity {
self.remote.files.get(self.remote.index).unwrap().clone();
let name: String = file.get_name();
// Call upload; pass realfile, keep link name
let wrkdir: PathBuf = self.local.wrkdir.clone();
self.filetransfer_recv(
&file.get_realfile(),
self.context.as_ref().unwrap().local.pwd().as_path(),
wrkdir.as_path(),
Some(name),
);
}
@@ -645,7 +694,7 @@ impl FileTransferActivity {
}
}
/// ### handle_input_event_mode_explorer_alert
/// ### handle_input_event_mode_popup_progress
///
/// Input event handler for popup alert
pub(super) fn handle_input_event_mode_popup_progress(&mut self, ev: &InputEvent) {
@@ -660,14 +709,14 @@ impl FileTransferActivity {
}
}
/// ### handle_input_event_mode_explorer_alert
/// ### handle_input_event_mode_popup_wait
///
/// Input event handler for popup alert
pub(super) fn handle_input_event_mode_popup_wait(&mut self, _ev: &InputEvent) {
// There's nothing you can do here I guess... maybe ctrl+c in the future idk
}
/// ### handle_input_event_mode_explorer_alert
/// ### handle_input_event_mode_popup_yesno
///
/// Input event handler for popup alert
pub(super) fn handle_input_event_mode_popup_yesno(

View File

@@ -28,7 +28,7 @@ use super::{
Context, DialogYesNoOption, FileExplorerTab, FileTransferActivity, FsEntry, InputField,
InputMode, LogLevel, LogRecord, PopupType,
};
use crate::utils::{align_text_center, time_to_str};
use crate::utils::fmt::{align_text_center, fmt_time};
use bytesize::ByteSize;
use std::path::{Path, PathBuf};
@@ -36,7 +36,9 @@ use tui::{
layout::{Constraint, Corner, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::{Span, Spans},
widgets::{Block, Borders, Clear, Gauge, List, ListItem, ListState, Paragraph, Tabs},
widgets::{
Block, BorderType, Borders, Clear, Gauge, List, ListItem, ListState, Paragraph, Tabs,
},
};
use unicode_width::UnicodeWidthStr;
#[cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))]
@@ -48,7 +50,6 @@ impl FileTransferActivity {
/// Draw UI
pub(super) fn draw(&mut self) {
let mut ctx: Context = self.context.take().unwrap();
let local_wrkdir: PathBuf = ctx.local.pwd();
let _ = ctx.terminal.draw(|f| {
// Prepare chunks
let chunks = Layout::default()
@@ -75,17 +76,12 @@ impl FileTransferActivity {
remote_state.select(Some(self.remote.index));
// Draw tabs
f.render_stateful_widget(
self.draw_local_explorer(local_wrkdir, tabs_chunks[0].width),
self.draw_local_explorer(tabs_chunks[0].width),
tabs_chunks[0],
&mut localhost_state,
);
// Get pwd
let remote_wrkdir: PathBuf = match self.client.pwd() {
Ok(p) => p,
Err(_) => PathBuf::from("/"),
};
f.render_stateful_widget(
self.draw_remote_explorer(remote_wrkdir, tabs_chunks[1].width),
self.draw_remote_explorer(tabs_chunks[1].width),
tabs_chunks[1],
&mut remote_state,
);
@@ -151,7 +147,7 @@ impl FileTransferActivity {
/// ### draw_local_explorer
///
/// Draw local explorer list
pub(super) fn draw_local_explorer(&self, local_wrkdir: PathBuf, width: u16) -> List {
pub(super) fn draw_local_explorer(&self, width: u16) -> List {
let hostname: String = match hostname::get() {
Ok(h) => {
let hostname: String = h.as_os_str().to_string_lossy().to_string();
@@ -186,7 +182,7 @@ impl FileTransferActivity {
"{}:{} ",
hostname,
FileTransferActivity::elide_wrkdir_path(
local_wrkdir.as_path(),
self.local.wrkdir.as_path(),
hostname.as_str(),
width
)
@@ -200,7 +196,7 @@ impl FileTransferActivity {
/// ### draw_remote_explorer
///
/// Draw remote explorer list
pub(super) fn draw_remote_explorer(&self, remote_wrkdir: PathBuf, width: u16) -> List {
pub(super) fn draw_remote_explorer(&self, width: u16) -> List {
let files: Vec<ListItem> = self
.remote
.files
@@ -227,7 +223,7 @@ impl FileTransferActivity {
"{}:{} ",
self.params.address,
FileTransferActivity::elide_wrkdir_path(
remote_wrkdir.as_path(),
self.remote.wrkdir.as_path(),
self.params.address.as_str(),
width
)
@@ -341,6 +337,7 @@ impl FileTransferActivity {
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(color))
.border_type(BorderType::Rounded)
.title("Alert"),
)
.start_corner(Corner::TopLeft)
@@ -362,6 +359,7 @@ impl FileTransferActivity {
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Red))
.border_type(BorderType::Rounded)
.title("Fatal error"),
)
.start_corner(Corner::TopLeft)
@@ -373,7 +371,12 @@ impl FileTransferActivity {
pub(super) fn draw_popup_input(&self, text: String) -> Paragraph {
Paragraph::new(self.input_txt.as_ref())
.style(Style::default().fg(Color::White))
.block(Block::default().borders(Borders::ALL).title(text))
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.title(text),
)
}
/// ### draw_popup_progress
@@ -381,16 +384,22 @@ impl FileTransferActivity {
/// Draw progress popup
pub(super) fn draw_popup_progress(&self, text: String) -> Gauge {
// Calculate ETA
let elapsed_secs: u64 = self.transfer.started.elapsed().as_secs();
let eta: String = match self.transfer.progress as u64 {
0 => String::from("--:--"), // NOTE: would divide by 0 :D
_ => {
let elapsed_secs: u64 = self.transfer.started.elapsed().as_secs();
let eta: u64 =
((elapsed_secs * 100) / (self.transfer.progress as u64)) - elapsed_secs;
format!("{:0width$}:{:0width$}", (eta / 60), (eta % 60), width = 2)
}
};
let label = format!("{:.2}% - ETA {}", self.transfer.progress, eta);
// Calculate bytes/s
let label = format!(
"{:.2}% - ETA {} ({}/s)",
self.transfer.progress,
eta,
ByteSize(self.transfer.bytes_per_second())
);
Gauge::default()
.block(Block::default().borders(Borders::ALL).title(text))
.gauge_style(
@@ -418,6 +427,7 @@ impl FileTransferActivity {
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::White))
.border_type(BorderType::Rounded)
.title("Please wait"),
)
.start_corner(Corner::TopLeft)
@@ -434,7 +444,12 @@ impl FileTransferActivity {
DialogYesNoOption::No => 1,
};
Tabs::new(choices)
.block(Block::default().borders(Borders::ALL).title(text))
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.title(text),
)
.select(index)
.style(Style::default())
.highlight_style(
@@ -469,10 +484,10 @@ impl FileTransferActivity {
// Get name and path
let abs_path: PathBuf = fsentry.get_abs_path();
let name: String = fsentry.get_name();
let ctime: String = time_to_str(fsentry.get_creation_time(), "%b %d %Y %H:%M:%S");
let ctime: String = fmt_time(fsentry.get_creation_time(), "%b %d %Y %H:%M:%S");
let atime: String =
time_to_str(fsentry.get_last_access_time(), "%b %d %Y %H:%M:%S");
let mtime: String = time_to_str(fsentry.get_creation_time(), "%b %d %Y %H:%M:%S");
fmt_time(fsentry.get_last_access_time(), "%b %d %Y %H:%M:%S");
let mtime: String = fmt_time(fsentry.get_creation_time(), "%b %d %Y %H:%M:%S");
let (bsize, size): (ByteSize, usize) =
(ByteSize(fsentry.get_size() as u64), fsentry.get_size());
let user: Option<u32> = fsentry.get_user();
@@ -599,6 +614,7 @@ impl FileTransferActivity {
Block::default()
.borders(Borders::ALL)
.border_style(Style::default())
.border_type(BorderType::Rounded)
.title(file_name),
)
.start_corner(Corner::TopLeft)
@@ -700,6 +716,16 @@ impl FileTransferActivity {
Span::raw(" "),
Span::raw("Delete file"),
])),
ListItem::new(Spans::from(vec![
Span::styled(
"<C>",
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
),
Span::raw(" "),
Span::raw("Copy"),
])),
ListItem::new(Spans::from(vec![
Span::styled(
"<D>",
@@ -806,6 +832,7 @@ impl FileTransferActivity {
Block::default()
.borders(Borders::ALL)
.border_style(Style::default())
.border_type(BorderType::Rounded)
.title("Help"),
)
.start_corner(Corner::TopLeft)

View File

@@ -19,7 +19,7 @@
*
*/
use super::{FileTransferActivity, InputField, InputMode, LogLevel, LogRecord, PopupType};
use super::{Color, FileTransferActivity, InputField, InputMode, LogLevel, LogRecord, PopupType};
impl FileTransferActivity {
/// ### log
@@ -38,6 +38,20 @@ impl FileTransferActivity {
self.log_index = 0;
}
/// ### log_and_alert
///
/// Add message to log events and also display it as an alert
pub(super) fn log_and_alert(&mut self, level: LogLevel, msg: String) {
// Set input mode
let color: Color = match level {
LogLevel::Error => Color::Red,
LogLevel::Info => Color::Green,
LogLevel::Warn => Color::Yellow,
};
self.log(level, msg.as_str());
self.input_mode = InputMode::Popup(PopupType::Alert(color, msg));
}
/// ### create_quit_popup
///
/// Create quit popup input mode (since must be shared between different input handlers)
@@ -69,18 +83,4 @@ impl FileTransferActivity {
InputField::Logs => InputField::Explorer,
}
}
/// ### set_progress
///
/// Calculate progress percentage based on current progress
pub(super) fn set_progress(&mut self, it: usize, sz: usize) {
let mut prog: f64 = ((it as f64) * 100.0) / (sz as f64);
// Check value
if prog > 100.0 {
prog = 100.0;
} else if prog < 0.0 {
prog = 0.0;
}
self.transfer.progress = prog;
}
}

View File

@@ -119,9 +119,10 @@ enum InputMode {
///
/// File explorer states
struct FileExplorer {
pub index: usize,
pub files: Vec<FsEntry>,
dirstack: VecDeque<PathBuf>,
pub wrkdir: PathBuf, // Current directory
pub index: usize, // Selected file
pub files: Vec<FsEntry>, // Files in directory
dirstack: VecDeque<PathBuf>, // Stack of visited directory (max 16)
}
impl FileExplorer {
@@ -130,6 +131,7 @@ impl FileExplorer {
/// Instantiates a new FileExplorer
pub fn new() -> FileExplorer {
FileExplorer {
wrkdir: PathBuf::from("/"),
index: 0,
files: Vec::new(),
dirstack: VecDeque::with_capacity(16),
@@ -209,9 +211,11 @@ impl LogRecord {
///
/// TransferStates contains the states related to the transfer process
struct TransferStates {
pub progress: f64, // Current read/write progress (percentage)
pub started: Instant, // Instant the transfer process started
pub aborted: bool, // Describes whether the transfer process has been aborted
pub progress: f64, // Current read/write progress (percentage)
pub started: Instant, // Instant the transfer process started
pub aborted: bool, // Describes whether the transfer process has been aborted
pub bytes_written: usize, // Bytes written during transfer
pub bytes_total: usize, // Total bytes to write
}
impl TransferStates {
@@ -223,6 +227,8 @@ impl TransferStates {
progress: 0.0,
started: Instant::now(),
aborted: false,
bytes_written: 0,
bytes_total: 0,
}
}
@@ -233,6 +239,36 @@ impl TransferStates {
self.progress = 0.0;
self.started = Instant::now();
self.aborted = false;
self.bytes_written = 0;
self.bytes_total = 0;
}
/// ### set_progress
///
/// Calculate progress percentage based on current progress
pub fn set_progress(&mut self, w: usize, sz: usize) {
self.bytes_written = w;
self.bytes_total = sz;
let mut prog: f64 = ((self.bytes_written as f64) * 100.0) / (self.bytes_total as f64);
// Check value
if prog > 100.0 {
prog = 100.0;
} else if prog < 0.0 {
prog = 0.0;
}
self.progress = prog;
}
/// ### byte_per_second
///
/// Calculate bytes per second
pub fn bytes_per_second(&self) -> u64 {
// bytes_written : elapsed_secs = x : 1
let elapsed_secs: u64 = self.started.elapsed().as_secs();
match elapsed_secs {
0 => 0, // NOTE: would divide by 0 :D
_ => self.bytes_written as u64 / elapsed_secs,
}
}
}
@@ -269,7 +305,7 @@ impl FileTransferActivity {
///
/// Instantiates a new FileTransferActivity
pub fn new(params: FileTransferParams) -> FileTransferActivity {
let protocol: FileTransferProtocol = params.protocol.clone();
let protocol: FileTransferProtocol = params.protocol;
FileTransferActivity {
disconnected: false,
quit: false,
@@ -310,11 +346,14 @@ impl Activity for FileTransferActivity {
// Set context
self.context = Some(context);
// Clear terminal
let _ = self.context.as_mut().unwrap().terminal.clear();
self.context.as_mut().unwrap().clear_screen();
// Put raw mode on enabled
let _ = enable_raw_mode();
// Set working directory
let pwd: PathBuf = self.context.as_ref().unwrap().local.pwd();
// Get files at current wd
self.local_scan(self.context.as_ref().unwrap().local.pwd().as_path());
self.local_scan(pwd.as_path());
self.local.wrkdir = pwd;
}
/// ### on_draw
@@ -322,8 +361,9 @@ impl Activity for FileTransferActivity {
/// `on_draw` is the function which draws the graphical interface.
/// This function must be called at each tick to refresh the interface
fn on_draw(&mut self) {
let mut redraw: bool = false; // Should ui actually be redrawned?
// Context must be something
// Should ui actually be redrawned?
let mut redraw: bool = false;
// Context must be something
if self.context.is_none() {
return;
}
@@ -364,7 +404,7 @@ impl Activity for FileTransferActivity {
// Clear terminal and return
match self.context.take() {
Some(mut ctx) => {
let _ = ctx.terminal.clear();
ctx.clear_screen();
Some(ctx)
}
None => None,

File diff suppressed because it is too large Load Diff

View File

@@ -32,8 +32,8 @@ use super::input::InputHandler;
use crate::host::Localhost;
// Includes
use crossterm::execute;
use crossterm::event::DisableMouseCapture;
use crossterm::execute;
use crossterm::terminal::{EnterAlternateScreen, LeaveAlternateScreen};
use std::io::{stdout, Stdout, Write};
use tui::backend::CrosstermBackend;
@@ -59,9 +59,29 @@ impl Context {
Context {
local,
input_hnd: InputHandler::new(),
terminal: Terminal::new(CrosstermBackend::new(stdout)).unwrap()
terminal: Terminal::new(CrosstermBackend::new(stdout)).unwrap(),
}
}
pub fn enter_alternate_screen(&mut self) {
let _ = execute!(
self.terminal.backend_mut(),
EnterAlternateScreen,
DisableMouseCapture
);
}
pub fn leave_alternate_screen(&mut self) {
let _ = execute!(
self.terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
);
}
pub fn clear_screen(&mut self) {
let _ = self.terminal.clear();
}
}
impl Drop for Context {

View File

@@ -45,6 +45,7 @@ impl InputHandler {
/// ### fetch_events
///
/// Check if new events have been received from handler
#[allow(dead_code)]
pub(crate) fn fetch_events(&self) -> Result<Vec<Event>, ()> {
let mut inbox: Vec<Event> = Vec::new();
loop {

172
src/utils/fmt.rs Normal file
View File

@@ -0,0 +1,172 @@
//! ## Fmt
//!
//! `fmt` is the module which provides utilities for formatting
/*
*
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
extern crate chrono;
extern crate textwrap;
use chrono::prelude::*;
use std::time::{Duration, SystemTime};
/// ### fmt_pex
///
/// Convert 3 bytes of permissions value into ls notation (e.g. rwx-wx--x)
pub fn fmt_pex(owner: u8, group: u8, others: u8) -> String {
let mut mode: String = String::with_capacity(9);
let read: u8 = (owner >> 2) & 0x1;
let write: u8 = (owner >> 1) & 0x1;
let exec: u8 = owner & 0x1;
mode.push_str(match read {
1 => "r",
_ => "-",
});
mode.push_str(match write {
1 => "w",
_ => "-",
});
mode.push_str(match exec {
1 => "x",
_ => "-",
});
let read: u8 = (group >> 2) & 0x1;
let write: u8 = (group >> 1) & 0x1;
let exec: u8 = group & 0x1;
mode.push_str(match read {
1 => "r",
_ => "-",
});
mode.push_str(match write {
1 => "w",
_ => "-",
});
mode.push_str(match exec {
1 => "x",
_ => "-",
});
let read: u8 = (others >> 2) & 0x1;
let write: u8 = (others >> 1) & 0x1;
let exec: u8 = others & 0x1;
mode.push_str(match read {
1 => "r",
_ => "-",
});
mode.push_str(match write {
1 => "w",
_ => "-",
});
mode.push_str(match exec {
1 => "x",
_ => "-",
});
mode
}
/// ### instant_to_str
///
/// Format a `Instant` into a time string
pub fn fmt_time(time: SystemTime, fmt: &str) -> String {
let datetime: DateTime<Local> = time.into();
format!("{}", datetime.format(fmt))
}
/// ### fmt_millis
///
/// Format duration as {secs}.{millis}
pub fn fmt_millis(duration: Duration) -> String {
let seconds: u128 = duration.as_millis() / 1000;
let millis: u128 = duration.as_millis() % 1000;
format!("{}.{:0width$}", seconds, millis, width = 3)
}
/// align_text_center
///
/// Align text to center for a given width
pub fn align_text_center(text: &str, width: u16) -> String {
let indent_size: usize = match (width as usize) >= text.len() {
// NOTE: The check prevents underflow
true => (width as usize - text.len()) / 2,
false => 0,
};
textwrap::indent(
text,
(0..indent_size).map(|_| " ").collect::<String>().as_str(),
)
.trim_end()
.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_utils_fmt_pex() {
assert_eq!(fmt_pex(7, 7, 7), String::from("rwxrwxrwx"));
assert_eq!(fmt_pex(7, 5, 5), String::from("rwxr-xr-x"));
assert_eq!(fmt_pex(6, 6, 6), String::from("rw-rw-rw-"));
assert_eq!(fmt_pex(6, 4, 4), String::from("rw-r--r--"));
assert_eq!(fmt_pex(6, 0, 0), String::from("rw-------"));
assert_eq!(fmt_pex(0, 0, 0), String::from("---------"));
assert_eq!(fmt_pex(4, 4, 4), String::from("r--r--r--"));
assert_eq!(fmt_pex(1, 2, 1), String::from("--x-w---x"));
}
#[test]
fn test_utils_fmt_time() {
let system_time: SystemTime = SystemTime::from(SystemTime::UNIX_EPOCH);
assert_eq!(
fmt_time(system_time, "%Y-%m-%d"),
String::from("1970-01-01")
);
}
#[test]
fn test_utils_align_text_center() {
assert_eq!(
align_text_center("hello world!", 24),
String::from(" hello world!")
);
// Bad case
assert_eq!(
align_text_center("hello world!", 8),
String::from("hello world!")
);
}
#[test]
fn test_utils_fmt_millis() {
assert_eq!(
fmt_millis(Duration::from_millis(2048)),
String::from("2.048")
);
assert_eq!(
fmt_millis(Duration::from_millis(8192)),
String::from("8.192")
);
assert_eq!(
fmt_millis(Duration::from_millis(18192)),
String::from("18.192")
);
}
}

75
src/utils/hash.rs Normal file
View File

@@ -0,0 +1,75 @@
//! ## Hash
//!
//! `hash` is the module which provides utilities for calculating digests
/*
*
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
extern crate data_encoding;
extern crate ring;
use data_encoding::HEXLOWER;
use ring::digest::{Context, Digest, SHA256};
use std::fs::File;
use std::io::Read;
use std::path::Path;
/// ### hash_sha256_file
///
/// Get SHA256 of provided path
pub fn hash_sha256_file(file: &Path) -> Result<String, std::io::Error> {
// Open file
let mut reader: File = File::open(file)?;
let mut context = Context::new(&SHA256);
let mut buffer = [0; 8192];
loop {
let count = reader.read(&mut buffer)?;
if count == 0 {
break;
}
context.update(&buffer[..count]);
}
// Finish context
let digest: Digest = context.finish();
Ok(HEXLOWER.encode(digest.as_ref()))
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
#[test]
fn test_utils_hash_sha256() {
let tmp: tempfile::NamedTempFile = tempfile::NamedTempFile::new().unwrap();
// Write
let mut fhnd: File = File::create(tmp.path()).unwrap();
assert!(fhnd.write_all(b"Hello world!\n").is_ok());
assert_eq!(
*hash_sha256_file(tmp.path()).ok().as_ref().unwrap(),
String::from("0ba904eae8773b70c75333db4de2f3ac45a8ad4ddba1b242f0b3cfc199391dd8")
);
// Bad file
assert!(hash_sha256_file(Path::new("/tmp/oiojjt5ig/aiehgoiwg")).is_err());
}
}

29
src/utils/mod.rs Normal file
View File

@@ -0,0 +1,29 @@
//! ## Utils
//!
//! `utils` is the module which provides utilities of different kind
/*
*
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
// modules
pub mod fmt;
pub mod hash;
pub mod parser;

View File

@@ -1,6 +1,6 @@
//! ## Utils
//! ## Parser
//!
//! `utils` is the module which provides utilities of different kind
//! `parser` is the module which provides utilities for parsing different kind of stuff
/*
*
@@ -25,7 +25,6 @@
// Dependencies
extern crate chrono;
extern crate textwrap;
extern crate whoami;
use crate::filetransfer::FileTransferProtocol;
@@ -99,8 +98,11 @@ pub fn parse_remote_opt(
}
_ => return Err(String::from("Bad syntax")), // Too many tokens...
}
// Set username to default if sftp
if protocol == FileTransferProtocol::Sftp {
// Set username to default if sftp or scp
if matches!(
protocol,
FileTransferProtocol::Sftp | FileTransferProtocol::Scp
) {
// Set username to current username
username = Some(whoami::username());
}
@@ -142,78 +144,13 @@ pub fn parse_remote_opt(
Ok((address, port, protocol, username))
}
/// ### fmt_pex
///
/// Convert 3 bytes of permissions value into ls notation (e.g. rwx-wx--x)
pub fn fmt_pex(owner: u8, group: u8, others: u8) -> String {
let mut mode: String = String::with_capacity(9);
let read: u8 = (owner >> 2) & 0x1;
let write: u8 = (owner >> 1) & 0x1;
let exec: u8 = owner & 0x1;
mode.push_str(match read {
1 => "r",
_ => "-",
});
mode.push_str(match write {
1 => "w",
_ => "-",
});
mode.push_str(match exec {
1 => "x",
_ => "-",
});
let read: u8 = (group >> 2) & 0x1;
let write: u8 = (group >> 1) & 0x1;
let exec: u8 = group & 0x1;
mode.push_str(match read {
1 => "r",
_ => "-",
});
mode.push_str(match write {
1 => "w",
_ => "-",
});
mode.push_str(match exec {
1 => "x",
_ => "-",
});
let read: u8 = (others >> 2) & 0x1;
let write: u8 = (others >> 1) & 0x1;
let exec: u8 = others & 0x1;
mode.push_str(match read {
1 => "r",
_ => "-",
});
mode.push_str(match write {
1 => "w",
_ => "-",
});
mode.push_str(match exec {
1 => "x",
_ => "-",
});
mode
}
/// ### instant_to_str
///
/// Format a `Instant` into a time string
pub fn time_to_str(time: SystemTime, fmt: &str) -> String {
let datetime: DateTime<Local> = time.into();
format!("{}", datetime.format(fmt))
}
/// ### lstime_to_systime
/// ### parse_lstime
///
/// Convert ls syntax time to System Time
/// ls time has two possible syntax:
/// 1. if year is current: %b %d %H:%M (e.g. Nov 5 13:46)
/// 2. else: %b %d %Y (e.g. Nov 5 2019)
pub fn lstime_to_systime(
tm: &str,
fmt_year: &str,
fmt_hours: &str,
) -> Result<SystemTime, ParseError> {
pub fn parse_lstime(tm: &str, fmt_year: &str, fmt_hours: &str) -> Result<SystemTime, ParseError> {
let datetime: NaiveDateTime = match NaiveDate::parse_from_str(tm, fmt_year) {
Ok(date) => {
// Case 2.
@@ -242,23 +179,6 @@ pub fn lstime_to_systime(
.unwrap_or(SystemTime::UNIX_EPOCH))
}
/// align_text_center
///
/// Align text to center for a given width
pub fn align_text_center(text: &str, width: u16) -> String {
let indent_size: usize = match (width as usize) >= text.len() {
// NOTE: The check prevents underflow
true => (width as usize - text.len()) / 2,
false => 0,
};
textwrap::indent(
text,
(0..indent_size).map(|_| " ").collect::<String>().as_str(),
)
.trim_end()
.to_string()
}
#[cfg(test)]
mod tests {
@@ -312,6 +232,14 @@ mod tests {
assert_eq!(result.2, FileTransferProtocol::Ftp(false));
assert!(result.3.is_none()); // Doesn't fall back
// Protocol
let result: (String, u16, FileTransferProtocol, Option<String>) =
parse_remote_opt(&String::from("sftp://172.26.104.1"))
.ok()
.unwrap();
assert_eq!(result.0, String::from("172.26.104.1"));
assert_eq!(result.1, 22); // Fallback to sftp default
assert_eq!(result.2, FileTransferProtocol::Sftp);
assert!(result.3.is_some()); // Doesn't fall back
let result: (String, u16, FileTransferProtocol, Option<String>) =
parse_remote_opt(&String::from("scp://172.26.104.1"))
.ok()
@@ -319,7 +247,7 @@ mod tests {
assert_eq!(result.0, String::from("172.26.104.1"));
assert_eq!(result.1, 22); // Fallback to scp default
assert_eq!(result.2, FileTransferProtocol::Scp);
assert!(result.3.is_none()); // Doesn't fall back
assert!(result.3.is_some()); // Doesn't fall back
// Protocol + user
let result: (String, u16, FileTransferProtocol, Option<String>) =
parse_remote_opt(&String::from("ftps://anon@172.26.104.1"))
@@ -346,31 +274,10 @@ mod tests {
}
#[test]
fn test_utils_fmt_pex() {
assert_eq!(fmt_pex(7, 7, 7), String::from("rwxrwxrwx"));
assert_eq!(fmt_pex(7, 5, 5), String::from("rwxr-xr-x"));
assert_eq!(fmt_pex(6, 6, 6), String::from("rw-rw-rw-"));
assert_eq!(fmt_pex(6, 4, 4), String::from("rw-r--r--"));
assert_eq!(fmt_pex(6, 0, 0), String::from("rw-------"));
assert_eq!(fmt_pex(0, 0, 0), String::from("---------"));
assert_eq!(fmt_pex(4, 4, 4), String::from("r--r--r--"));
assert_eq!(fmt_pex(1, 2, 1), String::from("--x-w---x"));
}
#[test]
fn test_utils_time_to_str() {
let system_time: SystemTime = SystemTime::from(SystemTime::UNIX_EPOCH);
assert_eq!(
time_to_str(system_time, "%Y-%m-%d"),
String::from("1970-01-01")
);
}
#[test]
fn test_utils_lstime_to_systime() {
fn test_utils_parse_lstime() {
// Good cases
assert_eq!(
lstime_to_systime("Nov 5 16:32", "%b %d %Y", "%b %d %H:%M")
parse_lstime("Nov 5 16:32", "%b %d %Y", "%b %d %H:%M")
.ok()
.unwrap()
.duration_since(SystemTime::UNIX_EPOCH)
@@ -379,7 +286,7 @@ mod tests {
Duration::from_secs(1604593920)
);
assert_eq!(
lstime_to_systime("Dec 2 21:32", "%b %d %Y", "%b %d %H:%M")
parse_lstime("Dec 2 21:32", "%b %d %Y", "%b %d %H:%M")
.ok()
.unwrap()
.duration_since(SystemTime::UNIX_EPOCH)
@@ -388,7 +295,7 @@ mod tests {
Duration::from_secs(1606944720)
);
assert_eq!(
lstime_to_systime("Nov 5 2018", "%b %d %Y", "%b %d %H:%M")
parse_lstime("Nov 5 2018", "%b %d %Y", "%b %d %H:%M")
.ok()
.unwrap()
.duration_since(SystemTime::UNIX_EPOCH)
@@ -397,7 +304,7 @@ mod tests {
Duration::from_secs(1541376000)
);
assert_eq!(
lstime_to_systime("Mar 18 2018", "%b %d %Y", "%b %d %H:%M")
parse_lstime("Mar 18 2018", "%b %d %Y", "%b %d %H:%M")
.ok()
.unwrap()
.duration_since(SystemTime::UNIX_EPOCH)
@@ -406,16 +313,8 @@ mod tests {
Duration::from_secs(1521331200)
);
// bad cases
assert!(lstime_to_systime("Oma 31 2018", "%b %d %Y", "%b %d %H:%M").is_err());
assert!(lstime_to_systime("Feb 31 2018", "%b %d %Y", "%b %d %H:%M").is_err());
assert!(lstime_to_systime("Feb 15 25:32", "%b %d %Y", "%b %d %H:%M").is_err());
}
#[test]
fn test_utils_align_text_center() {
assert_eq!(
align_text_center("hello world!", 24),
String::from(" hello world!")
);
assert!(parse_lstime("Oma 31 2018", "%b %d %Y", "%b %d %H:%M").is_err());
assert!(parse_lstime("Feb 31 2018", "%b %d %Y", "%b %d %H:%M").is_err());
assert!(parse_lstime("Feb 15 25:32", "%b %d %Y", "%b %d %H:%M").is_err());
}
}