#!/bin/bash

while true; do
SOCK_PORT=$((9000+$1))
CONTROL_PORT=$((9100+$1))
LOCAL_PORT=$((9200+$1))

echo "tor daemon serv"
/usr/local/bin/tor2 \
  --hush \
  --UseEntryGuards 0 \
  --SocksPort 0 \
  --ControlPort $CONTROL_PORT \
  --DataDirectory /tmp/tor$CONTROL_PORT &

echo "tor client serv"
/usr/local/bin/tor2 \
  --hush \
  --UseEntryGuards 0 \
  --SocksPort "127.0.0.1:$SOCK_PORT IsolateDestPort IsolateDestAddr IsolateClientAddr" \
  --DataDirectory /tmp/tor$SOCK_PORT &

sleep 5

echo "measlat serv"
rm -f os_${LOCAL_PORT}.{pub,priv}
/usr/local/bin/measlat \
  -l \
  -t tor \
  -q $CONTROL_PORT \
  -p $LOCAL_PORT \
  -c 135000 \
  -i 40 \
  -s 172 \
  -m torhsopt &

until [ -f os_${LOCAL_PORT}.pub ]; do sleep 1; done

echo "measlat cli"
/usr/local/bin/measlat \
  -t tor \
  -q $SOCK_PORT \
  -h $(cat os_${LOCAL_PORT}.pub).onion \
  -p $LOCAL_PORT \
  -c 135000 \
  -i 40 \
  -s 172 \
  -m torhsopt

kill -9 $(jobs -p)

sleep 5
done
