/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.transport.SsgConnector
 *  com.l7tech.util.InetAddressUtil
 *  org.apache.ftpserver.ConnectionConfig
 *  org.apache.ftpserver.ConnectionConfigFactory
 *  org.apache.ftpserver.DataConnectionConfiguration
 *  org.apache.ftpserver.DataConnectionConfigurationFactory
 *  org.apache.ftpserver.FtpServer
 *  org.apache.ftpserver.command.CommandFactory
 *  org.apache.ftpserver.ftplet.Ftplet
 *  org.apache.ftpserver.ftplet.UserManager
 *  org.apache.ftpserver.impl.DefaultFtpServer
 *  org.apache.ftpserver.impl.FtpServerContext
 *  org.apache.ftpserver.listener.Listener
 *  org.apache.ftpserver.listener.ListenerFactory
 *  org.apache.ftpserver.ssl.SslConfiguration
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.l7tech.server.transport.ftp;

import com.l7tech.gateway.common.transport.SsgConnector;
import com.l7tech.server.ServerConfig;
import com.l7tech.server.transport.ListenerException;
import com.l7tech.server.transport.SsgConnectorManager;
import com.l7tech.server.transport.ftp.FtpRequestProcessor;
import com.l7tech.server.transport.ftp.FtpRequestProcessorFactory;
import com.l7tech.server.transport.ftp.FtpSslFactory;
import com.l7tech.server.transport.ftp.SsgFtpServerContext;
import com.l7tech.server.transport.ftp.SsgFtpletFactory;
import com.l7tech.server.transport.ftp.d;
import com.l7tech.server.transport.ftp.g;
import com.l7tech.server.transport.ftp.h;
import com.l7tech.util.InetAddressUtil;
import org.apache.ftpserver.ConnectionConfig;
import org.apache.ftpserver.ConnectionConfigFactory;
import org.apache.ftpserver.DataConnectionConfiguration;
import org.apache.ftpserver.DataConnectionConfigurationFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.command.CommandFactory;
import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.impl.DefaultFtpServer;
import org.apache.ftpserver.impl.FtpServerContext;
import org.apache.ftpserver.listener.Listener;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.ssl.SslConfiguration;
import org.springframework.beans.factory.annotation.Autowired;

public class SsgFtpServerFactory {
    private static final String a;
    private static final String b;
    private static final String c;
    private static final String d;
    private static final String e;
    private static final String f;
    private static final String g;
    private static final String h;
    private static final String i;
    private static final String j;
    @Autowired
    private FtpSslFactory k;
    @Autowired
    private SsgFtpletFactory l;
    @Autowired
    private FtpRequestProcessorFactory m;
    @Autowired
    private SsgConnectorManager n;
    @Autowired
    private ServerConfig o;
    private static final String[] p;

    public FtpServer create(SsgConnector ssgConnector) throws ListenerException {
        return new DefaultFtpServer((FtpServerContext)this.a(ssgConnector));
    }

    private SsgFtpServerContext a(SsgConnector ssgConnector) throws ListenerException {
        String string;
        SsgFtpServerContext ssgFtpServerContext;
        Listener listener;
        SsgFtpServerContext ssgFtpServerContext2;
        block3: {
            block2: {
                CommandFactory commandFactory = this.b(ssgConnector);
                ConnectionConfig connectionConfig = this.c(ssgConnector);
                FtpRequestProcessor ftpRequestProcessor = this.a(ssgConnector, connectionConfig);
                UserManager userManager = this.d(ssgConnector);
                ssgFtpServerContext2 = new SsgFtpServerContext(commandFactory, connectionConfig, ftpRequestProcessor, userManager);
                listener = this.e(ssgConnector);
                try {
                    ssgFtpServerContext = ssgFtpServerContext2;
                    if (!listener.isImplicitSsl()) break block2;
                    string = p[3];
                    break block3;
                }
                catch (ListenerException listenerException) {
                    throw SsgFtpServerFactory.a(listenerException);
                }
            }
            String[] stringArray = p;
            string = stringArray[12];
        }
        ssgFtpServerContext.addListener(string, listener);
        ssgFtpServerContext2.addFtplet(p[11], this.a());
        return ssgFtpServerContext2;
    }

