/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.util.ConfigFactory
 *  org.apache.juli.logging.Log
 *  org.apache.tomcat.util.ExceptionUtils
 *  org.apache.tomcat.util.collections.SynchronizedStack
 *  org.apache.tomcat.util.net.AbstractEndpoint
 *  org.apache.tomcat.util.net.Nio2Channel
 *  org.apache.tomcat.util.net.Nio2Endpoint
 *  org.apache.tomcat.util.net.Nio2Endpoint$Nio2SocketWrapper
 *  org.apache.tomcat.util.net.SSLHostConfig
 *  org.apache.tomcat.util.net.SecureNio2Channel
 *  org.apache.tomcat.util.net.SocketBufferHandler
 *  org.apache.tomcat.util.net.SocketEvent
 *  org.apache.tomcat.util.net.SocketWrapperBase
 *  org.apache.tomcat.util.net.openssl.ciphers.Cipher
 */
package com.l7tech.server.tomcat.nio2;

import com.l7tech.objectmodel.Goid;
import com.l7tech.server.tomcat.SsgConnectorAwareEndpoint;
import com.l7tech.server.tomcat.SsgSSLImplementation;
import com.l7tech.server.tomcat.nio2.SsgAsynchronousSocketChannel;
import com.l7tech.server.tomcat.nio2.i;
import com.l7tech.server.tomcat.nio2.j;
import com.l7tech.server.tomcat.nio2.k;
import com.l7tech.server.transport.tls.SsgConnectorSslHelper;
import com.l7tech.util.ConfigFactory;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.List;
import javax.net.ssl.SSLEngine;
import org.apache.juli.logging.Log;
import org.apache.tomcat.util.ExceptionUtils;
import org.apache.tomcat.util.collections.SynchronizedStack;
import org.apache.tomcat.util.net.AbstractEndpoint;
import org.apache.tomcat.util.net.Nio2Channel;
import org.apache.tomcat.util.net.Nio2Endpoint;
import org.apache.tomcat.util.net.SSLHostConfig;
import org.apache.tomcat.util.net.SecureNio2Channel;
import org.apache.tomcat.util.net.SocketBufferHandler;
import org.apache.tomcat.util.net.SocketEvent;
import org.apache.tomcat.util.net.SocketWrapperBase;
import org.apache.tomcat.util.net.openssl.ciphers.Cipher;

