♬♬ v1.0.0版本的 MD5 值为:04cc17073568ba22bfb83ea139fbe521

以下内容为反编译后的 HttpProxyCacheServer.java 源代码,内容仅作参考


package com.danikula.videocache;

import android.content.Context;
import android.net.Uri;
import com.danikula.videocache.file.DiskUsage;
import com.danikula.videocache.file.FileNameGenerator;
import com.danikula.videocache.file.Md5FileNameGenerator;
import com.danikula.videocache.file.TotalCountLruDiskUsage;
import com.danikula.videocache.file.TotalSizeLruDiskUsage;
import com.danikula.videocache.headers.EmptyHeadersInjector;
import com.danikula.videocache.headers.HeaderInjector;
import com.danikula.videocache.sourcestorage.SourceInfoStorage;
import com.danikula.videocache.sourcestorage.SourceInfoStorageFactory;
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.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 obfuse.NPStringFog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpProxyCacheServer {
    private static final Logger LOG = LoggerFactory.getLogger(NPStringFog.decode("7945454161435E49487250525954625443475443"));
    private static final String PROXY_HOST = "127.0.0.1";
    private final Object clientsLock;
    private final Map<String, HttpProxyCacheServerClients> clientsMap;
    private final Config config;
    private final Pinger pinger;
    private final int port;
    private final ServerSocket serverSocket;
    private final ExecutorService socketProcessor;
    private final Thread waitConnectionThread;

    public HttpProxyCacheServer(Context context) {
        this(new Builder(context).buildConfig());
    }

    private HttpProxyCacheServer(Config config) {
        String decode = NPStringFog.decode("0003061F011F011F00");
        this.clientsLock = new Object();
        this.socketProcessor = Executors.newFixedThreadPool(8);
        this.clientsMap = new ConcurrentHashMap();
        this.config = (Config) Preconditions.checkNotNull(config);
        try {
            InetAddress inetAddress = InetAddress.getByName(decode);
            ServerSocket serverSocket = new ServerSocket(0, 8, inetAddress);
            this.serverSocket = serverSocket;
            int localPort = serverSocket.getLocalPort();
            this.port = localPort;
            IgnoreHostProxySelector.install(decode, localPort);
            CountDownLatch startSignal = new CountDownLatch(1);
            Thread thread = new Thread(new WaitRequestsRunnable(startSignal));
            this.waitConnectionThread = thread;
            thread.start();
            startSignal.await();
            this.pinger = new Pinger(decode, this.port);
            LOG.info(NPStringFog.decode("61435E49481152505259541142544347544311424550434554551F11784211584511505D5847540E11") + isAlive());
        } catch (IOException | InterruptedException e) {
            this.socketProcessor.shutdown();
            throw new IllegalStateException(NPStringFog.decode("7443435E43114245504345585F56115D5E52505D1141435E494811425443475443"), e);
        }
    }

    public String getProxyUrl(String url) {
        return getProxyUrl(url, true);
    }

    public String getProxyUrl(String url, boolean allowCachedFileUri) {
        if (!allowCachedFileUri || !isCached(url)) {
            return isAlive() ? appendToProxyUrl(url) : url;
        }
        File cacheFile = getCacheFile(url);
        touchFileSafely(cacheFile);
        return Uri.fromFile(cacheFile).toString();
    }

    public void registerCacheListener(CacheListener cacheListener, String url) {
        Preconditions.checkAllNotNull(cacheListener, url);
        synchronized (this.clientsLock) {
            try {
                getClients(url).registerCacheListener(cacheListener);
            } catch (ProxyCacheException e) {
                LOG.warn(NPStringFog.decode("7443435E43114354565842455443585F56115250525954115D584245545F5443"), (Throwable) e);
            }
        }
    }

    public void unregisterCacheListener(CacheListener cacheListener, String url) {
        Preconditions.checkAllNotNull(cacheListener, url);
        synchronized (this.clientsLock) {
            try {
                getClients(url).unregisterCacheListener(cacheListener);
            } catch (ProxyCacheException e) {
                LOG.warn(NPStringFog.decode("7443435E43114354565842455443585F56115250525954115D584245545F5443"), (Throwable) e);
            }
        }
    }

    public void unregisterCacheListener(CacheListener cacheListener) {
        Preconditions.checkNotNull(cacheListener);
        synchronized (this.clientsLock) {
            for (HttpProxyCacheServerClients clients : this.clientsMap.values()) {
                clients.unregisterCacheListener(cacheListener);
            }
        }
    }

    public boolean isCached(String url) {
        Preconditions.checkNotNull(url, NPStringFog.decode("64435D1152505F1645115354115F445D5D10"));
        return getCacheFile(url).exists();
    }

    public void shutdown() {
        LOG.info(NPStringFog.decode("62594445555E465F1141435E494811425443475443"));
        shutdownClients();
        this.config.sourceInfoStorage.release();
        this.waitConnectionThread.interrupt();
        try {
            if (!this.serverSocket.isClosed()) {
                this.serverSocket.close();
            }
        } catch (IOException e) {
            onError(new ProxyCacheException(NPStringFog.decode("7443435E43114259444545585F5611555E465F1141435E494811425443475443"), e));
        }
    }

    private boolean isAlive() {
        return this.pinger.ping(3, 70);
    }

    private String appendToProxyUrl(String url) {
        return String.format(Locale.US, NPStringFog.decode("594545410B1E1E14420B14551E1442"), NPStringFog.decode("0003061F011F011F00"), Integer.valueOf(this.port), ProxyCacheUtils.encode(url));
    }

    private File getCacheFile(String url) {
        File cacheDir = this.config.cacheRoot;
        String fileName = this.config.fileNameGenerator.generate(url);
        return new File(cacheDir, fileName);
    }

    private void touchFileSafely(File cacheFile) {
        try {
            this.config.diskUsage.touch(cacheFile);
        } catch (IOException e) {
            LOG.error(NPStringFog.decode("7443435E4311455E445259585F561157585D5411") + cacheFile, (Throwable) e);
        }
    }

    private void shutdownClients() {
        synchronized (this.clientsLock) {
            for (HttpProxyCacheServerClients clients : this.clientsMap.values()) {
                clients.shutdown();
            }
            this.clientsMap.clear();
        }
    }

    public void waitForRequest() {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                Socket socket = this.serverSocket.accept();
                LOG.debug(NPStringFog.decode("705252544145115F544611425E525A544511") + socket);
                this.socketProcessor.submit(new SocketProcessorRunnable(socket));
            } catch (IOException e) {
                onError(new ProxyCacheException(NPStringFog.decode("7443435E4311554443585F561146505845585F5611525E5F5F545245585E5F"), e));
                return;
            }
        }
    }

    public void processSocket(Socket socket) {
        Logger logger;
        StringBuilder sb;
        String decode = NPStringFog.decode("7E41545F545511525E5F5F545245585E5F420B11");
        try {
            try {
                GetRequest request = GetRequest.read(socket.getInputStream());
                LOG.debug(NPStringFog.decode("6354404454424511455E1152505259541141435E49480B") + request);
                String url = ProxyCacheUtils.decode(request.uri);
                if (this.pinger.isPingRequest(url)) {
                    this.pinger.responseToPing(socket);
                } else {
                    HttpProxyCacheServerClients clients = getClients(url);
                    clients.processRequest(request, socket);
                }
                releaseSocket(socket);
                logger = LOG;
                sb = new StringBuilder();
            } catch (ProxyCacheException e) {
                e = e;
                onError(new ProxyCacheException(NPStringFog.decode("7443435E431141435E52544242585F561143544044544245"), e));
                releaseSocket(socket);
                logger = LOG;
                sb = new StringBuilder();
            } catch (SocketException e2) {
                LOG.debug(NPStringFog.decode("725D5E42585F5611425E525A5445D3B19711625E525A544511584211525D5E42545511534811525D58545F451F"));
                releaseSocket(socket);
                logger = LOG;
                sb = new StringBuilder();
            } catch (IOException e3) {
                e = e3;
                onError(new ProxyCacheException(NPStringFog.decode("7443435E431141435E52544242585F561143544044544245"), e));
                releaseSocket(socket);
                logger = LOG;
                sb = new StringBuilder();
            }
            sb.append(decode);
            sb.append(getClientsCount());
            logger.debug(sb.toString());
        } catch (Throwable th) {
            releaseSocket(socket);
            LOG.debug(decode + getClientsCount());
            throw th;
        }
    }

    private HttpProxyCacheServerClients getClients(String url) throws ProxyCacheException {
        HttpProxyCacheServerClients clients;
        synchronized (this.clientsLock) {
            clients = this.clientsMap.get(url);
            if (clients == null) {
                clients = new HttpProxyCacheServerClients(url, this.config);
                this.clientsMap.put(url, clients);
            }
        }
        return clients;
    }

    private int getClientsCount() {
        int count;
        synchronized (this.clientsLock) {
            count = 0;
            for (HttpProxyCacheServerClients clients : this.clientsMap.values()) {
                count += clients.getClientsCount();
            }
        }
        return count;
    }

    private void releaseSocket(Socket socket) {
        closeSocketInput(socket);
        closeSocketOutput(socket);
        closeSocket(socket);
    }

    private void closeSocketInput(Socket socket) {
        try {
            if (!socket.isInputShutdown()) {
                socket.shutdownInput();
            }
        } catch (SocketException e) {
            LOG.debug(NPStringFog.decode("63545D545042585F5611585F4144451142454354505CD3B19711625E525A544511584211525D5E42545511534811525D58545F451F"));
        } catch (IOException e2) {
            onError(new ProxyCacheException(NPStringFog.decode("7443435E4311525D5E42585F5611425E525A544511585F4144451142454354505C"), e2));
        }
    }

    private void closeSocketOutput(Socket socket) {
        try {
            if (!socket.isOutputShutdown()) {
                socket.shutdownOutput();
            }
        } catch (IOException e) {
            LOG.warn(NPStringFog.decode("7750585D545511455E11525D5E425411425E525A5445115E5F1141435E494811425855540B114A4C1F117845114254545C4211525D58545F45115950475411505D435450554811525D5E42545511525E5F5F545245585E5F1F"), e.getMessage());
        }
    }

    private void closeSocket(Socket socket) {
        try {
            if (!socket.isClosed()) {
                socket.close();
            }
        } catch (IOException e) {
            onError(new ProxyCacheException(NPStringFog.decode("7443435E4311525D5E42585F5611425E525A5445"), e));
        }
    }

    private void onError(Throwable e) {
        LOG.error(NPStringFog.decode("7945454161435E49487250525954625443475443115443435E43"), e);
    }

    private final class WaitRequestsRunnable implements Runnable {
        private final CountDownLatch startSignal;

        public WaitRequestsRunnable(CountDownLatch startSignal) {
            this.startSignal = startSignal;
        }

        @Override
        public void run() {
            this.startSignal.countDown();
            HttpProxyCacheServer.this.waitForRequest();
        }
    }

    public final class SocketProcessorRunnable implements Runnable {
        private final Socket socket;

        public SocketProcessorRunnable(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            HttpProxyCacheServer.this.processSocket(this.socket);
        }
    }

    public static final class Builder {
        private static final long DEFAULT_MAX_SIZE = 536870912;
        private File cacheRoot;
        private DiskUsage diskUsage = new TotalSizeLruDiskUsage(DEFAULT_MAX_SIZE);
        private FileNameGenerator fileNameGenerator = new Md5FileNameGenerator();
        private HeaderInjector headerInjector = new EmptyHeadersInjector();
        private SourceInfoStorage sourceInfoStorage;

        public Builder(Context context) {
            this.sourceInfoStorage = SourceInfoStorageFactory.newSourceInfoStorage(context);
            this.cacheRoot = StorageUtils.getIndividualCacheDirectory(context);
        }

        public Builder cacheDirectory(File file) {
            this.cacheRoot = (File) Preconditions.checkNotNull(file);
            return this;
        }

        public Builder fileNameGenerator(FileNameGenerator fileNameGenerator) {
            this.fileNameGenerator = (FileNameGenerator) Preconditions.checkNotNull(fileNameGenerator);
            return this;
        }

        public Builder maxCacheSize(long maxSize) {
            this.diskUsage = new TotalSizeLruDiskUsage(maxSize);
            return this;
        }

        public Builder maxCacheFilesCount(int count) {
            this.diskUsage = new TotalCountLruDiskUsage(count);
            return this;
        }

        public Builder diskUsage(DiskUsage diskUsage) {
            this.diskUsage = (DiskUsage) Preconditions.checkNotNull(diskUsage);
            return this;
        }

        public Builder headerInjector(HeaderInjector headerInjector) {
            this.headerInjector = (HeaderInjector) Preconditions.checkNotNull(headerInjector);
            return this;
        }

        public HttpProxyCacheServer build() {
            Config config = buildConfig();
            return new HttpProxyCacheServer(config);
        }

        public Config buildConfig() {
            return new Config(this.cacheRoot, this.fileNameGenerator, this.diskUsage, this.sourceInfoStorage, this.headerInjector);
        }
    }
}