ssservice genkey, helper for generate encryption key

This commit is contained in:
zonyitoo
2022-10-17 22:29:02 +08:00
parent 0d6bfa32d5
commit 45cc770ef4
5 changed files with 55 additions and 1 deletions

2
Cargo.lock generated
View File

@@ -2062,6 +2062,7 @@ dependencies = [
name = "shadowsocks-rust"
version = "1.15.0-alpha.9"
dependencies = [
"base64",
"build-time",
"byte_string",
"byteorder",
@@ -2080,6 +2081,7 @@ dependencies = [
"mimalloc",
"num_cpus",
"qrcode",
"rand",
"rpassword",
"rpmalloc",
"serde",

View File

@@ -148,6 +148,7 @@ log4rs = { version = "1.2", optional = true }
serde = { version = "1.0", features = ["derive"] }
json5 = "0.4"
thiserror = "1.0"
base64 = "0.13"
clap = { version = "4.0", features = ["wrap_help", "suggestions"] }
cfg-if = "1"
@@ -158,6 +159,7 @@ directories = "4.0"
xdg = "2.4"
rpassword = "7.0"
libc = { version = "0.2", features = ["extra_traits"] }
rand = "0.8"
futures = "0.3"
tokio = { version = "1", features = ["rt", "signal"] }

View File

@@ -10,7 +10,7 @@
use std::{env, path::Path, process::ExitCode};
use clap::Command;
use shadowsocks_rust::service::{local, manager, server};
use shadowsocks_rust::service::{genkey, local, manager, server};
fn main() -> ExitCode {
let app = Command::new("shadowsocks")
@@ -36,12 +36,17 @@ fn main() -> ExitCode {
.subcommand(
manager::define_command_line_options(Command::new("manager")).about("Shadowsocks Server Manager service"),
)
.subcommand(
genkey::define_command_line_options(Command::new("genkey"))
.about("Generate shadowsocks encryption key for method"),
)
.get_matches();
match matches.subcommand() {
Some(("local", matches)) => local::main(matches),
Some(("server", matches)) => server::main(matches),
Some(("manager", matches)) => manager::main(matches),
Some(("genkey", matches)) => genkey::main(matches),
_ => unreachable!("expecting a subcommand"),
}
}

44
src/service/genkey.rs Normal file
View File

@@ -0,0 +1,44 @@
//! Generate sufficient key for method
use std::process::ExitCode;
use clap::{builder::PossibleValuesParser, Arg, ArgAction, ArgMatches, Command};
use rand::RngCore;
use shadowsocks_service::shadowsocks::crypto::{available_ciphers, CipherKind};
/// Defines command line options
pub fn define_command_line_options(mut app: Command) -> Command {
app = app.arg(
Arg::new("ENCRYPT_METHOD")
.short('m')
.long("encrypt-method")
.num_args(1)
.action(ArgAction::Set)
.required(true)
.value_parser(PossibleValuesParser::new(available_ciphers()))
.help("Server's encryption method"),
);
app
}
/// Program entrance `main`
pub fn main(matches: &ArgMatches) -> ExitCode {
let method = matches
.get_one::<String>("ENCRYPT_METHOD")
.map(|x| x.parse::<CipherKind>().expect("method"))
.expect("`method` is required");
let key_len = method.key_len();
if key_len > 0 {
let mut key = vec![0u8; key_len];
let mut rng = rand::thread_rng();
rng.fill_bytes(&mut key);
let encoded_key = base64::encode(&key);
println!("{}", encoded_key);
}
ExitCode::SUCCESS
}

View File

@@ -1,5 +1,6 @@
//! Service launchers
pub mod genkey;
#[cfg(feature = "local")]
pub mod local;
#[cfg(feature = "manager")]