/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.CertUtils
 *  com.l7tech.gateway.common.LicenseException
 *  com.l7tech.gateway.common.LicenseManager
 *  com.l7tech.gateway.common.admin.AdminSessionValidationRuntimeException
 *  com.l7tech.gateway.common.admin.Administrative
 *  com.l7tech.gateway.common.admin.LicenseRuntimeException
 *  com.l7tech.gateway.common.cluster.ClusterNodeInfo
 *  com.l7tech.gateway.common.spring.remoting.RemoteUtils
 *  com.l7tech.gateway.common.spring.remoting.RemotingProvider
 *  com.l7tech.gateway.common.spring.remoting.http.SecureHttpInvokerServiceExporter$SecurityCallback
 *  com.l7tech.gateway.common.transport.SsgConnector
 *  com.l7tech.gateway.common.transport.SsgConnector$Endpoint
 *  com.l7tech.identity.AuthenticationException
 *  com.l7tech.identity.User
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.ObjectModelException
 *  com.l7tech.util.ExceptionUtils
 *  jakarta.servlet.ServletRequest
 *  jakarta.servlet.http.HttpServletRequest
 */
package com.l7tech.server.admin;

import com.l7tech.common.io.CertUtils;
import com.l7tech.gateway.common.LicenseException;
import com.l7tech.gateway.common.LicenseManager;
import com.l7tech.gateway.common.admin.AdminSessionValidationRuntimeException;
import com.l7tech.gateway.common.admin.Administrative;
import com.l7tech.gateway.common.admin.LicenseRuntimeException;
import com.l7tech.gateway.common.cluster.ClusterNodeInfo;
import com.l7tech.gateway.common.spring.remoting.RemoteUtils;
import com.l7tech.gateway.common.spring.remoting.RemotingProvider;
import com.l7tech.gateway.common.spring.remoting.http.SecureHttpInvokerServiceExporter;
import com.l7tech.gateway.common.transport.SsgConnector;
import com.l7tech.identity.AuthenticationException;
import com.l7tech.identity.User;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.ObjectModelException;
import com.l7tech.server.DefaultKey;
import com.l7tech.server.admin.AdminLoginHelper;
import com.l7tech.server.admin.AdminSessionManager;
import com.l7tech.server.cluster.ClusterInfoManager;
import com.l7tech.server.transport.http.HttpTransportModule;
import com.l7tech.server.util.JaasUtils;
import com.l7tech.util.ExceptionUtils;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.server.ServerNotActiveException;
import java.security.Principal;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.Subject;

