/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.ProcUtils
 *  com.l7tech.gateway.common.transport.SsgConnector
 *  com.l7tech.gateway.common.transport.firewall.SsgFirewallRule
 *  com.l7tech.util.ConfigFactory
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.IpProtocol
 *  com.l7tech.util.SudoUtils
 */
package com.l7tech.server.util;

import com.l7tech.common.io.ProcUtils;
import com.l7tech.gateway.common.transport.SsgConnector;
import com.l7tech.gateway.common.transport.firewall.SsgFirewallRule;
import com.l7tech.server.util.FirewallRules;
import com.l7tech.util.ConfigFactory;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.IpProtocol;
import com.l7tech.util.SudoUtils;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FirewallUtils {
    private static final Logger a;
    private static final String b;
    private static final String c;
    private static final String d;
    private static final String e;
    private static final String f;
    private static final String g;
    private static final String[] h;

    public static void initializeFirewall() {
        FirewallUtils.a("-", IpProtocol.IPv4, false, d);
        FirewallUtils.a("-", IpProtocol.IPv6, false, e);
    }

    public static void openFirewallForConnectors(File file, Collection<SsgConnector> collection) {
        String[] stringArray = h;
        String string = new File(file, stringArray[4]).getPath();
        String string2 = new File(file, stringArray[3]).getPath();
        try {
            FirewallRules.writeFirewallDropfile(string, collection, IpProtocol.IPv4);
        }
        catch (IOException iOException) {
            stringArray = h;
            a.log(Level.WARNING, stringArray[0] + ExceptionUtils.getMessage((Throwable)iOException), iOException);
        }
        try {
            FirewallRules.writeFirewallDropfile(string2, collection, IpProtocol.IPv6);
        }
        catch (IOException iOException) {
            a.log(Level.WARNING, h[9] + ExceptionUtils.getMessage((Throwable)iOException), iOException);
        }
        FirewallUtils.a(string, IpProtocol.IPv4, true, d);
        FirewallUtils.a(string2, IpProtocol.IPv6, true, e);
    }

    public static void openFirewallForRules(File file, Collection<SsgFirewallRule> collection) {
        String[] stringArray;
        String string = new File(file, d).getPath();
        String string2 = new File(file, e).getPath();
        try {
            FirewallRules.writeFirewallDropfileForRules(string, collection, IpProtocol.IPv4);
        }
        catch (IOException iOException) {
            stringArray = h;
            a.log(Level.WARNING, stringArray[2] + d + stringArray[20] + ExceptionUtils.getMessage((Throwable)iOException), iOException);
        }
        try {
            FirewallRules.writeFirewallDropfileForRules(string2, collection, IpProtocol.IPv6);
        }
        catch (IOException iOException) {
            stringArray = h;
            a.log(Level.WARNING, stringArray[15] + e + stringArray[20] + ExceptionUtils.getMessage((Throwable)iOException), iOException);
        }
        FirewallUtils.a(string, IpProtocol.IPv4, true, d);
        FirewallUtils.a(string2, IpProtocol.IPv6, true, e);
    }

    public static void closeFirewallForConnectors(File file) {
        String string = new File(file, d).getPath();
        String string2 = new File(file, e).getPath();
        FirewallUtils.a(string, IpProtocol.IPv4, false, d);
        FirewallUtils.a(string2, IpProtocol.IPv6, false, e);
    }

    private static void a(String string, IpProtocol ipProtocol, boolean bl2, String string2) {
        File file = null;
        try {
            file = SudoUtils.findSudo();
        }
        catch (IOException iOException) {
            // empty catch block
        }
        try {
            if (!ipProtocol.isEnabled()) {
                return;
            }
        }
        catch (IOException iOException) {
            throw FirewallUtils.a(iOException);
        }
        try {
            if (file == null) {
                return;
            }
        }
        catch (IOException iOException) {
            throw FirewallUtils.a(iOException);
        }
        File file2 = FirewallUtils.a();
        try {
            if (file2 == null) {
                return;
            }
        }
        catch (IOException iOException) {
            throw FirewallUtils.a(iOException);
        }
        String[] stringArray = h;
        a.log(Level.FINE, stringArray[6] + String.valueOf(file2));
        try {
            String string3;
            String[] stringArray2 = new String[5];
            stringArray2[0] = file2.getAbsolutePath();
            stringArray2[1] = ipProtocol.name().toLowerCase();
            stringArray2[2] = string;
            if (bl2) {
                string3 = stringArray[11];
            } else {
                stringArray = h;
                string3 = stringArray[12];
            }
            stringArray2[3] = string3;
            stringArray2[4] = string2;
            ProcUtils.exec(null, (File)file, (String[])stringArray2, (byte[])null, (boolean)false);
        }
        catch (IOException iOException) {
            stringArray = h;
            a.log(Level.WARNING, stringArray[14] + String.valueOf(file2) + stringArray[21] + ExceptionUtils.getMessage((Throwable)iOException), ExceptionUtils.getDebugException((Throwable)iOException));
        }
    }

    private static File a() {
        String[] stringArray = h;
        File file = new File(stringArray[8]);
        String string = ConfigFactory.getProperty((String)stringArray[19], (String)file.getAbsolutePath());
        if (string == null || string.length() < 1) {
            return null;
        }
        File file2 = new File(string);
        return file2.exists() && file2.canExecute() ? file2 : null;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[22];
                var3_1 = 0;
                var2_2 = "\u001a\u0011B\u0002&}x;\u0010\u0003\u0015:|9;\u001a\u0003\u0010%j,o\u0013J\u0013>8<=\u0010S\u0006#t=o\u0013J\u0013>}6\u0010\u000fL\u0012>kbo*,\u0010NN&/,*\u001cKN9}*9\u001aQN,q**\bB\f&.v=\nO\u000596>&\u0013F\u000e+u=$\u001a\u0011B\u0002&}x;\u0010\u0003\u0015:|9;\u001a\u0003\u0010%j,o\u0013J\u0013>8<=\u0010S\u0006#t=o\r#\u0016P\u0014/vn\u0010\u000fL\u0012>k\f#\u0016P\u0014/v\u0007?\u0010Q\u00149),\u0010NN&/,*\u001cKN9}*9\u001aQN,q**\bB\f&6*:\u0013F\u0013d~1#\u001aM\u0001'}+\u001a\fJ\u000e-8>&\rF\u0017+t4o\rV\f/kx:\u000fG\u0001>}*o\u000fQ\u000f-j9\"E\u0003\u0013?|7o\u000f)\u0016Q\u0005=y4#I|\u0012?t=<1`\u0010S\u0014eK=,\nQ\u0005\u0019h9!Pb\u0010:t1.\u0011@\u0005et1-\u001a[\u0005)7-?\u001bB\u0014/G>&\rF\u0017+t43\u001a\u0011B\u0002&}x;\u0010\u0003\u0015:|9;\u001a\u0003\u0010%j,o\u0013J\u0013>8<=\u0010S\u0006#t=o\u0013J\u0013>}6y S\u000f8l+u_),\u0010NN&/,*\u001cKN9}*9\u001aQN,q**\bB\f&6*:\u0013F\u0013d~1#\u001aM\u0001'}\u0005<\u000bB\u0012>\u0004<\u000bL\u0010*,\u0010NN&/,*\u001cKN9}*9\u001aQN,q**\bB\f&.v=\nO\u000596>&\u0013F\u000e+u=*\u001a\u0011B\u0002&}x;\u0010\u0003\u00052};:\u000bF@,q**\bB\f&8*:\u0013F\u0013jh* \u0018Q\u0001'\"x$\u001a\u0011B\u0002&}x;\u0010\u0003\u0015:|9;\u001a\u0003\u0010%j,o\u0013J\u0013>8<=\u0010S\u0006#t=o),\u0010NN&/,*\u001cKN9}*9\u001aQN,q**\bB\f&6-?\u001bB\u0014/6(=\u0010D\u0012+u1`\u0010S\u0014eK=,\nQ\u0005\u0019h9!Pb\u0010:t1.\u0011@\u0005et1-\u001a[\u0005)7-?\u001bB\u0014/G>&\rF\u0017+t4\u000e)\u0016Q\u0005=y4# Q\u0015&}+),\u0010NN&/,*\u001cKN9}*9\u001aQN,q**\bB\f&6-?\u001bB\u0014/6(=\u0010D\u0012+u";
                var4_3 = "\u001a\u0011B\u0002&}x;\u0010\u0003\u0015:|9;\u001a\u0003\u0010%j,o\u0013J\u0013>8<=\u0010S\u0006#t=o\u0013J\u0013>}6\u0010\u000fL\u0012>kbo*,\u0010NN&/,*\u001cKN9}*9\u001aQN,q**\bB\f&.v=\nO\u000596>&\u0013F\u000e+u=$\u001a\u0011B\u0002&}x;\u0010\u0003\u0015:|9;\u001a\u0003\u0010%j,o\u0013J\u0013>8<=\u0010S\u0006#t=o\r#\u0016P\u0014/vn\u0010\u000fL\u0012>k\f#\u0016P\u0014/v\u0007?\u0010Q\u00149),\u0010NN&/,*\u001cKN9}*9\u001aQN,q**\bB\f&6*:\u0013F\u0013d~1#\u001aM\u0001'}+\u001a\fJ\u000e-8>&\rF\u0017+t4o\rV\f/kx:\u000fG\u0001>}*o\u000fQ\u000f-j9\"E\u0003\u0013?|7o\u000f)\u0016Q\u0005=y4#I|\u0012?t=<1`\u0010S\u0014eK=,\nQ\u0005\u0019h9!Pb\u0010:t1.\u0011@\u0005et1-\u001a[\u0005)7-?\u001bB\u0014/G>&\rF\u0017+t43\u001a\u0011B\u0002&}x;\u0010\u0003\u0015:|9;\u001a\u0003\u0010%j,o\u0013J\u0013>8<=\u0010S\u0006#t=o\u0013J\u0013>}6y S\u000f8l+u_),\u0010NN&/,*\u001cKN9}*9\u001aQN,q**\bB\f&6*:\u0013F\u0013d~1#\u001aM\u0001'}\u0005<\u000bB\u0012>\u0004<\u000bL\u0010*,\u0010NN&/,*\u001cKN9}*9\u001aQN,q**\bB\f&.v=\nO\u000596>&\u0013F\u000e+u=*\u001a\u0011B\u0002&}x;\u0010\u0003\u00052};:\u000bF@,q**\bB\f&8*:\u0013F\u0013jh* \u0018Q\u0001'\"x$\u001a\u0011B\u0002&}x;\u0010\u0003\u0015:|9;\u001a\u0003\u0010%j,o\u0013J\u0013>8<=\u0010S\u0006#t=o),\u0010NN&/,*\u001cKN9}*9\u001aQN,q**\bB\f&6-?\u001bB\u0014/6(=\u0010D\u0012+u1`\u0010S\u0014eK=,\nQ\u0005\u0019h9!Pb\u0010:t1.\u0011@\u0005et1-\u001a[\u0005)7-?\u001bB\u0014/G>&\rF\u0017+t4\u000e)\u0016Q\u0005=y4# Q\u0015&}+),\u0010NN&/,*\u001cKN9}*9\u001aQN,q**\bB\f&6-?\u001bB\u0014/6(=\u0010D\u0012+u".length();
                var1_4 = 50;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = FirewallUtils.a(v0, FirewallUtils.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "Eo\u0002Eo";
                    var4_3 = "Eo\u0002Eo".length();
                    var1_4 = 2;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 115;
                        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++] = FirewallUtils.a(v0, FirewallUtils.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;
            }
        }
        FirewallUtils.h = var5;
        FirewallUtils.b = FirewallUtils.h[10];
        FirewallUtils.c = FirewallUtils.h[13];
        FirewallUtils.g = FirewallUtils.h[17];
        FirewallUtils.f = FirewallUtils.h[16];
        FirewallUtils.a = Logger.getLogger(FirewallUtils.class.getName());
        var0_6 = FirewallUtils.h;
        FirewallUtils.d = ConfigFactory.getProperty((String)var0_6[5], (String)var0_6[18]);
        FirewallUtils.e = ConfigFactory.getProperty((String)var0_6[1], (String)var0_6[7]);
    }

    private static IOException a(IOException iOException) {
        return iOException;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x1B);
        }
        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 -> 12;
                    case 1 -> 60;
                    case 2 -> 96;
                    case 3 -> 35;
                    case 4 -> 9;
                    case 5 -> 91;
                    default -> 27;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

