/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.transport.InterfaceTag
 *  com.l7tech.gateway.common.transport.firewall.SsgFirewallRule
 *  com.l7tech.objectmodel.Entity
 *  com.l7tech.objectmodel.EntityHeader
 *  com.l7tech.objectmodel.EntityType
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.util.ConfigFactory
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.Functions
 *  com.l7tech.util.Functions$Unary
 *  com.l7tech.util.InetAddressUtil
 *  com.l7tech.util.Pair
 *  org.springframework.beans.factory.DisposableBean
 *  org.springframework.context.ApplicationEvent
 */
package com.l7tech.server.transport.firewall;

import com.l7tech.gateway.common.transport.InterfaceTag;
import com.l7tech.gateway.common.transport.firewall.SsgFirewallRule;
import com.l7tech.objectmodel.Entity;
import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.server.HibernateEntityManager;
import com.l7tech.server.ServerConfig;
import com.l7tech.server.event.EntityInvalidationEvent;
import com.l7tech.server.transport.ListenerException;
import com.l7tech.server.transport.firewall.SsgFirewallRuleManager;
import com.l7tech.server.transport.firewall.a;
import com.l7tech.server.transport.firewall.b;
import com.l7tech.server.transport.firewall.c;
import com.l7tech.server.util.ApplicationEventProxy;
import com.l7tech.server.util.FirewallUtils;
import com.l7tech.util.ConfigFactory;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.Functions;
import com.l7tech.util.InetAddressUtil;
import com.l7tech.util.Pair;
import java.io.File;
import java.net.InetAddress;
import java.net.SocketException;
import java.text.ParseException;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationEvent;

