/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.Audit
 *  com.l7tech.gateway.common.audit.LoggingAudit
 *  com.l7tech.gateway.common.transport.ftp.FtpAdmin
 *  com.l7tech.gateway.common.transport.ftp.FtpClientConfig
 *  com.l7tech.gateway.common.transport.ftp.FtpSecurity
 *  com.l7tech.gateway.common.transport.ftp.FtpTestException
 *  com.l7tech.gateway.common.transport.ftp.FtpUtils
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.util.ExceptionUtils
 *  org.jetbrains.annotations.Nullable
 */
package com.l7tech.server.transport.ftp;

import com.l7tech.gateway.common.audit.Audit;
import com.l7tech.gateway.common.audit.LoggingAudit;
import com.l7tech.gateway.common.transport.ftp.FtpAdmin;
import com.l7tech.gateway.common.transport.ftp.FtpClientConfig;
import com.l7tech.gateway.common.transport.ftp.FtpSecurity;
import com.l7tech.gateway.common.transport.ftp.FtpTestException;
import com.l7tech.gateway.common.transport.ftp.FtpUtils;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.server.DefaultKey;
import com.l7tech.server.policy.variable.ServerVariables;
import com.l7tech.server.transport.ftp.FtpClientUtils;
import com.l7tech.util.ExceptionUtils;
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.X509TrustManager;
import org.jetbrains.annotations.Nullable;

public class FtpAdminImpl
implements FtpAdmin {
    private static final Logger a;
    private final X509TrustManager b;
    private final HostnameVerifier c;
    private final DefaultKey d;
    private static final String e;

    public FtpAdminImpl(X509TrustManager x509TrustManager, HostnameVerifier hostnameVerifier, DefaultKey defaultKey) {
        this.b = x509TrustManager;
        this.c = hostnameVerifier;
        this.d = defaultKey;
    }

    public void testConnection(boolean bl2, boolean bl3, boolean bl4, String string, int n2, String string2, String string3, Goid goid, boolean bl5, @Nullable Goid goid2, String string4, String string5, int n3) throws FtpTestException {
        block13: {
            FtpClientConfig ftpClientConfig;
            block12: {
                String string6;
                FtpSecurity ftpSecurity;
                FtpClientConfig ftpClientConfig2;
                block11: {
                    block10: {
                        ftpClientConfig = FtpClientUtils.newConfig(string);
                        try {
                            ftpClientConfig2 = ftpClientConfig;
                            if (bl2) break block10;
                            ftpSecurity = FtpSecurity.FTP_UNSECURED;
                            break block11;
                        }
                        catch (FindException findException) {
                            throw FtpAdminImpl.a(findException);
                        }
                    }
                    try {
                        ftpSecurity = bl3 ? FtpSecurity.FTPS_EXPLICIT : FtpSecurity.FTPS_IMPLICIT;
                    }
                    catch (FindException findException) {
                        throw FtpAdminImpl.a(findException);
                    }
                }
                ftpClientConfig2.setSecurity(ftpSecurity);
                try {
                    string6 = null == goid ? ServerVariables.expandPasswordOnlyVariable((Audit)new LoggingAudit(a), string3) : ServerVariables.getSecurePasswordByGoid((Audit)new LoggingAudit(a), goid);
                }
                catch (FindException findException) {
                    String string7 = e + ExceptionUtils.getMessage((Throwable)findException);
                    throw (FtpTestException)new FtpTestException(string7, string7).initCause((Throwable)findException);
                }
                ftpClientConfig.setPort(n2).setUser(string2).setPass(string6).setDirectory(string5).setTimeout(n3);
                X509TrustManager x509TrustManager = null;
                if (bl4) {
                    ftpClientConfig.setVerifyServerCert(true);
                    x509TrustManager = this.b;
                }
                DefaultKey defaultKey = null;
                if (bl5) {
                    ftpClientConfig.setUseClientCert(true).setClientCertId(goid2).setClientCertAlias(string4);
                    defaultKey = this.d;
                }
                try {
                    if (!bl2) break block12;
                    FtpClientUtils.testFtpsConnection(ftpClientConfig, defaultKey, x509TrustManager, this.c);
                    break block13;
                }
                catch (FindException findException) {
                    throw FtpAdminImpl.a(findException);
                }
            }
            FtpUtils.testFtpConnection((FtpClientConfig)ftpClientConfig);
        }
    }

    /*
     * Enabled aggressive block sorting
     */
    static {
        e = FtpAdminImpl.a(3, FtpAdminImpl.a(" V\u001fcy'>\u0001W^mz-uUM\u000e!f'}\u0000J\u001b!e#m\u0006O\u0011sqbl\u0010^\u001bsp,}\u0010\u0002^"));
        a = Logger.getLogger(FtpAdminImpl.class.getName());
    }

    private static FindException a(FindException findException) {
        return findException;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x1D);
        }
        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 -> 118;
                    case 1 -> 59;
                    case 2 -> 125;
                    case 3 -> 2;
                    case 4 -> 22;
                    case 5 -> 65;
                    default -> 29;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

