package epson.print.inkrpln;


import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class LocalSSLSocketFactory extends SSLSocketFactory {
    private SSLSocketFactory mOrigSSLSocketFactory;

    public LocalSSLSocketFactory(SSLSocketFactory sSLSocketFactory) {
        mOrigSSLSocketFactory = sSLSocketFactory;
    }

    public String[] getDefaultCipherSuites() {
        return mOrigSSLSocketFactory.getDefaultCipherSuites();
    }

    public String[] getSupportedCipherSuites() {
        return mOrigSSLSocketFactory.getSupportedCipherSuites();
    }

    public Socket createSocket(Socket socket, String str, int i, boolean z) throws IOException {
        SSLSocket sSLSocket = (SSLSocket) mOrigSSLSocketFactory.createSocket(socket, str, i, z);
        localEnableProtocols(sSLSocket);
        return sSLSocket;
    }

    public Socket createSocket(String str, int i) throws IOException, UnknownHostException {
        SSLSocket sSLSocket = (SSLSocket) mOrigSSLSocketFactory.createSocket(str, i);
        localEnableProtocols(sSLSocket);
        return sSLSocket;
    }

    public Socket createSocket(String str, int i, InetAddress inetAddress, int i2) throws IOException, UnknownHostException {
        SSLSocket sSLSocket = (SSLSocket) mOrigSSLSocketFactory.createSocket(str, i, inetAddress, i2);
        localEnableProtocols(sSLSocket);
        return sSLSocket;
    }

    public Socket createSocket(InetAddress inetAddress, int i) throws IOException {
        SSLSocket sSLSocket = (SSLSocket) mOrigSSLSocketFactory.createSocket(inetAddress, i);
        localEnableProtocols(sSLSocket);
        return sSLSocket;
    }

    public Socket createSocket(InetAddress inetAddress, int i, InetAddress inetAddress2, int i2) throws IOException {
        SSLSocket sSLSocket = (SSLSocket) mOrigSSLSocketFactory.createSocket(inetAddress, i, inetAddress2, i2);
        localEnableProtocols(sSLSocket);
        return sSLSocket;
    }

    private void old_localEnableProtocols(SSLSocket sSLSocket) {
        sSLSocket.setEnabledProtocols(complementArray(sSLSocket.getSupportedProtocols(), Arrays.asList(new String[]{"TLSv1"})));
    }

    private void localEnableProtocols(SSLSocket sSLSocket) throws IOException {
        String[] filterArray = filterArray(sSLSocket.getSupportedProtocols(), Arrays.asList(new String[]{"TLSv1.2"}));
        if (filterArray.length > 0) {
            sSLSocket.setEnabledProtocols(filterArray);
            return;
        }
        throw new IOException("Protocol not supported");
    }

    @NonNull
    public static String[] complementArray(@NonNull String[] strArr, @NonNull List<String> list) {
        List<String> asList = Arrays.asList(strArr);
        LinkedList linkedList = new LinkedList();
        for (String str : asList) {
            if (list.indexOf(str) == -1) {
                linkedList.add(str);
            }
        }
        return (String[]) linkedList.toArray(new String[linkedList.size()]);
    }

    @NonNull
    public static String[] filterArray(@NonNull String[] strArr, @NonNull List<String> list) {
        List<String> asList = Arrays.asList(strArr);
        LinkedList linkedList = new LinkedList();
        for (String str : asList) {
            if (list.indexOf(str) != -1) {
                linkedList.add(str);
            }
        }
        return (String[]) linkedList.toArray(new String[linkedList.size()]);
    }
}
