summarylogtreecommitdiffstats
path: root/server.sh
blob: e272cbbcceb874e6a73c60ddca70815620b2f353 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env sh
tmpdir=$(mktemp -d)

mkfifo "${tmpdir}/response"

handle() {
  while read line; do
    trline=$(echo "$line" | tr -d '[\r\n]')
    [ -z "$trline" ] && break

    echo $trline
    MAC_REGEX='.*\s.*mac=(.{17})\sHTTP.*'
    [[ "$trline" =~ $MAC_REGEX ]] &&
      MAC=$(echo $trline | sed -E "s/$MAC_REGEX/\1/")
  done

  domain=$(for name in $(virsh list --name); do virsh domiflist "${name}" | grep -i -q "${MAC}" && echo "${name}" && break; done)
  echo "${domain}"
  ([[ -z "${MAC}" ]] || [[ -z "${domain}" ]]) &&
    domain="-"

  RESPONSE="$(cat <<EOF
HTTP/1.1 200
Content-Type: text/plain

${domain}
EOF
)"

  echo -e "$RESPONSE" > "${tmpdir}/response"
}

while true; do
  cat "${tmpdir}/response" | nc -lN "${PORT:-3000}" | handle
done