From 4292b413141de8f5ae13317dea3a9c329274e684 Mon Sep 17 00:00:00 2001 From: RafayAhmad7548 Date: Tue, 8 Jul 2025 09:40:46 +0500 Subject: [PATCH] added config, make config be used in general next step make config be used in database as well --- Cargo.lock | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/config.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 41 ++++++++++++++++++++++++++++++---- 4 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 src/config.rs diff --git a/Cargo.lock b/Cargo.lock index 665e621..e07aea6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -672,8 +672,10 @@ dependencies = [ "regex", "russh", "russh-sftp", + "serde", "sqlx", "tokio", + "toml", ] [[package]] @@ -1864,6 +1866,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -2356,6 +2367,47 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "toml_write", + "winnow", +] + +[[package]] +name = "toml_write" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" + [[package]] name = "tracing" version = "0.1.41" @@ -2856,6 +2908,15 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "winnow" +version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74c7b26e3480b707944fc872477815d29a8e429d2f93a1ce000f5fa84a15cbcd" +dependencies = [ + "memchr", +] + [[package]] name = "writeable" version = "0.6.1" diff --git a/Cargo.toml b/Cargo.toml index b1af28b..901eeb0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,5 @@ russh = "0.52.1" russh-sftp = "2.1.1" tokio = { version = "1.45.1", features = ["full"] } sqlx = { version = "0.8.6", features = [ "runtime-tokio", "postgres", "sqlite", "mysql" ] } +toml = "0.8.23" +serde = "1.0.219" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..44f79b0 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,53 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub(crate) struct Config { + pub(crate) general: GeneralConfig, + pub(crate) database: DBConfig +} + +#[derive(Serialize, Deserialize)] +pub(crate) struct GeneralConfig { + pub(crate) listen_address: String, + pub(crate) port: u16, +} + +#[derive(Serialize, Deserialize)] +#[serde(tag = "driver")] +pub(crate) enum DBConfig { + #[serde(rename = "sqlite")] + Sqlite { + path: String + }, + #[serde(rename = "postgres")] + Postgres { + host: String, + port: u16, + user: String, + password: String, + dbname: String + }, + #[serde(rename = "mysql")] + Mysql { + host: String, + port: u16, + user: String, + password: String, + dbname: String + } +} + + +impl Default for Config { + fn default() -> Self { + Config { + general: GeneralConfig { + listen_address: String::from("0.0.0.0"), + port: 2222 + }, + database: DBConfig::Sqlite { + path: String::from("/var/lib/flux-sftp/auth.db") + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 1344671..774c0a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,12 @@ mod sftp; -use std::{net::SocketAddr, sync::Arc, time::Duration}; +mod config; + +use std::{io::ErrorKind, net::SocketAddr, sync::Arc, time::Duration}; +use config::Config; use russh::{keys::ssh_key::{rand_core::OsRng, PublicKey}, server::{Auth, Handler as SshHandler, Msg, Server, Session}, Channel, ChannelId}; use sftp::SftpSession; use sqlx::{sqlite::SqlitePoolOptions, Pool, Row, Sqlite}; - +use tokio::fs; struct SftpServer { pool: Arc> @@ -101,15 +104,45 @@ impl SshHandler for SshSession { } + #[tokio::main] async fn main() -> Result<(), sqlx::Error> { + + const CONFIG_PATH: &str = "/etc/flux-sftp/config.toml"; + let config: Config; + match fs::read_to_string(CONFIG_PATH).await { + Ok(toml) => { + match toml::from_str::(&toml) { + Ok(c) => config = c, + Err(e) => { + println!("error parsing config file: {}\n please make sure config file is valid", e); + return Ok(()) + } + } + + } + Err(e) => { + match e.kind() { + ErrorKind::NotFound => println!("config file not found, please ensure config file is present at: {}", CONFIG_PATH), + _ => println!("error occured reading config file: {}", e) + } + return Ok(()) + } + } + + // let url = match &config.database { + // DBConfig::Sqlite { path } => format!("sqlite:{}", path), + // DBConfig::Postgres { host, port, user, password, dbname } => format!("postgres://{}:{}@{}:{}/{}", user, password, host, port, dbname), + // DBConfig::Mysql { host, port, user, password, dbname } => format!("mysql://{}:{}@{}:{}/{}", user, password, host, port, dbname), + // }; + let pool = SqlitePoolOptions::new() .max_connections(3) .connect("sqlite:/home/rafayahmad/Stuff/Coding/Rust/flux-sftp/auth.db").await?; let mut server = SftpServer { pool: Arc::new(pool) }; - let config = russh::server::Config { + let russh_config = russh::server::Config { auth_rejection_time: Duration::from_secs(3), auth_rejection_time_initial: Some(Duration::from_secs(0)), keys: vec![ @@ -118,6 +151,6 @@ async fn main() -> Result<(), sqlx::Error> { ..Default::default() }; - server.run_on_address(Arc::new(config), ("0.0.0.0", 2222)).await.unwrap(); + server.run_on_address(Arc::new(russh_config), (config.general.listen_address, config.general.port)).await.unwrap(); Ok(()) }