package com.baidu.techain.i; import android.accounts.NetworkErrorException; import android.content.Context; import android.net.Proxy; import android.os.Build; import android.text.TextUtils; import com.meizu.cloud.pushsdk.constants.PushConstants; import com.xiaomi.mipush.sdk.Constants; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; import java.security.KeyStore; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.CertificateExpiredException; import java.security.cert.CertificateNotYetValidException; import java.security.cert.X509Certificate; import java.util.Arrays; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.zip.GZIPInputStream; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509TrustManager; /* compiled from: HttpUtil */ public final class l { private static SSLSocketFactory h; Context a; byte[] b = new byte[8192]; private String c; private String d; private int e = 120000; private int f = 120000; private boolean g = false; public l(Context context) { this.a = context; } private void a(String str, String str2) { this.c = str; this.d = str2; } private HttpURLConnection a() throws IOException { String str; HttpURLConnection httpURLConnection; if (TextUtils.isEmpty(this.c) || TextUtils.isEmpty(this.d)) { throw new IllegalArgumentException(); } if (!this.c.equals("POST") && !this.c.equals("GET")) { this.c = "POST"; } URL url = new URL(this.d); int i = -1; if (c.e(this.a)) { str = null; i = 0; } else if (Build.VERSION.SDK_INT >= 13) { str = System.getProperties().getProperty("http.proxyHost"); String property = System.getProperties().getProperty("http.proxyPort"); if (!TextUtils.isEmpty(property)) { try { i = Integer.parseInt(property); } catch (Throwable unused) { } } } else { str = Proxy.getHost(this.a); i = Proxy.getPort(this.a); } if (str == null || i <= 0) { httpURLConnection = (HttpURLConnection) url.openConnection(); } else { httpURLConnection = (HttpURLConnection) url.openConnection(new java.net.Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(str, i))); } if ("https".equals(url.getProtocol())) { HttpsURLConnection httpsURLConnection = (HttpsURLConnection) httpURLConnection; try { if (h != null) { httpsURLConnection.setHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); httpsURLConnection.setSSLSocketFactory(h); } else { SSLContext instance = SSLContext.getInstance("TLS"); TrustManagerFactory instance2 = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); instance2.init((KeyStore) null); TrustManager[] trustManagers = instance2.getTrustManagers(); if (trustManagers.length <= 0 || !(trustManagers[0] instanceof X509TrustManager)) { throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers)); } instance.init(null, new TrustManager[]{new a((X509TrustManager) trustManagers[0])}, new SecureRandom()); h = instance.getSocketFactory(); httpsURLConnection.setHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); httpsURLConnection.setSSLSocketFactory(h); } } catch (Throwable unused2) { c.a(); } } httpURLConnection.setRequestMethod(this.c); httpURLConnection.setDoInput(true); if ("POST".equals(this.c)) { httpURLConnection.setDoOutput(true); } httpURLConnection.setInstanceFollowRedirects(true); httpURLConnection.setConnectTimeout(this.e); httpURLConnection.setReadTimeout(this.f); httpURLConnection.setRequestProperty("User-Agent", "techain" + "/" + c.g(this.a)[0] + "/" + x.a(this.a) + "/3.5.9.0.1"); httpURLConnection.setRequestProperty("Pragma", "no-cache"); httpURLConnection.setRequestProperty("Accept", "*/*"); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpURLConnection.setRequestProperty("Accept-Encoding", "gzip,deflate"); httpURLConnection.setRequestProperty("Accept-Language", Locale.getDefault().getLanguage() + Constants.ACCEPT_TIME_SEPARATOR_SERVER + Locale.getDefault().getCountry()); httpURLConnection.setRequestProperty("x-device-id", n.a(e.b(this.a))); return httpURLConnection; } /* JADX WARNING: Code restructure failed: missing block: B:33:0x006c, code lost: r0 = r5; */ /* JADX WARNING: Code restructure failed: missing block: B:35:0x006e, code lost: r8 = move-exception; */ /* JADX WARNING: Code restructure failed: missing block: B:37:0x0071, code lost: r8 = move-exception; */ /* JADX WARNING: Code restructure failed: missing block: B:42:0x007d, code lost: r8 = move-exception; */ /* JADX WARNING: Code restructure failed: missing block: B:43:0x007f, code lost: r8 = move-exception; */ /* JADX WARNING: Code restructure failed: missing block: B:44:0x0081, code lost: r8 = move-exception; */ /* JADX WARNING: Code restructure failed: missing block: B:45:0x0082, code lost: if (r0 != null) goto L_0x0084; */ /* JADX WARNING: Code restructure failed: missing block: B:47:?, code lost: r0.close(); */ /* JADX WARNING: Code restructure failed: missing block: B:48:0x0087, code lost: throw r8; */ /* JADX WARNING: Code restructure failed: missing block: B:50:?, code lost: throw r8; */ /* JADX WARNING: Code restructure failed: missing block: B:51:?, code lost: throw r8; */ /* JADX WARNING: Code restructure failed: missing block: B:52:?, code lost: throw r8; */ /* JADX WARNING: Code restructure failed: missing block: B:53:?, code lost: throw r8; */ /* JADX WARNING: Exception block dominator not found, dom blocks: [B:5:0x000c, B:19:0x003f, B:39:0x0074] */ /* JADX WARNING: Missing exception handler attribute for start block: B:39:0x0074 */ private InputStream a(byte[] bArr, HttpURLConnection httpURLConnection) throws IOException, NetworkErrorException { BufferedOutputStream bufferedOutputStream = null; if (httpURLConnection == null) { return null; } if (bArr == null) { int responseCode = httpURLConnection.getResponseCode(); if (responseCode == 200) { String contentEncoding = httpURLConnection.getContentEncoding(); if (TextUtils.isEmpty(contentEncoding) || !"gzip".equalsIgnoreCase(contentEncoding)) { this.g = false; } else { this.g = true; } return httpURLConnection.getInputStream(); } throw new NetworkErrorException(String.valueOf(responseCode)); } BufferedOutputStream bufferedOutputStream2 = new BufferedOutputStream(httpURLConnection.getOutputStream()); bufferedOutputStream2.write(bArr); bufferedOutputStream2.flush(); int responseCode2 = httpURLConnection.getResponseCode(); if (responseCode2 == 200) { if ("gzip".equalsIgnoreCase(httpURLConnection.getContentEncoding())) { this.g = true; } else { this.g = false; } InputStream inputStream = httpURLConnection.getInputStream(); try { bufferedOutputStream2.close(); } catch (Throwable unused) { } return inputStream; } throw new NetworkErrorException(String.valueOf(responseCode2)); c.a(); throw new IOException(); } private InputStream a(HttpURLConnection httpURLConnection) { if (!(!c.f(this.a) || httpURLConnection == null || httpURLConnection == null)) { try { if ("gzip".equalsIgnoreCase(httpURLConnection.getContentEncoding())) { this.g = true; } else { this.g = false; } return httpURLConnection.getInputStream(); } catch (IOException unused) { c.a(); } } return null; } private String a(InputStream inputStream) throws IOException, InterruptedException { if (inputStream != null) { try { byte[] b2 = b(inputStream); if (b2 != null) { if (this.g) { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(b2); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); i.b(byteArrayInputStream, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.flush(); byteArrayOutputStream.close(); byteArrayInputStream.close(); b2 = byteArray; } if (b2 != null) { return new String(b2); } throw new IOException(); } throw new IOException("responseBytes"); } catch (Throwable unused) { c.a(); throw new IOException(); } } else { throw new IOException("InputStream"); } } /* JADX WARNING: Removed duplicated region for block: B:20:0x0031 */ /* JADX WARNING: Removed duplicated region for block: B:22:0x0036 */ public final String a(String str, byte[] bArr) throws IOException, InterruptedException, NetworkErrorException { Throwable th; HttpURLConnection httpURLConnection; w.a(); try { if (s.m(this.a)) { a("POST", str); InputStream inputStream = null; try { httpURLConnection = a(); try { inputStream = a(bArr, httpURLConnection); String a2 = a(inputStream); if (inputStream != null) { inputStream.close(); } if (httpURLConnection != null) { httpURLConnection.disconnect(); } return a2; } catch (Throwable th2) { th = th2; if (inputStream != null) { inputStream.close(); } if (httpURLConnection != null) { httpURLConnection.disconnect(); } throw th; } } catch (Throwable th3) { th = th3; httpURLConnection = null; if (inputStream != null) { } if (httpURLConnection != null) { } throw th; } } else { throw new NetworkErrorException("Not allow background connect."); } } finally { w.b(); } } private static byte[] b(InputStream inputStream) throws IOException, InterruptedException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] bArr = new byte[1024]; while (true) { int read = inputStream.read(bArr); if (read != -1) { byteArrayOutputStream.write(bArr, 0, read); } else { byte[] byteArray = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.close(); return byteArray; } } } /* JADX WARNING: Code restructure failed: missing block: B:32:?, code lost: com.baidu.techain.i.c.a(); */ /* JADX WARNING: Code restructure failed: missing block: B:33:0x004e, code lost: if (r0 != null) goto L_0x0050; */ /* JADX WARNING: Code restructure failed: missing block: B:35:?, code lost: r0.close(); */ /* JADX WARNING: Code restructure failed: missing block: B:36:0x0053, code lost: if (r4 != null) goto L_0x0055; */ /* JADX WARNING: Code restructure failed: missing block: B:37:0x0055, code lost: r4.disconnect(); */ /* JADX WARNING: Code restructure failed: missing block: B:38:0x0059, code lost: com.baidu.techain.i.c.a(); */ /* JADX WARNING: Code restructure failed: missing block: B:40:0x005c, code lost: com.baidu.techain.i.w.b(); */ /* JADX WARNING: Code restructure failed: missing block: B:41:0x005f, code lost: return false; */ /* JADX WARNING: Code restructure failed: missing block: B:42:0x0060, code lost: r5 = move-exception; */ /* JADX WARNING: Code restructure failed: missing block: B:43:0x0061, code lost: if (r0 != null) goto L_0x0063; */ /* JADX WARNING: Code restructure failed: missing block: B:45:?, code lost: r0.close(); */ /* JADX WARNING: Code restructure failed: missing block: B:46:0x0066, code lost: if (r4 != null) goto L_0x0068; */ /* JADX WARNING: Code restructure failed: missing block: B:47:0x0068, code lost: r4.disconnect(); */ /* JADX WARNING: Code restructure failed: missing block: B:48:0x006c, code lost: com.baidu.techain.i.c.a(); */ /* JADX WARNING: Code restructure failed: missing block: B:50:0x006f, code lost: throw r5; */ /* JADX WARNING: Failed to process nested try/catch */ /* JADX WARNING: Missing exception handler attribute for start block: B:31:0x004b */ public final boolean a(String str, File file) { HttpURLConnection httpURLConnection; w.a(); try { if (!c.f(this.a)) { return false; } if (TextUtils.isEmpty(str)) { w.b(); return false; } else if (!s.m(this.a)) { w.b(); return false; } else { InputStream inputStream = null; try { a("GET", str); httpURLConnection = a(); inputStream = a(httpURLConnection); boolean a2 = a(inputStream, file); if (inputStream != null) { try { inputStream.close(); } catch (Throwable unused) { c.a(); } } if (httpURLConnection != null) { httpURLConnection.disconnect(); } w.b(); return a2; } catch (Throwable unused2) { httpURLConnection = null; } } } finally { w.b(); } } /* JADX WARNING: Can't wrap try/catch for region: R(4:24|25|(2:27|28)|31) */ /* JADX WARNING: Code restructure failed: missing block: B:25:?, code lost: com.baidu.techain.i.c.a(); */ /* JADX WARNING: Code restructure failed: missing block: B:31:0x0044, code lost: return false; */ /* JADX WARNING: Code restructure failed: missing block: B:32:0x0045, code lost: r5 = move-exception; */ /* JADX WARNING: Code restructure failed: missing block: B:33:0x0046, code lost: if (r1 != null) goto L_0x0048; */ /* JADX WARNING: Code restructure failed: missing block: B:35:?, code lost: r1.close(); */ /* JADX WARNING: Code restructure failed: missing block: B:36:0x004c, code lost: com.baidu.techain.i.c.a(); */ /* JADX WARNING: Code restructure failed: missing block: B:38:0x0050, code lost: throw r5; */ /* JADX WARNING: Failed to process nested try/catch */ /* JADX WARNING: Missing exception handler attribute for start block: B:24:0x0038 */ private boolean a(InputStream inputStream, File file) { BufferedOutputStream bufferedOutputStream; if (this.g) { try { inputStream = new GZIPInputStream(inputStream); } catch (IOException unused) { c.a(); } } if (inputStream == null) { return false; } try { bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); byte[] bArr = new byte[1024]; while (true) { int read = inputStream.read(bArr); if (read != -1) { bufferedOutputStream.write(bArr, 0, read); bufferedOutputStream.flush(); } else { try { bufferedOutputStream.close(); return true; } catch (IOException unused2) { c.a(); return true; } } } } catch (Throwable unused3) { bufferedOutputStream = null; } } /* access modifiers changed from: package-private */ /* compiled from: HttpUtil */ public class a implements X509TrustManager { private X509TrustManager b = null; a(X509TrustManager x509TrustManager) { this.b = x509TrustManager; } @Override // javax.net.ssl.X509TrustManager public final void checkClientTrusted(X509Certificate[] x509CertificateArr, String str) throws CertificateException { this.b.checkClientTrusted(x509CertificateArr, str); } @Override // javax.net.ssl.X509TrustManager public final void checkServerTrusted(X509Certificate[] x509CertificateArr, String str) throws CertificateException { try { this.b.checkServerTrusted(x509CertificateArr, str); } catch (Throwable th) { c.a(); for (Throwable th2 = th; th2 != null; th2 = th2.getCause()) { if ((th2 instanceof CertificateExpiredException) || (th2 instanceof CertificateNotYetValidException)) { HashMap hashMap = new HashMap(); hashMap.put(PushConstants.PUSH_TYPE_NOTIFY, Long.valueOf(System.currentTimeMillis())); c.a(l.this.a.getApplicationContext(), "1003121", (Map<String, Object>) hashMap, true); return; } } if (th instanceof CertificateException) { throw th; } throw new CertificateException(); } } public final X509Certificate[] getAcceptedIssuers() { return this.b.getAcceptedIssuers(); } } }