Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ socket2 = "0.5.9"
tokio-tungstenite = "0.26.2"
futures = "0.3.31"
ctrlc = "3.4"
rand_core = "=0.6.4"
anstyle = "1.0"
clap = "4.5"

# data
chrono = { version = "0.4", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
anyhow = "1.0.95"
etherparse = "0.18"
bincode = "2.0.1"
toml = "0.8.22"
thiserror = "2.0.12"

# crypto
x25519-dalek = "2.0.1"

# logging
tracing = "0.1"
Expand Down
8 changes: 4 additions & 4 deletions crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = { workspace = true }
autobins = false

[features]
default = ["cli"]
default = ["cli", "udp"]
udp = ["socket2"]
ws = ["tokio-tungstenite", "futures"]
cli = ["clap", "ctrlc", "anstyle"]
Expand All @@ -24,18 +24,18 @@ tokio = { workspace = true }
tun-rs = { workspace = true }
socket2 = { workspace = true, optional = true}
ctrlc = { workspace = true, optional = true}
clap = { version = "4.5.23", features = ["derive"], optional = true}
clap = { workspace = true, features = ["derive", "wrap_help"], optional = true}
anstyle = { version = "1.0", optional = true }
tokio-tungstenite = { workspace = true, optional = true }
futures = { workspace = true, optional = true }
ipnetwork = "0.21.1"
console-subscriber = "0.4.1"
# console-subscriber = "0.4.1"

# Crypto
snow = "0.9"

# Data
thiserror = "2.0"
thiserror = { workspace = true }
anyhow = { workspace = true }
bincode = { workspace = true }

Expand Down
44 changes: 32 additions & 12 deletions crates/client/src/bin/command/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ use client::runtime::Runtime;
use client::runtime::state::RuntimeState;
use shared::connection_config::{ConnectionConfig, InterfaceConfig, RuntimeConfig};
use shared::network::find_available_ifname;
use shared::success_err;

#[derive(Debug, Args)]
#[group(required = true, multiple = false)]
pub struct ConnectCmd {
/// common connection (config or key)
#[arg(value_name = "CONNECTION")]
connection: Option<String>,
/// config file
#[arg(short, long, value_name = "FILE PATH")]
config: Option<PathBuf>,
Expand All @@ -29,23 +33,39 @@ pub struct ConnectCmd {
impl ConnectCmd {
pub async fn exec(self) {

let mut config = match self.config {
Some(ref path) => match ConnectionConfig::load(path) {
let mut config = match self.connection {
Some(ref connection) => match ConnectionConfig::from_base64(connection) {
Ok(config) => config,
Err(err) => {
eprintln!("failed to load config: {}", err);
process::exit(1);
Err(parse_key_err) => match ConnectionConfig::load(&PathBuf::from(connection)) {
Ok(config) => config,
Err(parse_config_err) => {
success_err!(
"parse connection\n\n\t if this key: {}\n\n\t if this config path: {}\n",
parse_key_err,
parse_config_err
);
process::exit(1);
}
}
},
None => match self.key {
Some(key) => match ConnectionConfig::from_base64(&key) {
Ok(config) => config,
Err(err) => {
eprintln!("failed to parse config key: {}", err);
success_err!("parse config key: {}", err);
process::exit(1);
}
},
None => unreachable!("config or key is required should protected by clap")
None => match self.config {
Some(ref path) => match ConnectionConfig::load(path) {
Ok(config) => config,
Err(err) => {
success_err!("load config: {}", err);
process::exit(1);
}
},
None => unreachable!("config or key is required should protected by clap")
}
}
};

Expand All @@ -62,15 +82,15 @@ impl ConnectCmd {

if let Some(path) = self.config {
if let Err(err) = config.save(&path) {
eprintln!("failed to save config: {}", err);
success_err!("save config: {}", err);
process::exit(1);
}
}

let sock_addr = match config.general.host.parse() {
Ok(addr) => SocketAddr::new(addr, config.general.port),
Err(err) => {
eprintln!("failed to resolve host: {}", err);
success_err!("resolve host: {}", err);
process::exit(1);
}
};
Expand All @@ -83,7 +103,7 @@ impl ConnectCmd {
).await {
Ok(tun) => Arc::new(tun),
Err(err) => {
eprintln!("failed to setup tun: {}", err);
success_err!("setup tun: {}", err);
process::exit(1);
}
};
Expand All @@ -92,7 +112,7 @@ impl ConnectCmd {
{
Ok(routes) => Arc::new(routes),
Err(err) => {
eprintln!("failed to setup routes: {}", err);
success_err!("setup routes: {}", err);
process::exit(1);
}
};
Expand Down Expand Up @@ -135,7 +155,7 @@ impl ConnectCmd {
}
_ => {
routes_clone.restore();
error!("{}", error);
success_err!("{}", error);
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion crates/client/src/bin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ mod style;

use crate::command::Commands;
use clap::Parser;
use shared::success_err;

const LOG_DIR: &str = "logs";
const LOG_PREFIX: &str = "client.log";

#[tokio::main(flavor = "multi_thread")]
async fn main() {
let opt = opt::Opt::parse();
opt.init_logging();
let _guard = match opt.init_logging() {
Ok(guard) => guard,
Err(err) => {
success_err!("{}\n", err);
std::process::exit(1);
}
};
// console_subscriber::init();

match opt.cmd {
Expand Down
36 changes: 19 additions & 17 deletions crates/client/src/bin/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::command::Commands;
use crate::{LOG_DIR, LOG_PREFIX};
use clap::Parser;
use std::io::IsTerminal;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, Layer};
use tracing_appender::non_blocking::WorkerGuard;
use tracing_appender::rolling::{RollingFileAppender, Rotation};
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer};

#[derive(Debug, Parser)]
#[clap(about = "The Holynet vpn client command-line interface.", version, arg_required_else_help = true, styles=styles())]
Expand All @@ -19,29 +20,30 @@ pub struct Opt {
}

impl Opt {
pub fn init_logging(&self) {
let log_level = LevelFilter::from_level(if self.debug {
tracing::Level::DEBUG
} else {
tracing::Level::INFO
});

let file_appender = tracing_appender::rolling::daily(LOG_DIR, LOG_PREFIX);
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
pub fn init_logging(&self) -> anyhow::Result<WorkerGuard> {
let appender = RollingFileAppender::builder()
.rotation(Rotation::DAILY)
.filename_prefix(LOG_PREFIX)
.build(LOG_DIR)?;

let (non_blocking, guard) = tracing_appender::non_blocking(appender);

let filter = if self.debug { "server=debug" } else { "server=info" };

let file_layer = fmt::layer()
.with_writer(non_blocking)
.with_ansi(false)
.with_filter(log_level);
.with_filter(EnvFilter::new(filter));

let console_layer = fmt::layer()
.with_ansi(std::io::stdout().is_terminal())
.with_filter(log_level);
.with_filter(EnvFilter::new(filter));

tracing_subscriber::registry()
.with(file_layer)
.with(console_layer)
.init();


Ok(guard)
}
}
11 changes: 6 additions & 5 deletions crates/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ autobins = false
default = ["cli"]
udp = ["socket2"]
ws = ["tokio-tungstenite", "futures", "socket2"]
cli = ["clap", "inquire", "anstyle", "ctrlc", "fjall"]
cli = ["clap", "inquire", "anstyle", "ctrlc", "fjall", "qrcode"]

[[bin]]
name = "server"
Expand All @@ -17,21 +17,21 @@ required-features = ["cli"]

[dependencies]
shared = { workspace = true }
clap = { version = "4.5", features = ["cargo", "color", "derive", "env"], optional = true}
clap = { workspace = true, features = ["derive", "env", "wrap_help"], optional = true}
inquire = { version = "0.7", optional = true }
anstyle = { version = "1.0", optional = true }
anstyle = { workspace = true, optional = true }
ctrlc = { version = "3.4", optional = true }

# data
chrono = { workspace = true }
etherparse = { workspace = true }
serde = { workspace = true }
anyhow = { workspace = true }
thiserror = "2.0.12"
thiserror = { workspace = true }
dashmap = "7.0.0-rc2"
bincode = { workspace = true }
derive_more = { version = "2.0.1", features = ["display"] }
toml = "0.8.19"
toml = { workspace = true }

# logging
tracing-subscriber = { workspace = true }
Expand All @@ -48,6 +48,7 @@ tokio = { workspace = true }
socket2 = { workspace = true, optional = true }
async-trait = "0.1"
rand = "0.9.1"
qrcode = { version = "0.14.1", default-features = false, features = ["pic"], optional = true }
tokio-tungstenite = { workspace = true, optional = true }
futures = { workspace = true, optional = true }

Expand Down
19 changes: 10 additions & 9 deletions crates/server/src/bin/command/start.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::process;
use std::{process, thread};
use std::time::Duration;
use clap::Parser;
use tracing::{debug, error, info, warn};
use tracing::{debug, error, info};
use server::config::Config;
use server::runtime::error::RuntimeError;
use server::runtime::Runtime;
use crate::storage::{database, Clients};
use shared::{success_err, success_warn};

#[derive(Debug, Parser)]
pub struct StartCmd {
Expand Down Expand Up @@ -34,27 +36,27 @@ impl StartCmd {
}

if let Err(err) = config.save() {
warn!("cant update configuration: {}", err);
success_warn!("cant update configuration: {}", err);
}

let clients = match database(&config.general.storage) {
Ok(db) => match Clients::new(db) {
Ok(store) => store,
Err(err) => {
error!("failed to create client storage: {}", err);
success_err!("failed to create client storage: {}\n", err);
process::exit(1);
}
},
Err(err) => {
error!("load storage: {}", err);
success_err!("load storage: {}\n", err);
process::exit(1);
}
};

let mut runtime = match Runtime::from_config(config) {
Ok(runtime) => runtime,
Err(err) => {
error!("create runtime: {}", err);
success_err!("create runtime: {}\n", err);
process::exit(1);
}
};
Expand All @@ -75,8 +77,8 @@ impl StartCmd {
debug!("stop signal not sent from Ctrl-C handler: {}", err);
}
}
// thread::sleep(Duration::from_secs(1));
// process::exit(0);
thread::sleep(Duration::from_secs(1));
process::exit(0);
}).expect("error setting Ctrl-C handler");

if let Err(errors) = runtime.run().await {
Expand All @@ -91,6 +93,5 @@ impl StartCmd {
}
}
}

}
}
3 changes: 2 additions & 1 deletion crates/server/src/bin/command/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use server::config::Config;
use crate::command::users::add::AddCmd;
use crate::command::users::list::ListCmd;
use crate::command::users::remove::RemoveCmd;
use crate::success_err;

#[derive(Debug, Subcommand)]
pub enum UsersCmd {
Expand All @@ -25,7 +26,7 @@ impl UsersCmd {
UsersCmd::List(cmd) => cmd.exec(config).await,
UsersCmd::Remove(cmd) => cmd.exec(config).await
} {
eprintln!("{}", error);
success_err!("{}\n", error);
std::process::exit(1);
}
}
Expand Down
Loading