package com.l7tech.server.transport;

import com.l7tech.common.io.PortRanges;
import com.l7tech.gateway.common.transport.ResolutionConfiguration;
import com.l7tech.gateway.common.transport.SsgActiveConnector;
import com.l7tech.gateway.common.transport.SsgConnector;
import com.l7tech.gateway.common.transport.TransportAdmin;
import com.l7tech.gateway.common.transport.TransportDescriptor;
import com.l7tech.gateway.common.transport.TransportAdmin.CurrentAdminConnectionException;
import com.l7tech.gateway.common.transport.firewall.SsgFirewallRule;
import com.l7tech.message.Message;
import com.l7tech.objectmodel.DeleteException;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.SaveException;
import com.l7tech.objectmodel.UpdateException;
import com.l7tech.server.DefaultKey;
import com.l7tech.server.tomcat.SsgHttpConstants;
import com.l7tech.server.transport.firewall.SsgFirewallRuleManager;
import com.l7tech.util.Config;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.InetAddressUtil;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;

public class TransportAdminImpl implements TransportAdmin {
   private final SsgActiveConnectorManager a;
   private final SsgConnectorManager b;
   private final ResolutionConfigurationManager c;
   private final Config d;
   private final SsgFirewallRuleManager e;
   private final TransportAdminHelper f;
   private static final String[] g;

   public TransportAdminImpl(
      SsgActiveConnectorManager var1, SsgConnectorManager var2, SsgFirewallRuleManager var3, ResolutionConfigurationManager var4, DefaultKey var5, Config var6
   ) {
      this.a = var1;
      this.b = var2;
      this.c = var4;
      this.d = var6;
      this.e = var3;
      this.f = new TransportAdminHelper(var5);
   }

   public Collection<SsgConnector> findAllSsgConnectors() throws FindException {
      return this.b.findAll();
   }

   public SsgConnector findSsgConnectorByPrimaryKey(Goid var1) throws FindException {
      return (SsgConnector)this.b.findByPrimaryKey(var1);
   }

   private boolean a(Goid var1) {
      Goid var2 = SsgHttpConstants.getCurrentThreadSsgConnectorGoid();
      return var1.equals(var2);
   }

   public Goid saveSsgConnector(SsgConnector var1) throws SaveException, UpdateException, CurrentAdminConnectionException {
      try {
         if (this.a(var1.getGoid())) {
            throw new CurrentAdminConnectionException(g[3]);
         }
      } catch (SaveException var2) {
         throw a(var2);
      }

      try {
         if (var1.getGoid().equals(SsgConnector.DEFAULT_GOID)) {
            return this.b.save(var1);
         }
      } catch (SaveException var3) {
         throw a(var3);
      }

      this.b.update(var1);
      return var1.getGoid();
   }

   public void deleteSsgConnector(Goid var1) throws DeleteException, FindException, CurrentAdminConnectionException {
      try {
         if (this.a(var1)) {
            throw new CurrentAdminConnectionException(g[1]);
         }
      } catch (DeleteException var2) {
         throw a(var2);
      }

      this.b.delete(var1);
   }

   public SsgActiveConnector findSsgActiveConnectorByPrimaryKey(Goid var1) throws FindException {
      return (SsgActiveConnector)this.a.findByPrimaryKey(var1);
   }

   public SsgActiveConnector findSsgActiveConnectorByTypeAndName(String param1, String param2) throws FindException {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: aload 1
      // 01: ifnull 0f
      // 04: aload 2
      // 05: ifnonnull 15
      // 08: goto 0f
      // 0b: invokestatic com/l7tech/server/transport/TransportAdminImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 0e: athrow
      // 0f: aconst_null
      // 10: areturn
      // 11: invokestatic com/l7tech/server/transport/TransportAdminImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 14: athrow
      // 15: aload 0
      // 16: aload 1
      // 17: invokevirtual com/l7tech/server/transport/TransportAdminImpl.findSsgActiveConnectorsByType (Ljava/lang/String;)Ljava/util/Collection;
      // 1a: astore 3
      // 1b: aload 3
      // 1c: invokeinterface java/util/Collection.iterator ()Ljava/util/Iterator; 1
      // 21: astore 4
      // 23: aload 4
      // 25: invokeinterface java/util/Iterator.hasNext ()Z 1
      // 2a: ifeq 4f
      // 2d: aload 4
      // 2f: invokeinterface java/util/Iterator.next ()Ljava/lang/Object; 1
      // 34: checkcast com/l7tech/gateway/common/transport/SsgActiveConnector
      // 37: astore 5
      // 39: aload 2
      // 3a: aload 5
      // 3c: invokevirtual com/l7tech/gateway/common/transport/SsgActiveConnector.getName ()Ljava/lang/String;
      // 3f: invokevirtual java/lang/String.equals (Ljava/lang/Object;)Z
      // 42: ifeq 4c
      // 45: aload 5
      // 47: areturn
      // 48: invokestatic com/l7tech/server/transport/TransportAdminImpl.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 4b: athrow
      // 4c: goto 23
      // 4f: aconst_null
      // 50: areturn
      // try (0 -> 4): 5 com/l7tech/objectmodel/FindException
      // try (2 -> 9): 9 com/l7tech/objectmodel/FindException
      // try (25 -> 32): 32 com/l7tech/objectmodel/FindException
   }