public class SsgNio2Endpoint
extends Nio2Endpoint
implements SsgConnectorAwareEndpoint {
    private SsgConnectorSslHelper a;
    private boolean b;
    private Goid c;
    private static final boolean d;
    private static final String[] e;

    public SsgNio2Endpoint() {
        this.setSslImplementationName(SsgSSLImplementation.class.getName());
    }

    public void bind() throws Exception {
        boolean bl2 = this.internalExecutor;
        this.internalExecutor = true;
        super.bind();
        this.internalExecutor = bl2;
    }

    protected SSLHostConfig getSSLHostConfig(String string) {
        return super.getSSLHostConfig(string);
    }

    protected SSLEngine createSSLEngine(String string, List<Cipher> list, List<String> list2) {
        return super.createSSLEngine(string, list, list2);
    }

    protected AsynchronousSocketChannel serverSocketAccept() throws Exception {
        return new SsgAsynchronousSocketChannel(super.serverSocketAccept(), this.c);
    }

    protected void createSSLContext(SSLHostConfig sSLHostConfig) throws IllegalArgumentException {
        block2: {
            try {
                if (this.b || !(this.getSslImplementation() instanceof SsgSSLImplementation)) break block2;
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw SsgNio2Endpoint.a(illegalArgumentException);
            }
            SsgSSLImplementation ssgSSLImplementation = (SsgSSLImplementation)this.getSslImplementation();
            ssgSSLImplementation.setSsgConnectorSslHelper(this.a);
            sSLHostConfig.setHonorCipherOrder(this.a.useCipherSuitesOrder());
            this.b = true;
        }
        super.createSSLContext(sSLHostConfig);
    }

    protected void startAcceptorThread() {
        try {
            if (this.acceptor == null) {
                this.acceptor = new j(this, (AbstractEndpoint)this);
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw SsgNio2Endpoint.a(illegalArgumentException);
        }
        super.startAcceptorThread();
    }

    @Override
    public void setConnectorGoid(Goid goid) {
        this.c = goid;
    }

    @Override
    public void setSsgConnectorSslHelper(SsgConnectorSslHelper ssgConnectorSslHelper) {
        this.a = ssgConnectorSslHelper;
    }

    protected boolean setSocketOptions(AsynchronousSocketChannel asynchronousSocketChannel) {
        Nio2Endpoint.Nio2SocketWrapper nio2SocketWrapper = null;
        try {
            Nio2Channel nio2Channel = null;
            SynchronizedStack synchronizedStack = super.getNioChannels();
            if (synchronizedStack != null) {
                nio2Channel = (Nio2Channel)synchronizedStack.pop();
            }
            if (nio2Channel == null) {
                nio2Channel = this.a();
            }
            Nio2Endpoint.Nio2SocketWrapper nio2SocketWrapper2 = new Nio2Endpoint.Nio2SocketWrapper(nio2Channel, (Nio2Endpoint)this);
            nio2Channel.reset(asynchronousSocketChannel, (SocketWrapperBase)nio2SocketWrapper2);
            this.connections.put(asynchronousSocketChannel, nio2SocketWrapper2);
            nio2SocketWrapper = nio2SocketWrapper2;
            this.socketProperties.setProperties(asynchronousSocketChannel);
            nio2SocketWrapper.setReadTimeout((long)this.getConnectionTimeout());
            nio2SocketWrapper.setWriteTimeout((long)this.getConnectionTimeout());
            nio2SocketWrapper.setKeepAliveLeft(this.getMaxKeepAliveRequests());
            return this.processSocket((SocketWrapperBase)nio2SocketWrapper, SocketEvent.OPEN_READ, false);
        }
        catch (Throwable throwable) {
            ExceptionUtils.handleThrowable((Throwable)throwable);
            Log log = super.getLog();
            try {
                log.error((Object)sm.getString(e[1]), throwable);
                if (nio2SocketWrapper == null) {
                    this.destroySocket(asynchronousSocketChannel);
                }
            }
            catch (Throwable throwable2) {
                throw SsgNio2Endpoint.a(throwable2);
            }
            return false;
        }
    }

    private Nio2Channel a() {
        SocketBufferHandler socketBufferHandler;
        block7: {
            block8: {
                socketBufferHandler = new SocketBufferHandler(this.socketProperties.getAppReadBufSize(), this.socketProperties.getAppWriteBufSize(), this.socketProperties.getDirectBuffer());
                try {
                    try {
                        if (d) break block7;
                        if (!this.isSSLEnabled()) break block8;
                    }
                    catch (IllegalArgumentException illegalArgumentException) {
                        throw SsgNio2Endpoint.a(illegalArgumentException);
                    }
                    return new SecureNio2Channel(socketBufferHandler, (Nio2Endpoint)this);
                }
                catch (IllegalArgumentException illegalArgumentException) {
                    throw SsgNio2Endpoint.a(illegalArgumentException);
                }
            }
            return new Nio2Channel(socketBufferHandler);
        }
        try {
            if (this.isSSLEnabled()) {
                return this.b(socketBufferHandler);
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw SsgNio2Endpoint.a(illegalArgumentException);
        }
        return this.a(socketBufferHandler);
    }

    private Nio2Channel a(SocketBufferHandler socketBufferHandler) {
        return new i(this, socketBufferHandler);
    }

    private Nio2Channel b(SocketBufferHandler socketBufferHandler) {
        return new k(this, socketBufferHandler, this);
    }

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[2];
        int n2 = 0;
        String string = "GaI\u001d|N\bAmL\u001dc\u001c\u000eRkV\u001dd\u0016\u0011GoP\u001du\u0017\u001dFbAgb\u0018\u001fA\u001bA`@C\u007f\u0010\u0012P W\\s\u0012\u0019PATGy\u0016\u0012WKVA\u007f\u000b";
        int n3 = "GaI\u001d|N\bAmL\u001dc\u001c\u000eRkV\u001dd\u0016\u0011GoP\u001du\u0017\u001dFbAgb\u0018\u001fA\u001bA`@C\u007f\u0010\u0012P W\\s\u0012\u0019PATGy\u0016\u0012WKVA\u007f\u000b".length();
        int n4 = 36;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = SsgNio2Endpoint.a(106, SsgNio2Endpoint.a(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                e = stringArray;
                d = ConfigFactory.getBooleanProperty((String)e[0], (boolean)true);
                return;
            }
            n4 = string.charAt(n5);
        }
    }

    private static Throwable a(Throwable throwable) {
        return throwable;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x16);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 78;
                    case 1 -> 100;
                    case 2 -> 78;
                    case 3 -> 89;
                    case 4 -> 122;
                    case 5 -> 19;
                    default -> 22;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

