Skip to content

Commit

Permalink
Consolidate some config handling functions
Browse files Browse the repository at this point in the history
  • Loading branch information
loops committed Sep 7, 2024
1 parent 3b3022f commit 6f52a70
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
14 changes: 14 additions & 0 deletions wezterm-ssh/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ use std::path::{Path, PathBuf};

pub type ConfigMap = BTreeMap<String, String>;

pub trait ConfigMapOps {
fn is_yes(&self, name: &'static str) -> bool;
fn as_str(&self, name: &'static str) -> Option<&str>;
}

impl ConfigMapOps for ConfigMap {
fn is_yes(&self, name: &'static str) -> bool {
Some("yes") == self.get(name).map(|s| s.as_str())
}
fn as_str(&self, name: &'static str) -> Option<&str> {
self.get(name).map(|s| s.as_str())
}
}

/// A Pattern in a `Host` list
#[derive(Debug, PartialEq, Eq, Clone)]
struct Pattern {
Expand Down
14 changes: 7 additions & 7 deletions wezterm-ssh/src/sessioninner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::channelwrap::ChannelWrap;
use crate::config::ConfigMap;
use crate::config::{ConfigMap, ConfigMapOps};
use crate::dirwrap::DirWrap;
use crate::filewrap::FileWrap;
use crate::pty::*;
Expand Down Expand Up @@ -242,7 +242,7 @@ impl SessionInner {
.try_send(SessionEvent::Authenticated)
.context("notifying user that session is authenticated")?;

if let Some("yes") = self.config.get("forwardagent").map(|s| s.as_str()) {
if self.config.is_yes("forwardagent") {
if self.identity_agent().is_some() {
sess.enable_accept_agent_forward(true);
} else {
Expand Down Expand Up @@ -330,7 +330,7 @@ impl SessionInner {
port: u16,
verbose: bool,
) -> anyhow::Result<(Socket, Option<KillOnDropChild>)> {
match self.config.get("proxycommand").map(|s| s.as_str()) {
match self.config.as_str("proxycommand") {
Some("none") | None => {}
Some(proxy_command) => {
let mut cmd;
Expand Down Expand Up @@ -358,8 +358,8 @@ impl SessionInner {
use std::os::unix::io::{FromRawFd, IntoRawFd};

let raw = a.into_raw_fd();
let dest = match self.config.get("proxyusefdpass").map(|s| s.as_str()) {
Some("yes") => raw.recv_fd()?,
let dest = match self.config.is_yes("proxyusefdpass") {
true => raw.recv_fd()?,
_ => raw,
};

Expand Down Expand Up @@ -404,7 +404,7 @@ impl SessionInner {
/// Used to restrict to_socket_addrs results to the address
/// family specified by the config
fn filter_sock_addr(&self, addr: &std::net::SocketAddr) -> bool {
match self.config.get("addressfamily").map(|s| s.as_str()) {
match self.config.as_str("addressfamily") {
Some("inet") => addr.is_ipv4(),
Some("inet6") => addr.is_ipv6(),
None | Some("any") | Some(_) => true,
Expand Down Expand Up @@ -895,7 +895,7 @@ impl SessionInner {
pub fn exec(&mut self, sess: &mut SessionWrap, exec: Exec) -> anyhow::Result<ExecResult> {
let mut channel = sess.open_session()?;

if let Some("yes") = self.config.get("forwardagent").map(|s| s.as_str()) {
if self.config.is_yes("forwardagent") {
if self.identity_agent().is_some() {
if let Err(err) = channel.request_auth_agent_forwarding() {
log::error!("Failed to request agent forwarding: {:#}", err);
Expand Down

0 comments on commit 6f52a70

Please sign in to comment.