/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.TcpKnob
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.RemoteIpRange
 *  com.l7tech.policy.variable.NoSuchVariableException
 *  com.l7tech.policy.variable.Syntax
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.InetAddressUtil
 */
package com.l7tech.server.policy.assertion;

import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.TcpKnob;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.RemoteIpRange;
import com.l7tech.policy.variable.NoSuchVariableException;
import com.l7tech.policy.variable.Syntax;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.InetAddressUtil;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ServerRemoteIpRange
extends AbstractServerAssertion<RemoteIpRange> {
    private static final String[] d;

    public ServerRemoteIpRange(RemoteIpRange remoteIpRange) {
        super(remoteIpRange);
    }

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        String string;
        Object object;
        String string2;
        String string3;
        try {
            string3 = this.a(((RemoteIpRange)this.assertion).getStartIp(), policyEnforcementContext);
            string2 = this.a(((RemoteIpRange)this.assertion).getNetworkMask(), policyEnforcementContext);
            RemoteIpRange.validateRange((String)string3, (String)string2);
        }
        catch (IllegalArgumentException illegalArgumentException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.IP_INVALID_RANGE, illegalArgumentException.getMessage());
            return AssertionStatus.FAILED;
        }
        catch (NoSuchVariableException noSuchVariableException) {
            String[] stringArray = d;
            this.logger.log(Level.WARNING, ((RemoteIpRange)this.assertion).getStartIp() + "/" + ((RemoteIpRange)this.assertion).getNetworkMask() + stringArray[4], ExceptionUtils.getDebugException((Throwable)noSuchVariableException));
            return AssertionStatus.SERVER_ERROR;
        }
        String string4 = ((RemoteIpRange)this.assertion).getIpSourceContextVariable();
        if (string4 == null) {
            object = (TcpKnob)policyEnforcementContext.getRequest().getKnob(TcpKnob.class);
            try {
                if (object == null) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.IP_NOT_TCP);
                    return AssertionStatus.BAD_REQUEST;
                }
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw ServerRemoteIpRange.a(illegalArgumentException);
            }
            string = object.getRemoteAddress();
        } else {
            try {
                object = policyEnforcementContext.getVariable(string4);
                try {
                    if (object == null) {
                        throw new NoSuchVariableException(string4, d[1] + string4);
                    }
                }
                catch (IllegalArgumentException illegalArgumentException) {
                    throw ServerRemoteIpRange.a(illegalArgumentException);
                }
                string = (String)InetAddressUtil.getHostAndPort((String)object.toString(), null).left;
            }
            catch (NoSuchVariableException noSuchVariableException) {
                this.logger.log(Level.WARNING, d[2], ExceptionUtils.getDebugException((Throwable)noSuchVariableException));
                this.logAndAudit((AuditDetailMessage)AssertionMessages.IP_ADDRESS_UNAVAILABLE, string4);
                return AssertionStatus.SERVER_ERROR;
            }
        }
        string = InetAddressUtil.stripIpv6Brackets((String)string);
        object = InetAddressUtil.getAddress((String)string);
        try {
            if (object == null) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.IP_ADDRESS_INVALID, string);
                return AssertionStatus.FALSIFIED;
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw ServerRemoteIpRange.a(illegalArgumentException);
        }
        try {
            if (this.a((InetAddress)object, string3, string2)) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.IP_ACCEPTED, string);
                return AssertionStatus.NONE;
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw ServerRemoteIpRange.a(illegalArgumentException);
        }
        this.logAndAudit((AuditDetailMessage)AssertionMessages.IP_REJECTED, string);
        return AssertionStatus.FALSIFIED;
    }

    private Pattern a(String string, Map<String, Pattern> map) {
        try {
            if (map.containsKey(string)) {
                return map.get(string);
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw ServerRemoteIpRange.a(illegalArgumentException);
        }
        Pattern pattern = Pattern.compile(d[3] + string + "}", 16);
        map.put(string, pattern);
        return pattern;
    }

    private String a(String string, PolicyEnforcementContext policyEnforcementContext) throws NoSuchVariableException {
        String[] stringArray = Syntax.getReferencedNamesIndexedVarsNotOmitted((String)string);
        try {
            if (stringArray.length < 1) {
                return string;
            }
        }
        catch (NoSuchVariableException noSuchVariableException) {
            throw ServerRemoteIpRange.a((Exception)((Object)noSuchVariableException));
        }
        HashMap<String, Pattern> hashMap = new HashMap<String, Pattern>();
        for (String string2 : stringArray) {
            Pattern pattern = this.a(string2, hashMap);
            Matcher matcher = pattern.matcher(string);
            Object object = null;
            try {
                object = policyEnforcementContext.getVariable(string2);
                try {
                    if (object == null) {
                        throw new NoSuchVariableException(string2, d[0]);
                    }
                }
                catch (NoSuchVariableException noSuchVariableException) {
                    throw ServerRemoteIpRange.a((Exception)((Object)noSuchVariableException));
                }
            }
            catch (NoSuchVariableException noSuchVariableException) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.NO_SUCH_VARIABLE, string2);
                throw new NoSuchVariableException(string2, d[1]);
            }
            string = matcher.replaceFirst(object.toString());
        }
        return string;
    }

    private boolean a(InetAddress inetAddress, String string, String string2) {
        boolean bl2;
        boolean bl3;
        try {
            bl3 = InetAddressUtil.patternMatchesAddress((String)(string + "/" + string2), (InetAddress)inetAddress);
            bl2 = !((RemoteIpRange)this.assertion).isAllowRange();
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw ServerRemoteIpRange.a(illegalArgumentException);
        }
        return bl3 ^ bl2;
    }

    boolean a(InetAddress inetAddress, PolicyEnforcementContext policyEnforcementContext) throws NoSuchVariableException {
        String string = this.a(((RemoteIpRange)this.assertion).getStartIp(), policyEnforcementContext);
        String string2 = this.a(((RemoteIpRange)this.assertion).getNetworkMask(), policyEnforcementContext);
        return this.a(inetAddress, string, string2);
    }

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

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[5];
                var3_1 = 0;
                var2_2 = "\b;~\u001b|}\"\u0004 +\u0005}.#\u0007\"nW\u0012\b;~\u001b|}\"\u0004 +\u0005}.#\u0007\"nWB91f\u0018l8l\u0002$+\u0011j2!K7d\u0019l84\u001ft}\u0016j4-\t8nWm3-\u001d5b\u001by? \u000ez+'w.?\u00026g\u00128-#\u0007=h\u000e88>\u0019;y";
                var4_3 = "\b;~\u001b|}\"\u0004 +\u0005}.#\u0007\"nW\u0012\b;~\u001b|}\"\u0004 +\u0005}.#\u0007\"nWB91f\u0018l8l\u0002$+\u0011j2!K7d\u0019l84\u001ft}\u0016j4-\t8nWm3-\u001d5b\u001by? \u000ez+'w.?\u00026g\u00128-#\u0007=h\u000e88>\u0019;y".length();
                var1_4 = 18;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerRemoteIpRange.b(v0, ServerRemoteIpRange.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\tiA\r?mC;}o_w#R;\u007f*N}#E;c~\rd,C7zhAwmD0z|L{!P<wo\u00032\u001d^-hcO~(\u0011.tfDq4\u0011;ixB`";
                    var4_3 = "\tiA\r?mC;}o_w#R;\u007f*N}#E;c~\rd,C7zhAwmD0z|L{!P<wo\u00032\u001d^-hcO~(\u0011.tfDq4\u0011;ixB`".length();
                    var1_4 = 2;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 37;
                        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++] = ServerRemoteIpRange.b(v0, ServerRemoteIpRange.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;
            }
        }
        ServerRemoteIpRange.d = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x2F);
        }
        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 -> 8;
                    case 1 -> 55;
                    case 2 -> 104;
                    case 3 -> 20;
                    case 4 -> 123;
                    case 5 -> 62;
                    default -> 47;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

