/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.Component
 *  com.l7tech.gateway.common.LicenseManager
 *  com.l7tech.gateway.common.transport.SsgConnector
 *  com.l7tech.gateway.common.transport.SsgConnector$Endpoint
 *  com.l7tech.gateway.common.transport.TransportDescriptor
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.util.CollectionUtils
 *  com.l7tech.util.Config
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.Pair
 *  org.apache.ftpserver.FtpServer
 *  org.springframework.context.ApplicationEvent
 */
package com.l7tech.server.transport.ftp;

import com.l7tech.gateway.common.Component;
import com.l7tech.gateway.common.LicenseManager;
import com.l7tech.gateway.common.transport.SsgConnector;
import com.l7tech.gateway.common.transport.TransportDescriptor;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.server.DefaultKey;
import com.l7tech.server.GatewayState;
import com.l7tech.server.LifecycleException;
import com.l7tech.server.event.system.ReadyForMessages;
import com.l7tech.server.identity.cert.TrustedCertServices;
import com.l7tech.server.transport.ListenerException;
import com.l7tech.server.transport.SsgConnectorManager;
import com.l7tech.server.transport.TransportModule;
import com.l7tech.server.transport.ftp.DefaultFtpsCiphers;
import com.l7tech.server.transport.ftp.SsgFtpServerFactory;
import com.l7tech.util.CollectionUtils;
import com.l7tech.util.Config;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.Pair;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.apache.ftpserver.FtpServer;
import org.springframework.context.ApplicationEvent;

