package com.kwad.sdk.core.videocache; import android.content.Context; import android.net.Uri; import android.util.Log; import com.kwad.sdk.core.network.o; import com.kwad.sdk.core.network.q; import com.kwad.sdk.core.videocache.b.a; import com.kwad.sdk.core.videocache.kwai.g; import com.kwad.sdk.utils.an; import com.kwai.video.player.KsMediaMeta; import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.net.URL; import java.util.Locale; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; public final class f { private final Object bdh; private final ExecutorService bdi; private final Map<String, g> bdj; private final ServerSocket bdk; private final Thread bdl; private final c bdm; private final int port; public static final class a { private File bcU; private com.kwad.sdk.core.videocache.kwai.c bcV = new com.kwad.sdk.core.videocache.kwai.f(); private com.kwad.sdk.core.videocache.kwai.a bcW = new g(KsMediaMeta.AV_CH_STEREO_LEFT); private com.kwad.sdk.core.videocache.c.b bcX; private com.kwad.sdk.core.videocache.a.b bcY = new com.kwad.sdk.core.videocache.a.a(); public a(Context context) { this.bcX = com.kwad.sdk.core.videocache.c.c.bG(context); this.bcU = n.bD(context); } private c ON() { return new c(this.bcU, this.bcV, this.bcW, this.bcX, this.bcY); } public final f OM() { return new f(ON(), (byte) 0); } public final a ae(long j) { this.bcW = new g(104857600); return this; } } /* access modifiers changed from: package-private */ public final class b implements Runnable { private final Socket bdn; public b(Socket socket) { this.bdn = socket; } public final void run() { com.kwad.sdk.core.e.b.d("HttpProxyCacheServer", "schedule SocketProcessorRunnable run"); f.this.a((f) this.bdn); } } final class c implements Runnable { private final CountDownLatch bdp; public c(CountDownLatch countDownLatch) { this.bdp = countDownLatch; } public final void run() { try { this.bdp.countDown(); f.this.OK(); } catch (Throwable th) { com.kwad.sdk.core.e.b.printStackTrace(th); } } } private f(c cVar) { this.bdh = new Object(); this.bdi = com.kwad.sdk.core.threads.b.Om(); this.bdj = new ConcurrentHashMap(); this.bdm = (c) an.checkNotNull(cVar); try { ServerSocket serverSocket = new ServerSocket(0, 8, InetAddress.getByName("127.0.0.1")); this.bdk = serverSocket; int localPort = serverSocket.getLocalPort(); this.port = localPort; i.install("127.0.0.1", localPort); CountDownLatch countDownLatch = new CountDownLatch(1); Thread thread = new Thread(new c(countDownLatch)); this.bdl = thread; thread.start(); countDownLatch.await(); } catch (IOException | InterruptedException e) { this.bdi.shutdown(); throw new IllegalStateException("Error starting local proxy server", e); } } /* synthetic */ f(c cVar, byte b2) { this(cVar); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void OK() { while (!Thread.currentThread().isInterrupted()) { try { Socket accept = this.bdk.accept(); com.kwad.sdk.core.e.b.d("HttpProxyCacheServer", "Accept new socket " + accept); this.bdi.submit(new b(accept)); } catch (IOException e) { onError(new ProxyCacheException("Error during waiting connection", e)); return; } } } private int OL() { int i; synchronized (this.bdh) { i = 0; for (g gVar : this.bdj.values()) { i += gVar.OL(); } } return i; } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void a(Socket socket) { StringBuilder sb; try { d c2 = d.c(socket.getInputStream()); com.kwad.sdk.core.e.b.d("HttpProxyCacheServer", "Request to cache proxy:" + c2); dY(k.decode(c2.bdb)).a(c2, socket); b(socket); sb = new StringBuilder("Opened connections: "); } catch (SocketException e) { com.kwad.sdk.core.e.b.d("HttpProxyCacheServer", "Closing socket… Socket is closed by client."); e.printStackTrace(); b(socket); sb = new StringBuilder("Opened connections: "); } catch (ProxyCacheException | IOException e2) { onError(new ProxyCacheException("Error processing request", e2)); b(socket); sb = new StringBuilder("Opened connections: "); } catch (Throwable th) { b(socket); com.kwad.sdk.core.e.b.d("HttpProxyCacheServer", "Opened connections: " + OL()); throw th; } sb.append(OL()); com.kwad.sdk.core.e.b.d("HttpProxyCacheServer", sb.toString()); } private void b(Socket socket) { c(socket); d(socket); e(socket); } private File bV(String str) { return new File(this.bdm.bcU, this.bdm.bcV.ea(str)); } private void c(Socket socket) { try { if (!socket.isInputShutdown()) { socket.shutdownInput(); } } catch (SocketException unused) { com.kwad.sdk.core.e.b.d("HttpProxyCacheServer", "Releasing input stream… Socket is closed by client."); } catch (IOException e) { onError(new ProxyCacheException("Error closing socket input stream", e)); } } private static void d(Socket socket) { try { if (!socket.isOutputShutdown()) { socket.shutdownOutput(); } } catch (IOException unused) { com.kwad.sdk.core.e.b.w("HttpProxyCacheServer", "Failed to close socket on proxy side: {}. It seems client have already closed connection."); } } private boolean dT(String str) { an.ag(str, "Url can't be null!"); return bV(str).exists(); } private String dW(String str) { return String.format(Locale.US, "http://%s:%d/%s", "127.0.0.1", Integer.valueOf(this.port), k.encode(str)); } private File dX(String str) { File file = this.bdm.bcU; return new File(file, this.bdm.bcV.ea(str) + ".download"); } private g dY(String str) { g gVar; synchronized (this.bdh) { gVar = this.bdj.get(str); if (gVar == null) { gVar = new g(str, this.bdm); this.bdj.put(str, gVar); } } return gVar; } private String e(String str, boolean z) { if (!bV(str).exists()) { return dW(str); } File bV = bV(str); m(bV); return Uri.fromFile(bV).toString(); } private void e(Socket socket) { try { if (!socket.isClosed()) { socket.close(); } } catch (IOException e) { onError(new ProxyCacheException("Error closing socket", e)); } } private void m(File file) { try { this.bdm.bcW.n(file); } catch (IOException unused) { com.kwad.sdk.core.e.b.e("HttpProxyCacheServer", "Error touching file " + file); } } private static void onError(Throwable th) { com.kwad.sdk.core.e.b.e("HttpProxyCacheServer", "HttpProxyCacheServer error"); com.kwad.sdk.core.e.b.printStackTraceOnly(th); } /* JADX WARNING: Removed duplicated region for block: B:23:0x009d A[SYNTHETIC, Splitter:B:23:0x009d] */ public final boolean a(String str, int i, a.C0155a aVar) { Throwable th; HttpURLConnection httpURLConnection; BufferedInputStream bufferedInputStream; com.kwad.sdk.core.e.b.d("HttpProxyCacheServer", "preloadSync preloadUrl " + str); if (dT(str)) { return true; } long j = 0; BufferedInputStream bufferedInputStream2 = null; try { httpURLConnection = (HttpURLConnection) new URL(dW(str)).openConnection(); try { q.wrapHttpURLConnection(httpURLConnection); httpURLConnection.setRequestProperty("Accept-Language", "zh-CN"); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setReadTimeout(120000); httpURLConnection.setUseCaches(false); httpURLConnection.setDoInput(true); httpURLConnection.setRequestProperty("Connection", "keep-alive"); httpURLConnection.setRequestProperty("Charset", "UTF-8"); httpURLConnection.setRequestProperty("User-Agent", o.getUserAgent()); httpURLConnection.setRequestProperty("BrowserUa", o.MG()); httpURLConnection.setRequestProperty("SystemUa", o.MF()); bufferedInputStream = new BufferedInputStream(httpURLConnection.getInputStream()); } catch (Throwable th2) { th = th2; if (aVar != null) { } com.kwad.sdk.core.e.b.printStackTraceOnly(th); com.kwad.sdk.core.e.b.e("HttpProxyCacheServer", "downloadUrlToStream file crash", th); com.kwad.sdk.crash.utils.b.closeQuietly(bufferedInputStream2); com.kwad.sdk.crash.utils.b.a(httpURLConnection); return false; } try { byte[] bArr = new byte[1024]; do { int read = bufferedInputStream.read(bArr); if (read == -1) { break; } j += (long) read; } while (j <= ((long) i)); com.kwad.sdk.crash.utils.b.closeQuietly(bufferedInputStream); com.kwad.sdk.crash.utils.b.a(httpURLConnection); return true; } catch (Throwable th3) { th = th3; bufferedInputStream2 = bufferedInputStream; if (aVar != null) { try { aVar.aHF = Log.getStackTraceString(th); } catch (Throwable th4) { com.kwad.sdk.crash.utils.b.closeQuietly(bufferedInputStream2); com.kwad.sdk.crash.utils.b.a(httpURLConnection); throw th4; } } com.kwad.sdk.core.e.b.printStackTraceOnly(th); com.kwad.sdk.core.e.b.e("HttpProxyCacheServer", "downloadUrlToStream file crash", th); com.kwad.sdk.crash.utils.b.closeQuietly(bufferedInputStream2); com.kwad.sdk.crash.utils.b.a(httpURLConnection); return false; } } catch (Throwable th5) { th = th5; httpURLConnection = null; if (aVar != null) { } com.kwad.sdk.core.e.b.printStackTraceOnly(th); com.kwad.sdk.core.e.b.e("HttpProxyCacheServer", "downloadUrlToStream file crash", th); com.kwad.sdk.crash.utils.b.closeQuietly(bufferedInputStream2); com.kwad.sdk.crash.utils.b.a(httpURLConnection); return false; } } public final String dS(String str) { return e(str, true); } public final boolean dU(String str) { an.ag(str, "Url can't be null!"); return dX(str).exists() || bV(str).exists(); } public final boolean dV(String str) { g gVar = this.bdj.get(str); if (gVar == null) { return false; } gVar.shutdown(); this.bdj.remove(str); return true; } public final boolean q(String str, int i) { return a(str, i, null); } }