package a.a.a.h; import a.a.a.h.l; import android.text.TextUtils; import android.util.Log; import java.io.IOException; import java.net.InetAddress; import java.net.ProxySelector; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.net.URI; import java.net.URISyntaxException; import java.util.Locale; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; /* compiled from: HttpProxyCacheServer */ public class g { /* renamed from: a reason: collision with root package name */ public final Object f1687a = new Object(); public final ExecutorService b = Executors.newFixedThreadPool(8); public final Map<String, h> c = new ConcurrentHashMap(); public final ServerSocket d; public final int e; public final Thread f; public final c g; public final l h; /* compiled from: HttpProxyCacheServer */ public final class a implements Runnable { /* renamed from: a reason: collision with root package name */ public final Socket f1688a; public a(Socket socket) { this.f1688a = socket; } public void run() { g gVar = g.this; Socket socket = this.f1688a; if (gVar != null) { try { d a2 = d.a(socket.getInputStream()); String a3 = o.a(a2.f1686a); if (gVar.h != null) { if ("ping".equals(a3)) { gVar.h.a(socket); } else { gVar.a(a3).a(a2, socket); } return; } throw null; } catch (SocketException unused) { } catch (n | IOException e) { String message = new n("Error processing request", e).getMessage(); if (!TextUtils.isEmpty(message)) { Log.e("HttpProxyCacheServer error", message); } } finally { gVar.a(socket); f.a("Opened connections: " + gVar.a()); } } else { throw null; } } } /* compiled from: HttpProxyCacheServer */ public final class b implements Runnable { /* renamed from: a reason: collision with root package name */ public final CountDownLatch f1689a; public b(CountDownLatch countDownLatch) { this.f1689a = countDownLatch; } public void run() { this.f1689a.countDown(); g gVar = g.this; if (gVar != null) { while (!Thread.currentThread().isInterrupted()) { try { gVar.b.submit(new a(gVar.d.accept())); } catch (IOException e) { String message = new n("Error during waiting connection", e).getMessage(); if (!TextUtils.isEmpty(message)) { Log.e("HttpProxyCacheServer error", message); return; } return; } } return; } throw null; } } public g(c cVar) { this.g = (c) a.a.a.a.a(cVar); try { ServerSocket serverSocket = new ServerSocket(0, 8, InetAddress.getByName("127.0.0.1")); this.d = serverSocket; int localPort = serverSocket.getLocalPort(); this.e = localPort; j.a("127.0.0.1", localPort); CountDownLatch countDownLatch = new CountDownLatch(1); Thread thread = new Thread(new b(countDownLatch)); this.f = thread; thread.start(); countDownLatch.await(); this.h = new l("127.0.0.1", this.e); f.a("Proxy cache server started. Is it alive? " + b()); } catch (IOException | InterruptedException e2) { this.b.shutdown(); throw new IllegalStateException("Error starting local proxy server", e2); } } public final int a() { int i; synchronized (this.f1687a) { i = 0; for (h hVar : this.c.values()) { i += hVar.f1690a.get(); } } return i; } public final h a(String str) { h hVar; synchronized (this.f1687a) { hVar = this.c.get(str); if (hVar == null) { hVar = new h(str, this.g); this.c.put(str, hVar); } } return hVar; } /* JADX WARNING: Can't wrap try/catch for region: R(7:0|1|(1:3)|4|5|(1:7)|(3:10|11|(2:13|15)(1:17))) */ /* JADX WARNING: Code restructure failed: missing block: B:8:0x0013, code lost: r0 = move-exception; */ /* JADX WARNING: Code restructure failed: missing block: B:9:0x0014, code lost: a.a.a.h.f.a("Failed to close socket on proxy side: {}. It seems client have already closed connection.", r0.getMessage()); */ /* JADX WARNING: Failed to process nested try/catch */ /* JADX WARNING: Missing exception handler attribute for start block: B:4:0x0009 */ /* JADX WARNING: Removed duplicated region for block: B:13:0x0023 A[Catch:{ IOException -> 0x0026 }] */ /* JADX WARNING: Removed duplicated region for block: B:17:? A[RETURN, SYNTHETIC] */ /* JADX WARNING: Removed duplicated region for block: B:7:0x000f A[Catch:{ IOException -> 0x0013 }] */ public final void a(Socket socket) { if (!socket.isInputShutdown()) { socket.shutdownInput(); } if (!socket.isOutputShutdown()) { socket.shutdownOutput(); } try { if (socket.isClosed()) { socket.close(); } } catch (IOException unused) { } } public final boolean b() { l lVar = this.h; if (lVar != null) { int i = 0; int i2 = 70; while (i < 3) { try { if (((Boolean) lVar.f1694a.submit(new l.a()).get((long) i2, TimeUnit.MILLISECONDS)).booleanValue()) { return true; } i++; i2 *= 2; } catch (TimeoutException unused) { f.a("HttpProxyCacheDebuger", "Error pinging server (attempt: " + i + ", timeout: " + i2 + "). "); } catch (InterruptedException | ExecutionException e2) { f.a("Error pinging server due to unexpected error", e2); } } Locale locale = Locale.US; Object[] objArr = new Object[3]; objArr[0] = Integer.valueOf(i); objArr[1] = Integer.valueOf(i2 / 2); try { objArr[2] = ProxySelector.getDefault().select(new URI(lVar.a())); String format = String.format(locale, "Error pinging server (attempts: %d, max timeout: %d). If you see this message, please, report at https://github.com/danikula/AndroidVideoCache/issues/134. Default proxies are: %s", objArr); f.a(format, new n(format)); return false; } catch (URISyntaxException e3) { throw new IllegalStateException(e3); } } else { throw null; } } }