public class AdminRemotingProvider
implements RemotingProvider<Administrative>,
SecureHttpInvokerServiceExporter.SecurityCallback {
    public final String FACILITY_ADMIN;
    public final String FACILITY_CLUSTER;
    private static final Logger a;
    private final LicenseManager b;
    private final AdminSessionManager c;
    private final ClusterInfoManager d;
    private final DefaultKey e;
    private static final String[] f;

    public AdminRemotingProvider(LicenseManager licenseManager, AdminSessionManager adminSessionManager, ClusterInfoManager clusterInfoManager, DefaultKey defaultKey) {
        String[] stringArray = f;
        this.FACILITY_ADMIN = stringArray[4];
        this.FACILITY_CLUSTER = stringArray[2];
        this.b = licenseManager;
        this.c = adminSessionManager;
        this.d = clusterInfoManager;
        this.e = defaultKey;
    }

    public void checkPermitted(Administrative administrative, String string, String string2) {
        block17: {
            block18: {
                boolean bl2;
                AdminRemotingProvider adminRemotingProvider;
                block24: {
                    block23: {
                        boolean bl3;
                        AdminRemotingProvider adminRemotingProvider2;
                        block22: {
                            block21: {
                                block20: {
                                    block16: {
                                        try {
                                            if (!f[16].equals(string)) break block16;
                                            this.b();
                                            break block17;
                                        }
                                        catch (IllegalArgumentException illegalArgumentException) {
                                            throw AdminRemotingProvider.a(illegalArgumentException);
                                        }
                                    }
                                    try {
                                        block19: {
                                            try {
                                                try {
                                                    if (!f[6].equals(string)) break block18;
                                                    if (administrative == null) break block19;
                                                }
                                                catch (IllegalArgumentException illegalArgumentException) {
                                                    throw AdminRemotingProvider.a(illegalArgumentException);
                                                }
                                                if (!administrative.licensed()) break block20;
                                            }
                                            catch (IllegalArgumentException illegalArgumentException) {
                                                throw AdminRemotingProvider.a(illegalArgumentException);
                                            }
                                        }
                                        this.a(string2);
                                    }
                                    catch (IllegalArgumentException illegalArgumentException) {
                                        throw AdminRemotingProvider.a(illegalArgumentException);
                                    }
                                }
                                try {
                                    try {
                                        adminRemotingProvider2 = this;
                                        if (administrative != null && !administrative.authenticated()) break block21;
                                    }
                                    catch (IllegalArgumentException illegalArgumentException) {
                                        throw AdminRemotingProvider.a(illegalArgumentException);
                                    }
                                    bl3 = true;
                                    break block22;
                                }
                                catch (IllegalArgumentException illegalArgumentException) {
                                    throw AdminRemotingProvider.a(illegalArgumentException);
                                }
                            }
                            bl3 = false;
                        }
                        try {
                            try {
                                adminRemotingProvider2.b(bl3);
                                adminRemotingProvider = this;
                                if (administrative != null && administrative.background()) break block23;
                            }
                            catch (IllegalArgumentException illegalArgumentException) {
                                throw AdminRemotingProvider.a(illegalArgumentException);
                            }
                            bl2 = true;
                            break block24;
                        }
                        catch (IllegalArgumentException illegalArgumentException) {
                            throw AdminRemotingProvider.a(illegalArgumentException);
                        }
                    }
                    bl2 = false;
                }
                adminRemotingProvider.a(bl2);
                break block17;
            }
            throw new IllegalArgumentException(f[8] + string);
        }
    }

    private void a(boolean bl2) {
        block4: {
            User user = JaasUtils.getCurrentUser();
            String string = AdminLoginHelper.a();
            try {
                try {
                    if (user == null || !this.c.isExpired(string, bl2)) break block4;
                }
                catch (IllegalArgumentException illegalArgumentException) {
                    throw AdminRemotingProvider.a(illegalArgumentException);
                }
                throw new SecurityException(f[20]);
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw AdminRemotingProvider.a(illegalArgumentException);
            }
        }
    }

    public void checkSecured() throws IOException {
        try {
            this.a();
        }
        catch (Exception exception) {
            throw new IOException(ExceptionUtils.getMessage((Throwable)exception), exception);
        }
    }

    private void a(String string) {
        try {
            String[] stringArray = f;
            this.b.requireFeature(stringArray[15]);
        }
        catch (LicenseException licenseException) {
            a.log(Level.WARNING, f[14] + string);
            throw new LicenseRuntimeException(licenseException);
        }
    }

    private void b(boolean bl2) {
        block13: {
            HttpServletRequest httpServletRequest = RemoteUtils.getHttpServletRequest();
            try {
                if (httpServletRequest == null) {
                    throw new SecurityException(f[19]);
                }
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw AdminRemotingProvider.a(illegalArgumentException);
            }
            SsgConnector ssgConnector = HttpTransportModule.getConnector((ServletRequest)httpServletRequest);
            try {
                if (ssgConnector == null) {
                    throw new SecurityException(f[24]);
                }
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw AdminRemotingProvider.a(illegalArgumentException);
            }
            try {
                try {
                    if (ssgConnector.offersEndpoint(SsgConnector.Endpoint.ADMIN_APPLET) || ssgConnector.offersEndpoint(SsgConnector.Endpoint.ADMIN_REMOTE_SSM)) break block13;
                }
                catch (IllegalArgumentException illegalArgumentException) {
                    throw AdminRemotingProvider.a(illegalArgumentException);
                }
                throw new SecurityException(f[3] + ssgConnector.getPort());
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw AdminRemotingProvider.a(illegalArgumentException);
            }
        }
        try {
            if (bl2) {
                this.a();
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw AdminRemotingProvider.a(illegalArgumentException);
        }
    }

    private void a() {
        block13: {
            Subject subject = JaasUtils.getCurrentSubject();
            try {
                if (subject == null) {
                    throw new SecurityException(f[1]);
                }
            }
            catch (ObjectModelException objectModelException) {
                throw AdminRemotingProvider.a((Exception)((Object)objectModelException));
            }
            if (subject.getPrincipals(User.class).isEmpty()) {
                String string = AdminLoginHelper.a(subject);
                try {
                    if (string == null) {
                        throw new SecurityException(f[5]);
                    }
                }
                catch (ObjectModelException objectModelException) {
                    throw AdminRemotingProvider.a((Exception)((Object)objectModelException));
                }
                try {
                    block12: {
                        User user = this.c.resumeSession(string);
                        try {
                            if (user == null) break block12;
                            subject.getPrincipals().add((Principal)user);
                            break block13;
                        }
                        catch (ObjectModelException objectModelException) {
                            throw AdminRemotingProvider.a((Exception)((Object)objectModelException));
                        }
                    }
                    String[] stringArray = f;
                    throw new SecurityException(stringArray[17]);
                }
                catch (ObjectModelException objectModelException) {
                    String[] stringArray = f;
                    a.log(Level.WARNING, stringArray[0] + ExceptionUtils.getMessage((Throwable)objectModelException) + stringArray[22], objectModelException);
                }
                catch (AuthenticationException authenticationException) {
                    throw new AdminSessionValidationRuntimeException(f[11]);
                }
            }
        }
    }

    /*
     * Loose catch block
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    private void b() {
        try {
            if (this.d == null) {
                throw new SecurityException(f[12]);
            }
        }
        catch (UnknownHostException unknownHostException) {
            throw AdminRemotingProvider.a(unknownHostException);
        }
        try {
            block24: {
                HttpServletRequest httpServletRequest = RemoteUtils.getHttpServletRequest();
                try {
                    if (httpServletRequest == null) {
                        throw new SecurityException(f[7]);
                    }
                }
                catch (UnknownHostException unknownHostException) {
                    throw AdminRemotingProvider.a(unknownHostException);
                }
                SsgConnector ssgConnector = HttpTransportModule.getConnector((ServletRequest)httpServletRequest);
                try {
                    if (ssgConnector == null) {
                        throw new SecurityException(f[18]);
                    }
                }
                catch (UnknownHostException unknownHostException) {
                    throw AdminRemotingProvider.a(unknownHostException);
                }
                try {
                    if (!ssgConnector.offersEndpoint(SsgConnector.Endpoint.NODE_COMMUNICATION)) {
                        throw new SecurityException(f[21]);
                    }
                }
                catch (UnknownHostException unknownHostException) {
                    throw AdminRemotingProvider.a(unknownHostException);
                }
                X509Certificate x509Certificate = RemoteUtils.getClientCertificate();
                if (x509Certificate == null) throw new SecurityException(f[25]);
                try {
                    if (CertUtils.certsAreEqual((X509Certificate)this.e.getServerSslInfo().getCertificate(), (X509Certificate)x509Certificate)) break block24;
                    throw new SecurityException(f[25]);
                    catch (UnknownHostException unknownHostException) {
                        throw AdminRemotingProvider.a(unknownHostException);
                    }
                }
                catch (UnknownHostException unknownHostException) {
                    throw AdminRemotingProvider.a(unknownHostException);
                }
            }
            InetAddress inetAddress = InetAddress.getByName(RemoteUtils.getClientHost());
            boolean bl2 = false;
            Collection<ClusterNodeInfo> collection = this.d.retrieveClusterStatus();
            for (ClusterNodeInfo clusterNodeInfo : collection) {
                ClusterNodeInfo clusterNodeInfo2 = clusterNodeInfo;
                String string = clusterNodeInfo2.getAddress();
                InetAddress inetAddress2 = InetAddress.getByName(string);
                if (!inetAddress2.equals(inetAddress)) continue;
                bl2 = true;
            }
            try {
                if (bl2) return;
                throw new SecurityException(f[13] + String.valueOf(inetAddress) + f[23]);
            }
            catch (UnknownHostException unknownHostException) {
                throw AdminRemotingProvider.a(unknownHostException);
            }
        }
        catch (UnknownHostException unknownHostException) {
            String[] stringArray = f;
            throw new SecurityException(stringArray[9], unknownHostException);
        }
        catch (ServerNotActiveException serverNotActiveException) {
            String[] stringArray = f;
            throw new SecurityException(stringArray[10], serverNotActiveException);
        }
        catch (FindException findException) {
            String[] stringArray = f;
            throw new SecurityException(stringArray[10], findException);
        }
        catch (IOException iOException) {
            throw new SecurityException(f[10], iOException);
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[26];
                var3_1 = 0;
                var2_2 = "+/EN9\t,\u000b.BL\"G9N<SL\"G7\u001d)E@?@(\u000b}BR.[~\u001d8DR\"F0Nz) 2\u0017R>K4\u000b>C\u0001;H-\u001d8S\rkH+\u001a5RO?@=\u000f)^N%\t8\u000f4[D/\u0007\u0007-\u0011br\u001fl\f*/9ZH%\t,\u000b,BD8]~\u00002C\u0001;L,\u00034CU.M~\u00013\u0017U#@-N-XS?\u0005~\u0005/\u0019zh\u0005)/9ZH%\t,\u000b,BD8]~\n4D@'E1\u00198S\u001bkG1N>ED/L0\u001a4VM8\u0007\u0005/\u0019zh\u00058-1BR?L,N/RP>L-\u001a}SH8H2\u00022@D/\u0013~ 2\u0017S.X+\u000b.C\u0001(F0\u001a8OUkH(\u000f4[@)E;\u0011;3\\O$^0N;VB\"E7\u001a$\u0017.-1BR?L,N4YW$J?\u001a4XOkM;\u00004REg\t+\u0000<UM.\t*\u0001}TI.J5N\u0014g\u000f.-1BR?L,N4YW$J?\u001a4XOkM;\u00004REg\t+\u0000<UM.\t*\u0001}TI.J5N\u0014g\u000f\u0012>8EL\"Z-\u00072Y\u0001/L0\u00078S\u000f\"-1BR?L,N4YW$J?\u001a4XO8\t0\u0001)\u0017Q.[3\u0007)CD/\u0007.-1BR?L,N4YW$J?\u001a4XOkM;\u00004REkO1\u001c}YN%\u0004=\u0002(DU.[~\u0007-\u0017\u00062\"4TD%Z;N>_D(B7\u0000:\u0017G*@2\u000b9\u0017V#L0N4YW$B7\u0000:\u0017U#L~\u00038CI$MrN\r\u001d8EW\"J;T\u001cSL\"G\u0007-\u0011br\u001fl\f,/9ZH%\t,\u000b,BD8]~\n4D@'E1\u00198S\u001bkZ;\u001d.^N%\t0\u0001)\u0017G$\\0\nsW-1BR?L,N/RP>L-\u001a}SH8H2\u00022@D/\u0013~;3VC'L~\u001a2\u0017E.];\u001c0^O.\t)\u00064TIkJ1\u00003RB?F,N)_H8\t,\u000b,BD8]~\r<ZDk@0N2Y6/9ZH%\t,\u000b,BD8]~\n4D@'E1\u00198S\u001bkg1N/RP>L-\u001a}TN%];\u0016)\u0017@=H7\u0002<UM.\u000f=8DR\"F0N8OQ\"[;\n\"<8FT.Z*N3XUkY;\u001c0^U?L:N2Y\u0001?A7\u001d}GN9]\u0002Is\u0002Is";
                var4_3 = "+/EN9\t,\u000b.BL\"G9N<SL\"G7\u001d)E@?@(\u000b}BR.[~\u001d8DR\"F0Nz) 2\u0017R>K4\u000b>C\u0001;H-\u001d8S\rkH+\u001a5RO?@=\u000f)^N%\t8\u000f4[D/\u0007\u0007-\u0011br\u001fl\f*/9ZH%\t,\u000b,BD8]~\u00002C\u0001;L,\u00034CU.M~\u00013\u0017U#@-N-XS?\u0005~\u0005/\u0019zh\u0005)/9ZH%\t,\u000b,BD8]~\n4D@'E1\u00198S\u001bkG1N>ED/L0\u001a4VM8\u0007\u0005/\u0019zh\u00058-1BR?L,N/RP>L-\u001a}SH8H2\u00022@D/\u0013~ 2\u0017S.X+\u000b.C\u0001(F0\u001a8OUkH(\u000f4[@)E;\u0011;3\\O$^0N;VB\"E7\u001a$\u0017.-1BR?L,N4YW$J?\u001a4XOkM;\u00004REg\t+\u0000<UM.\t*\u0001}TI.J5N\u0014g\u000f.-1BR?L,N4YW$J?\u001a4XOkM;\u00004REg\t+\u0000<UM.\t*\u0001}TI.J5N\u0014g\u000f\u0012>8EL\"Z-\u00072Y\u0001/L0\u00078S\u000f\"-1BR?L,N4YW$J?\u001a4XO8\t0\u0001)\u0017Q.[3\u0007)CD/\u0007.-1BR?L,N4YW$J?\u001a4XOkM;\u00004REkO1\u001c}YN%\u0004=\u0002(DU.[~\u0007-\u0017\u00062\"4TD%Z;N>_D(B7\u0000:\u0017G*@2\u000b9\u0017V#L0N4YW$B7\u0000:\u0017U#L~\u00038CI$MrN\r\u001d8EW\"J;T\u001cSL\"G\u0007-\u0011br\u001fl\f,/9ZH%\t,\u000b,BD8]~\n4D@'E1\u00198S\u001bkZ;\u001d.^N%\t0\u0001)\u0017G$\\0\nsW-1BR?L,N/RP>L-\u001a}SH8H2\u00022@D/\u0013~;3VC'L~\u001a2\u0017E.];\u001c0^O.\t)\u00064TIkJ1\u00003RB?F,N)_H8\t,\u000b,BD8]~\r<ZDk@0N2Y6/9ZH%\t,\u000b,BD8]~\n4D@'E1\u00198S\u001bkg1N/RP>L-\u001a}TN%];\u0016)\u0017@=H7\u0002<UM.\u000f=8DR\"F0N8OQ\"[;\n\"<8FT.Z*N3XUkY;\u001c0^U?L:N2Y\u0001?A7\u001d}GN9]\u0002Is\u0002Is".length();
                var1_4 = 44;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = AdminRemotingProvider.a(v0, AdminRemotingProvider.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u001e\bky\u00148\u001d:\u001dsu\tlO;\u0005uq\u0016t\u0000(\tb*ZM\u0001>\u000ejuZl\u0000\u007f\bcd\u001fj\u00026\u0002c0\rp\u0006<\u0004&s\u0015v\u0001:\u000fr\u007f\b8\u001b7\u0005u0\b}\u001e*\tudZ{\u000e2\t&y\u00148\u00001;\u001c\u0000sc\u000e}\u001d\u007f\u001eca\u000f}\u001c+Lby\ty\u00033\u0003qu\u001e#O2\u0005uc\u0013v\b\u007f\u0003t0\u0013v\u0019>\u0000otZ{\u001d:\bc~\u000eq\u000e3\u001f(";
                    var4_3 = "\u001e\bky\u00148\u001d:\u001dsu\tlO;\u0005uq\u0016t\u0000(\tb*ZM\u0001>\u000ejuZl\u0000\u007f\bcd\u001fj\u00026\u0002c0\rp\u0006<\u0004&s\u0015v\u0001:\u000fr\u007f\b8\u001b7\u0005u0\b}\u001e*\tudZ{\u000e2\t&y\u00148\u00001;\u001c\u0000sc\u000e}\u001d\u007f\u001eca\u000f}\u001c+Lby\ty\u00033\u0003qu\u001e#O2\u0005uc\u0013v\b\u007f\u0003t0\u0013v\u0019>\u0000otZ{\u001d:\bc~\u000eq\u000e3\u001f(".length();
                    var1_4 = 85;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 117;
                        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++] = AdminRemotingProvider.a(v0, AdminRemotingProvider.b(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;
            }
        }
        AdminRemotingProvider.f = var5;
        AdminRemotingProvider.a = Logger.getLogger(AdminRemotingProvider.class.getName());
    }

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

    private static char[] b(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x1A);
        }
        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 -> 42;
                    case 1 -> 25;
                    case 2 -> 115;
                    case 3 -> 101;
                    case 4 -> 15;
                    case 5 -> 109;
                    default -> 26;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

