Repository navigation
fix: harden install cleanup and agent startup #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a9c6e49
d000922
9280154
918d416
95c2c7a
38ea3bf
982fe63
68f0af1
b3b41b0
c8f1793
91bb115
0d02e85
5947e0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "vmsan": patch | ||
| --- | ||
|
|
||
| Harden installer and VM networking reliability across mixed Linux hosts. | ||
|
|
||
| - fix branch/commit installs and uninstalls in `install.sh`, including safer cleanup of per-VM iptables rules | ||
| - migrate the default VM subnet to `198.19.x.x` while preserving compatibility with legacy persisted `172.16.x.x` states | ||
| - keep stopped VM slots reserved, tighten persisted IP parsing, and restore agent connectivity on hosts with restrictive local firewalls |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,7 +8,7 @@ set -euo pipefail | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Install a commit: curl -fsSL https://vmsan.dev/install | bash -s -- --sha <commit> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Uninstall: curl -fsSL https://vmsan.dev/install | bash -s -- --uninstall | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| VMSAN_DIR="${VMSAN_DIR:-$HOME/.vmsan}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| VMSAN_DIR="${VMSAN_DIR:-}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| VMSAN_REPO="angelorc/vmsan" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| VMSAN_INSTALL_BOOTSTRAPPED="${VMSAN_INSTALL_BOOTSTRAPPED:-}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| VMSAN_INSTALL_SOURCE_SHA="${VMSAN_INSTALL_SOURCE_SHA:-}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -29,6 +29,24 @@ success() { printf "\033[1;32m[ok]\033[0m %s\n" "$*"; } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| warn() { printf "\033[1;33m[warn]\033[0m %s\n" "$*"; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error() { printf "\033[1;31m[error]\033[0m %s\n" "$*" >&2; exit 1; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resolve_vmsan_dir() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ -n "$VMSAN_DIR" ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printf '%s\n' "$VMSAN_DIR" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ -n "${SUDO_USER:-}" ] && command -v getent >/dev/null 2>&1; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| local sudo_home | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sudo_home="$(getent passwd "$SUDO_USER" 2>/dev/null | cut -d: -f6)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ -n "$sudo_home" ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printf '%s/.vmsan\n' "$sudo_home" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printf '%s/.vmsan\n' "$HOME" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print_usage() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cat <<EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Usage: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -43,6 +61,96 @@ need_cmd() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| command -v "$1" >/dev/null 2>&1 || error "Required command not found: $1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default_iface() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ip route show default 2>/dev/null | awk '/default/ {print $5; exit}' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| json_string_field() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| local key="$1" file="$2" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| grep -oP "\"$key\"\\s*:\\s*\"\\K[^\"]+" "$file" 2>/dev/null | head -n1 || true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| json_bool_field() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| local key="$1" file="$2" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| grep -oP "\"$key\"\\s*:\\s*\\K(true|false)" "$file" 2>/dev/null | head -n1 || true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| json_ports_field() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| local file="$1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sed -n '/"publishedPorts"\s*:/,/\]/p' "$file" 2>/dev/null | grep -oP '\d+' || true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cidr30_from_ip() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| local ip="$1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IFS=. read -r a b c _ <<EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $ip | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ -n "${a:-}" ] && [ -n "${b:-}" ] && [ -n "${c:-}" ] || return 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printf '%s.%s.%s.0/30\n' "$a" "$b" "$c" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables_delete_rule() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables "$@" 2>/dev/null || true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| delete_iptables_rule_spec() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| local rule="$1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| local -a rule_parts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| read -r -a rule_parts <<<"$rule" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ "${#rule_parts[@]}" -gt 0 ] || return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ "${rule_parts[0]}" = "-A" ] || return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rule_parts[0]="-D" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables_delete_rule "${rule_parts[@]}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cleanup_iptables_rules_for_vm() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| local state_file="$1" default_iface_name="$2" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| local tap_device guest_ip skip_dnat slot veth_host guest_cidr | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tap_device="$(json_string_field tapDevice "$state_file")" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| guest_ip="$(json_string_field guestIp "$state_file")" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| skip_dnat="$(json_bool_field skipDnat "$state_file")" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ -n "$guest_ip" ] || return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| guest_cidr="$(cidr30_from_ip "$guest_ip" || true)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ -n "$default_iface_name" ] && [ -n "$guest_cidr" ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables_delete_rule -t nat -D POSTROUTING -s "$guest_cidr" -o "$default_iface_name" -j MASQUERADE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables_delete_rule -D OUTPUT -d "$guest_ip" -j ACCEPT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables_delete_rule -D INPUT -s "$guest_ip" -j ACCEPT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ "$skip_dnat" != "true" ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while IFS= read -r port; do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ -n "$port" ] || continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables_delete_rule -t nat -D PREROUTING -i "$default_iface_name" -p tcp --dport "$port" -j DNAT --to-destination "${guest_ip}:${port}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables_delete_rule -D FORWARD -p tcp -d "$guest_ip" --dport "$port" -j ACCEPT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done < <(json_ports_field "$state_file") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+117
to
+129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don’t gate The guest-specific 💡 Suggested change+ iptables_delete_rule -D OUTPUT -d "$guest_ip" -j ACCEPT
+ iptables_delete_rule -D INPUT -s "$guest_ip" -j ACCEPT
+
if [ -n "$default_iface_name" ] && [ -n "$guest_cidr" ]; then
iptables_delete_rule -t nat -D POSTROUTING -s "$guest_cidr" -o "$default_iface_name" -j MASQUERADE
- iptables_delete_rule -D OUTPUT -d "$guest_ip" -j ACCEPT
- iptables_delete_rule -D INPUT -s "$guest_ip" -j ACCEPT
if [ "$skip_dnat" != "true" ]; then
while IFS= read -r port; do📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| slot="$(printf '%s\n' "$tap_device" | sed -n 's/^fhvm//p')" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ -n "$default_iface_name" ] && [ -n "$slot" ] && [ -n "$guest_cidr" ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| veth_host="veth-h-$slot" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables_delete_rule -D FORWARD -i "$veth_host" -o "$default_iface_name" -s "$guest_cidr" -j ACCEPT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables_delete_rule -D FORWARD -i "$default_iface_name" -o "$veth_host" -d "$guest_cidr" -m state --state RELATED,ESTABLISHED -j ACCEPT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ -n "$tap_device" ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables -S 2>/dev/null | grep -E "(^-A .* -i ${tap_device}( |$)|^-A .* -o ${tap_device}( |$))" | while IFS= read -r rule; do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| delete_iptables_rule_spec "$rule" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done || true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ -n "$slot" ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| veth_host="veth-h-$slot" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables -S 2>/dev/null | grep -E "(^-A .* -i ${veth_host}( |$)|^-A .* -o ${veth_host}( |$))" | while IFS= read -r rule; do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| delete_iptables_rule_spec "$rule" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done || true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| VMSAN_DIR="$(resolve_vmsan_dir)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| download() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| local url="$1" dest="$2" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info "Downloading $(basename "$dest")..." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -82,7 +190,6 @@ bootstrap_ref_installer() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| shift | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| local script_tmp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| script_tmp="$(mktemp)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| trap 'rm -f "$script_tmp"' EXIT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| download "https://raw.githubusercontent.com/$VMSAN_REPO/$resolved_sha/install.sh" "$script_tmp" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| chmod +x "$script_tmp" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -94,7 +201,10 @@ bootstrap_ref_installer() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "VMSAN_INSTALL_REQUESTED_REF=$REQUESTED_REF" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "VMSAN_INSTALL_REQUESTED_SHA=$REQUESTED_SHA" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| env "${env_args[@]}" bash "$script_tmp" "$@" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exec 3<"$script_tmp" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rm -f -- "$script_tmp" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exec env "${env_args[@]}" bash -s -- "$@" <&3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while [ $# -gt 0 ]; do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -191,6 +301,19 @@ if [ "$UNINSTALL" -eq 1 ]; then | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| success "vmsan CLI not installed" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Clean up iptables rules owned by vmsan using per-VM state instead of broad 172.16.* matching. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DEFAULT_IFACE="$(default_iface || true)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IPTABLES_CLEANED=0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ -d "$VMSAN_DIR/vms" ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for state_file in "$VMSAN_DIR"/vms/*.json; do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ -f "$state_file" ] || continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ "$IPTABLES_CLEANED" -eq 0 ] && info "Cleaning vmsan iptables rules..." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cleanup_iptables_rules_for_vm "$state_file" "$DEFAULT_IFACE" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IPTABLES_CLEANED=1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ "$IPTABLES_CLEANED" -eq 1 ] && success "iptables rules cleaned" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Clean up TAP and veth interfaces (host namespace) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NET_COUNT=0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for iface_path in /sys/class/net/fhvm* /sys/class/net/veth-h-* /sys/class/net/veth-g-*; do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -200,24 +323,12 @@ if [ "$UNINSTALL" -eq 1 ]; then | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Clean up network namespaces created by vmsan | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for ns in $(ip netns list 2>/dev/null | awk '{print $1}' | grep '^vmsan-'); do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for ns in $(ip netns list 2>/dev/null | awk '{print $1}' | grep '^vmsan-' || true); do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ip netns delete "$ns" 2>/dev/null && NET_COUNT=$((NET_COUNT + 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ "$NET_COUNT" -gt 0 ] && success "Cleaned up $NET_COUNT network resources" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Clean up iptables rules referencing vmsan | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables-save 2>/dev/null | grep -cE 'fhvm|172\.16\.' >/dev/null 2>&1 && { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| info "Cleaning iptables rules..." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables -t nat -S 2>/dev/null | grep -E 'fhvm|172\.16\.' | while IFS= read -r rule; do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eval "iptables -t nat $(echo "$rule" | sed 's/^-A/-D/')" 2>/dev/null || true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iptables -S FORWARD 2>/dev/null | grep -E 'fhvm|172\.16\.' | while IFS= read -r rule; do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eval "iptables $(echo "$rule" | sed 's/^-A/-D/')" 2>/dev/null || true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| done | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| success "iptables rules cleaned" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Delete Cloudflare tunnel via API before removing local files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ -f "$VMSAN_DIR/cloudflare/cloudflare.json" ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CF_TOKEN="$(grep -oP '"token"\s*:\s*"\K[^"]+' "$VMSAN_DIR/cloudflare/cloudflare.json" 2>/dev/null || true)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| const VM_NETWORK_FIRST_OCTET = 198; | ||
| const VM_NETWORK_SECOND_OCTET = 19; | ||
|
|
||
| export const VM_SUBNET_MASK: string = "255.255.255.252"; | ||
| export const VM_NETWORK_PREFIX: string = `${VM_NETWORK_FIRST_OCTET}.${VM_NETWORK_SECOND_OCTET}`; | ||
| export const SUPPORTED_VM_ADDRESS_BLOCKS: readonly string[] = ["198.19.0.0/16", "172.16.0.0/16"]; | ||
|
|
||
| function assertValidSlot(slot: number): void { | ||
| if (!Number.isInteger(slot) || slot < 0 || slot > 254) { | ||
| throw new Error(`invalid VM network slot: ${slot}`); | ||
| } | ||
| } | ||
|
|
||
| function parseIpv4OrNull(ip: string): [number, number, number, number] | null { | ||
| const octets = ip.split("."); | ||
| if (octets.length !== 4 || octets.some((part) => !/^\d+$/.test(part))) { | ||
| return null; | ||
| } | ||
|
|
||
| const parts = octets.map((part) => Number.parseInt(part, 10)); | ||
| if (parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) { | ||
| return null; | ||
| } | ||
|
|
||
| return parts as [number, number, number, number]; | ||
| } | ||
|
|
||
| function parseIpv4(ip: string): [number, number, number, number] { | ||
| const parts = parseIpv4OrNull(ip); | ||
| if (!parts) { | ||
| throw new Error(`invalid IPv4 address: ${ip}`); | ||
| } | ||
| return parts; | ||
| } | ||
|
|
||
| export function vmHostIp(slot: number): string { | ||
| assertValidSlot(slot); | ||
| return `${VM_NETWORK_PREFIX}.${slot}.1`; | ||
| } | ||
|
|
||
| export function vmGuestIp(slot: number): string { | ||
| assertValidSlot(slot); | ||
| return `${VM_NETWORK_PREFIX}.${slot}.2`; | ||
| } | ||
|
|
||
| export function slotFromVmHostIpOrNull(hostIp: string): number | null { | ||
| const parts = parseIpv4OrNull(hostIp); | ||
| if (!parts) { | ||
| return null; | ||
| } | ||
|
|
||
| const slot = parts[2]; | ||
| if (!Number.isInteger(slot) || slot < 0 || slot > 254) { | ||
| return null; | ||
| } | ||
|
|
||
| return slot; | ||
| } | ||
|
|
||
| export function slotFromVmHostIp(hostIp: string): number { | ||
| const slot = slotFromVmHostIpOrNull(hostIp); | ||
| if (slot === null) { | ||
| throw new Error(`invalid VM host IP: ${hostIp}`); | ||
| } | ||
| return slot; | ||
| } | ||
|
|
||
| export function vmLinkCidrFromIp(ip: string): string { | ||
| const [first, second, third] = parseIpv4(ip); | ||
| return `${first}.${second}.${third}.0/30`; | ||
| } | ||
|
|
||
| export function vmAddressBlockCidrFromIp(ip: string): string { | ||
| const [first, second] = parseIpv4(ip); | ||
| return `${first}.${second}.0.0/16`; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parse the
devtoken instead of assuming field 5.ip route show defaultcan also render asdefault dev <iface> .... In that form,$5is empty and the uninstall path silently skips MASQUERADE/DNAT cleanup.💡 Suggested change
default_iface() { - ip route show default 2>/dev/null | awk '/default/ {print $5; exit}' + ip route show default 2>/dev/null | awk '{ + for (i = 1; i < NF; i++) { + if ($i == "dev") { + print $(i + 1) + exit + } + } + }' }📝 Committable suggestion
🤖 Prompt for AI Agents