This commit is contained in:
2025-01-05 16:18:37 +08:00
parent 9bb6854125
commit f62fea3d2b
11 changed files with 400 additions and 42 deletions

View File

@@ -3,11 +3,16 @@ use std::io;
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
use ratatui::{DefaultTerminal, Frame};
use crate::view::{home::Home, View};
use crate::{
session::{connection::ConnectionManager, credential::CredentialManager},
view::{home::Home, View},
};
pub struct App {
running: bool,
view: Home,
connection_manager: ConnectionManager,
credential_manager: CredentialManager,
}
impl Default for App {
@@ -15,6 +20,8 @@ impl Default for App {
App {
running: true,
view: Home::default(),
connection_manager: ConnectionManager::default(),
credential_manager: CredentialManager::default(),
}
}
}

View File

@@ -1 +1,10 @@
use std::path::PathBuf;
use lazy_static::lazy_static;
lazy_static! {
pub static ref CONFIG_DIR: PathBuf = dirs::config_local_dir().unwrap().join("tethers");
pub static ref CACHE_DIR: PathBuf = dirs::cache_dir().unwrap().join("tethers");
pub static ref SSH_CONFIG_DIR: PathBuf = dirs::home_dir().unwrap().join(".ssh");
pub static ref SSH_CONFIG_PATH: PathBuf = dirs::home_dir().unwrap().join(".ssh").join("config");
}

View File

@@ -1,24 +1,69 @@
use std::io;
use std::{io, path::Path};
use app::App;
use session::connection::{Connection, ShConnection};
use base64::{engine::general_purpose::STANDARD, Engine as _};
use session::{
connection::{Connection, ConnectionManager, Connections, ShConnection, SshConnection},
credential::{Credential, CredentialManager, CredentialType},
SessionManager,
};
use util::file;
mod app;
mod session;
mod view;
mod global;
mod session;
mod util;
// fn main() -> io::Result<()> {
// let mut terminal = ratatui::init();
// let app_result = App::default().run(&mut terminal);
// ratatui::restore();
// app_result
// }
//
mod view;
fn main() -> io::Result<()> {
let sh = ShConnection::new("zsh".to_string(), "/usr/bin/nu".to_string(), None);
let _ = sh.exec_cmd();
Ok(())
let mut terminal = ratatui::init();
let app_result = App::default().run(&mut terminal);
ratatui::restore();
app_result
}
// fn main() -> io::Result<()> {
// // let sh = ShConnection::new("zsh".to_string(), "/usr/bin/nu".to_string(), None);
// // let _ = sh.exec_cmd();
// let ssh = SshConnection::new("".to_string(), "myhost.fallen-angle.com".to_string(), 7044, "rainbus".to_string(), "".to_string());
// ssh.exec_cmd();
// Ok(())
// }
/* fn main() -> io::Result<()> {
let mut cnm = ConnectionManager::default();
let sh = Connections::Sh(ShConnection::new(
"zsh".to_string(),
"/usr/bin/zsh".to_string(),
None,
));
let ssh_sec = Connections::Ssh(SshConnection::new(
"Tencent".to_string(),
"myhost.fallen-angle.com".to_string(),
22,
"rainbus".to_string(),
"Server".to_string(),
));
let ssh_pwd = Connections::Ssh(SshConnection::new(
"Aliyun".to_string(),
"myweb.fallen-angle.com".to_string(),
22,
"rainbus".to_string(),
"AliPwd".to_string(),
));
cnm.connections().extend([sh, ssh_sec, ssh_pwd]);
let mut crm = CredentialManager::default();
let ser_crd = Credential::new(
"Server".to_string(),
CredentialType::Secret(STANDARD.encode(file::read_file(Path::new("")))),
);
let pwd_crd = Credential::new(
"Alipwd".to_string(),
CredentialType::Password("20001001".to_string()),
);
crm.credentials().extend([ser_crd, pwd_crd]);
let sem = SessionManager::new(cnm, crm);
// println!("{}", toml::to_string_pretty(&sem).unwrap());
file::write_file(&global::CONFIG_DIR.join("tethers.toml"), toml::to_string_pretty(&sem).unwrap().as_str());
Ok(())
} */

View File

