fix traffic gen keypress interrupt
Some checks failed
Build and Deploy MITM Webserver / build (push) Failing after 2s

This commit is contained in:
2026-03-06 20:31:32 +01:00
parent 2aca2760df
commit 991f26e5f2

View File

@@ -101,6 +101,27 @@ cleanup() {
fi fi
} }
on_interrupt() {
STOP=1
}
run_interruptible() {
"$@" &
local pid=$!
while kill -0 "$pid" >/dev/null 2>&1; do
if [[ "$STOP" -eq 1 ]]; then
kill "$pid" >/dev/null 2>&1 || true
wait "$pid" >/dev/null 2>&1 || true
return 130
fi
sleep 0.1
done
wait "$pid" >/dev/null 2>&1 || true
return 0
}
parse_args() { parse_args() {
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case "$1" in case "$1" in
@@ -190,7 +211,7 @@ run_arp() {
has_cmd arping || return 0 has_cmd arping || return 0
for _ in $(seq 1 "$REQUESTS"); do for _ in $(seq 1 "$REQUESTS"); do
[[ "$STOP" -eq 1 ]] && return [[ "$STOP" -eq 1 ]] && return
arping -I "$IFACE" -c 1 "$TARGET" >/dev/null 2>&1 || true run_interruptible arping -I "$IFACE" -c 1 "$TARGET" || true
maybe_wait maybe_wait
done done
} }
@@ -199,7 +220,7 @@ run_icmp() {
has_cmd ping || return 0 has_cmd ping || return 0
for _ in $(seq 1 "$REQUESTS"); do for _ in $(seq 1 "$REQUESTS"); do
[[ "$STOP" -eq 1 ]] && return [[ "$STOP" -eq 1 ]] && return
ping -c 1 -W 1 "$TARGET" >/dev/null 2>&1 || true run_interruptible ping -c 1 -W 1 "$TARGET" || true
maybe_wait maybe_wait
done done
} }
@@ -208,7 +229,7 @@ run_http() {
has_cmd curl || return 0 has_cmd curl || return 0
for _ in $(seq 1 "$REQUESTS"); do for _ in $(seq 1 "$REQUESTS"); do
[[ "$STOP" -eq 1 ]] && return [[ "$STOP" -eq 1 ]] && return
curl -s "http://${TARGET}:${HTTP_PORT}/" >/dev/null 2>&1 || true run_interruptible curl -s "http://${TARGET}:${HTTP_PORT}/" || true
maybe_wait maybe_wait
done done
} }
@@ -217,7 +238,7 @@ run_https() {
has_cmd curl || return 0 has_cmd curl || return 0
for _ in $(seq 1 "$REQUESTS"); do for _ in $(seq 1 "$REQUESTS"); do
[[ "$STOP" -eq 1 ]] && return [[ "$STOP" -eq 1 ]] && return
curl -sk "https://${TARGET}:${HTTPS_PORT}/" >/dev/null 2>&1 || true run_interruptible curl -sk "https://${TARGET}:${HTTPS_PORT}/" || true
maybe_wait maybe_wait
done done
} }
@@ -226,7 +247,7 @@ run_dns() {
if has_cmd dig; then if has_cmd dig; then
for _ in $(seq 1 "$REQUESTS"); do for _ in $(seq 1 "$REQUESTS"); do
[[ "$STOP" -eq 1 ]] && return [[ "$STOP" -eq 1 ]] && return
dig @"$TARGET" -p "$DNS_PORT" example.com A +tries=1 +time=1 +short >/dev/null 2>&1 || true run_interruptible dig @"$TARGET" -p "$DNS_PORT" example.com A +tries=1 +time=1 +short || true
maybe_wait maybe_wait
done done
fi fi
@@ -236,7 +257,7 @@ run_tcp_echo() {
has_cmd nc || return 0 has_cmd nc || return 0
for i in $(seq 1 "$REQUESTS"); do for i in $(seq 1 "$REQUESTS"); do
[[ "$STOP" -eq 1 ]] && return [[ "$STOP" -eq 1 ]] && return
printf 'tcp-echo-%s\n' "$i" | nc -w1 "$TARGET" "$TCP_ECHO_PORT" >/dev/null 2>&1 || true printf 'tcp-echo-%s\n' "$i" | run_interruptible nc -w1 "$TARGET" "$TCP_ECHO_PORT" || true
maybe_wait maybe_wait
done done
} }
@@ -245,26 +266,26 @@ run_udp_echo() {
has_cmd nc || return 0 has_cmd nc || return 0
for i in $(seq 1 "$REQUESTS"); do for i in $(seq 1 "$REQUESTS"); do
[[ "$STOP" -eq 1 ]] && return [[ "$STOP" -eq 1 ]] && return
printf 'udp-echo-%s\n' "$i" | nc -u -w1 "$TARGET" "$UDP_ECHO_PORT" >/dev/null 2>&1 || true printf 'udp-echo-%s\n' "$i" | run_interruptible nc -u -w1 "$TARGET" "$UDP_ECHO_PORT" || true
maybe_wait maybe_wait
done done
} }
run_tcp_syn() { run_tcp_syn() {
if has_cmd nping; then if has_cmd nping; then
nping --interface "$IFACE" --tcp -p "$TCP_ECHO_PORT" --flags syn -c "$REQUESTS" "$TARGET" >/dev/null 2>&1 || true run_interruptible nping --interface "$IFACE" --tcp -p "$TCP_ECHO_PORT" --flags syn -c "$REQUESTS" "$TARGET" || true
fi fi
} }
run_tcp_flags() { run_tcp_flags() {
if has_cmd nping; then if has_cmd nping; then
nping --interface "$IFACE" --tcp -p "$TCP_ECHO_PORT" --flags syn,ack,fin,rst,psh,urg -c "$REQUESTS" "$TARGET" >/dev/null 2>&1 || true run_interruptible nping --interface "$IFACE" --tcp -p "$TCP_ECHO_PORT" --flags syn,ack,fin,rst,psh,urg -c "$REQUESTS" "$TARGET" || true
fi fi
} }
run_udp_raw() { run_udp_raw() {
if has_cmd nping; then if has_cmd nping; then
nping --interface "$IFACE" --udp -p "$UDP_ECHO_PORT" --data-length 256 -c "$REQUESTS" "$TARGET" >/dev/null 2>&1 || true run_interruptible nping --interface "$IFACE" --udp -p "$UDP_ECHO_PORT" --data-length 256 -c "$REQUESTS" "$TARGET" || true
fi fi
} }
@@ -272,19 +293,19 @@ run_traceroute() {
has_cmd traceroute || return 0 has_cmd traceroute || return 0
for _ in $(seq 1 "$REQUESTS"); do for _ in $(seq 1 "$REQUESTS"); do
[[ "$STOP" -eq 1 ]] && return [[ "$STOP" -eq 1 ]] && return
traceroute -n -m 5 "$TARGET" >/dev/null 2>&1 || true run_interruptible traceroute -n -m 5 "$TARGET" || true
maybe_wait maybe_wait
done done
} }
run_iperf_tcp() { run_iperf_tcp() {
has_cmd iperf3 || return 0 has_cmd iperf3 || return 0
iperf3 -c "$TARGET" -p "$IPERF_TCP_PORT" -t 10 >/dev/null 2>&1 || true run_interruptible iperf3 -c "$TARGET" -p "$IPERF_TCP_PORT" -t 10 || true
} }
run_iperf_udp() { run_iperf_udp() {
has_cmd iperf3 || return 0 has_cmd iperf3 || return 0
iperf3 -c "$TARGET" -p "$IPERF_UDP_PORT" -u -b 100M -t 10 >/dev/null 2>&1 || true run_interruptible iperf3 -c "$TARGET" -p "$IPERF_UDP_PORT" -u -b 100M -t 10 || true
} }
run_port_probe() { run_port_probe() {
@@ -292,7 +313,7 @@ run_port_probe() {
has_cmd nc || return 0 has_cmd nc || return 0
for _ in $(seq 1 "$REQUESTS"); do for _ in $(seq 1 "$REQUESTS"); do
[[ "$STOP" -eq 1 ]] && return [[ "$STOP" -eq 1 ]] && return
nc -z -w1 "$TARGET" "$port" >/dev/null 2>&1 || true run_interruptible nc -z -w1 "$TARGET" "$port" || true
maybe_wait maybe_wait
done done
} }
@@ -304,7 +325,7 @@ run_ntp_probe() {
has_cmd nc || return 0 has_cmd nc || return 0
for _ in $(seq 1 "$REQUESTS"); do for _ in $(seq 1 "$REQUESTS"); do
[[ "$STOP" -eq 1 ]] && return [[ "$STOP" -eq 1 ]] && return
printf 'ntp?\n' | nc -u -w1 "$TARGET" 123 >/dev/null 2>&1 || true printf 'ntp?\n' | run_interruptible nc -u -w1 "$TARGET" 123 || true
maybe_wait maybe_wait
done done
} }
@@ -312,7 +333,7 @@ run_mdns_query() {
if has_cmd dig; then if has_cmd dig; then
for _ in $(seq 1 "$REQUESTS"); do for _ in $(seq 1 "$REQUESTS"); do
[[ "$STOP" -eq 1 ]] && return [[ "$STOP" -eq 1 ]] && return
dig @224.0.0.251 -p 5353 _services._dns-sd._udp.local PTR +tries=1 +time=1 +short >/dev/null 2>&1 || true run_interruptible dig @224.0.0.251 -p 5353 _services._dns-sd._udp.local PTR +tries=1 +time=1 +short || true
maybe_wait maybe_wait
done done
fi fi
@@ -357,6 +378,7 @@ main() {
fi fi
start_keypress_listener start_keypress_listener
trap on_interrupt INT TERM USR1
trap cleanup EXIT trap cleanup EXIT
log "Target=${TARGET}, iface=${IFACE}, requests=${REQUESTS}, protocols=${PROTOCOLS_CSV}, stop_on_keypress=${STOP_ON_KEYPRESS}" log "Target=${TARGET}, iface=${IFACE}, requests=${REQUESTS}, protocols=${PROTOCOLS_CSV}, stop_on_keypress=${STOP_ON_KEYPRESS}"