@kvbox/server 0.6.15 → 0.6.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kvbox/server",
3
- "version": "0.6.15",
3
+ "version": "0.6.17",
4
4
  "description": "kvbox host daemon: owns Firecracker microVMs on a Linux host and serves the kvbox API. Managed with the @kvbox/cli CLI.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -26,6 +26,11 @@
26
26
  # box <from-ip> may open TCP connections to <to-ip>:<port> (#98), through
27
27
  # this host; rule tagged kvbox:<box-id> (the target's) in chain KVBOX-LINKS
28
28
  # net-setup.sh link rm <box-id> <from-ip> <to-ip> <port> remove exactly that
29
+ # net-setup.sh farlink add <box-id> <from-ip> <stand-in-ip> <port> <via-port>
30
+ # box <from-ip> reaches <port> of a box on another host (#207): what it sends to
31
+ # <stand-in-ip>:<port> arrives at kvbox-server on <via-port>, which carries it to
32
+ # that host; rule tagged kvbox:<box-id> (the box that connects)
33
+ # net-setup.sh farlink rm <box-id> <from-ip> <stand-in-ip> <port> remove it, whatever port it led to
29
34
  # net-setup.sh relay up <conf> [port] install <conf> (written by kvbox-server relay) as /etc/wireguard/kvbox.conf,
30
35
  # keep the tunnel to the relay up, now and after reboots (wg-quick@kvbox), and
31
36
  # let the control plane's HTTPS port (default 7777) in on the tunnel interface
@@ -49,6 +54,7 @@ action="${1:-up}"
49
54
  tap_op="${2:-}"; tap_name="${3:-}"; tap_ip="${4:-}"; tap_mac="${5:-}"; tap_mode="${6:-}"
50
55
  px_op="${2:-}"; px_box="${3:-}"; px_ip="${4:-}"; px_hport="${5:-}"; px_gport="${6:-}"; px_proto="${7:-tcp}"
51
56
  lk_op="${2:-}"; lk_box="${3:-}"; lk_from="${4:-}"; lk_to="${5:-}"; lk_port="${6:-}"
57
+ fl_op="${2:-}"; fl_box="${3:-}"; fl_from="${4:-}"; fl_standin="${5:-}"; fl_port="${6:-}"; fl_via="${7:-}"
52
58
  relay_op="${2:-}"; relay_conf="${3:-}"; relay_port="${4:-7777}"
53
59
  RELAY_IF=kvbox # wg-quick@kvbox reads /etc/wireguard/kvbox.conf
54
60
 
@@ -68,10 +74,13 @@ case "$action" in
68
74
  link) [[ "$lk_op" =~ ^(add|rm)$ && "$lk_box" =~ ^[a-z0-9][a-z0-9._-]{0,14}$ && "$lk_from" =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ \
69
75
  && "$lk_to" =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ && "$lk_port" =~ ^[0-9]{1,5}$ ]] \
70
76
  || fail "usage: $0 link add|rm <box-id> <from-ip> <to-ip> <port>" ;;
77
+ farlink) [[ "$fl_op" =~ ^(add|rm)$ && "$fl_box" =~ ^[a-z0-9][a-z0-9._-]{0,14}$ && "$fl_from" =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ \
78
+ && "$fl_standin" =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ && "$fl_port" =~ ^[0-9]{1,5}$ && ( "$fl_op" == rm || "$fl_via" =~ ^[0-9]{1,5}$ ) ]] \
79
+ || fail "usage: $0 farlink add <box-id> <from-ip> <stand-in-ip> <port> <via-port> | farlink rm <box-id> <from-ip> <stand-in-ip> <port>" ;;
71
80
  relay) if [[ ! "$relay_op" =~ ^(up|restart|down|status)$ ]] || { [[ "$relay_op" == up ]] && [[ -z "$relay_conf" ]]; } || [[ ! "$relay_port" =~ ^[0-9]{1,5}$ ]]; then
72
81
  fail "usage: $0 relay up <conf> [port] | relay restart | relay down | relay status"
73
82
  fi ;;
74
- *) fail "usage: $0 [up|down|status|tap add|attach|del <name> ...|proxy add|rm ...|link add|rm ...|relay up|restart|down|status]" ;;
83
+ *) fail "usage: $0 [up|down|status|tap add|attach|del <name> ...|proxy add|rm ...|link add|rm ...|farlink add|rm ...|relay up|restart|down|status]" ;;
75
84
  esac
76
85
  for bin in ip iptables sysctl; do command -v "$bin" >/dev/null 2>&1 || fail "$bin not found"; done
77
86
  if [[ "$action" == tap ]]; then command -v tc >/dev/null 2>&1 || fail "tc not found (iproute2)"; fi
