#!/usr/bin/env python3
# What do websites see? Asks am.i.mullvad.net (Python, not curl, so split
# tunnelling of curl does not affect it). A non-Mullvad location is never printed.
import json, urllib.request
import time
for attempt in range(4):  # the first request right after a tunnel change can fail
    try:
        j = json.load(urllib.request.urlopen("https://am.i.mullvad.net/json", timeout=8))
        break
    except Exception as e:
        err = type(e).__name__
        time.sleep(2)
else:
    print("no answer", err); raise SystemExit
if j.get("mullvad_exit_ip"):
    print(f"through Mullvad     yes, exit {j.get('mullvad_exit_ip_hostname')}")
    print(f"websites see        {j.get('country')}, {j.get('city')}")
else:
    print("through Mullvad     no")
    print("websites see        [hidden, my real location]")
