Skip to content

Commit

Permalink
added -n flag to not run any commands and fixed trailing space bug in…
Browse files Browse the repository at this point in the history
… template creation port enty
  • Loading branch information
ericfinger committed Jul 7, 2023
1 parent 54011c8 commit bf19a8b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "connection"
version = "0.3.2"
version = "1.3.3"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
4 changes: 4 additions & 0 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ impl Connection {
}

pub fn exec_cmd(self) -> Result<()> {
if self.cli.no_command {
return Ok(());
}

if let Some(cmd) = self.cli.command {
let mut split = cmd.split(' ');
let mut command = Command::new(split.next().unwrap());
Expand Down
47 changes: 32 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,59 @@ struct Cli {
ports: Vec<String>,

/// Make all ports hits use UDP (default is TCP)
#[arg(short, long, requires = "host")]
#[arg(short, long)]
udp: bool,

/// Wait <t> milliseconds between Port hits
#[arg(short, long, value_names = ["t"], default_value_t = 0, requires = "host")]
#[arg(short, long, value_names = ["t"], default_value_t = 0)]
delay: u64,

/// Force usage of IPv4
#[arg(short = '4', long, requires = "host")]
#[arg(short = '4', long)]
ipv4: bool,

/// Force usage of IPv6
#[arg(short = '6', long, requires = "host")]
#[arg(short = '6', long)]
ipv6: bool,

/// Be verbose
#[arg(short, long, requires = "host")]
#[arg(short, long)]
verbose: bool,

/// Run a command after the knock
#[arg(short, long, value_names = ["cmd"], requires = "host")]
#[arg(short, long, value_names = ["cmd"])]
command: Option<String>,

/// Don't run a command after the knock, even if configured in preset
#[arg(short, long)]
#[serde(skip_deserializing, skip_serializing)]
no_command: bool,

/// List all presets
#[arg(short, long, help_heading = Some("Presets"), exclusive = true)]
#[serde(skip_deserializing, skip_serializing)]
list: bool,

/// Run the Wizard to create a new preset
#[arg(long, value_names = ["name"], help_heading = Some("Presets"), exclusive = true)]
#[serde(skip_deserializing, skip_serializing)]
new: Option<String>,

/// Run the wizard to change a preset
#[arg(long, value_names = ["name"], help_heading = Some("Presets"), exclusive = true)]
#[serde(skip_deserializing, skip_serializing)]
reconfigure: Option<String>,

/// Delete a preset
#[arg(long, value_names = ["name"], help_heading = Some("Presets"), exclusive = true)]
#[serde(skip_deserializing, skip_serializing)]
delete: Option<String>,

/// Delete all presets
#[arg(long, help_heading = Some("Presets"), exclusive = true)]
#[serde(skip_deserializing, skip_serializing)]
delete_all: bool,

#[clap(skip)]
encrypted: bool,
#[clap(skip)]
encrypted_content: Option<Vec<u8>>,
}
Expand Down Expand Up @@ -176,28 +184,37 @@ fn main() -> Result<()> {
);
}

// This variable will get overwritten on cli soon so we need to snapshot it:
let no_comand = cli.no_command;
let cli_loaded: Cli = load("connection", Some(cli.host.as_ref().unwrap().as_ref()))
.context(format!(
"Could not load config file for '{}'",
cli.host.as_ref().unwrap()
))?;

if cli_loaded.encrypted {
if let Some(encrypted_content) = cli_loaded.encrypted_content {
let key = Password::new(&format!(
"Enter the password for config file {}",
"Enter the password for config file '{}'",
cli.host.as_ref().unwrap()
))
.with_validator(ValueRequiredValidator::default())
.without_confirmation()
.prompt()?;

let crypt = new_magic_crypt!(key, 256);
let decrypted_content =
crypt.decrypt_bytes_to_bytes(&cli_loaded.encrypted_content.clone().unwrap())?;
let decrypted_content = crypt
.decrypt_bytes_to_bytes(&encrypted_content)
.context("Could not decrypt config. Wrong Password?")?;
cli = serde_json::from_str(
&String::from_utf8(decrypted_content)
.expect("Could not decode encrypted config as json"),
)?;
} else {
cli = cli_loaded;
}

if no_comand {
cli.no_command = true;
}
}

Expand Down Expand Up @@ -226,7 +243,7 @@ fn create_config(name: &str) -> Result<()> {
.prompt()?;

let mut ports = Vec::new();
for port in ports_str.split(' ') {
for port in ports_str.trim().split(' ') {
ports.push(port.to_string());
}

Expand Down Expand Up @@ -273,12 +290,12 @@ fn create_config(name: &str) -> Result<()> {
ipv6,
verbose,
command,
no_command: false,
list: false,
new: None,
reconfigure: None,
delete: None,
delete_all: false,
encrypted,
encrypted_content: None,
};

Expand All @@ -300,12 +317,12 @@ fn create_config(name: &str) -> Result<()> {
ipv6: false,
verbose: false,
command: None,
no_command: false,
list: false,
new: None,
reconfigure: None,
delete: None,
delete_all: false,
encrypted: true,
encrypted_content: Some(encrypted_content),
};
store("connection", name, cli_encrypted).context("Could not store config")?;
Expand Down

0 comments on commit bf19a8b

Please sign in to comment.