add cmd serve

This commit is contained in:
Dmitry Belyaev 2022-09-29 10:58:42 +03:00
parent b10eca9688
commit 065af6ed2d
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
3 changed files with 49 additions and 1 deletions

23
Cargo.lock generated
View File

@ -151,6 +151,16 @@ dependencies = [
"once_cell",
]
[[package]]
name = "ctrlc"
version = "3.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d91974fbbe88ec1df0c24a4f00f99583667a7e2e6272b2b92d294d81e462173"
dependencies = [
"nix",
"winapi",
]
[[package]]
name = "err-derive"
version = "0.3.1"
@ -304,6 +314,18 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "nix"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e322c04a9e3440c327fca7b6c8a63e6890a32fa2ad689db972425f07e0d22abb"
dependencies = [
"autocfg",
"bitflags",
"cfg-if",
"libc",
]
[[package]]
name = "num_cpus"
version = "1.13.1"
@ -495,6 +517,7 @@ version = "0.1.0"
dependencies = [
"async-stream",
"clap",
"ctrlc",
"fast-socks5",
"flexi_logger",
"log",

View File

@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
async-stream = "0.3.3"
clap = { version = "3.2.22", features = ["derive"] }
ctrlc = "3.2.3"
fast-socks5 = "0.8.1"
flexi_logger = { version = "0.23.3", features = ["specfile_without_notification", "async"] }
log = "0.4.17"

View File

@ -4,11 +4,13 @@ mod config;
mod server;
mod service;
use config::Config;
use flexi_logger::{
AdaptiveFormat, Age, Cleanup, Criterion, Duplicate, FileSpec, Logger, LoggerHandle, Naming,
};
use clap::{Parser, Subcommand};
use tokio_util::sync::CancellationToken;
#[derive(Subcommand, Debug)]
enum Command {
@ -24,6 +26,8 @@ enum Command {
Run,
/// save default config
SaveConfig,
/// run server as foreground proccess
Serve,
}
/// SOCKS5 proxy windows service
@ -64,6 +68,22 @@ fn create_logger() -> LoggerHandle {
.expect("can't start logger")
}
fn server_foreground() {
let control_token = CancellationToken::new();
let server_token = control_token.child_token();
let res = ctrlc::set_handler(move || {
log::info!("recieved Ctrl-C");
control_token.cancel();
});
if res.is_ok() {
log::info!("Press Ctrl-C to stop server");
}
server::server_executor(Config::get(), server_token).unwrap();
}
fn main() {
let args = Cli::parse();
let logger = create_logger();
@ -75,7 +95,11 @@ fn main() {
Command::Start => service::start(),
Command::Stop => service::stop(),
Command::SaveConfig => {
config::Config::default().save();
Config::default().save();
Ok(())
}
Command::Serve => {
server_foreground();
Ok(())
}
};