package com.tencent.mm.plugin.appbrand.debugger;

import android.util.Log;
import com.tencent.luggage.g.h;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.plugin.appbrand.l.j;
import com.tencent.mm.plugin.appbrand.l.k.b;
import com.tencent.mm.plugin.appbrand.t.a.a;
import com.tencent.mm.plugin.appbrand.t.b.d;
import com.tencent.mm.protocal.protobuf.cqs;
import com.tencent.mm.sdk.platformtools.ab;
import java.net.Proxy;
import java.net.Socket;
import java.net.URI;
import java.util.HashMap;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import org.xwalk.core.XWalkFeature;

public final class s {
    a hkv;
    private SSLSocketFactory hkw;

    public s(com.tencent.mm.plugin.appbrand.l.a aVar) {
        AppMethodBeat.i(101902);
        SSLContext a = j.a(aVar);
        if (a != null) {
            this.hkw = a.getSocketFactory();
        }
        AppMethodBeat.o(101902);
    }

    public final void a(String str, b bVar) {
        AppMethodBeat.i(101903);
        ab.i("MicroMsg.RemoteDebugSocket", "connectSocket url is %s", new Object[]{str});
        try {
            URI uri = new URI(str);
            HashMap hashMap = new HashMap();
            if (str.startsWith("ws://localhost:")) {
                hashMap.put("Sec-WebSocket-Protocol", "client");
            }
            try {
                this.hkv = new 1(this, uri, new d(), hashMap, bVar);
                if (h.t(str, "ws://")) {
                    this.hkv.a(new Socket(Proxy.NO_PROXY));
                    this.hkv.connect();
                    AppMethodBeat.o(101903);
                    return;
                }
                SSLSocketFactory sSLSocketFactory;
                if (this.hkw != null) {
                    sSLSocketFactory = this.hkw;
                } else {
                    sSLSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
                }
                this.hkv.a(sSLSocketFactory.createSocket());
                this.hkv.connect();
                AppMethodBeat.o(101903);
            } catch (Exception e) {
                ab.e("MicroMsg.RemoteDebugSocket", "onSocketMessage %s", new Object[]{Log.getStackTraceString(e)});
                AppMethodBeat.o(101903);
            }
        } catch (Exception e2) {
            ab.e("MicroMsg.RemoteDebugSocket", "connect fail : %s ", new Object[]{e2.toString()});
            bVar.zK("url not well format");
            AppMethodBeat.o(101903);
        }
    }

    public final boolean isOpen() {
        AppMethodBeat.i(101904);
        if (this.hkv == null) {
            AppMethodBeat.o(101904);
            return false;
        }
        boolean isOpen = this.hkv.iSO.isOpen();
        AppMethodBeat.o(101904);
        return isOpen;
    }

    public final boolean a(cqs cqs) {
        AppMethodBeat.i(101905);
        ab.d("MicroMsg.RemoteDebugSocket", "sendSocketMsg");
        if (!isOpen()) {
            ab.w("MicroMsg.RemoteDebugSocket", "sendSocketMsg fail, not open");
            AppMethodBeat.o(101905);
            return false;
        } else if (cqs == null) {
            ab.w("MicroMsg.RemoteDebugSocket", "sendSocketMsg fail");
            AppMethodBeat.o(101905);
            return false;
        } else {
            this.hkv.p(t.c(cqs));
            AppMethodBeat.o(101905);
            return true;
        }
    }

    public final boolean zN(String str) {
        AppMethodBeat.i(101906);
        if (this.hkv == null) {
            ab.w("MicroMsg.RemoteDebugSocket", "client is null");
            AppMethodBeat.o(101906);
            return false;
        }
        this.hkv.ah(XWalkFeature.INTERFACE_GEO_LOCATION_PERMISSION, str);
        ab.d("MicroMsg.RemoteDebugSocket", "closeSocket code %d, reason %s", new Object[]{Integer.valueOf(XWalkFeature.INTERFACE_GEO_LOCATION_PERMISSION), str});
        AppMethodBeat.o(101906);
        return true;
    }
}
