Skip to content

Commit

Permalink
CLI: add options for TCP link filter.
Browse files Browse the repository at this point in the history
Fixes #5.
  • Loading branch information
surban committed Feb 5, 2024
1 parent d0105d2 commit c76a11a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
14 changes: 12 additions & 2 deletions aggligator-util/src/bin/agg-speed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ use tokio::{

use aggligator::{cfg::Cfg, dump::dump_to_json_line_file};
use aggligator_util::{
cli::{init_log, load_cfg, print_default_cfg},
cli::{init_log, load_cfg, parse_tcp_link_filter, print_default_cfg},
monitor::{format_speed, interactive_monitor},
speed::{speed_test, INTERVAL},
transport::{
tcp::{IpVersion, TcpAcceptor, TcpConnector},
tcp::{IpVersion, TcpAcceptor, TcpConnector, TcpLinkFilter},
tls::{TlsClient, TlsServer},
websocket::{WebSocketAcceptor, WebSocketConnector},
AcceptorBuilder, ConnectorBuilder, LinkTagBox,
Expand Down Expand Up @@ -198,6 +198,15 @@ pub struct ClientCli {
/// TCP server name or IP addresses and port number.
#[arg(long)]
tcp: Vec<String>,
/// TCP link filter.
///
/// none: no link filtering.
///
/// interface-interface: one link for each pair of local and remote interface.
///
/// interface-ip: one link for each pair of local interface and remote IP address.
#[arg(long, value_parser = parse_tcp_link_filter, default_value = "interface-interface")]
tcp_link_filter: TcpLinkFilter,
/// WebSocket hosts or URLs.
///
/// Default server port number is 8080 and path is /agg-speed.
Expand Down Expand Up @@ -255,6 +264,7 @@ impl ClientCli {
let mut tcp_connector =
TcpConnector::new(self.tcp.clone(), TCP_PORT).await.context("cannot resolve TCP target")?;
tcp_connector.set_ip_version(ip_version);
tcp_connector.set_link_filter(self.tcp_link_filter);
targets.push(tcp_connector.to_string());
connector.add(tcp_connector);
}
Expand Down
14 changes: 12 additions & 2 deletions aggligator-util/src/bin/agg-tunnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ use aggligator::{
dump::dump_to_json_line_file,
};
use aggligator_util::{
cli::{init_log, load_cfg, print_default_cfg},
cli::{init_log, load_cfg, parse_tcp_link_filter, print_default_cfg},
monitor::{interactive_monitor, watch_tags},
transport::{
tcp::{IpVersion, TcpAcceptor, TcpConnector},
tcp::{IpVersion, TcpAcceptor, TcpConnector, TcpLinkFilter},
AcceptorBuilder, ConnectingTransport, ConnectorBuilder, LinkTagBox,
},
};
Expand Down Expand Up @@ -135,6 +135,15 @@ pub struct ClientCli {
/// TCP server name or IP addresses and port number.
#[arg(long)]
tcp: Vec<String>,
/// TCP link filter.
///
/// none: no link filtering.
///
/// interface-interface: one link for each pair of local and remote interface.
///
/// interface-ip: one link for each pair of local interface and remote IP address.
#[arg(long, value_parser = parse_tcp_link_filter, default_value = "interface-interface")]
tcp_link_filter: TcpLinkFilter,
/// Bluetooth RFCOMM server address.
#[cfg(feature = "rfcomm")]
#[arg(long)]
Expand Down Expand Up @@ -168,6 +177,7 @@ impl ClientCli {
match TcpConnector::new(self.tcp.clone(), TCP_PORT).await {
Ok(mut tcp) => {
tcp.set_ip_version(IpVersion::from_only(self.ipv4, self.ipv6)?);
tcp.set_link_filter(self.tcp_link_filter);
targets.push(tcp.to_string());
watch_conn.push(Box::new(tcp.clone()));
Some(tcp)
Expand Down
13 changes: 12 additions & 1 deletion aggligator-util/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Utility functions for command line utilities.
use anyhow::Context;
use anyhow::{bail, Context};
use std::path::PathBuf;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

use crate::transport::tcp::TcpLinkFilter;
use aggligator::cfg::Cfg;

/// Initializes logging for command line utilities.
Expand All @@ -28,3 +29,13 @@ pub fn load_cfg(path: &Option<PathBuf>) -> anyhow::Result<Cfg> {
None => Ok(Cfg::default()),
}
}

/// Parse [TcpLinkFilter] option.
pub fn parse_tcp_link_filter(s: &str) -> anyhow::Result<TcpLinkFilter> {
match s {
"none" => Ok(TcpLinkFilter::None),
"interface-interface" => Ok(TcpLinkFilter::InterfaceInterface),
"interface-ip" => Ok(TcpLinkFilter::InterfaceIp),
other => bail!("unknown TCP link filter: {other}"),
}
}
6 changes: 4 additions & 2 deletions aggligator-util/src/transport/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub enum TcpLinkFilter {
/// Filter based on local interface and remote IP address.
///
/// One link for each pair of local interface and remote IP address is established.
Ip,
InterfaceIp,
}

/// TCP transport for outgoing connections.
Expand Down Expand Up @@ -299,7 +299,9 @@ impl ConnectingTransport for TcpConnector {
TcpLinkFilter::InterfaceInterface => {
tag.interface == new_tag.interface && link.remote_user_data() == new.remote_user_data()
}
TcpLinkFilter::Ip => tag.interface == new_tag.interface && tag.remote.ip() == new_tag.remote.ip(),
TcpLinkFilter::InterfaceIp => {
tag.interface == new_tag.interface && tag.remote.ip() == new_tag.remote.ip()
}
}
}) {
Some(other) => {
Expand Down

0 comments on commit c76a11a

Please sign in to comment.