package com.tencent.liteav.network; import com.tencent.ijk.media.player.IjkMediaPlayer; import com.tencent.liteav.basic.log.TXCLog; import com.tencent.liteav.basic.util.TXCCommonUtil; import com.umeng.analytics.pro.b; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.regex.Pattern; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /* compiled from: TXCIntelligentRoute */ class c { /* renamed from: a reason: collision with root package name */ public b f11784a = null; /* renamed from: b reason: collision with root package name */ public int f11785b = 5; private final String c = "https://tcdns.myqcloud.com/queryip"; private final String d = "forward_stream"; private final String e = "forward_num"; private final String f = "request_type"; private final String g = "sdk_version"; private final String h = "51451748-d8f2-4629-9071-db2983aa7251"; private Thread i = null; c() { } public void a(final String str, final int i2) { this.i = new Thread("TXCPushRoute") { /* class com.tencent.liteav.network.c.AnonymousClass1 */ public void run() { if (c.this.f11784a != null) { ArrayList<a> arrayList = new ArrayList<>(); for (int i = 0; i < 5; i++) { try { String b2 = c.this.b(str, i2); try { JSONObject jSONObject = new JSONObject(b2); if (jSONObject.has("use") && jSONObject.getInt("use") == 0) { break; } } catch (Exception e) { e.printStackTrace(); } arrayList = c.this.a((c) b2); if (arrayList != null && arrayList.size() > 0) { break; } sleep(1000, 0); } catch (Exception e2) { e2.printStackTrace(); } } c.this.f11784a.onFetchDone(0, arrayList); } } }; this.i.start(); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private String b(String str, int i2) { StringBuffer stringBuffer = new StringBuffer(""); try { InputStream c2 = c(str, i2); if (c2 == null) { return ""; } BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(c2)); while (true) { String readLine = bufferedReader.readLine(); if (readLine == null) { break; } stringBuffer.append(readLine); } return stringBuffer.toString(); } catch (IOException e2) { e2.printStackTrace(); } } private InputStream c(String str, int i2) throws IOException { try { HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("https://tcdns.myqcloud.com/queryip").openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setRequestProperty("forward_stream", str); httpURLConnection.setRequestProperty("forward_num", "2"); httpURLConnection.setRequestProperty("sdk_version", TXCCommonUtil.getSDKVersionStr()); if (i2 == 1) { httpURLConnection.setRequestProperty("request_type", "1"); } else if (i2 == 2) { httpURLConnection.setRequestProperty("request_type", "2"); } else { httpURLConnection.setRequestProperty("request_type", "3"); } if (this.f11785b > 0) { httpURLConnection.setConnectTimeout(this.f11785b * 1000); httpURLConnection.setReadTimeout(this.f11785b * 1000); } httpURLConnection.connect(); if (httpURLConnection.getResponseCode() == 200) { return httpURLConnection.getInputStream(); } return null; } catch (Exception e2) { e2.printStackTrace(); return null; } } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private ArrayList<a> a(String str) { JSONArray jSONArray; ArrayList<a> arrayList = new ArrayList<>(); try { JSONObject jSONObject = new JSONObject(str); if (jSONObject.getInt("state") != 0 || (jSONArray = jSONObject.getJSONObject(b.W).getJSONArray("list")) == null) { return null; } for (int i2 = 0; i2 < jSONArray.length(); i2++) { a a2 = a((JSONObject) jSONArray.opt(i2)); if (a2 != null && a2.c) { arrayList.add(a2); } } for (int i3 = 0; i3 < jSONArray.length(); i3++) { a a3 = a((JSONObject) jSONArray.opt(i3)); if (a3 != null && !a3.c) { arrayList.add(a3); } } if (com.tencent.liteav.basic.e.b.a().a("Network", "EnableRouteOptimize") != 1 || !m.a().c()) { long a4 = com.tencent.liteav.basic.e.b.a().a("Network", "RouteSamplingMaxCount"); if (a4 >= 1) { long a5 = m.a().a("51451748-d8f2-4629-9071-db2983aa7251"); if (a5 <= a4) { arrayList = a(arrayList, false); m.a().a("51451748-d8f2-4629-9071-db2983aa7251", a5 + 1); } } a(arrayList); return arrayList; } ArrayList<a> a6 = a(arrayList, true); a(a6); return a6; } catch (JSONException e2) { e2.printStackTrace(); return arrayList; } } private a a(JSONObject jSONObject) { a aVar = new a(); try { aVar.f11771a = jSONObject.getString(IjkMediaPlayer.OnNativeInvokeListener.ARG_IP); aVar.f11772b = jSONObject.getString(IjkMediaPlayer.OnNativeInvokeListener.ARG_PORT); aVar.e = 0; aVar.c = false; aVar.d = b(aVar.f11771a); if (jSONObject.has("type") && jSONObject.getInt("type") == 2) { aVar.c = true; } return aVar; } catch (JSONException e2) { e2.printStackTrace(); return null; } } private boolean b(String str) { if (str != null) { for (String str2 : str.split("[.]")) { if (!c(str2)) { return true; } } } return false; } private boolean c(String str) { return Pattern.compile("[0-9]*").matcher(str).matches(); } /* JADX DEBUG: Multi-variable search result rejected for r6v2, resolved type: java.util.ArrayList<com.tencent.liteav.network.a> */ /* JADX WARN: Multi-variable type inference failed */ private ArrayList<a> a(ArrayList<a> arrayList, boolean z) { a aVar; a aVar2 = null; if (arrayList == null || arrayList.size() == 0) { return null; } ArrayList arrayList2 = new ArrayList(); ArrayList arrayList3 = new ArrayList(); Iterator<a> it2 = arrayList.iterator(); loop0: while (true) { while (true) { if (!it2.hasNext()) { break loop0; } aVar2 = it2.next(); if (aVar2.c) { arrayList2.add(aVar2); } else if (!aVar2.d) { arrayList3.add(aVar2); } } } ArrayList<a> arrayList4 = new ArrayList<>(); while (true) { if (arrayList2.size() <= 0 && arrayList3.size() <= 0) { break; } if (z) { if (aVar2 != null) { arrayList4.add(aVar2); } if (arrayList2.size() > 0) { arrayList4.add(arrayList2.get(0)); arrayList2.remove(0); } } else { if (arrayList2.size() > 0) { arrayList4.add(arrayList2.get(0)); arrayList2.remove(0); } if (aVar2 != null) { arrayList4.add(aVar2); } } if (arrayList3.size() > 0) { arrayList4.add(arrayList3.get(0)); arrayList3.remove(0); } } int size = arrayList4.size(); if (size > 0 && (aVar = (a) arrayList4.get(size - 1)) != null && !b(aVar.f11771a) && aVar2 != null) { arrayList4.add(aVar2); } return arrayList4; } private void a(ArrayList<a> arrayList) { if (arrayList != null && arrayList.size() > 0) { Iterator<a> it2 = arrayList.iterator(); String str = ""; while (it2.hasNext()) { a next = it2.next(); str = str + " \n Nearest IP: " + next.f11771a + " Port: " + next.f11772b + " Q Channel: " + next.c; } TXCLog.e("TXCIntelligentRoute", str); } } }