/*
 * Simple URL connection logger.
 * Hooks at the lowest level to catch ALL HTTP traffic regardless of OkHttp version.
 */
Java.perform(function() {
    console.log("[*] Simple HTTP hook starting...");

    // Hook HttpURLConnection
    try {
        var HttpURLConnection = Java.use("java.net.HttpURLConnection");
        HttpURLConnection.getInputStream.implementation = function() {
            var url = this.getURL().toString();
            var code = this.getResponseCode();
            console.log("[HTTP] " + code + " " + url);
            return this.getInputStream();
        };
        console.log("[+] HttpURLConnection.getInputStream hooked");
    } catch(e) {
        console.log("[-] HttpURLConnection hook failed: " + e);
    }

    // Hook OkHttp Response - try multiple class paths
    var okHttpClasses = [
        "okhttp3.internal.http.RealInterceptorChain",
        "okhttp3.internal.connection.RealInterceptorChain",
        "okhttp3.RealCall"
    ];

    for (var i = 0; i < okHttpClasses.length; i++) {
        try {
            var cls = Java.use(okHttpClasses[i]);
            var methods = cls.class.getDeclaredMethods();
            console.log("[*] Found " + okHttpClasses[i] + " with " + methods.length + " methods:");
            for (var j = 0; j < methods.length; j++) {
                console.log("    " + methods[j].getName());
            }
        } catch(e) {
            console.log("[-] " + okHttpClasses[i] + ": not found");
        }
    }

    // Hook OkHttp Response.body() to see what responses come back
    try {
        var Response = Java.use("okhttp3.Response");
        Response.body.implementation = function() {
            var body = this.body();
            try {
                var url = this.request().url().toString();
                var code = this.code();
                console.log("[OkHttp] " + code + " " + url);

                if (body !== null && url.indexOf("spirit.com") !== -1 ||
                    url.indexOf("delta.com") !== -1 || url.indexOf("united.com") !== -1) {

                    // Try to peek at the body
                    try {
                        var source = body.source();
                        source.request(java_lang_Long.MAX_VALUE.value);
                        var buffer = source.getBuffer().clone();
                        var bodyStr = buffer.readUtf8();

                        if (bodyStr.length > 0) {
                            console.log("[BODY] (" + bodyStr.length + " chars) " + bodyStr.substring(0, Math.min(2000, bodyStr.length)));

                            send({
                                type: "RESPONSE",
                                url: url,
                                status: code,
                                body: bodyStr
                            });
                        }
                    } catch(be) {
                        console.log("[BODY-ERR] " + be);
                    }
                }
            } catch(e) {
                // ignore
            }
            return body;
        };
        console.log("[+] OkHttp Response.body() hooked");
    } catch(e) {
        console.log("[-] OkHttp Response hook failed: " + e);
    }

    var java_lang_Long = Java.use("java.lang.Long");

    // Also hook URL.openConnection for non-OkHttp traffic
    try {
        var URL = Java.use("java.net.URL");
        URL.openConnection.overload().implementation = function() {
            console.log("[URL] " + this.toString());
            return this.openConnection();
        };
        console.log("[+] URL.openConnection hooked");
    } catch(e) {
        console.log("[-] URL hook failed: " + e);
    }

    console.log("[*] Simple HTTP hook ready.");
});
