port labels and move logout to settings

This commit is contained in:
Matt Hill
2026-03-09 17:15:09 -06:00
parent be921b7865
commit 30f6492abc
13 changed files with 280 additions and 59 deletions

View File

@@ -11,6 +11,7 @@ use crate::db::model::public::NetworkInterfaceType;
use crate::net::forward::add_iptables_rule;
use crate::prelude::*;
use crate::tunnel::context::TunnelContext;
use crate::tunnel::db::PortForwardEntry;
use crate::tunnel::wg::{WIREGUARD_INTERFACE_NAME, WgConfig, WgSubnetClients, WgSubnetConfig};
use crate::util::serde::{HandlerExtSerde, display_serializable};
@@ -51,6 +52,14 @@ pub fn tunnel_api<C: Context>() -> ParentHandler<C> {
.no_display()
.with_about("about.remove-port-forward")
.with_call_remote::<CliContext>(),
)
.subcommand(
"update-label",
from_fn_async(update_forward_label)
.with_metadata("sync_db", Value::Bool(true))
.no_display()
.with_about("about.update-port-forward-label")
.with_call_remote::<CliContext>(),
),
)
.subcommand(
@@ -453,11 +462,17 @@ pub async fn show_config(
pub struct AddPortForwardParams {
source: SocketAddrV4,
target: SocketAddrV4,
#[arg(long)]
label: String,
}
pub async fn add_forward(
ctx: TunnelContext,
AddPortForwardParams { source, target }: AddPortForwardParams,
AddPortForwardParams {
source,
target,
label,
}: AddPortForwardParams,
) -> Result<(), Error> {
let prefix = ctx
.net_iface
@@ -482,10 +497,12 @@ pub async fn add_forward(
m.insert(source, rc);
});
let entry = PortForwardEntry { target, label };
ctx.db
.mutate(|db| {
db.as_port_forwards_mut()
.insert(&source, &target)
.insert(&source, &entry)
.and_then(|replaced| {
if replaced.is_some() {
Err(Error::new(
@@ -523,3 +540,31 @@ pub async fn remove_forward(
}
Ok(())
}
#[derive(Deserialize, Serialize, Parser)]
#[serde(rename_all = "camelCase")]
pub struct UpdatePortForwardLabelParams {
source: SocketAddrV4,
label: String,
}
pub async fn update_forward_label(
ctx: TunnelContext,
UpdatePortForwardLabelParams { source, label }: UpdatePortForwardLabelParams,
) -> Result<(), Error> {
ctx.db
.mutate(|db| {
db.as_port_forwards_mut().mutate(|pf| {
let entry = pf.0.get_mut(&source).ok_or_else(|| {
Error::new(
eyre!("Port forward from {source} not found"),
ErrorKind::NotFound,
)
})?;
entry.label = label.clone();
Ok(())
})
})
.await
.result
}

View File

@@ -184,7 +184,8 @@ impl TunnelContext {
}
let mut active_forwards = BTreeMap::new();
for (from, to) in peek.as_port_forwards().de()?.0 {
for (from, entry) in peek.as_port_forwards().de()?.0 {
let to = entry.target;
let prefix = net_iface
.peek(|i| {
i.iter()

View File

@@ -53,7 +53,7 @@ impl Model<TunnelDatabase> {
}
self.as_port_forwards_mut().mutate(|pf| {
Ok(pf.0.retain(|k, v| {
if keep_targets.contains(v.ip()) {
if keep_targets.contains(v.target.ip()) {
keep_sources.insert(*k);
true
} else {
@@ -70,11 +70,19 @@ fn export_bindings_tunnel_db() {
TunnelDatabase::export_all_to("bindings/tunnel").unwrap();
}
#[derive(Clone, Debug, Deserialize, Serialize, TS)]
#[serde(rename_all = "camelCase")]
pub struct PortForwardEntry {
pub target: SocketAddrV4,
#[serde(default)]
pub label: String,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize, TS)]
pub struct PortForwards(pub BTreeMap<SocketAddrV4, SocketAddrV4>);
pub struct PortForwards(pub BTreeMap<SocketAddrV4, PortForwardEntry>);
impl Map for PortForwards {
type Key = SocketAddrV4;
type Value = SocketAddrV4;
type Value = PortForwardEntry;
fn key_str(key: &Self::Key) -> Result<impl AsRef<str>, Error> {
Self::key_string(key)
}