package com.mbridge.msdk.click; import android.net.Uri; import android.text.TextUtils; import com.facebook.share.internal.ShareConstants; import com.mbridge.msdk.foundation.entity.CampaignEx; import com.mbridge.msdk.foundation.tools.ab; import com.mbridge.msdk.foundation.tools.af; import com.mbridge.msdk.playercommon.exoplayer2.DefaultLoadControl; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; import java.util.concurrent.atomic.AtomicInteger; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import org.json.JSONObject; /* loaded from: classes4.dex */ public final class l { private static volatile AtomicInteger a = new AtomicInteger(1); private com.mbridge.msdk.c.g b; private String c; private int d = 9377; public l() { com.mbridge.msdk.c.g b = com.mbridge.msdk.c.h.a().b(com.mbridge.msdk.foundation.controller.c.m().k()); this.b = b; if (b == null) { com.mbridge.msdk.c.h.a(); this.b = com.mbridge.msdk.c.i.a(); } } public final com.mbridge.msdk.click.entity.a a(String str, CampaignEx campaignEx, boolean z, boolean z2) { com.mbridge.msdk.click.entity.a aVar = new com.mbridge.msdk.click.entity.a(); aVar.g = str; if (TextUtils.isEmpty(str)) { aVar.h = "request url can not null."; return aVar; } String replace = str.replace(" ", "%20"); JSONObject b = b(replace, campaignEx, z, z2); if (b.length() == 0) { aVar.h = "request content generation failed."; return aVar; } if (TextUtils.isEmpty(b.optString(ShareConstants.MEDIA_URI))) { aVar.h = "request url parse error."; return aVar; } if (campaignEx != null) { int trackingTcpPort = campaignEx.getTrackingTcpPort(); if (trackingTcpPort == 0) { trackingTcpPort = 9377; } this.d = trackingTcpPort; } if (!TextUtils.isEmpty(this.c)) { return a(replace, b.toString()); } aVar.h = "request url parse error."; return aVar; } /* JADX WARN: Multi-variable type inference failed */ private com.mbridge.msdk.click.entity.a a(String str, String str2) { OutputStream outputStream; Socket socket; int length; InputStream inputStream; int i; boolean z; Object[] objArr; byte[] bArr; com.mbridge.msdk.click.entity.a aVar = new com.mbridge.msdk.click.entity.a(); Socket socket2 = null; byte[] byteArray = null; try { try { socket = new Socket(this.c, this.d); try { socket.setSoTimeout(DefaultLoadControl.DEFAULT_MIN_BUFFER_MS); outputStream = socket.getOutputStream(); try { ByteBuffer wrap = ByteBuffer.wrap(new byte[8]); ByteOrder byteOrder = ByteOrder.BIG_ENDIAN; wrap.order(byteOrder); wrap.put((byte) 2); wrap.put((byte) 3); wrap.putShort((short) a.getAndIncrement()); if (TextUtils.isEmpty(str2)) { wrap.putInt(0); outputStream.write(wrap.array()); length = 0; } else { if (!TextUtils.isEmpty(str2)) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(byteArrayOutputStream); gZIPOutputStream.write(str2.getBytes()); gZIPOutputStream.close(); byteArray = byteArrayOutputStream.toByteArray(); } length = byteArray.length; wrap.putInt(length); outputStream.write(wrap.array()); outputStream.write(byteArray); } outputStream.flush(); af.a("SocketSpider", "Socket Request : header : " + Arrays.toString(wrap.array()) + " length : " + length); inputStream = socket.getInputStream(); byte[] bArr2 = new byte[8]; inputStream.read(bArr2, 0, 8); ByteBuffer wrap2 = ByteBuffer.wrap(bArr2); wrap2.order(byteOrder); i = wrap2.getInt(4); byte b = bArr2[1]; z = b == 3; objArr = b == 2; af.b("SocketSpider", "Socket Response : header : " + Arrays.toString(bArr2) + " length : " + i + " isGzip : " + z); bArr = new byte[i]; new DataInputStream(socket.getInputStream()).readFully(bArr); if (!z && i > 2 && ((bArr[0] << 8) | (bArr[1] & 255)) == 8075) { z = true; } } catch (Throwable th) { th = th; socket2 = socket; try { af.a("SocketSpider", "Socket exception: " + th.getMessage()); aVar.h = th.getMessage(); if (socket2 != null) { socket2.close(); if (outputStream != null) { outputStream.close(); } } return aVar; } finally { } } } catch (Throwable th2) { th = th2; outputStream = null; } } catch (Throwable th3) { th = th3; outputStream = null; } } catch (Exception e) { af.b("SocketSpider", e.getMessage()); } if (objArr == true && i == 0) { aVar.f = 200; aVar.g = str; aVar.e = 0; try { socket.close(); outputStream.close(); } catch (Exception e2) { af.b("SocketSpider", e2.getMessage()); } return aVar; } if (i < 1) { aVar.f = 200; aVar.g = str; aVar.e = 0; try { socket.close(); outputStream.close(); } catch (Exception e3) { af.b("SocketSpider", e3.getMessage()); } return aVar; } try { String a2 = z ? a(bArr) : new String(bArr); if (!TextUtils.isEmpty(a2)) { aVar.f = 200; aVar.g = str; aVar.e = 0; try { JSONObject optJSONObject = new JSONObject(a2).optJSONObject("data"); if (optJSONObject != null) { String optString = optJSONObject.optString("location"); if (!TextUtils.isEmpty(optString)) { aVar.f = 302; aVar.a = optString; } } } catch (Throwable th4) { af.b("SocketSpider", th4.getMessage()); } try { socket.close(); outputStream.close(); } catch (Exception e4) { af.b("SocketSpider", e4.getMessage()); } return aVar; } } catch (Throwable th5) { af.b("SocketSpider", th5.getMessage()); aVar.h = th5.getMessage(); } inputStream.close(); socket.close(); outputStream.close(); return aVar; } private JSONObject b(String str, CampaignEx campaignEx, boolean z, boolean z2) { JSONObject jSONObject = new JSONObject(); try { Uri parse = Uri.parse(str); if (parse != null) { String host = parse.getHost(); String path = parse.getPath(); String encodedQuery = parse.getEncodedQuery(); this.c = host; jSONObject.put(ShareConstants.MEDIA_URI, "https://" + host + path); jSONObject.put("data", encodedQuery); } } catch (Throwable th) { af.b("SocketSpider", th.getMessage()); } try { JSONObject jSONObject2 = new JSONObject(); if (!z && !z2) { jSONObject2.put("User-Agent", ab.g()); } if (campaignEx != null) { if (z && campaignEx.getcUA() == 1) { jSONObject2.put("User-Agent", ab.g()); } if (z2 && campaignEx.getImpUA() == 1) { jSONObject2.put("User-Agent", ab.g()); } } else { jSONObject2.put("User-Agent", ab.g()); } jSONObject2.put("Accept-Encoding", "gzip"); if (this.b.aJ() && !TextUtils.isEmpty(str)) { jSONObject2.put("referer", str); } jSONObject.put("header", jSONObject2); } catch (Throwable th2) { af.b("SocketSpider", th2.getMessage()); } return jSONObject; } private String a(byte[] bArr) throws IOException { if (bArr == null || bArr.length == 0) { return null; } ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr); GZIPInputStream gZIPInputStream = new GZIPInputStream(byteArrayInputStream); byte[] bArr2 = new byte[1024]; while (true) { int read = gZIPInputStream.read(bArr2, 0, 1024); if (read > 0) { byteArrayOutputStream.write(bArr2, 0, read); } else { gZIPInputStream.close(); byteArrayInputStream.close(); byteArrayOutputStream.flush(); byteArrayOutputStream.close(); return byteArrayOutputStream.toString(); } } } }