/*
 * 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.NioChannel
 *  org.apache.tomcat.util.net.NioEndpoint
 *  org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper
 *  org.apache.tomcat.util.net.SSLHostConfig
 *  org.apache.tomcat.util.net.SecureNioChannel
 *  org.apache.tomcat.util.net.SocketBufferHandler
 *  org.apache.tomcat.util.net.openssl.ciphers.Cipher
 */
package com.l7tech.server.tomcat.nio;

import com.l7tech.objectmodel.Goid;
import com.l7tech.server.tomcat.SsgConnectorAwareEndpoint;
import com.l7tech.server.tomcat.SsgSSLImplementation;
import com.l7tech.server.tomcat.nio.SsgSocketChannel;
import com.l7tech.server.tomcat.nio.a;
import com.l7tech.server.tomcat.nio.b;
import com.l7tech.server.transport.tls.SsgConnectorSslHelper;
import com.l7tech.util.ConfigFactory;
import java.nio.channels.SocketChannel;
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.NioChannel;
import org.apache.tomcat.util.net.NioEndpoint;
import org.apache.tomcat.util.net.SSLHostConfig;
import org.apache.tomcat.util.net.SecureNioChannel;
import org.apache.tomcat.util.net.SocketBufferHandler;
import org.apache.tomcat.util.net.openssl.ciphers.Cipher;

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

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

    public SocketChannel serverSocketAccept() throws Exception {
        return new SsgSocketChannel(super.serverSocketAccept(), this.d);
    }

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

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

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

    protected boolean setSocketOptions(SocketChannel socketChannel) {
        NioEndpoint.NioSocketWrapper nioSocketWrapper = null;
        try {
            NioChannel nioChannel = null;
            SynchronizedStack synchronizedStack = super.getNioChannels();
            if (synchronizedStack != null) {
                nioChannel = (NioChannel)synchronizedStack.pop();
            }
            if (nioChannel == null) {
                nioChannel = this.a();
            }
            NioEndpoint.NioSocketWrapper nioSocketWrapper2 = new NioEndpoint.NioSocketWrapper(nioChannel, (NioEndpoint)this);
            nioChannel.reset(socketChannel, nioSocketWrapper2);
            this.connections.put(socketChannel, nioSocketWrapper2);
            nioSocketWrapper = nioSocketWrapper2;
            try {
                socketChannel.configureBlocking(false);
                if (this.getUnixDomainSocketPath() == null) {
                    this.socketProperties.setProperties(socketChannel.socket());
                }
            }
            catch (Throwable throwable) {
                throw SsgNioEndpoint.a(throwable);
            }
            nioSocketWrapper.setReadTimeout((long)this.getConnectionTimeout());
            nioSocketWrapper.setWriteTimeout((long)this.getConnectionTimeout());
            nioSocketWrapper.setKeepAliveLeft(this.getMaxKeepAliveRequests());
            nioSocketWrapper.getPoller().register(nioSocketWrapper);
            return true;
        }
        catch (Throwable throwable) {
            ExceptionUtils.handleThrowable((Throwable)throwable);
            try {
                Log log = super.getLog();
                log.error((Object)sm.getString(e[1]), throwable);
            }
            catch (Throwable throwable2) {
                ExceptionUtils.handleThrowable((Throwable)throwable2);
            }
            try {
                if (nioSocketWrapper == null) {
                    this.destroySocket(socketChannel);
                }
            }
            catch (Throwable throwable3) {
                throw SsgNioEndpoint.a(throwable3);
            }
            return false;
        }
    }

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

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

    private NioChannel a() {
        SocketBufferHandler socketBufferHandler;
        block7: {
            block8: {
                socketBufferHandler = new SocketBufferHandler(this.socketProperties.getAppReadBufSize(), this.socketProperties.getAppWriteBufSize(), this.socketProperties.getDirectBuffer());
                try {
                    try {
                        if (a) break block7;
                        if (!this.isSSLEnabled()) break block8;
                    }
                    catch (IllegalArgumentException illegalArgumentException) {
                        throw SsgNioEndpoint.a(illegalArgumentException);
                    }
                    return new SecureNioChannel(socketBufferHandler, (NioEndpoint)this);
                }
                catch (IllegalArgumentException illegalArgumentException) {
                    throw SsgNioEndpoint.a(illegalArgumentException);
                }
            }
            return new NioChannel(socketBufferHandler);
        }
        try {
            if (this.isSSLEnabled()) {
                return this.b(socketBufferHandler);
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw SsgNioEndpoint.a(illegalArgumentException);
        }
        return this.a(socketBufferHandler);
    }

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

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

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[2];
        int n2 = 0;
        String string = "\u001ao\nf\u0006/]\u001cc\u000ff\u0019}[\u000fe\u0015f\u001ewD\u001aa\u0013f\u000fvH\u001bl\u0002\u001c\u0018yJ\u001c\u001b\u001cn\u00038\u0005qG\r.\u0014'\tsL\rO\u0017<\u0003wG\nE\u0015:\u0005j";
        int n3 = "\u001ao\nf\u0006/]\u001cc\u000ff\u0019}[\u000fe\u0015f\u001ewD\u001aa\u0013f\u000fvH\u001bl\u0002\u001c\u0018yJ\u001c\u001b\u001cn\u00038\u0005qG\r.\u0014'\tsL\rO\u0017<\u0003wG\nE\u0015:\u0005j".length();
        int n4 = 36;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = SsgNioEndpoint.a(7, SsgNioEndpoint.a(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                e = stringArray;
                a = 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] ^ 0x2E);
        }
        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 -> 126;
                    case 1 -> 7;
                    case 2 -> 96;
                    case 3 -> 79;
                    case 4 -> 109;
                    case 5 -> 31;
                    default -> 46;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