    private CommandFactory b(SsgConnector ssgConnector) {
        Object object;
        boolean bl2 = ssgConnector.getBooleanProperty(SsgConnector.PROP_SUPPORT_EXTENDED_FTP_COMMANDS);
        try {
            object = bl2 ? new d() : new h();
        }
        catch (NumberFormatException numberFormatException) {
            throw SsgFtpServerFactory.a(numberFormatException);
        }
        return object;
    }

    private ConnectionConfig c(SsgConnector ssgConnector) throws ListenerException {
        int n2;
        int n3;
        int n4;
        boolean bl2;
        block4: {
            block5: {
                String[] stringArray = p;
                String string = ssgConnector.getProperty(stringArray[1]);
                if (null != string) {
                    bl2 = Boolean.parseBoolean(string);
                } else {
                    stringArray = p;
                    bl2 = Boolean.parseBoolean(this.o.getProperty(stringArray[4]));
                }
                stringArray = p;
                n4 = this.a(ssgConnector, stringArray[13]);
                n3 = this.a(ssgConnector, stringArray[19]);
                n2 = this.a(ssgConnector, stringArray[9]);
                try {
                    if (n2 >= 1) break block4;
                    if (n3 <= 0) break block5;
                }
                catch (ListenerException listenerException) {
                    throw SsgFtpServerFactory.a(listenerException);
                }
                n2 = n3;
                break block4;
            }
            n2 = 10;
        }
        ConnectionConfigFactory connectionConfigFactory = new ConnectionConfigFactory();
        connectionConfigFactory.setAnonymousLoginEnabled(bl2);
        connectionConfigFactory.setMaxAnonymousLogins(n4);
        connectionConfigFactory.setMaxLogins(n3);
        connectionConfigFactory.setMaxThreads(n2);
        return connectionConfigFactory.createConnectionConfig();
    }

    private FtpRequestProcessor a(SsgConnector ssgConnector, ConnectionConfig connectionConfig) throws ListenerException {
        return this.m.create(ssgConnector, connectionConfig);
    }

    private UserManager d(SsgConnector ssgConnector) throws ListenerException {
        String[] stringArray = p;
        int n2 = this.a(ssgConnector, stringArray[21]);
        int n3 = this.a(ssgConnector, stringArray[18]);
        int n4 = this.a(ssgConnector, stringArray[14]);
        return new g(n2, n3, n4);
    }

    private Listener e(SsgConnector ssgConnector) throws ListenerException {
        SslConfiguration sslConfiguration;
        String string;
        ListenerFactory listenerFactory;
        block6: {
            String string2;
            block8: {
                block7: {
                    listenerFactory = new ListenerFactory();
                    String[] stringArray = p;
                    int n2 = this.a(ssgConnector, stringArray[17]);
                    listenerFactory.setIdleTimeout(n2);
                    listenerFactory.setPort(ssgConnector.getPort());
                    string = this.n.translateBindAddress(ssgConnector.getProperty(SsgConnector.PROP_BIND_ADDRESS), ssgConnector.getPort());
                    try {
                        try {
                            if (null != string) break block6;
                            if (!InetAddressUtil.isUseIpv6()) break block7;
                        }
                        catch (ListenerException listenerException) {
                            throw SsgFtpServerFactory.a(listenerException);
                        }
                        string2 = p[10];
                        break block8;
                    }
                    catch (ListenerException listenerException) {
                        throw SsgFtpServerFactory.a(listenerException);
                    }
                }
                string2 = p[23];
            }
            string = string2;
        }
        listenerFactory.setServerAddress(string);
        boolean bl2 = SsgConnector.SCHEME_FTPS.equals(ssgConnector.getScheme());
        try {
            listenerFactory.setImplicitSsl(bl2);
            sslConfiguration = bl2 ? this.f(ssgConnector) : null;
        }
        catch (ListenerException listenerException) {
            throw SsgFtpServerFactory.a(listenerException);
        }
        SslConfiguration sslConfiguration2 = sslConfiguration;
        listenerFactory.setSslConfiguration(sslConfiguration2);
        listenerFactory.setDataConnectionConfiguration(this.a(ssgConnector, sslConfiguration2));
        return listenerFactory.createListener();
    }