@@ -1,2 +1,21 @@
use connection::ConnectionManager;
use credential::CredentialManager;
use serde::{Deserialize, Serialize};
pub mod connection;
mod credential;
pub mod credential;
#[derive(Serialize, Deserialize)]
pub struct SessionManager {
connection_manager: ConnectionManager,
credential_manager: CredentialManager,
}
impl SessionManager {
pub fn new(cnm: ConnectionManager, crm: CredentialManager) -> Self {
Self {
connection_manager: cnm,
credential_manager: crm,
}
}
}

View File

@@ -1,4 +1,8 @@
use std::{env, io, path::Path, process::Command};
use std::{env, fs, io, path::Path, process::Command};
use serde::{Deserialize, Serialize};
use crate::util::file;
use super::credential::{Credential, CredentialType};
@@ -6,6 +10,7 @@ pub trait Connection {
fn exec_cmd(&self) -> io::Result<()>;
}
#[derive(Serialize, Deserialize)]
pub struct ShConnection {
name: String,
path: String,
@@ -38,6 +43,7 @@ impl ShConnection {
}
}
#[derive(Serialize, Deserialize)]
pub struct SshConnection {
name: String,
host: String,
@@ -66,10 +72,13 @@ impl SshConnection {
impl Connection for SshConnection {
fn exec_cmd(&self) -> io::Result<()> {
let credential =
Credential::new("zsh".to_string(), CredentialType::Password("".to_string()));
let credential = Credential::new(
"zsh".to_string(),
CredentialType::Secret(file::read_file(Path::new(""))),
);
let mut ssh_command = Command::new("ssh");
let secret_path = env::temp_dir().join("tethers").join("secret.tmp");
let _ = fs::create_dir_all(secret_path.parent().unwrap());
ssh_command
.args(["-p", &self.port.to_string()])
.args(["-l", &self.user]);
@@ -89,11 +98,19 @@ impl Connection for SshConnection {
}
}
#[derive(Serialize, Deserialize)]
pub enum Connections {
Sh(ShConnection),
Ssh(SshConnection),
}
#[derive(Default, Serialize, Deserialize)]
pub struct ConnectionManager {
connections: Vec<Connections>,
}
impl ConnectionManager {
pub fn connections(&mut self) -> &mut Vec<Connections> {
&mut self.connections
}
}

View File

@@ -5,11 +5,18 @@ use std::{
path::Path,
};
use base64::{engine::general_purpose::STANDARD, Engine};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub enum CredentialType {
Password(String),
Secret(String),
}
impl CredentialType {}
#[derive(Serialize, Deserialize)]
pub struct Credential {
credential: CredentialType,
name: String,
@@ -35,6 +42,13 @@ impl Credential {
impl Credential {
pub fn new(name: String, credential: CredentialType) -> Self {
// TODO: modify this to Type
let credential = match credential {
CredentialType::Password(password) => {
CredentialType::Password(STANDARD.encode(password))
}
CredentialType::Secret(secret) => CredentialType::Secret(STANDARD.encode(secret)),
};
Self { name, credential }
}
@@ -43,12 +57,16 @@ impl Credential {
}
}
#[derive(Default)]
#[derive(Default, Serialize, Deserialize)]
pub struct CredentialManager {
credentials: Vec<Credential>,
}
impl CredentialManager {
pub fn credentials(&mut self) -> &mut Vec<Credential> {
&mut self.credentials
}
pub fn find_by_name(&self, name: &str) -> io::Result<&Credential> {
self.credentials
.iter()

View File

@@ -1 +1 @@
pub mod file;

View File

@@ -1,2 +1,18 @@
use std::{
fs::File,
io::{Read, Write},
path::{self, Path},
};
pub fn read_file(path: &Path) -> String {
let path = dirs::home_dir().unwrap().join(".ssh/Server");
let mut file = File::open(path).unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
content
}
pub fn write_file(path: &Path, content: &str) {
let mut file = File::create(path).unwrap();
let res = file.write_all(content.as_bytes());
}

View File

@@ -37,8 +37,8 @@ impl Default for Home {
help: HelpComponent::default(),
setting: SettingView::default(),
};
// home.table.active_state().set_active(true);
home.setting.active_state().set_active(true);
home.table.active_state().set_active(true);
// home.setting.active_state().set_active(true);
home
}
}