package com.bytedance.bdlocation.traceroute.service;

import X.8e1;
import X.8e2;
import X.8eG;
import X.8eL;
import X.8q8;
import X.C0522K7d;
import X.C0523K7e;
import X.C0524K7f;
import X.C0525K7g;
import X.C0526K7h;
import X.C0527K7i;
import X.C0528K7j;
import X.C0529K7k;
import X.C0530K7l;
import android.content.Context;
import android.text.TextUtils;
import com.bytedance.bdlocation.traceroute.TraceRoute;
import com.bytedance.bdlocation.traceroute.data.HopData;
import com.bytedance.bdlocation.traceroute.data.HopInfo;
import com.bytedance.bdlocation.traceroute.data.TraceEntity;
import com.bytedance.bdlocation.traceroute.data.TracerouteInfo;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import org.json.JSONObject;

public class TraceRouteService {
    public static Object object = new Object();
    public List<HopData> hopDataList = new ArrayList();
    public List<HopInfo> hopInfoList;
    public Context mContext;
    public TracerouteInfo tracerouteInfo;
    public List<TracerouteInfo> tracerouteInfoList;

    static {
        Covode.recordClassIndex(24355);
    }

    public static ExecutorService INVOKESTATIC_com_bytedance_bdlocation_traceroute_service_TraceRouteService_com_ss_android_ugc_aweme_lancet_thread_ThreadPoolLancet_newSingleThreadExecutor() {
        8e1 LIZ = 8e2.LIZ(8eG.FIXED);
        LIZ.LIZJ = 1;
        return 8eL.LIZ(LIZ.LIZ());
    }

    public void resetHopDataList() {
        List<HopData> list = this.hopDataList;
        if (list != null && list.size() > 1) {
            for (int i = 0; i < this.hopDataList.size() - 1; i++) {
                for (int size = this.hopDataList.size() - 1; size > i; size--) {
                    if (this.hopDataList.get(size).getIp().equals(this.hopDataList.get(i).getIp())) {
                        this.hopDataList.get(i).getCost().add(this.hopDataList.get(size).getCost().get(0));
                        this.hopDataList.remove(size);
                    }
                }
            }
        }
    }

    private void startTraceRoute(C0526K7h k7h) {
        execute(this.mContext, k7h.LJFF, k7h.LIZLLL, k7h.LJ);
    }

    private void executeTraceRouteInterval(C0526K7h k7h) {
        if (k7h == null) {
            try {
                C0525K7g.LJ();
            } catch (Exception unused) {
                C0525K7g.LJ();
            }
        } else {
            boolean z = k7h.LIZ;
            "traceroute server enable is:".concat(String.valueOf(z));
            C0525K7g.LJ();
            if (z) {
                int i = k7h.LIZJ;
                long j = C0523K7e.LIZ(this.mContext).LIZ.getLong("trace_router_interval", 0);
                C0525K7g.LJ();
                if ((System.currentTimeMillis() / 1000) - j < ((long) i)) {
                    C0525K7g.LJ();
                    return;
                }
                double d = k7h.LIZIZ;
                if (d >= 1.0d) {
                    startTraceRoute(k7h);
                    return;
                }
                C0525K7g.LJ();
                if (((double) (C0529K7k.LIZ.nextInt(99999) + 1)) <= d * 100000.0d) {
                    startTraceRoute(k7h);
                }
            }
        }
    }

    private void initConfig(JSONObject jSONObject) {
        boolean optBoolean = jSONObject.optBoolean("is_debug", false);
        if (optBoolean) {
            C0525K7g.LIZ.LIZ = true;
        }
        C0524K7f.LIZIZ = optBoolean;
        String optString = jSONObject.optString("base_url");
        if (TextUtils.isEmpty(optString) || optString.contains("https") || C0524K7f.LIZIZ) {
            C0524K7f.LIZ = optString;
            C0524K7f.LIZJ = jSONObject.optBoolean("is_privacy_confirmed");
            C0524K7f.LIZLLL = jSONObject.optBoolean("is_inner");
            C0524K7f.LJ = jSONObject.optBoolean("x-use-boe");
            return;
        }
        throw new IllegalArgumentException("need fix host is https");
    }

    public void executeTraceRoute(Context context, String str, JSONObject jSONObject) {
        this.mContext = context;
        try {
            initConfig(jSONObject);
            executeTraceRouteInterval((C0526K7h) new Gson().LIZ(str, C0526K7h.class));
        } catch (Exception e) {
            e.getMessage();
            C0525K7g.LJ();
        }
    }