    private DataConnectionConfiguration a(SsgConnector ssgConnector, SslConfiguration sslConfiguration) throws ListenerException {
        boolean bl2;
        DataConnectionConfigurationFactory dataConnectionConfigurationFactory;
        String[] stringArray = p;
        int n2 = this.a(stringArray[6], ssgConnector.getProperty(SsgConnector.PROP_PORT_RANGE_START));
        int n3 = this.a(stringArray[0], ssgConnector.getProperty(SsgConnector.PROP_PORT_RANGE_COUNT)) + n2 - 1;
        DataConnectionConfigurationFactory dataConnectionConfigurationFactory2 = new DataConnectionConfigurationFactory();
        try {
            dataConnectionConfigurationFactory2.setActiveEnabled(false);
            dataConnectionConfigurationFactory2.setPassivePorts(n2 + "-" + n3);
            dataConnectionConfigurationFactory = dataConnectionConfigurationFactory2;
            bl2 = null != sslConfiguration;
        }
        catch (ListenerException listenerException) {
            throw SsgFtpServerFactory.a(listenerException);
        }
        dataConnectionConfigurationFactory.setImplicitSsl(bl2);
        dataConnectionConfigurationFactory2.setSslConfiguration(sslConfiguration);
        return dataConnectionConfigurationFactory2.createDataConnectionConfiguration();
    }

    private SslConfiguration f(SsgConnector ssgConnector) throws ListenerException {
        return this.k.create(ssgConnector);
    }

    private Ftplet a() throws ListenerException {
        return this.l.create();
    }

    private int a(SsgConnector ssgConnector, String string) throws ListenerException {
        String string2 = ssgConnector.getProperty(string);
        int n2 = null != string2 ? this.a(string, string2) : this.a(string, this.o.getProperty(string));
        return n2;
    }

    private int a(String string, String string2) throws ListenerException {
        try {
            return Integer.parseInt(string2);
        }
        catch (NumberFormatException numberFormatException) {
            String[] stringArray = p;
            throw new ListenerException(stringArray[5] + string + stringArray[20] + string2, numberFormatException);
        }
    }