@@ -107,7 +116,7 @@ if [[ $EUID -ne 0 ]]; then
107
116
  if [[ -t 0 ]]; then exec sudo --preserve-env=KVBOX_TAP_USER,KVBOX_BRIDGE,KVBOX_SUBNET,KVBOX_GATEWAY "$0" "$@"; fi
108
117
  exec sudo -n --preserve-env=KVBOX_TAP_USER,KVBOX_BRIDGE,KVBOX_SUBNET,KVBOX_GATEWAY "$0" "$@"
109
118
  fi
110
- if [[ "$action" == proxy ]]; then
119
+ if [[ "$action" == proxy || "$action" == farlink ]]; then
111
120
  command -v iptables-save >/dev/null 2>&1 || fail "iptables-save not found"
112
121
  fi
113
122
 
@@ -142,6 +151,7 @@ RULE_DESC+=(
142
151
  "DNS: boxes asking this host for a name reach kvbox-server, which answers <box>.kvbox"
143
152
  "firewall: boxes may ask this host for names (only that)"
144
153
  "firewall: a box may reach another box's port when they are linked (kvbox vm link)"
154
+ "firewall: a box linked to a box on another host may hand this host that connection (only that)"
145
155
  )
146
156
  RULE_SPEC+=(
147
157
  "filter FORWARD -i $BRIDGE -p tcp --dport 25 -m comment --comment $TAG -j DROP"
@@ -152,6 +162,7 @@ RULE_SPEC+=(
152
162
  "nat PREROUTING -i $BRIDGE -d $GATEWAY -p udp --dport 53 -m comment --comment $TAG -j REDIRECT --to-ports $DNS_PORT"
153
163
  "filter INPUT -i $BRIDGE -d $GATEWAY -p udp --dport $DNS_PORT -m comment --comment $TAG -j ACCEPT"
154
164
  "filter FORWARD -i $BRIDGE -o $BRIDGE -m comment --comment $TAG -j $LINKS"
165
+ "filter INPUT -i $BRIDGE -p tcp -m conntrack --ctstate DNAT -m comment --comment $TAG -j ACCEPT"
155
166
  )
156
167
  has_rule() { local t=$1 c=$2; shift 2; iptables -t "$t" -C "$c" "$@" 2>/dev/null; }
157
168
  add_rule() { local t=$1 c=$2; shift 2; iptables -t "$t" -I "$c" 1 "$@"; }
@@ -249,6 +260,8 @@ do_status() {
249
260
  if [[ -z "$fwds" ]]; then skip "no box ports published on this host"; else ok "box ports published on this host:"; printf '%s\n' "$fwds"; fi
250
261
  local links; links="$(link_summary)"
251
262
  if [[ -z "$links" ]]; then skip "no links between boxes"; else ok "links between boxes:"; printf '%s\n' "$links"; fi
263
+ local fars; fars="$(far_summary)"
264
+ if [[ -z "$fars" ]]; then skip "no links to boxes on other hosts"; else ok "links to boxes on other hosts:"; printf '%s\n' "$fars"; fi
252
265
  if [[ $missing -eq 0 ]]; then echo; ok "host network is ready"; else echo; printf '%s✗%s host network incomplete; run: kvbox-server net up\n' "$RED" "$NC"; fi
253
266
  return $missing
254
267
  }
@@ -342,7 +355,7 @@ proxy_rules() {
342
355
  }
343
356
  # One line per published port: "port 31207 -> box a3f9c1 (172.30.0.2:22)".
344
357
  proxy_summary() {
345
- proxy_rules | grep -F "PREROUTING" | sed -E 's/.*-p ([a-z]+) .*--dport ([0-9]+) .*--comment kvbox:([^ ]+) .*--to-destination ([0-9.:]+).*/ \1 port \2 -> box \3 (\4)/' || true
358
+ proxy_rules | grep -F "PREROUTING" | grep -F -- "-j DNAT" | sed -E 's/.*-p ([a-z]+) .*--dport ([0-9]+) .*--comment kvbox:([^ ]+) .*--to-destination ([0-9.:]+).*/ \1 port \2 -> box \3 (\4)/' || true
346
359
  }
347
360
 
348
361
  # ── link add|rm ──────────────────────────────────────────────────────────────
@@ -374,6 +387,52 @@ link_summary() {
374
387
  | sed -E 's/.*-s ([0-9.]+)(\/32)? -d ([0-9.]+)(\/32)? .*--dport ([0-9]+).*/ \1 -> \3 port \5/' || true
375
388
  }
376
389
 
390
+ # ── farlink add|rm ───────────────────────────────────────────────────────────
391
+ # A link to a box on another host (#207). That box has no address here, so the box that connects is given a
392
+ # stand-in address for it: what it sends there (to the linked port only) is redirected to kvbox-server, which
393
+ # carries the connection to the other host. One rule per link, tagged with the connecting box's id; a base
394
+ # rule lets such a redirected connection in, and nothing else a box sends to this host.
395
+ far_accept="filter INPUT -i $BRIDGE -p tcp -m conntrack --ctstate DNAT -m comment --comment $TAG -j ACCEPT"
396
+ far_spec() { printf '%s' "nat PREROUTING -i $BRIDGE -s $2 -d $3 -p tcp --dport $4 -m comment --comment kvbox:$1 -j REDIRECT --to-ports $5"; }
397
+ # The rules of one such link as iptables-save writes them (without -A), found by what they match,
398
+ # whatever port they lead to: kvbox-server takes a new port each time it starts listening.
399
+ far_rules() {
400
+ iptables-save -t nat 2>/dev/null | sed -n 's/^-A //p' | sed 's/--comment "\([^"]*\)"/--comment \1/' \
401
+ | grep -F -- "--comment kvbox:$1 " | grep -F -- "-s $2/32 " | grep -F -- "-d $3/32 " | grep -F -- "--dport $4 " | grep -F -- "-j REDIRECT" || true
402
+ }
403
+ far_drop() {
404
+ local rule n=0
405
+ while IFS= read -r rule; do
406
+ [[ -n "$rule" ]] || continue
407
+ # shellcheck disable=SC2086 # iptables-save wrote the rule as a word list
408
+ eval "iptables -t nat -D $rule" && n=$((n + 1))
409
+ done < <(far_rules "$@")
410
+ [[ $n -gt 0 ]]
411
+ }
412
+ do_farlink_add() {
413
+ # shellcheck disable=SC2086 # the rule is a word list by design
414
+ if ! has_rule $far_accept; then fail "this host's network predates links to boxes on other hosts; run: kvbox-server net up"; fi
415
+ # Like a link on one host, this trusts the source IP: refuse while any box could still send as another.
416
+ local tap
417
+ for tap in $(taps_on_bridge); do tap_pinned "$tap" || fail "a box on this host ($tap) runs from before boxes were pinned to their address; restart kvbox-server, which pins it"; done
418
+ local spec; spec="$(far_spec "$@")"
419
+ # shellcheck disable=SC2086
420
+ if has_rule $spec; then skip "box $2 already reaches port $4 of a box on another host (as $3)"; return; fi
421
+ far_drop "$1" "$2" "$3" "$4" || true # the same link from before kvbox-server restarted
422
+ # shellcheck disable=SC2086
423
+ add_rule $spec; ok "box $2 now reaches port $4 of a box on another host (as $3)"
424
+ }
425
+ do_farlink_rm() {
426
+ if far_drop "$@"; then ok "box $2 no longer reaches port $4 of the box on another host ($3)"; else skip "box $2 did not reach port $4 of a box on another host as $3"; fi
427
+ # Open connections outlive the rule otherwise.
428
+ if command -v conntrack >/dev/null 2>&1; then conntrack -D -p tcp -s "$2" -d "$3" --dport "$4" >/dev/null 2>&1 || true; fi
429
+ }
430
+ # One line per such link: "172.30.0.5 -> port 5432 of a box on another host (as 172.30.255.1)".
431
+ far_summary() {
432
+ proxy_rules | grep -F "PREROUTING" | grep -F -- "-j REDIRECT" \
433
+ | sed -E 's/.*-s ([0-9.]+)(\/32)? -d ([0-9.]+)(\/32)? .*--dport ([0-9]+).*/ \1 -> port \5 of a box on another host (as \3)/' || true
434
+ }
435
+
377
436
  # ── relay up|down|status ─────────────────────────────────────────────────────
378
437
  # The host's end of the tunnel to its relay (#50). kvbox-server writes the wg-quick conf (private
379
438
  # key inside) under its state dir; root copies it to /etc/wireguard and lets systemd keep the tunnel
@@ -468,6 +527,10 @@ case "$action" in
468
527
  add) do_link_add "$lk_box" "$lk_from" "$lk_to" "$lk_port" ;;
469
528
  rm) do_link_rm "$lk_box" "$lk_from" "$lk_to" "$lk_port" ;;
470
529
  esac ;;
530
+ farlink) case "$fl_op" in
531
+ add) do_farlink_add "$fl_box" "$fl_from" "$fl_standin" "$fl_port" "$fl_via" ;;
532
+ rm) do_farlink_rm "$fl_box" "$fl_from" "$fl_standin" "$fl_port" ;;
533
+ esac ;;
471
534
  relay) case "$relay_op" in
472
535
  up) do_relay_up "$relay_conf" "$relay_port" ;;
473
536
  restart) do_relay_restart ;;