@kvbox/server 0.6.20 → 0.6.22
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/dist/kvbox-server.js +138 -21
- package/dist/kvbox-server.js.map +1 -1
- package/package.json +1 -1
- package/scripts/net-setup.sh +99 -4
package/package.json
CHANGED
package/scripts/net-setup.sh
CHANGED
|
@@ -31,6 +31,12 @@
|
|
|
31
31
|
# <stand-in-ip>:<port> arrives at kvbox-server on <via-port>, which carries it to
|
|
32
32
|
# that host; rule tagged kvbox:<box-id> (the box that connects)
|
|
33
33
|
# net-setup.sh farlink rm <box-id> <from-ip> <stand-in-ip> <port> remove it, whatever port it led to
|
|
34
|
+
# net-setup.sh cluster add <box-id> <ip> <cluster-key>
|
|
35
|
+
# box <ip> joins the cluster <cluster-key> (16 hex chars): the boxes of one
|
|
36
|
+
# cluster reach each other on any port and protocol, both ways, through this
|
|
37
|
+
# host; two rules tagged kvbox:<box-id>, one in KVBOX-CLUSTERS and one in the
|
|
38
|
+
# cluster's own chain KVBOX-C-<cluster-key>. A box is in one cluster at most
|
|
39
|
+
# net-setup.sh cluster rm <box-id> <ip> take the box out of whatever cluster it is in
|
|
34
40
|
# net-setup.sh relay up <conf> [port] install <conf> (written by kvbox-server relay) as /etc/wireguard/kvbox.conf,
|
|
35
41
|
# keep the tunnel to the relay up, now and after reboots (wg-quick@kvbox), and
|
|
36
42
|
# let the control plane's HTTPS port (default 7777) in on the tunnel interface
|
|
@@ -50,10 +56,13 @@ PREFIX="${SUBNET#*/}"
|
|
|
50
56
|
TAG="kvbox:$BRIDGE" # iptables comment; `down` finds our rules by it
|
|
51
57
|
DNS_PORT=10053 # where kvbox-server answers <name>.kvbox (DNS_PORT in core/dns.ts)
|
|
52
58
|
LINKS=KVBOX-LINKS # chain of the links between boxes (#98), entered from FORWARD above the drops
|
|
59
|
+
CLUSTERS=KVBOX-CLUSTERS # where a box of a cluster is sent to its cluster's chain, entered the same way
|
|
60
|
+
CLUSTER_PREFIX=KVBOX-C- # one chain per cluster: KVBOX-C-<cluster-key>, accepts what goes to a box of it
|
|
53
61
|
action="${1:-up}"
|
|
54
62
|
tap_op="${2:-}"; tap_name="${3:-}"; tap_ip="${4:-}"; tap_mac="${5:-}"; tap_mode="${6:-}"
|
|
55
63
|
px_op="${2:-}"; px_box="${3:-}"; px_ip="${4:-}"; px_hport="${5:-}"; px_gport="${6:-}"; px_proto="${7:-tcp}"
|
|
56
64
|
lk_op="${2:-}"; lk_box="${3:-}"; lk_from="${4:-}"; lk_to="${5:-}"; lk_port="${6:-}"
|
|
65
|
+
cl_op="${2:-}"; cl_box="${3:-}"; cl_ip="${4:-}"; cl_key="${5:-}"
|
|
57
66
|
fl_op="${2:-}"; fl_box="${3:-}"; fl_from="${4:-}"; fl_standin="${5:-}"; fl_port="${6:-}"; fl_via="${7:-}"
|
|
58
67
|
relay_op="${2:-}"; relay_conf="${3:-}"; relay_port="${4:-7777}"
|
|
59
68
|
RELAY_IF=kvbox # wg-quick@kvbox reads /etc/wireguard/kvbox.conf
|
|
@@ -77,10 +86,13 @@ case "$action" in
|
|
|
77
86
|
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
87
|
&& "$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
88
|
|| 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>" ;;
|
|
89
|
+
cluster) [[ "$cl_op" =~ ^(add|rm)$ && "$cl_box" =~ ^[a-z0-9][a-z0-9._-]{0,14}$ && "$cl_ip" =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ \
|
|
90
|
+
&& ( "$cl_op" == rm || "$cl_key" =~ ^[0-9a-f]{16}$ ) ]] \
|
|
91
|
+
|| fail "usage: $0 cluster add <box-id> <ip> <cluster-key> | cluster rm <box-id> <ip> (cluster key: 16 hex chars)" ;;
|
|
80
92
|
relay) if [[ ! "$relay_op" =~ ^(up|restart|down|status)$ ]] || { [[ "$relay_op" == up ]] && [[ -z "$relay_conf" ]]; } || [[ ! "$relay_port" =~ ^[0-9]{1,5}$ ]]; then
|
|
81
93
|
fail "usage: $0 relay up <conf> [port] | relay restart | relay down | relay status"
|
|
82
94
|
fi ;;
|
|
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]" ;;
|
|
95
|
+
*) fail "usage: $0 [up|down|status|tap add|attach|del <name> ...|proxy add|rm ...|link add|rm ...|farlink add|rm ...|cluster add|rm ...|relay up|restart|down|status]" ;;
|
|
84
96
|
esac
|
|
85
97
|
for bin in ip iptables sysctl; do command -v "$bin" >/dev/null 2>&1 || fail "$bin not found"; done
|
|
86
98
|
if [[ "$action" == tap ]]; then command -v tc >/dev/null 2>&1 || fail "tc not found (iproute2)"; fi
|
|
@@ -116,9 +128,16 @@ if [[ $EUID -ne 0 ]]; then
|
|
|
116
128
|
if [[ -t 0 ]]; then exec sudo --preserve-env=KVBOX_TAP_USER,KVBOX_BRIDGE,KVBOX_SUBNET,KVBOX_GATEWAY "$0" "$@"; fi
|
|
117
129
|
exec sudo -n --preserve-env=KVBOX_TAP_USER,KVBOX_BRIDGE,KVBOX_SUBNET,KVBOX_GATEWAY "$0" "$@"
|
|
118
130
|
fi
|
|
119
|
-
if [[ "$action" == proxy || "$action" == farlink ]]; then
|
|
131
|
+
if [[ "$action" == proxy || "$action" == farlink || "$action" == cluster ]]; then
|
|
120
132
|
command -v iptables-save >/dev/null 2>&1 || fail "iptables-save not found"
|
|
121
133
|
fi
|
|
134
|
+
# Boxes start and stop at once, each under its own lock in kvbox-server. A cluster's chain is made empty
|
|
135
|
+
# and filled a moment later, while another box's add or rm drops every empty one: one at a time (F1).
|
|
136
|
+
if [[ "$action" == cluster ]]; then
|
|
137
|
+
command -v flock >/dev/null 2>&1 || fail "flock not found (util-linux)"
|
|
138
|
+
exec 9>/run/kvbox-net-cluster.lock
|
|
139
|
+
flock 9
|
|
140
|
+
fi
|
|
122
141
|
|
|
123
142
|
# ── rule set ─────────────────────────────────────────────────────────────────
|
|
124
143
|
# RULE_DESC[i] is what we tell the user; RULE_SPEC[i] is "<table> <chain> <rule...>".
|
|
@@ -152,6 +171,7 @@ RULE_DESC+=(
|
|
|
152
171
|
"firewall: boxes may ask this host for names (only that)"
|
|
153
172
|
"firewall: a box may reach another box's port when they are linked (kvbox vm link)"
|
|
154
173
|
"firewall: a box linked to a box on another host may hand this host that connection (only that)"
|
|
174
|
+
"firewall: the boxes of one cluster may reach each other (kvbox k8s)"
|
|
155
175
|
)
|
|
156
176
|
RULE_SPEC+=(
|
|
157
177
|
"filter FORWARD -i $BRIDGE -p tcp --dport 25 -m comment --comment $TAG -j DROP"
|
|
@@ -163,6 +183,7 @@ RULE_SPEC+=(
|
|
|
163
183
|
"filter INPUT -i $BRIDGE -d $GATEWAY -p udp --dport $DNS_PORT -m comment --comment $TAG -j ACCEPT"
|
|
164
184
|
"filter FORWARD -i $BRIDGE -o $BRIDGE -m comment --comment $TAG -j $LINKS"
|
|
165
185
|
"filter INPUT -i $BRIDGE -p tcp -m conntrack --ctstate DNAT -m comment --comment $TAG -j ACCEPT"
|
|
186
|
+
"filter FORWARD -i $BRIDGE -o $BRIDGE -m comment --comment $TAG -j $CLUSTERS"
|
|
166
187
|
)
|
|
167
188
|
has_rule() { local t=$1 c=$2; shift 2; iptables -t "$t" -C "$c" "$@" 2>/dev/null; }
|
|
168
189
|
add_rule() { local t=$1 c=$2; shift 2; iptables -t "$t" -I "$c" 1 "$@"; }
|
|
@@ -186,8 +207,9 @@ do_up() {
|
|
|
186
207
|
if bridge_is_up; then skip "$BRIDGE is already up"; else ip link set "$BRIDGE" up; ok "brought $BRIDGE up"; fi
|
|
187
208
|
if [[ "$(sysctl -n net.ipv4.ip_forward)" == 1 ]]; then skip "IP forwarding already enabled"; else sysctl -q -w net.ipv4.ip_forward=1; ok "enabled IP forwarding (this host routes for the boxes)"; fi
|
|
188
209
|
if pvlan_on; then skip "box to box already goes through this host's firewall"; else sysctl -q -w "$pvlan_key=1"; ok "box to box now goes through this host's firewall (closed unless linked)"; fi
|
|
189
|
-
# The base rules jump to
|
|
210
|
+
# The base rules jump to them, so they must exist before the rules are checked.
|
|
190
211
|
iptables -t filter -N "$LINKS" 2>/dev/null || true
|
|
212
|
+
iptables -t filter -N "$CLUSTERS" 2>/dev/null || true
|
|
191
213
|
local i missing=0 present=()
|
|
192
214
|
for i in "${!RULE_SPEC[@]}"; do
|
|
193
215
|
# shellcheck disable=SC2086 # the rule is a word list by design
|
|
@@ -231,8 +253,10 @@ do_down() {
|
|
|
231
253
|
# shellcheck disable=SC2086 # iptables-save wrote the rule as a word list
|
|
232
254
|
eval "iptables -t $t -D $rule" && n=$((n + 1))
|
|
233
255
|
done < <(proxy_rules)
|
|
234
|
-
if [[ $n -gt 0 ]]; then ok "removed $n box port forward and
|
|
256
|
+
if [[ $n -gt 0 ]]; then ok "removed $n box port forward, link and cluster rule(s)"; else skip "no box port forwards, links or clusters"; fi
|
|
235
257
|
if iptables -t filter -X "$LINKS" 2>/dev/null; then ok "removed the links chain"; fi
|
|
258
|
+
drop_empty_clusters
|
|
259
|
+
if iptables -t filter -X "$CLUSTERS" 2>/dev/null; then ok "removed the clusters chain"; fi
|
|
236
260
|
}
|
|
237
261
|
|
|
238
262
|
# ── status ───────────────────────────────────────────────────────────────────
|
|
@@ -262,6 +286,8 @@ do_status() {
|
|
|
262
286
|
if [[ -z "$links" ]]; then skip "no links between boxes"; else ok "links between boxes:"; printf '%s\n' "$links"; fi
|
|
263
287
|
local fars; fars="$(far_summary)"
|
|
264
288
|
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
|
|
289
|
+
local clusters; clusters="$(cluster_summary)"
|
|
290
|
+
if [[ -z "$clusters" ]]; then skip "no clusters on this host"; else ok "clusters (their boxes reach each other on any port):"; printf '%s\n' "$clusters"; fi
|
|
265
291
|
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
|
|
266
292
|
return $missing
|
|
267
293
|
}
|
|
@@ -433,6 +459,71 @@ far_summary() {
|
|
|
433
459
|
| 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
460
|
}
|
|
435
461
|
|
|
462
|
+
# ── cluster add|rm ───────────────────────────────────────────────────────────
|
|
463
|
+
# A cluster (kvbox k8s): its boxes reach each other on any port and protocol, both ways, and stay strangers
|
|
464
|
+
# to every other box. FORWARD enters KVBOX-CLUSTERS for bridge-to-bridge traffic above the drops (a base
|
|
465
|
+
# rule, so `up` keeps the order). There a box of a cluster has one rule, by its address, that sends what it
|
|
466
|
+
# sends to its cluster's chain; that chain accepts what goes to a box of the cluster, one rule per box, and
|
|
467
|
+
# anything else comes back and meets the links and the drops as before. So two rules per box, whatever the
|
|
468
|
+
# size of the cluster, both tagged with the box's id.
|
|
469
|
+
cluster_jump="filter FORWARD -i $BRIDGE -o $BRIDGE -m comment --comment $TAG -j $CLUSTERS"
|
|
470
|
+
cluster_chain_re="${CLUSTER_PREFIX}[0-9a-f]{16}"
|
|
471
|
+
# The cluster rules of one box as iptables-save writes them (without -A), whatever cluster they name.
|
|
472
|
+
cluster_rules() {
|
|
473
|
+
iptables-save -t filter 2>/dev/null | sed -n 's/^-A //p' | sed 's/--comment "\([^"]*\)"/--comment \1/' \
|
|
474
|
+
| grep -E -- "^($CLUSTERS|$cluster_chain_re) " | grep -F -- "--comment kvbox:$1 " || true
|
|
475
|
+
}
|
|
476
|
+
# A cluster's chain goes with its last box (-X refuses one that still has a rule or is still jumped to).
|
|
477
|
+
drop_empty_clusters() {
|
|
478
|
+
local chain
|
|
479
|
+
while IFS= read -r chain; do
|
|
480
|
+
[[ -n "$chain" ]] || continue
|
|
481
|
+
iptables -t filter -X "$chain" 2>/dev/null || true
|
|
482
|
+
done < <(iptables-save -t filter 2>/dev/null | sed -n -E "s/^:($cluster_chain_re) .*/\1/p")
|
|
483
|
+
}
|
|
484
|
+
cluster_drop() {
|
|
485
|
+
local rule n=0
|
|
486
|
+
while IFS= read -r rule; do
|
|
487
|
+
[[ -n "$rule" ]] || continue
|
|
488
|
+
# shellcheck disable=SC2086 # iptables-save wrote the rule as a word list
|
|
489
|
+
eval "iptables -t filter -D $rule" && n=$((n + 1))
|
|
490
|
+
done < <(cluster_rules "$1")
|
|
491
|
+
drop_empty_clusters
|
|
492
|
+
[[ $n -gt 0 ]]
|
|
493
|
+
}
|
|
494
|
+
do_cluster_add() {
|
|
495
|
+
local box=$1 ip=$2 chain="$CLUSTER_PREFIX$3"
|
|
496
|
+
# shellcheck disable=SC2086 # the rule is a word list by design
|
|
497
|
+
if ! pvlan_on || ! has_rule $cluster_jump; then fail "this host's network predates clusters; run: kvbox-server net up"; fi
|
|
498
|
+
# A cluster trusts the source IP: refuse while any box could still send as another (a tap from before the pin).
|
|
499
|
+
local tap
|
|
500
|
+
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
|
|
501
|
+
local enter="filter $CLUSTERS -s $ip -m comment --comment kvbox:$box -j $chain"
|
|
502
|
+
local accept="filter $chain -d $ip -m comment --comment kvbox:$box -j ACCEPT"
|
|
503
|
+
# shellcheck disable=SC2086
|
|
504
|
+
if has_rule $enter && has_rule $accept; then skip "box $ip is already in its cluster"; return; fi
|
|
505
|
+
cluster_drop "$box" || true # half of it, another cluster's, or the box's rules from before it changed address
|
|
506
|
+
iptables -t filter -N "$chain" 2>/dev/null || true
|
|
507
|
+
# shellcheck disable=SC2086
|
|
508
|
+
add_rule $accept
|
|
509
|
+
# shellcheck disable=SC2086
|
|
510
|
+
add_rule $enter
|
|
511
|
+
ok "box $ip and the other boxes of its cluster may now reach each other on any port"
|
|
512
|
+
}
|
|
513
|
+
do_cluster_rm() {
|
|
514
|
+
if cluster_drop "$1"; then ok "box $2 is out of its cluster: its boxes no longer reach it"; else skip "box $2 was in no cluster"; fi
|
|
515
|
+
# Open connections outlive the rules otherwise: the ones between this box and the other boxes.
|
|
516
|
+
if command -v conntrack >/dev/null 2>&1; then
|
|
517
|
+
conntrack -D -s "$2" -d "$SUBNET" >/dev/null 2>&1 || true
|
|
518
|
+
conntrack -D -s "$SUBNET" -d "$2" >/dev/null 2>&1 || true
|
|
519
|
+
fi
|
|
520
|
+
}
|
|
521
|
+
# One line per cluster: "<cluster-key>: 172.30.0.5 172.30.0.6".
|
|
522
|
+
cluster_summary() {
|
|
523
|
+
iptables-save -t filter 2>/dev/null | sed -n -E "s/^-A $CLUSTER_PREFIX([0-9a-f]{16}) -d ([0-9.]+)(\/32)? .*/\1 \2/p" \
|
|
524
|
+
| awk '{ m[$1] = m[$1] " " $2 } END { for (k in m) print " " k ":" m[k] }' | sort || true
|
|
525
|
+
}
|
|
526
|
+
|
|
436
527
|
# ── relay up|down|status ─────────────────────────────────────────────────────
|
|
437
528
|
# The host's end of the tunnel to its relay (#50). kvbox-server writes the wg-quick conf (private
|
|
438
529
|
# key inside) under its state dir; root copies it to /etc/wireguard and lets systemd keep the tunnel
|
|
@@ -531,6 +622,10 @@ case "$action" in
|
|
|
531
622
|
add) do_farlink_add "$fl_box" "$fl_from" "$fl_standin" "$fl_port" "$fl_via" ;;
|
|
532
623
|
rm) do_farlink_rm "$fl_box" "$fl_from" "$fl_standin" "$fl_port" ;;
|
|
533
624
|
esac ;;
|
|
625
|
+
cluster) case "$cl_op" in
|
|
626
|
+
add) do_cluster_add "$cl_box" "$cl_ip" "$cl_key" ;;
|
|
627
|
+
rm) do_cluster_rm "$cl_box" "$cl_ip" ;;
|
|
628
|
+
esac ;;
|
|
534
629
|
relay) case "$relay_op" in
|
|
535
630
|
up) do_relay_up "$relay_conf" "$relay_port" ;;
|
|
536
631
|
restart) do_relay_restart ;;
|