infrastructure/machines/nixos/vault01/ulogd/nat-request-daddr.nix

46 lines
1.3 KiB
Nix

# SPDX-FileCopyrightText: 2025 Lubin Bailly <lubin.bailly@dgnum.eu>
#
# SPDX-License-Identifier: EUPL-1.2
{
writeShellApplication,
postgresql,
}:
writeShellApplication {
name = "nat-request-daddr";
runtimeInputs = [ postgresql ];
text = ''
TARGET_TIMESTAMP=$3
TARGET_PREFIX=$2
SRC_PREFIX=$1
if [ -z "$TARGET_TIMESTAMP" ] || [ -z "$TARGET_PREFIX" ] || [ -z "$SRC_PREFIX" ]; then
echo "$(basename "$0") SOURCE_IP DISTANT_IP TIMESTAMP"
exit 1
fi
psql -d ulogd -U ulogd -c "
select
vlan_id,
reply_ip_daddr_str as used_ip,
reply_l4_dport as used_port,
orig_ip_daddr_str as daddr,
orig_l4_dport as dport,
flow_start_sec, flow_end_sec
from ulog2_ct
join kea_log on ulog2_ct.orig_ip_saddr_str <<= kea_log.ip_addr
where
lease_start_sec <= $TARGET_TIMESTAMP
and
$TARGET_TIMESTAMP <= lease_end_sec
and
-- if we don't have conn start, we considered it started before the target time
( flow_start_sec IS NULL or flow_start_sec <= $TARGET_TIMESTAMP )
and
-- similar for conn end
( flow_end_sec IS NULL or flow_end_sec >= $TARGET_TIMESTAMP )
and
orig_ip_daddr_str <<= inet '$TARGET_PREFIX'
and
reply_ip_daddr_str <<= inet '$SRC_PREFIX'
;"
'';
}