public class FtpServerManager
extends TransportModule {
    private static final Logger l;
    private final GatewayState m;
    private final Set<String> n;
    private final Map<Goid, Pair<SsgConnector, FtpServer>> o;
    private SsgFtpServerFactory p;
    private static final String[] q;

    public FtpServerManager(Config config, GatewayState gatewayState, LicenseManager licenseManager, DefaultKey defaultKey, TrustedCertServices trustedCertServices, SsgConnectorManager ssgConnectorManager) {
        String[] stringArray = q;
        super(stringArray[11], Component.GW_FTPSERVER, l, stringArray[17], licenseManager, ssgConnectorManager, trustedCertServices, defaultKey, config);
        this.n = CollectionUtils.caseInsensitiveSet((String[])new String[]{SsgConnector.SCHEME_FTP, SsgConnector.SCHEME_FTPS});
        this.o = new ConcurrentHashMap<Goid, Pair<SsgConnector, FtpServer>>();
        this.m = gatewayState;
    }

    /*
     * WARNING - Removed back jump from a try to a catch block - possible behaviour change.
     * Unable to fully structure code
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    @Override
    public void onApplicationEvent(ApplicationEvent var1_1) {
        try {
            if (TransportModule.isEventIgnorable(var1_1)) {
                return;
            }
        }
        catch (LifecycleException v0) {
            throw FtpServerManager.a(v0);
        }
        try {
            super.onApplicationEvent(var1_1);
            if (!this.isStarted()) {
                return;
            }
        }
        catch (LifecycleException v1) {
            throw FtpServerManager.a(v1);
        }
        try {
            if (var1_1 instanceof ReadyForMessages == false) return;
            ** GOTO lbl21
        }
        catch (LifecycleException v2) {
            try {
                throw FtpServerManager.a(v2);
lbl21:
                // 2 sources

                if (this.o.isEmpty() == false) return;
            }
            catch (RuntimeException v3) {
                throw FtpServerManager.a(v3);
            }
            {
                try {}
                catch (LifecycleException var2_2) {
                    var3_3 = FtpServerManager.q;
                    this.auditError(var3_3[4], var3_3[10], var2_2);
                    return;
                }
            }
        }
        this.b();
    }

    @Override
    protected boolean isValidConnectorConfig(SsgConnector ssgConnector) {
        try {
            if (!super.isValidConnectorConfig(ssgConnector)) {
                return false;
            }
        }
        catch (RuntimeException runtimeException) {
            throw FtpServerManager.a(runtimeException);
        }
        try {
            if (!ssgConnector.offersEndpoint(SsgConnector.Endpoint.MESSAGE_INPUT)) {
                l.log(Level.WARNING, q[3] + String.valueOf(ssgConnector.getGoid()) + q[12]);
                return false;
            }
        }
        catch (RuntimeException runtimeException) {
            throw FtpServerManager.a(runtimeException);
        }
        return true;
    }

    @Override
    public void reportMisconfiguredConnector(Goid goid) {
        String[] stringArray = q;
        l.log(Level.WARNING, stringArray[7] + String.valueOf(goid) + stringArray[21]);
        this.removeConnector(goid);
    }

    @Override
    protected void addConnector(SsgConnector ssgConnector) throws ListenerException {
        try {
            if (!this.isLicensed()) {
                return;
            }
        }
        catch (Exception exception) {
            throw FtpServerManager.a(exception);
        }
        ssgConnector = ssgConnector.getReadOnlyCopy();
        try {
            this.removeConnector(ssgConnector.getGoid());
            if (!this.connectorIsOwnedByThisModule(ssgConnector)) {
                return;
            }
        }
        catch (Exception exception) {
            throw FtpServerManager.a(exception);
        }
        FtpServer ftpServer = this.createFtpServer(ssgConnector);
        this.auditStart(ssgConnector.getScheme(), this.describe(ssgConnector));
        try {
            ftpServer.start();
            this.o.put(ssgConnector.getGoid(), (Pair<SsgConnector, FtpServer>)Pair.pair((Object)ssgConnector, (Object)ftpServer));
        }
        catch (Exception exception) {
            String[] stringArray = q;
            throw new ListenerException(stringArray[19] + this.describe(ssgConnector) + stringArray[18] + ExceptionUtils.getMessage((Throwable)exception), exception);
        }
    }

    @Override
    protected void removeConnector(Goid goid) {
        Pair<SsgConnector, FtpServer> pair = this.o.remove(goid);
        try {
            if (pair == null) {
                return;
            }
        }
        catch (Exception exception) {
            throw FtpServerManager.a(exception);
        }
        String string = this.describe((SsgConnector)pair.left);
        this.auditStop(((SsgConnector)pair.left).getScheme(), string);
        try {
            ((FtpServer)pair.right).stop();
        }
        catch (Exception exception) {
            String[] stringArray = q;
            l.log(Level.WARNING, stringArray[6] + string + stringArray[15] + ExceptionUtils.getMessage((Throwable)exception), exception);
        }
    }

    @Override
    protected Set<String> getSupportedSchemes() {
        return this.n;
    }

    @Override
    protected void doStart() throws LifecycleException {
        try {
            this.c();
            if (this.m.isReadyForMessages()) {
                this.b();
            }
        }
        catch (Exception exception) {
            String[] stringArray = q;
            this.auditError(stringArray[1], stringArray[5], exception);
        }
    }

    @Override
    protected void doStop() throws LifecycleException {
        try {
            this.d();
            for (Goid goid : this.o.keySet()) {
                this.removeConnector(goid);
            }
        }
        catch (Exception exception) {
            String[] stringArray = q;
            this.auditError(stringArray[4], stringArray[9], exception);
        }
    }

    public FtpServer createFtpServer(SsgConnector ssgConnector) throws ListenerException {
        try {
            if (null == this.p) {
                this.p = (SsgFtpServerFactory)this.getApplicationContext().getBean(SsgFtpServerFactory.class);
            }
        }
        catch (ListenerException listenerException) {
            throw FtpServerManager.a(listenerException);
        }
        return this.p.create(ssgConnector);
    }

    private List<SsgConnector> a() throws FindException {
        Collection collection = this.ssgConnectorManager.findAll();
        ArrayList<SsgConnector> arrayList = new ArrayList<SsgConnector>();
        for (SsgConnector ssgConnector : collection) {
            try {
                try {
                    if (!ssgConnector.isEnabled() || !this.connectorIsOwnedByThisModule(ssgConnector)) continue;
                }
                catch (FindException findException) {
                    throw FtpServerManager.a((Exception)((Object)findException));
                }
                arrayList.add(ssgConnector);
            }
            catch (FindException findException) {
                throw FtpServerManager.a((Exception)((Object)findException));
            }
        }
        return arrayList;
    }

    private void b() throws LifecycleException {
        try {
            List<SsgConnector> list = this.a();
            for (SsgConnector ssgConnector : list) {
                try {
                    this.addConnector(ssgConnector);
                }
                catch (ListenerException listenerException) {
                    String string = ExceptionUtils.getMessage((Throwable)listenerException);
                    int n2 = ssgConnector.getPort();
                    String string2 = String.valueOf(ssgConnector.getGoid());
                    String[] stringArray = q;
                    l.log(Level.WARNING, stringArray[8] + string2 + stringArray[2] + n2 + stringArray[14] + string, ExceptionUtils.getDebugException((Throwable)listenerException));
                }
            }
        }
        catch (FindException findException) {
            throw new RuntimeException(q[0] + ExceptionUtils.getMessage((Throwable)findException), findException);
        }
    }

    private void c() {
        TransportDescriptor transportDescriptor = new TransportDescriptor(SsgConnector.SCHEME_FTP, false);
        transportDescriptor.setFtpBased(true);
        transportDescriptor.setSupportsHardwiredServiceResolution(true);
        transportDescriptor.setSupportsSpecifiedContentType(true);
        this.ssgConnectorManager.registerTransportProtocol(transportDescriptor, this);
        TransportDescriptor transportDescriptor2 = new TransportDescriptor(SsgConnector.SCHEME_FTPS, true);
        transportDescriptor2.setFtpBased(true);
        transportDescriptor2.setSupportsHardwiredServiceResolution(true);
        transportDescriptor2.setSupportsSpecifiedContentType(true);
        String[] stringArray = new String[3];
        String[] stringArray2 = q;
        stringArray[0] = stringArray2[13];
        stringArray[1] = stringArray2[16];
        stringArray[2] = stringArray2[20];
        transportDescriptor2.setSupportedTlsVersions(Arrays.stream(stringArray).collect(Collectors.toSet()));
        transportDescriptor2.setEnabledCipherSuites(DefaultFtpsCiphers.getFtpsAllVisibleCiphers());
        this.ssgConnectorManager.registerTransportProtocol(transportDescriptor2, this);
    }

    private void d() {
        this.ssgConnectorManager.unregisterTransportProtocol(SsgConnector.SCHEME_FTP);
        this.ssgConnectorManager.unregisterTransportProtocol(SsgConnector.SCHEME_FTPS);
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[22];
                var3_1 = 0;
                var2_2 = "B\u0004M&\n\u001fqc\u0005\f\"\u000f\u001457\u0003B-\u0012\u00130{Jj\u00106Z2x\u0004B!\u0005\u000e>e\u0019\u0016d\u0006Q>|l5S\u00157BY7\u000f\u001467\tC*\u0012\b>{J\\+\u0014\u000eq\u0012Q>|d\u0005\u0015?y\u000fO0\t\bqX#hd\u0006Q>|l5S\u0015R\u0018^+\u0014Z5b\u0018E*\u0001Z\"c\u000b^0\u0013\n\u007f\u001eR\u0018^+\u0014Z\"c\u0005\\4\u000f\u001467,x\u0014F\t4e\u001cI6F\u001c>eJ?D\u0002Y0\u0012\u0013?pJH+\u0011\u0014qQ>|d\u0005\u0015?y\u000fO0\t\bqq\u0005^d\u0005\u0015?c\u0018C(F\n>e\u001e\f+\u0000Z2x\u0004B!\u0005\u000e>eJk\u000b/>q#B\u0004M&\n\u001fqc\u0005\f7\u0012\u001b#cJj\u00106Z2x\u0004B!\u0005\u000e>eJk\u000b/>q\u001aR\u0018^+\u0014Z&\u007f\u0003@!F\t9b\u001eX-\b\u001dqs\u0005[*H\u0015R\u0018^+\u0014Z5b\u0018E*\u0001Z\"c\u000b^0\u0013\n\u007f\u0012Q>|d5\u001f#a\u000f^d+\u001b?v\rI6/7\u000eC!\u0015Z?x\u001e\f%\n\u0016>`J\\1\u0004\u00168d\u0002I F\t4e\u001cE'\u0003Z<r\u0019_%\u0001\u001fq~\u0004\\1\u0012\u0005C&\u007f2W\u0003>P\f\u0002-J\u0007C&\u007f2WT`\u0017d\u000f^2\u000f\u00194-,X4+\u001f\"d\u000bK!/\u0014!b\u001e\u0002-J\u001bB\u0004M&\n\u001fqc\u0005\f7\u0012\u001b#cJj\u00106Z\"r\u0018Z!\u0014Z";
                var4_3 = "B\u0004M&\n\u001fqc\u0005\f\"\u000f\u001457\u0003B-\u0012\u00130{Jj\u00106Z2x\u0004B!\u0005\u000e>e\u0019\u0016d\u0006Q>|l5S\u00157BY7\u000f\u001467\tC*\u0012\b>{J\\+\u0014\u000eq\u0012Q>|d\u0005\u0015?y\u000fO0\t\bqX#hd\u0006Q>|l5S\u0015R\u0018^+\u0014Z5b\u0018E*\u0001Z\"c\u000b^0\u0013\n\u007f\u001eR\u0018^+\u0014Z\"c\u0005\\4\u000f\u001467,x\u0014F\t4e\u001cI6F\u001c>eJ?D\u0002Y0\u0012\u0013?pJH+\u0011\u0014qQ>|d\u0005\u0015?y\u000fO0\t\bqq\u0005^d\u0005\u0015?c\u0018C(F\n>e\u001e\f+\u0000Z2x\u0004B!\u0005\u000e>eJk\u000b/>q#B\u0004M&\n\u001fqc\u0005\f7\u0012\u001b#cJj\u00106Z2x\u0004B!\u0005\u000e>eJk\u000b/>q\u001aR\u0018^+\u0014Z&\u007f\u0003@!F\t9b\u001eX-\b\u001dqs\u0005[*H\u0015R\u0018^+\u0014Z5b\u0018E*\u0001Z\"c\u000b^0\u0013\n\u007f\u0012Q>|d5\u001f#a\u000f^d+\u001b?v\rI6/7\u000eC!\u0015Z?x\u001e\f%\n\u0016>`J\\1\u0004\u00168d\u0002I F\t4e\u001cE'\u0003Z<r\u0019_%\u0001\u001fq~\u0004\\1\u0012\u0005C&\u007f2W\u0003>P\f\u0002-J\u0007C&\u007f2WT`\u0017d\u000f^2\u000f\u00194-,X4+\u001f\"d\u000bK!/\u0014!b\u001e\u0002-J\u001bB\u0004M&\n\u001fqc\u0005\f7\u0012\u001b#cJj\u00106Z\"r\u0018Z!\u0014Z".length();
                var1_4 = 39;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = FtpServerManager.c(v0, FtpServerManager.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u0013v/b\u0007\u00043;gX\u0019wW_r\"\u001a\u0015`\u0016I`)T\u0013`\u0016HdgU\fqXOegM\u0015`^\nh3I\\wCXs\"T\b4UEo!S\u001baDKu.U\u0012";
                    var4_3 = "\u0013v/b\u0007\u00043;gX\u0019wW_r\"\u001a\u0015`\u0016I`)T\u0013`\u0016HdgU\fqXOegM\u0015`^\nh3I\\wCXs\"T\b4UEo!S\u001baDKu.U\u0012".length();
                    var1_4 = 7;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 33;
                        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++] = FtpServerManager.c(v0, FtpServerManager.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;
            }
        }
        FtpServerManager.q = var5;
        FtpServerManager.l = Logger.getLogger(FtpServerManager.class.getName());
    }

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

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

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String c(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 -> 102;
                    case 1 -> 27;
                    case 2 -> 93;
                    case 3 -> 53;
                    case 4 -> 23;
                    case 5 -> 11;
                    default -> 32;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