   public Collection<SsgActiveConnector> findSsgActiveConnectorsByType(String var1) throws FindException {
      return this.a.findSsgActiveConnectorsByType(var1);
   }

   public Goid saveSsgActiveConnector(SsgActiveConnector var1) throws SaveException, UpdateException {
      try {
         if (var1.getGoid().equals(SsgActiveConnector.DEFAULT_GOID)) {
            return this.a.save(var1);
         }
      } catch (SaveException var2) {
         throw a(var2);
      }

      this.a.update(var1);
      return var1.getGoid();
   }

   public void deleteSsgActiveConnector(Goid var1) throws DeleteException, FindException {
      this.a.delete(var1);
   }

   public String[] getAllProtocolVersions(boolean var1) {
      return this.f.getAllProtocolVersions(var1);
   }

   public String[] getAllCipherSuiteNames() {
      return this.f.getAllCipherSuiteNames();
   }

   @Deprecated
   public String[] getDefaultCipherSuiteNames() {
      return this.f.getDefaultCipherSuiteNames();
   }

   public String[] getDefaultInboundCipherSuiteNames() {
      return this.f.getDefaultInboundCipherSuiteNames();
   }

   public String[] getDefaultOutboundCipherSuiteNames() {
      return this.f.getDefaultOutboundCipherSuiteNames();
   }

   public String[] getVisibleCipherSuiteNames() {
      return this.f.getVisibleCipherSuiteNames();
   }

   public InetAddress[] getAvailableBindAddresses() {
      try {
         ArrayList var1 = new ArrayList();
         Enumeration var2 = NetworkInterface.getNetworkInterfaces();

         while (var2.hasMoreElements()) {
            NetworkInterface var3 = (NetworkInterface)var2.nextElement();
            Enumeration var4 = var3.getInetAddresses();

            try {
               while (var4.hasMoreElements()) {
                  var1.add((InetAddress)var4.nextElement());
               }
            } catch (SocketException var5) {
               throw a(var5);
            }
         }

         return var1.toArray(new InetAddress[var1.size()]);
      } catch (SocketException var6) {
         throw new RuntimeException(g[2] + ExceptionUtils.getMessage(var6), var6);
      }
   }

   public TransportDescriptor[] getModularConnectorInfo() {
      return this.b.getTransportProtocols();
   }

   public PortRanges getReservedPorts() {
      return this.b.getReservedPorts();
   }

   public boolean isUseIpv6() {
      return InetAddressUtil.isUseIpv6();
   }

   public ResolutionConfiguration getResolutionConfigurationByName(String var1) throws FindException {
      return (ResolutionConfiguration)this.c.findByUniqueName(var1);
   }

   public Goid saveResolutionConfiguration(ResolutionConfiguration var1) throws SaveException {
      Goid var2;
      if (var1.isUnsaved()) {
         var2 = this.c.save(var1);
      } else {
         var2 = var1.getGoid();

         try {
            this.c.update(var1);
         } catch (UpdateException var4) {
            throw new SaveException(ExceptionUtils.getMessage(var4), var4);
         }
      }

      return var2;
   }

   public long getXmlMaxBytes() {
      return Message.getMaxBytes();
   }

   public boolean isSnmpQueryEnabled() {
      return this.d.getBooleanProperty(g[4], true);
   }

   public Collection<SsgFirewallRule> findAllFirewallRules() throws FindException {
      return this.e.findAll();
   }

   public void deleteFirewallRule(Goid var1) throws DeleteException, FindException, CurrentAdminConnectionException {
      this.e.delete(var1);
   }

   public Goid saveFirewallRule(SsgFirewallRule var1) throws SaveException, UpdateException, CurrentAdminConnectionException {
      try {
         if (this.a(var1.getGoid())) {
            throw new CurrentAdminConnectionException(g[0]);
         }
      } catch (SaveException var2) {
         throw a(var2);
      }

      try {
         if (var1.getGoid().equals(SsgFirewallRule.DEFAULT_GOID)) {
            return this.e.save(var1);
         }
      } catch (SaveException var3) {
         throw a(var3);
      }

      this.e.update(var1);
      return var1.getGoid();
   }

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

