import ipaddress
import json
import os
import requests
from qg_client import connection

def check():
    with requests.Session() as s:
        s.trust_env = False
        r = s.get('https://ip.quotaguard.com',
                  proxies={'http': connection(), 'https': connection()},
                  timeout=(10, 30), allow_redirects=False)
        r.raise_for_status()
        return r.json()['ip']

if __name__ == '__main__':
    try:
        observed = str(ipaddress.IPv4Address(check()))
        pair = [str(ipaddress.IPv4Address(x.strip()))
                for x in os.environ.get('QG_EXPECTED_IPS', '').split(',')]
        if len(pair) != 2 or len(set(pair)) != 2 or observed not in pair:
            raise ValueError('Dashboard pair mismatch')
        print(json.dumps({'client': 'requests', 'matches_dashboard_pair': True}))
    except Exception:
        raise SystemExit('Requests proxy check failed') from None