    public void execute(Context context, ArrayList<String> arrayList, int i, int i2) {
        final ArrayList<String> arrayList2 = arrayList;
        final Context context2 = context;
        final int i3 = i2;
        final int i4 = i;
        INVOKESTATIC_com_bytedance_bdlocation_traceroute_service_TraceRouteService_com_ss_android_ugc_aweme_lancet_thread_ThreadPoolLancet_newSingleThreadExecutor().execute(new Runnable() {
            static {
                Covode.recordClassIndex(24356);
            }

            public final void run() {
                C0527K7i k7i;
                if (i4 == 0 || i3 == 0) {
                    C0525K7g.LJ();
                    return;
                }
                ArrayList arrayList = arrayList2;
                if (arrayList == null || arrayList.size() <= 0) {
                    C0525K7g.LJ();
                    return;
                }
                try {
                    TraceEntity traceEntity = new TraceEntity();
                    TraceRouteService.this.tracerouteInfoList = new ArrayList();
                    traceEntity.setTracerouteInfo(TraceRouteService.this.tracerouteInfoList);
                    Iterator it = arrayList2.iterator();
                    while (it.hasNext()) {
                        TraceRouteService.this.doTraceRoute(context2, (String) it.next(), i4, i3);
                    }
                    String LIZIZ2 = new Gson().LIZIZ(traceEntity);
                    Context context = TraceRouteService.this.mContext;
                    if (!C0524K7f.LIZJ) {
                        C0525K7g.LIZ();
                    } else if (LIZIZ2 == null) {
                        C0525K7g.LIZ();
                    } else if (C0524K7f.LIZLLL) {
                        "upload request".concat(String.valueOf(LIZIZ2));
                        C0525K7g.LIZLLL();
                        LinkedHashMap linkedHashMap = new LinkedHashMap();
                        linkedHashMap.put("report", C0522K7d.LIZIZ(LIZIZ2));
                        LinkedHashMap linkedHashMap2 = new LinkedHashMap();
                        linkedHashMap2.put("sdk_version", "1.5.6-alpha.2.4-bugfix");
                        String LIZ2 = C0522K7d.LIZ("/location/report/", linkedHashMap, linkedHashMap2);
                        "upload result:".concat(String.valueOf(LIZ2));
                        C0525K7g.LIZLLL();
                        if (!TextUtils.isEmpty(LIZ2) && (k7i = (C0527K7i) new Gson().LIZ(LIZ2, C0527K7i.class)) != null && k7i.LIZJ == 0) {
                            C0522K7d.LIZ(context);
                        }
                    } else {
                        String LIZ3 = C0522K7d.LIZ(LIZIZ2);
                        "setOuterTraceRouterCache upload result:".concat(String.valueOf(LIZ3));
                        C0525K7g.LIZLLL();
                        if (!TextUtils.isEmpty(LIZ3)) {
                            try {
                                C0527K7i k7i2 = (C0527K7i) new Gson().LIZ(LIZ3, C0527K7i.class);
                                if (k7i2 != null && k7i2.LIZ == 0) {
                                    C0522K7d.LIZ(context);
                                }
                            } catch (Exception e) {
                                e.getMessage();
                                C0525K7g.LJFF();
                            }
                        }
                    }
                } catch (Exception unused) {
                    C0525K7g.LIZLLL();
                }
                C0525K7g.LIZLLL();
            }
        });
    }

    public void doTraceRoute(final Context context, String str, int i, final int i2) {
        MethodCollector.i(6736);
        synchronized (object) {
            try {
                C0525K7g.LJ();
                TraceRoute LIZ = TraceRoute.LIZ();
                LIZ.LIZIZ = i;
                LIZ.LIZJ = i2;
                LIZ.LIZ = new C0528K7j() {
                    static {
                        Covode.recordClassIndex(24357);
                    }

                    public final void LIZ(C0530K7l k7l) {
                        C0525K7g.LJ();
                    }

                    public final void LIZ(String str) {
                        double d;
                        "traceroute update:".concat(String.valueOf(str));
                        C0525K7g.LJ();
                        if (!TextUtils.isEmpty(str)) {
                            try {
                                String[] split = str.split(",");
                                if (split != null && split.length > 0) {
                                    if (split[0].equals("header")) {
                                        TraceRouteService.this.hopInfoList = new ArrayList();
                                        String str2 = split[1];
                                        TraceRouteService.this.tracerouteInfo = new TracerouteInfo();
                                        TraceRouteService.this.tracerouteInfo.setDomain(str2);
                                        TraceRouteService.this.tracerouteInfo.setTimestamp(System.currentTimeMillis());
                                        TraceRouteService.this.tracerouteInfo.setNetworkType(8q8.LIZ(8q8.LIZLLL(context)));
                                        TraceRouteService.this.tracerouteInfo.setExecInfo("traceroute to " + str2 + "(" + split[2] + ")," + split[3] + " hops max," + split[4] + " byte packets");
                                        TraceRouteService.this.tracerouteInfo.setHopInfo(TraceRouteService.this.hopInfoList);
                                        TraceRouteService.this.tracerouteInfoList.add(TraceRouteService.this.tracerouteInfo);
                                    }
                                    if (split[0].equals("hop")) {
                                        int parseInt = Integer.parseInt(split[1]);
                                        String str3 = split[2];
                                        String str4 = split[3];
                                        if (split[4].contains("null")) {
                                            d = -1.0d;
                                        } else {
                                            d = Double.parseDouble(split[4]);
                                        }
                                        HopData hopData = new HopData();
                                        hopData.setIp(str4);
                                        hopData.setHost(str3);
                                        ArrayList arrayList = new ArrayList();
                                        arrayList.add(Double.valueOf(d));
                                        hopData.setCost(arrayList);
                                        TraceRouteService.this.hopDataList.add(hopData);
                                        if (TraceRouteService.this.hopDataList.size() == i2) {
                                            TraceRouteService.this.resetHopDataList();
                                            HopInfo hopInfo = new HopInfo();
                                            hopInfo.setHop(parseInt);
                                            hopInfo.setHopData(TraceRouteService.this.hopDataList);
                                            TraceRouteService.this.hopDataList = new ArrayList();
                                            TraceRouteService.this.hopInfoList.add(hopInfo);
                                        }
                                    }
                                }
                            } catch (Exception unused) {
                                C0525K7g.LIZLLL();
                            }
                        }
                    }

                    public final void LIZ(int i, String str) {
                        C0525K7g.LJ();
                    }
                };
                LIZ.LIZ(str);
            } finally {
                MethodCollector.o(6736);
            }
        }
    }
}
