#!/usr/bin/env python3
import frida, time, subprocess
ADB="/opt/android-sdk/platform-tools/adb"
JS=r"""
var CN=null,GC=null;
function cn(o){try{if(o.isNull())return "NULL";return CN(GC(o)).readUtf8String();}catch(e){return "?";}}
function rs(p){try{if(p==0||p.isNull())return null;var l=p.add(8).readS32();if(l<0||l>200)return "<l>";return p.add(0xc).readUtf16String(l);}catch(e){return "<e>";}}
function desc(p){if(p.isNull())return "null";var c=cn(p);var v="";if(c=="String")v="="+JSON.stringify(rs(p));else if(c=="Int64"||c=="Int32"){try{v="="+p.add(8).readS32();}catch(e){}}return c+v;}
function dumplist(p,label){try{var items=p.add(8).readPointer();var cnt=p.add(0xc).readS32();var out=[];for(var i=0;i<cnt&&i<12;i++){var el=items.add(0x10+i*4).readPointer();out.push("["+i+"]"+desc(el));}send({g:label+" List cnt="+cnt+": "+out.join(" ")});}catch(e){send({g:label+" err "+e});}}
function ins(){var m=Process.findModuleByName("libil2cpp.so");if(!m)return false;var b=m.base;
 function T(f){try{f();}catch(e){send({w:""+e});}}
 GC=new NativeFunction(b.add(0xbc7128),'pointer',['pointer']);
 CN=new NativeFunction(b.add(0xbc61fc),'pointer',['pointer']);
 var dep=0;
 T(function(){Interceptor.attach(b.add(0xd34fb9),{onEnter:function(a){dep++;var recv=a[2];send({g:"== DecodeResponse recv cls="+cn(recv)});dumplist(recv,"recv");},onLeave:function(){dep--;}});});
 // GET6 first call returns recv[1] into out; hook 0xd35068 area? just hook GET6 and log idx+result kind
 T(function(){Interceptor.attach(b.add(0x13a1ff6),{onEnter:function(a){this.o=a[0];this.i=a[2].toInt32();},onLeave:function(){if(dep<=0)return;try{var node=this.o.add(4).readPointer();send({g:"  GET6 idx="+this.i+" -> "+(node.isNull()?"null":desc(node))});}catch(e){}}});});
 [[0xd35228,"cmpErrCode"],[0xd35308,"cmpRes"],[0xd353f8,"cmpServerAssert"]].forEach(function(x){
   T(function(){Interceptor.attach(b.add(x[0]),{onEnter:function(a){if(dep>0)send({g:"  "+x[1]+" myKey="+JSON.stringify(rs(a[0]))});}});});});
 send({g:"__ready"});return true;}
if(!ins()){var iv=setInterval(function(){if(ins())clearInterval(iv);},120);}
"""
subprocess.run([ADB,"shell","am","force-stop","se.foglo.svt"]); time.sleep(3)
subprocess.run([ADB,"shell","monkey","-p","se.foglo.svt","-c","android.intent.category.LAUNCHER","1"],stdout=-3,stderr=-3)
time.sleep(43)
subprocess.run([ADB,"shell","input","tap","700","730"]); time.sleep(3)
subprocess.run([ADB,"shell","input","tap","700","730"]); time.sleep(4)
dev=frida.get_usb_device(timeout=10)
pid=int(subprocess.run([ADB,"shell","pidof","se.foglo.svt"],capture_output=True,text=True).stdout.split()[0])
s=dev.attach(pid); sc=s.create_script(JS)
sc.on("message",lambda m,d: print(m.get("payload",m),flush=True)); sc.load(); time.sleep(1)
subprocess.run([ADB,"shell","input","tap","1355","970"]); time.sleep(3)
subprocess.run([ADB,"shell","input","tap","1355","970"]); time.sleep(14)
print("=== done ===",flush=True)