   static {
      String[] var5 = new String[5];
      int var3 = 0;
      String var2 = "`\r\u0019lL\u0002_A\fXcO\u0003\u0016S\u001aXmO\t\u0011P\u0000\faRG\u0019Z\u0011XmU\u0015\rP\r\f.A\u0003\u0012\\\rXmO\t\u0011P\u0000\fgO\t7`\r\u0019lL\u0002_A\fXjE\u000b\u001aA\u0006XmO\t\u0011P\u0000\faRG\u0019Z\u0011XmU\u0015\rP\r\f.A\u0003\u0012\\\rXmO\t\u0011P\u0000\fgO\t\"`\r\u0019lL\u0002_A\fXiE\u0013_[\u0006\fyO\u0015\u0014\u0015\n\u0016zE\u0015\u0019T\u0000\u001d}\u001aG";
      int var4 = "`\r\u0019lL\u0002_A\fXcO\u0003\u0016S\u001aXmO\t\u0011P\u0000\faRG\u0019Z\u0011XmU\u0015\rP\r\f.A\u0003\u0012\\\rXmO\t\u0011P\u0000\fgO\t7`\r\u0019lL\u0002_A\fXjE\u000b\u001aA\u0006XmO\t\u0011P\u0000\faRG\u0019Z\u0011XmU\u0015\rP\r\f.A\u0003\u0012\\\rXmO\t\u0011P\u0000\fgO\t\"`\r\u0019lL\u0002_A\fXiE\u0013_[\u0006\fyO\u0015\u0014\u0015\n\u0016zE\u0015\u0019T\u0000\u001d}\u001aG"
         .length();
      char var1 = '7';
      int var7 = -1;

      label24:
      while (true) {
         byte var10000 = 60;
         String var10001 = var2.substring(++var7, var7 + var1);
         byte var10002 = -1;

         while (true) {
            String var8 = a(var10000, a(var10001));
            switch (var10002) {
               case 0:
                  var5[var3++] = var8;
                  if ((var7 += var1) >= var4) {
                     g = var5;
                     return;
                  }

                  var1 = var2.charAt(var7);
                  break;
               default:
                  var5[var3++] = var8;
                  if ((var7 += var1) < var4) {
                     var1 = var2.charAt(var7);
                     continue label24;
                  }

                  var2 = "w\u001a\u000e{[\u0015HV\u001bOtX\u0014\u0001D\rOzX\u001e\u0006G\u0017\u001bvEP\u000eM\u0006OzB\u0002\u001aG\u001a\u001b9V\u0014\u0005K\u001aOzX\u001e\u0006G\u0017\u001bpX\u001e @\u0001\u0006uC\u0019\u0006q\u0011\u001do^\u0013\r\f\u0007\u0001tG!\u001dG\u0006\u00167R\u001e\t@\u0018\n}";
                  var4 = "w\u001a\u000e{[\u0015HV\u001bOtX\u0014\u0001D\rOzX\u001e\u0006G\u0017\u001bvEP\u000eM\u0006OzB\u0002\u001aG\u001a\u001b9V\u0014\u0005K\u001aOzX\u001e\u0006G\u0017\u001bpX\u001e @\u0001\u0006uC\u0019\u0006q\u0011\u001do^\u0013\r\f\u0007\u0001tG!\u001dG\u0006\u00167R\u001e\t@\u0018\n}"
                     .length();
                  var1 = '7';
                  var7 = -1;
            }

            var10000 = 43;
            var10001 = var2.substring(++var7, var7 + var1);
            var10002 = 0;
         }
      }
   }

   private static char[] a(String var0) {
      char[] var10000 = var0.toCharArray();
      if (var10000.length < 2) {
         var10000[0] = (char)(var10000[0] ^ 67);
      }

      return var10000;
   }

   private static String a(int var0, char[] var1) {
      int var10003 = var1.length;
      int var10002 = var0;
      char[] var10001 = var1;
      int var10000 = var10003;

      for (int var2 = 0; var10000 > var2; var2++) {
         var10001[var2] = (char)(var10001[var2] ^ var10002 ^ switch (var2 % 7) {
            case 0 -> 9;
            case 1 -> 95;
            case 2 -> 68;
            case 3 -> 50;
            case 4 -> 28;
            case 5 -> 91;
            default -> 67;
         });
      }

      return new String(var10001).intern();
   }
}
