247 lines
6.9 KiB
Java
247 lines
6.9 KiB
Java
package com.baidu.cloud.videocache;
|
|
|
|
import android.net.Uri;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.net.InetAddress;
|
|
import java.net.ServerSocket;
|
|
import java.net.Socket;
|
|
import java.net.SocketException;
|
|
import java.util.Iterator;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.CountDownLatch;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class g {
|
|
private static final Logger i = LoggerFactory.getLogger("HttpProxyCacheServer");
|
|
private final Object a;
|
|
private final ExecutorService b;
|
|
private final Map c;
|
|
private final ServerSocket d;
|
|
private final int e;
|
|
private Thread f;
|
|
private final a g;
|
|
private final q h;
|
|
|
|
private g(a aVar) {
|
|
this.a = new Object();
|
|
this.b = Executors.newFixedThreadPool(8);
|
|
this.c = new ConcurrentHashMap();
|
|
t.a(aVar);
|
|
this.g = aVar;
|
|
try {
|
|
this.d = new ServerSocket(0, 8, InetAddress.getByName("127.0.0.1"));
|
|
this.e = this.d.getLocalPort();
|
|
o.a("127.0.0.1", this.e);
|
|
a();
|
|
this.h = new q("127.0.0.1", this.e);
|
|
i.info("Proxy cache server started. Is it alive? " + b());
|
|
} catch (IOException | InterruptedException e) {
|
|
this.b.shutdown();
|
|
throw new IllegalStateException("Error starting local proxy server", e);
|
|
}
|
|
}
|
|
|
|
private void a() {
|
|
CountDownLatch countDownLatch = new CountDownLatch(1);
|
|
this.f = new Thread(new k(this, countDownLatch));
|
|
this.f.start();
|
|
countDownLatch.await();
|
|
}
|
|
|
|
private void a(File file) {
|
|
try {
|
|
this.g.c.a(file);
|
|
} catch (IOException e) {
|
|
i.error("Error touching file " + file, e);
|
|
}
|
|
}
|
|
|
|
private void a(Throwable th) {
|
|
i.error("HttpProxyCacheServer error", th);
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public void a(Socket socket) {
|
|
Logger logger;
|
|
StringBuilder sb;
|
|
try {
|
|
try {
|
|
b a = b.a(socket.getInputStream());
|
|
i.debug("Request to cache proxy:" + a);
|
|
String c = z.c(a.a);
|
|
if (this.h.a(c)) {
|
|
this.h.a(socket);
|
|
} else {
|
|
e(c).a(a, socket);
|
|
}
|
|
b(socket);
|
|
logger = i;
|
|
sb = new StringBuilder();
|
|
} catch (x e) {
|
|
e = e;
|
|
a(new x("Error processing request", e));
|
|
b(socket);
|
|
logger = i;
|
|
sb = new StringBuilder();
|
|
} catch (SocketException unused) {
|
|
i.debug("Closing socket… Socket is closed by client.");
|
|
b(socket);
|
|
logger = i;
|
|
sb = new StringBuilder();
|
|
} catch (IOException e2) {
|
|
e = e2;
|
|
a(new x("Error processing request", e));
|
|
b(socket);
|
|
logger = i;
|
|
sb = new StringBuilder();
|
|
}
|
|
sb.append("Opened connections: ");
|
|
sb.append(d());
|
|
logger.debug(sb.toString());
|
|
} catch (Throwable th) {
|
|
b(socket);
|
|
i.debug("Opened connections: " + d());
|
|
throw th;
|
|
}
|
|
}
|
|
|
|
private void b(Socket socket) {
|
|
c(socket);
|
|
d(socket);
|
|
e(socket);
|
|
}
|
|
|
|
private boolean b() {
|
|
return this.h.a(3, 70);
|
|
}
|
|
|
|
private String c(String str) {
|
|
return String.format(Locale.US, "http://%s:%d/%s", "127.0.0.1", Integer.valueOf(this.e), z.b(str));
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public void c() {
|
|
while (!Thread.currentThread().isInterrupted()) {
|
|
try {
|
|
Socket accept = this.d.accept();
|
|
i.debug("Accept new socket " + accept);
|
|
this.b.submit(new j(this, accept));
|
|
} catch (IOException e) {
|
|
a(new x("Error during waiting connection", e));
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void c(Socket socket) {
|
|
try {
|
|
if (socket.isInputShutdown()) {
|
|
return;
|
|
}
|
|
socket.shutdownInput();
|
|
} catch (SocketException unused) {
|
|
i.debug("Releasing input stream… Socket is closed by client.");
|
|
} catch (IOException e) {
|
|
a(new x("Error closing socket input stream", e));
|
|
}
|
|
}
|
|
|
|
private int d() {
|
|
int i2;
|
|
synchronized (this.a) {
|
|
i2 = 0;
|
|
Iterator it = this.c.values().iterator();
|
|
while (it.hasNext()) {
|
|
i2 += ((l) it.next()).a();
|
|
}
|
|
}
|
|
return i2;
|
|
}
|
|
|
|
private File d(String str) {
|
|
a aVar = this.g;
|
|
return new File(aVar.a, aVar.b.a(str));
|
|
}
|
|
|
|
private void d(Socket socket) {
|
|
try {
|
|
if (socket.isOutputShutdown()) {
|
|
return;
|
|
}
|
|
socket.shutdownOutput();
|
|
} catch (IOException e) {
|
|
i.warn("Failed to close socket on proxy side: {}. It seems client have already closed connection.", e.getMessage());
|
|
}
|
|
}
|
|
|
|
private l e(String str) {
|
|
l lVar;
|
|
synchronized (this.a) {
|
|
lVar = (l) this.c.get(str);
|
|
if (lVar == null) {
|
|
lVar = new l(str, this.g);
|
|
this.c.put(str, lVar);
|
|
}
|
|
}
|
|
return lVar;
|
|
}
|
|
|
|
private void e(Socket socket) {
|
|
try {
|
|
if (socket.isClosed()) {
|
|
return;
|
|
}
|
|
socket.close();
|
|
} catch (IOException e) {
|
|
a(new x("Error closing socket", e));
|
|
}
|
|
}
|
|
|
|
public String a(String str) {
|
|
int a = ad.a(str);
|
|
return (a == 1 || a == 2) ? a(str, false) : a(str, true);
|
|
}
|
|
|
|
public String a(String str, boolean z) {
|
|
if (!z || !b(str)) {
|
|
return b() ? c(str) : str;
|
|
}
|
|
File d = d(str);
|
|
a(d);
|
|
return Uri.fromFile(d).toString();
|
|
}
|
|
|
|
public void a(CacheListener cacheListener) {
|
|
t.a(cacheListener);
|
|
synchronized (this.a) {
|
|
Iterator it = this.c.values().iterator();
|
|
while (it.hasNext()) {
|
|
((l) it.next()).b(cacheListener);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void a(CacheListener cacheListener, String str) {
|
|
t.a(cacheListener, str);
|
|
synchronized (this.a) {
|
|
try {
|
|
e(str).a(cacheListener);
|
|
} catch (x e) {
|
|
i.warn("Error registering cache listener", e);
|
|
}
|
|
}
|
|
}
|
|
|
|
public boolean b(String str) {
|
|
t.a(str, "Url can't be null!");
|
|
return d(str).exists();
|
|
}
|
|
}
|