    private static Exception a(Exception exception) {
        return exception;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[24];
                var3_1 = 0;
                var2_2 = "|,z<\u001bI\u001fNXX}\u0005A\b\u001a\u001bEi\u0005R\u001a\\\fZ2\nH\u0002T\u0001Gs\u001eU!U\u001fCr\u0018c\u0003[\u001aFy\u000f\u001f\\\fZ2\u0006G\u0015h\u001d[i\u000eU\u0019j\nE\u007f\u000eU\u001eS\u0016MH\u0003T\b[\u001cY\u0006I\u001dIi\u0019C\u001a\\\fZ2\nH\u0002T\u0001Gs\u001eU!U\u001fCr\u0018c\u0003[\u001aFy\u000f\u001ds\u0016\\}\u0007O\t\u001a\u000eKp\u001eCM\\\u0017X<\u001bG\u001f[\u0015Oh\u000eTM\u001d\u0014|,z<\u001bI\u001fNXX}\u0005A\b\u001a\u000b^}\u0019R\u0017\\\fZ2\u0006G\u0015y\u0017D\u007f\u001eT\u001f_\u0016^P\u0004A\u0004T\u000b \\\fZ2\u001eU\bH5Kd(I\u0003Y\rXn\u000eH\u0019v\u0017Mu\u0005U=_\ncl\u001f\\\fZ2\u0006G\u0015h\u001d[i\u000eU\u0019j\nE\u007f\u000eU\u001eS\u0016MH\u0003T\b[\u001cY\u0002\u0000B\u0007^\u001dL}\u001eJ\u0019\u0007^\u001dL}\u001eJ\u0019\u0016\\\fZ2\u0006G\u0015{\u0016Er\u0012K\u0002O\u000bfs\fO\u0003I\u0016\\\fZ2\u0018C\u001eI\u0011Er\"B\u0001_,Cq\u000eI\u0018N\u0006I\u001dIi\u0019C\u0016\\\fZ2\u0006G\u0015{\u0016Er\u0012K\u0002O\u000bfs\fO\u0003I\u0016\\\fZ2\u0018C\u001eI\u0011Er\"B\u0001_,Cq\u000eI\u0018N \\\fZ2\u001eU\bH5Kd(I\u0003Y\rXn\u000eH\u0019v\u0017Mu\u0005U=_\ncl\u0017\\\fZ2\u0006G\u0015y\u0017D\u007f\u001eT\u001f_\u0016^P\u0004A\u0004T\u000b\u0003\u001dB\n\u001b\\\fZ2\u001eU\bH5Kd(I\u0003Y\rXn\u000eH\u0019v\u0017Mu\u0005U";
                var4_3 = "|,z<\u001bI\u001fNXX}\u0005A\b\u001a\u001bEi\u0005R\u001a\\\fZ2\nH\u0002T\u0001Gs\u001eU!U\u001fCr\u0018c\u0003[\u001aFy\u000f\u001f\\\fZ2\u0006G\u0015h\u001d[i\u000eU\u0019j\nE\u007f\u000eU\u001eS\u0016MH\u0003T\b[\u001cY\u0006I\u001dIi\u0019C\u001a\\\fZ2\nH\u0002T\u0001Gs\u001eU!U\u001fCr\u0018c\u0003[\u001aFy\u000f\u001ds\u0016\\}\u0007O\t\u001a\u000eKp\u001eCM\\\u0017X<\u001bG\u001f[\u0015Oh\u000eTM\u001d\u0014|,z<\u001bI\u001fNXX}\u0005A\b\u001a\u000b^}\u0019R\u0017\\\fZ2\u0006G\u0015y\u0017D\u007f\u001eT\u001f_\u0016^P\u0004A\u0004T\u000b \\\fZ2\u001eU\bH5Kd(I\u0003Y\rXn\u000eH\u0019v\u0017Mu\u0005U=_\ncl\u001f\\\fZ2\u0006G\u0015h\u001d[i\u000eU\u0019j\nE\u007f\u000eU\u001eS\u0016MH\u0003T\b[\u001cY\u0002\u0000B\u0007^\u001dL}\u001eJ\u0019\u0007^\u001dL}\u001eJ\u0019\u0016\\\fZ2\u0006G\u0015{\u0016Er\u0012K\u0002O\u000bfs\fO\u0003I\u0016\\\fZ2\u0018C\u001eI\u0011Er\"B\u0001_,Cq\u000eI\u0018N\u0006I\u001dIi\u0019C\u0016\\\fZ2\u0006G\u0015{\u0016Er\u0012K\u0002O\u000bfs\fO\u0003I\u0016\\\fZ2\u0018C\u001eI\u0011Er\"B\u0001_,Cq\u000eI\u0018N \\\fZ2\u001eU\bH5Kd(I\u0003Y\rXn\u000eH\u0019v\u0017Mu\u0005U=_\ncl\u0017\\\fZ2\u0006G\u0015y\u0017D\u007f\u001eT\u001f_\u0016^P\u0004A\u0004T\u000b\u0003\u001dB\n\u001b\\\fZ2\u001eU\bH5Kd(I\u0003Y\rXn\u000eH\u0019v\u0017Mu\u0005U".length();
                var1_4 = 20;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 75;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = SsgFtpServerFactory.a(v0, SsgFtpServerFactory.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u001cL\u001ar^\u0015H\bu\u000b$h\tC\u0019M\u0018.N\bY6W\r5E\u0015\u0007J\u0016Zr\u001bH\u001d";
                    var4_3 = "\u001cL\u001ar^\u0015H\bu\u000b$h\tC\u0019M\u0018.N\bY6W\r5E\u0015\u0007J\u0016Zr\u001bH\u001d".length();
                    var1_4 = 27;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 11;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = SsgFtpServerFactory.a(v0, SsgFtpServerFactory.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        SsgFtpServerFactory.p = var5;
        SsgFtpServerFactory.j = SsgFtpServerFactory.p[8];
        SsgFtpServerFactory.f = SsgFtpServerFactory.p[4];
        SsgFtpServerFactory.i = SsgFtpServerFactory.p[22];
        SsgFtpServerFactory.e = SsgFtpServerFactory.p[2];
        SsgFtpServerFactory.a = SsgFtpServerFactory.p[15];
        SsgFtpServerFactory.g = SsgFtpServerFactory.p[16];
        SsgFtpServerFactory.h = SsgFtpServerFactory.p[7];
        SsgFtpServerFactory.c = SsgFtpServerFactory.p[11];
        SsgFtpServerFactory.b = SsgFtpServerFactory.p[11];
        SsgFtpServerFactory.d = SsgFtpServerFactory.p[14];
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x26);
        }
        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 -> 113;
                    case 1 -> 51;
                    case 2 -> 97;
                    case 3 -> 87;
                    case 4 -> 32;
                    case 5 -> 109;
                    default -> 38;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

