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
027    return new TrustManager[]{
028            new X509TrustManager() {
029              public X509Certificate[] getAcceptedIssuers() {
030
031                return null;
032              }
033
034              public void checkClientTrusted(final X509Certificate[] certs, final String authType) {
035                //nothing to see here
036              }
037
038              public void checkServerTrusted(final X509Certificate[] certs, final String authType) {
039                //nothing to see here
040              }
041            }
042    };
043  }
044
045  /**
046   * Used to read the current TNE release from tnemc.net.
047   *
048   * @return The version read, or an empty Optional if the call timed out.
049   */
050  public static Optional<String> readVersion() {
051
052    String build = null;
053    try {
054      final SSLContext sc = SSLContext.getInstance("TLS");
055      sc.init(null, selfCertificates(), new SecureRandom());
056      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
057
058      final URL url = new URL(PluginCore.engine().versionCheckSite());
059      final HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
060      connection.setReadTimeout(3000);
061
062      final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
063      build = in.readLine();
064      in.close();
065    } catch(final Exception ignore) {
066      PluginCore.log().warning("Unable to contact update server!", DebugLevel.OFF);
067    }
068    return Optional.ofNullable(build);
069  }
070
071  /**
072   * Used to find the absolute path based on a case-insensitive file name, in a directory.
073   *
074   * @param file      The file name to use for the search.
075   * @param directory The directory to search in.
076   *
077   * @return An optional containing the path, or an empty optional if unable to locate the file in
078   * the directory.
079   */
080  public static Optional<String> findFileInsensitive(final String file, final File directory) {
081
082    final File[] jars = directory.listFiles((dir, name)->name.endsWith(".jar"));
083
084    if(jars != null) {
085      for(final File jar : jars) {
086        if(jar.getAbsolutePath().toLowerCase().contains(file.toLowerCase() + ".jar")) {
087          return Optional.of(jar.getAbsolutePath());
088        }
089      }
090    }
091    return Optional.empty();
092  }
093
094  public static File[] getYAMLs(final File directory) {
095
096    if(!directory.exists()) {
097      directory.mkdirs();
098      return new File[0];
099    }
100    return directory.listFiles((dir, name)->name.endsWith(".yml") || name.endsWith(".yaml"));
101  }
102}