public class SsgFirewallRuleManagerImpl
extends HibernateEntityManager<SsgFirewallRule, EntityHeader>
implements SsgFirewallRuleManager,
DisposableBean {
    private static final Logger m;
    private final ServerConfig n;
    private final Map<Goid, SsgFirewallRule> o = new LinkedHashMap<Goid, SsgFirewallRule>();
    private final AtomicReference<Pair<String, Set<InterfaceTag>>> p = new AtomicReference<Object>(null);
    private static final String[] q;

    public SsgFirewallRuleManagerImpl(ServerConfig serverConfig, ApplicationEventProxy applicationEventProxy) {
        this.n = serverConfig;
        applicationEventProxy.addApplicationListener(new a(this));
    }

    public Class<? extends Entity> getImpClass() {
        return SsgFirewallRule.class;
    }

    @Override
    public Class<? extends Entity> getInterfaceClass() {
        return SsgFirewallRule.class;
    }

    @Override
    public EntityType getEntityType() {
        return EntityType.FIREWALL_RULE;
    }

    protected void initDao() throws Exception {
        super.initDao();
        for (SsgFirewallRule ssgFirewallRule : this.findAll()) {
            this.o.put(ssgFirewallRule.getGoid(), ssgFirewallRule);
        }
        this.a();
    }

    public void destroy() throws Exception {
        File file = this.n.getLocalDirectoryProperty(q[16], true);
        FirewallUtils.closeFirewallForConnectors(file);
    }

    private void a(ApplicationEvent applicationEvent) {
        if (!(applicationEvent instanceof EntityInvalidationEvent)) {
            return;
        }
        EntityInvalidationEvent entityInvalidationEvent = (EntityInvalidationEvent)applicationEvent;
        if (!SsgFirewallRule.class.isAssignableFrom(entityInvalidationEvent.getEntityClass())) {
            return;
        }
        Goid[] goidArray = entityInvalidationEvent.getEntityIds();
        char[] cArray = entityInvalidationEvent.getEntityOperations();
        block3: for (int i2 = 0; i2 < cArray.length; ++i2) {
            char c10 = cArray[i2];
            Goid goid = goidArray[i2];
            switch (c10) {
                case 'D': {
                    m.warning(q[12]);
                    this.o.remove(goid);
                    continue block3;
                }
                default: {
                    this.a(goid);
                }
            }
        }
        this.a();
    }

    /*
     * Loose catch block
     */
    private void a(Goid goid) {
        block7: {
            try {
                block6: {
                    SsgFirewallRule ssgFirewallRule = (SsgFirewallRule)this.findByPrimaryKey(goid);
                    if (ssgFirewallRule == null) break block6;
                    try {
                        block8: {
                            if (!ssgFirewallRule.isEnabled()) break block6;
                            break block8;
                            catch (FindException findException) {
                                throw SsgFirewallRuleManagerImpl.a((Exception)((Object)findException));
                            }
                        }
                        this.o.put(goid, ssgFirewallRule);
                        break block7;
                    }
                    catch (FindException findException) {
                        throw SsgFirewallRuleManagerImpl.a((Exception)((Object)findException));
                    }
                }
                this.o.remove(goid);
            }
            catch (FindException findException) {
                String[] stringArray = q;
                m.log(Level.WARNING, stringArray[14] + String.valueOf(goid) + stringArray[13] + ExceptionUtils.getMessage((Throwable)findException), findException);
            }
        }
    }

    private void a() {
        File file = this.n.getLocalDirectoryProperty(q[8], true);
        List list = Functions.sort(this.o.values(), (Comparator)new b(this));
        list = Functions.map((Iterable)list, (Functions.Unary)new c(this));
        FirewallUtils.openFirewallForRules(file, list);
    }

    private SsgFirewallRule a(SsgFirewallRule ssgFirewallRule, String string) {
        try {
            String string2 = ssgFirewallRule.getProperty(string);
            if (this.a(string2)) {
                String string3 = this.b(string2);
                ssgFirewallRule.putProperty(string, string3);
            }
        }
        catch (ListenerException listenerException) {
            m.log(Level.WARNING, q[5] + ExceptionUtils.getMessage((Throwable)listenerException), ExceptionUtils.getDebugException((Throwable)listenerException));
        }
        return ssgFirewallRule;
    }

    private boolean a(String string) {
        if (string == null || string.length() < 1 || string.contains(":")) {
            return false;
        }
        char c10 = string.charAt(0);
        return c10 < '0' || c10 > '9';
    }

    private String b(String string) throws ListenerException {
        List list;
        try {
            if (!this.a(string)) {
                return string;
            }
        }
        catch (SocketException socketException) {
            throw SsgFirewallRuleManagerImpl.a(socketException);
        }
        Pair<String, Set<InterfaceTag>> pair = this.b();
        try {
            if (pair == null) {
                throw new ListenerException(q[11] + string);
            }
        }
        catch (SocketException socketException) {
            throw SsgFirewallRuleManagerImpl.a(socketException);
        }
        try {
            list = InetAddressUtil.findAllLocalInetAddresses();
        }
        catch (SocketException socketException) {
            String[] stringArray = q;
            throw new ListenerException(stringArray[6] + string + stringArray[7] + ExceptionUtils.getMessage((Throwable)socketException));
        }
        InterfaceTag interfaceTag = this.a((Collection)pair.right, string);
        try {
            if (interfaceTag == null) {
                throw new ListenerException(q[1] + string + q[2]);
            }
        }
        catch (SocketException socketException) {
            throw SsgFirewallRuleManagerImpl.a(socketException);
        }
        Set set = interfaceTag.getIpPatterns();
        InetAddress inetAddress = null;
        block12: for (InetAddress inetAddress2 : list) {
            for (String string2 : set) {
                block18: {
                    try {
                        if (!InetAddressUtil.patternMatchesAddress((String)string2, (InetAddress)inetAddress2)) continue;
                        if (inetAddress != null) break block18;
                    }
                    catch (SocketException socketException) {
                        throw SsgFirewallRuleManagerImpl.a(socketException);
                    }
                    inetAddress = inetAddress2;
                    continue;
                }
                String[] stringArray = q;
                m.log(Level.WARNING, stringArray[3] + string + stringArray[0] + String.valueOf(inetAddress));
                break block12;
            }
        }
        try {
            if (inetAddress == null) {
                throw new ListenerException(q[4] + string + q[15]);
            }
        }
        catch (SocketException socketException) {
            throw SsgFirewallRuleManagerImpl.a(socketException);
        }
        return inetAddress.getHostAddress();
    }

    private Pair<String, Set<InterfaceTag>> b() {
        Pair<String, Set<InterfaceTag>> pair = this.p.get();
        if (pair == null) {
            try {
                pair = this.c();
                this.p.set(pair);
            }
            catch (FindException findException) {
                String[] stringArray = q;
                m.log(Level.WARNING, stringArray[10] + ExceptionUtils.getMessage((Throwable)findException), ExceptionUtils.getDebugException((Throwable)findException));
            }
            catch (ParseException parseException) {
                m.log(Level.WARNING, q[9] + ExceptionUtils.getMessage((Throwable)parseException), ExceptionUtils.getDebugException((Throwable)parseException));
            }
        }
        return pair;
    }

    private Pair<String, Set<InterfaceTag>> c() throws FindException, ParseException {
        Set set;
        String string = ConfigFactory.getUncachedConfig().getProperty(InterfaceTag.PROPERTY_NAME);
        try {
            set = string == null ? Collections.emptySet() : InterfaceTag.parseMultiple((String)string);
        }
        catch (FindException findException) {
            throw SsgFirewallRuleManagerImpl.a((Exception)((Object)findException));
        }
        Set set2 = set;
        return new Pair((Object)string, (Object)set2);
    }

    private InterfaceTag a(Collection<InterfaceTag> collection, String string) {
        for (InterfaceTag interfaceTag : collection) {
            if (!interfaceTag.getName().equalsIgnoreCase(string)) continue;
            return interfaceTag;
        }
        return null;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[17];
                var3_1 = 0;
                var2_2 = "\u001dJq\u0000:T:SZ>\u001e/A'X[p\u001dnX2IJv\u0007 RsPFl\u000bnA;\\G>\u0001 PsSLj\u0019!G8\u001dHz\n<P NLmN![sIAw\u001dn[<YL0Nnb:QE>\u001b=Ps[@l\u001d:\u0015>\\]}\u0006nZ5\u001d\u001esF>\u0007 A6OO\u007f\r+\u00157XOw\u0000'A:RG>\u0000/X6Y\t\n\u001d@mN%[<JG0\ntGj\u000b<S2^L>'sF>\u000f*Q!XZmN>T'ILl\u0000nS<O\tw\u0000:P![H}\u000bn[2PLzN@hG\u007f\f\"PsIF>\u001a<T=NE\u007f\u001a+\u00151TGzN/Q7OLm\u001dnB;TE{N;E7\\]w\u0000)\u00155T[{\u0019/Y?\u001d[k\u0002+Fi\u001d]hG\u007f\f\"PsIF>\u0002!Z8\u001d\\nN P'JFl\u0005n\\=ILl\b/V6N\ti\u0006'Y6\u001dOw\u0000*\\=Z\t\u007fN#T'^A>\b!Gs[@l\u000b9T?Q\tl\u001b\"PsHZw\u0000)\u0015:S]{\u001c(T0X\t\u0002\u0007\t\fKHl*'G6^]q\u001c7\u001etGh\u000f\"\\7\u001d@p\u001a+G5\\J{N*P5TGw\u001a'Z=\u0007\t&hG\u007f\f\"PsIF>\u0002!T7\u001d@p\u001a+G5\\J{N*P5TGw\u001a'Z=N\u0013>WsF>\u0007 A6OO\u007f\r+\u00157XOw\u0000'A:RGmN+M:N]%N;[2_E{N:Zs[@p\nnX2IJvN(Z!\u001dOw\u001c+B2QE>\u001c;Y6\u001d\\m\u0007 RsTGj\u000b<S2^L>\u0016YLr\u000b:\\=Z\tx\u0007<P$\\ErN<@?X\u0002\u0007\t9hG\u007f\f\"PsIF>\b'[7\u001dCk\u001d:\u00182YM{\nnZ!\u001d\u0004k\u001e*T'XM>\b'G6JHr\u0002nB:IA>\t!\\7\u001d";
                var4_3 = "\u001dJq\u0000:T:SZ>\u001e/A'X[p\u001dnX2IJv\u0007 RsPFl\u000bnA;\\G>\u0001 PsSLj\u0019!G8\u001dHz\n<P NLmN![sIAw\u001dn[<YL0Nnb:QE>\u001b=Ps[@l\u001d:\u0015>\\]}\u0006nZ5\u001d\u001esF>\u0007 A6OO\u007f\r+\u00157XOw\u0000'A:RG>\u0000/X6Y\t\n\u001d@mN%[<JG0\ntGj\u000b<S2^L>'sF>\u000f*Q!XZmN>T'ILl\u0000nS<O\tw\u0000:P![H}\u000bn[2PLzN@hG\u007f\f\"PsIF>\u001a<T=NE\u007f\u001a+\u00151TGzN/Q7OLm\u001dnB;TE{N;E7\\]w\u0000)\u00155T[{\u0019/Y?\u001d[k\u0002+Fi\u001d]hG\u007f\f\"PsIF>\u0002!Z8\u001d\\nN P'JFl\u0005n\\=ILl\b/V6N\ti\u0006'Y6\u001dOw\u0000*\\=Z\t\u007fN#T'^A>\b!Gs[@l\u000b9T?Q\tl\u001b\"PsHZw\u0000)\u0015:S]{\u001c(T0X\t\u0002\u0007\t\fKHl*'G6^]q\u001c7\u001etGh\u000f\"\\7\u001d@p\u001a+G5\\J{N*P5TGw\u001a'Z=\u0007\t&hG\u007f\f\"PsIF>\u0002!T7\u001d@p\u001a+G5\\J{N*P5TGw\u001a'Z=N\u0013>WsF>\u0007 A6OO\u007f\r+\u00157XOw\u0000'A:RGmN+M:N]%N;[2_E{N:Zs[@p\nnX2IJvN(Z!\u001dOw\u001c+B2QE>\u001c;Y6\u001d\\m\u0007 RsTGj\u000b<S2^L>\u0016YLr\u000b:\\=Z\tx\u0007<P$\\ErN<@?X\u0002\u0007\t9hG\u007f\f\"PsIF>\b'[7\u001dCk\u001d:\u00182YM{\nnZ!\u001d\u0004k\u001e*T'XM>\b'G6JHr\u0002nB:IA>\t!\\7\u001d".length();
                var1_4 = 99;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = SsgFirewallRuleManagerImpl.b(v0, SsgFirewallRuleManagerImpl.d(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "q(\u0013vA1Z\"e\u0013l[yQ41\u0005mP2\u001f0!\u0016pG*Lq*\u001c\"V1V\"e\u001cmF<\u001f7*\u0000\"D0M42\u0013nNyM$)\u0017,\f'$\u0000FK+Z21\u001dp[";
                    var4_3 = "q(\u0013vA1Z\"e\u0013l[yQ41\u0005mP2\u001f0!\u0016pG*Lq*\u001c\"V1V\"e\u001cmF<\u001f7*\u0000\"D0M42\u0013nNyM$)\u0017,\f'$\u0000FK+Z21\u001dp[".length();
                    var1_4 = 60;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 7;
                        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++] = SsgFirewallRuleManagerImpl.b(v0, SsgFirewallRuleManagerImpl.d(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;
            }
        }
        SsgFirewallRuleManagerImpl.q = var5;
        SsgFirewallRuleManagerImpl.m = Logger.getLogger(SsgFirewallRuleManagerImpl.class.getName());
    }

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

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

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String b(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 -> 86;
                    case 1 -> 66;
                    case 2 -> 117;
                    case 3 -> 5;
                    case 4 -> 37;
                    case 5 -> 94;
                    default -> 56;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

