001package net.tnemc.plugincore.core.utils;
002
003import net.tnemc.plugincore.PluginCore;
004import net.tnemc.plugincore.core.compatibility.log.DebugLevel;
005
006import javax.net.ssl.HttpsURLConnection;
007import javax.net.ssl.SSLContext;
008import javax.net.ssl.TrustManager;
009import javax.net.ssl.X509TrustManager;
010import java.io.BufferedReader;
011import java.io.File;
012import java.io.InputStreamReader;
013import java.net.URL;
014import java.security.SecureRandom;
015import java.security.cert.X509Certificate;
016import java.util.Optional;
017
018public class IOUtil {
019
020  /**
021   * Used to trust self-signed SSL certificates during an HTTP stream reading session.
022   *
023   * @return The {@link TrustManager} array.
024   */
025  public static TrustManager[] selfCertificates() {
026    return new TrustManager[] {
027        new X509TrustManager() {
028          public X509Certificate[] getAcceptedIssuers() {
029            return null;
030          }
031
032          public void checkClientTrusted(final X509Certificate[] certs, final String authType) {
033            //nothing to see here
034          }
035
036          public void checkServerTrusted(final X509Certificate[] certs, final String authType) {
037            //nothing to see here
038          }
039        }
040    };
041  }
042
043  /**
044   * Used to read the current TNE release from tnemc.net.
045   * @return The version read, or an empty Optional if the call timed out.
046   */
047  public static Optional<String> readVersion() {
048    String build = null;
049    try {
050      final SSLContext sc = SSLContext.getInstance("TLS");
051      sc.init(null, selfCertificates(), new SecureRandom());
052      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
053
054      final URL url = new URL(PluginCore.engine().versionCheckSite());
055      final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
056      connection.setReadTimeout(3000);
057
058      final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
059      build = in.readLine();
060      in.close();
061    } catch (final Exception ignore) {
062      PluginCore.log().warning("Unable to contact update server!", DebugLevel.OFF);
063    }
064    return Optional.ofNullable(build);
065  }
066
067  /**
068   * Used to find the absolute path based on a case-insensitive file name, in a directory.
069   * @param file The file name to use for the search.
070   * @param directory The directory to search in.
071   * @return An optional containing the path, or an empty optional if unable to locate the file in
072   * the directory.
073   */
074  public static Optional<String> findFileInsensitive(final String file, final File directory) {
075    final File[] jars = directory.listFiles((dir, name) -> name.endsWith(".jar"));
076
077    if(jars != null) {
078      for (final File jar : jars) {
079        if(jar.getAbsolutePath().toLowerCase().contains(file.toLowerCase() + ".jar")) {
080          return Optional.of(jar.getAbsolutePath());
081        }
082      }
083    }
084    return Optional.empty();
085  }
086
087  public static File[] getYAMLs(final File directory) {
088    if (!directory.exists()) {
089      directory.mkdirs();
090      return new File[0];
091    }
092    return directory.listFiles((dir, name) -> name.endsWith(".yml") || name.endsWith(".yaml"));
093  }
094}