Something went wrong. Try again.
Metric and Time-Series Engine: A Minimal Archiving Time Series Storage Service
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192#!/bin/bash# mats-net: Privileged network initialization helper for mats-proxy Netstack.## Sets up the TUN device, configures iptables/nftables DNAT forwarding,# drops privileges back to the invoking user, and cleans up on exit.## Usage:# sudo bin/mats-net bin/mats-proxy-netstack [proxy-arguments...]
set -o errexitset -o nounsetset -o pipefail
if [[ $EUID -ne 0 ]]; then echo "mats-net: must be run with root privileges (e.g. sudo bin/mats-net bin/mats-proxy-netstack)" >&2 exit 1fi
if [[ $# -eq 0 ]]; then echo "Usage: sudo $0 <command> [args...]" >&2 echo "Example: sudo $0 bin/mats-proxy-netstack" >&2 exit 1fi
# Detect invoking non-root userTARGET_USER="${SUDO_USER:-$(id -un)}"TARGET_UID="${SUDO_UID:-$(id -u)}"TARGET_GID="${SUDO_GID:-$(id -g)}"
TUN_DEV="${MATS_NET_TUN:-mats-tun0}"TUN_HOST_IP="${MATS_NET_HOST_IP:-10.254.0.1}"TUN_PEER_IP="${MATS_NET_PEER_IP:-10.254.0.2}"PORT="${MATS_NET_PORT:-38088}"
cleanup() { local exit_code=$? echo "" echo "[mats-net] Cleaning up network rules and TUN interface ${TUN_DEV}..." iptables -t mangle -D FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu 2>/dev/null || true iptables -t nat -D PREROUTING -p tcp --dport "$PORT" -j DNAT --to-destination "${TUN_PEER_IP}:${PORT}" 2>/dev/null || true iptables -t nat -D OUTPUT -p tcp -m addrtype --dst-type LOCAL --dport "$PORT" -j DNAT --to-destination "${TUN_PEER_IP}:${PORT}" 2>/dev/null || true iptables -t nat -D POSTROUTING -o "$TUN_DEV" -j MASQUERADE 2>/dev/null || true iptables -D FORWARD -p tcp -d "$TUN_PEER_IP" --dport "$PORT" -j ACCEPT 2>/dev/null || true iptables -D FORWARD -i "$TUN_DEV" -j ACCEPT 2>/dev/null || true iptables -D FORWARD -o "$TUN_DEV" -j ACCEPT 2>/dev/null || true ip link set "$TUN_DEV" down 2>/dev/null || true ip tuntap del dev "$TUN_DEV" mode tun 2>/dev/null || true echo "[mats-net] Teardown complete." exit "$exit_code"}trap cleanup EXIT INT TERM
# 1. Create TUN device owned by the target unprivileged userecho "[mats-net] Creating TUN device ${TUN_DEV} owned by user ${TARGET_USER} (${TARGET_UID}:${TARGET_GID})..."ip tuntap add dev "$TUN_DEV" mode tun user "$TARGET_USER"ip addr add "${TUN_HOST_IP}/30" dev "$TUN_DEV"ip link set "$TUN_DEV" up
# 2. Configure kernel forwarding and DNAT rules to userspace Netstackecho "[mats-net] Forwarding TCP port ${PORT} (external and local) to Netstack at ${TUN_PEER_IP}:${PORT}..."sysctl -q -w net.ipv4.ip_forward=1sysctl -q -w net.ipv4.conf.all.route_localnet=1sysctl -q -w net.ipv4.conf.default.route_localnet=1
# External traffic DNATiptables -t nat -A PREROUTING -p tcp --dport "$PORT" -j DNAT --to-destination "${TUN_PEER_IP}:${PORT}"# Local host traffic DNAT (localhost and host IP)iptables -t nat -A OUTPUT -p tcp -m addrtype --dst-type LOCAL --dport "$PORT" -j DNAT --to-destination "${TUN_PEER_IP}:${PORT}"# Outgoing masquerade on the TUN device for clean reply routingiptables -t nat -A POSTROUTING -o "$TUN_DEV" -j MASQUERADE# Forwarding rules inserted at position 1 to precede any firewall (e.g. UFW/Docker) reject rulesiptables -I FORWARD 1 -i "$TUN_DEV" -j ACCEPTiptables -I FORWARD 1 -o "$TUN_DEV" -j ACCEPT# Clamp TCP MSS to path MTU to prevent packet truncation on tunnel routingiptables -t mangle -I FORWARD 1 -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu 2>/dev/null || true
# 3. Export configuration to target processexport MATS_PROXY_TUN_DEV="$TUN_DEV"export MATS_PROXY_LISTEN="${TUN_PEER_IP}:${PORT}"
# 4. Drop privileges and execute unprivileged commandecho "[mats-net] Dropping privileges to ${TARGET_USER} and launching: $*"if command -v setpriv >/dev/null 2>&1; then setpriv --reuid="$TARGET_UID" --regid="$TARGET_GID" --init-groups -- "$@" & CHILD_PID=$! wait "$CHILD_PID"else su -s /bin/bash "$TARGET_USER" -c '"$@"' -- "$@" & CHILD_PID=$! wait "$CHILD_PID"fi