#!/bin/bash
# kuantum_olc on|off N
# Set quantum-resistant key exchange, then N times: disconnect, connect and
# time it. Packets to and from the relay during the connect are counted.
s="$1"; n="${2:-5}"; R="$HOME/Projects/csint-mullvad-test/raw"
mullvad tunnel set quantum-resistant "$s" >/dev/null
for i in $(seq 1 "$n"); do
  mullvad disconnect -w >/dev/null 2>&1; sleep 1
  P="$R/kuantum_${s}_$i.pcap"
  tcpdump -n -i en0 -s 128 -w "$P" host 146.70.188.130 2>/dev/null & tp=$!
  sleep 0.5; t0=$(now); mullvad connect -w >/dev/null 2>&1; t1=$(now); sleep 0.3
  kill $tp; wait $tp 2>/dev/null
  read pk by < <(tshark -r "$P" -Y "frame.time_epoch >= $t0 && frame.time_epoch <= $t1" -T fields -e frame.len 2>/dev/null | awk '{n++; b+=$1} END {print n+0, b+0}')
  printf 'quantum %-3s try %d   %.2f s   %3d packets  %5.1f KB\n' "$s" "$i" "$(echo "$t1 - $t0" | bc)" "$pk" "$(echo "$by / 1024" | bc -l)"
  echo "$s $i $t0 $t1 $pk $by" >> "$R/kuantum.log"
done
