001package net.tnemc.plugincore.core.id;
002
003import net.tnemc.plugincore.PluginCore;
004import org.json.simple.JSONObject;
005import org.json.simple.JSONValue;
006
007import javax.net.ssl.HttpsURLConnection;
008import javax.net.ssl.SSLContext;
009import javax.net.ssl.TrustManager;
010import javax.net.ssl.X509TrustManager;
011import java.io.BufferedReader;
012import java.io.InputStreamReader;
013import java.net.URL;
014import java.security.SecureRandom;
015import java.security.cert.X509Certificate;
016import java.util.UUID;
017
018/*
019 * The New Economy Minecraft Server Plugin
020 * <p>
021 * Created by creatorfromhell on 9/9/2020.
022 * <p>
023 * This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
024 * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ or send a letter to
025 * Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
026 * Created by creatorfromhell on 06/30/2017.
027 */
028
029/**
030 * Represents an API that could be used to look up a UUID from name, and sometimes additional
031 * functionality.
032 *
033 * @author creatorfromhell
034 * @since 0.1.2.0
035 */
036public interface UUIDAPI {
037
038  /**
039   * @return The URL for this UUID API Service.
040   */
041  String url();
042
043  /**
044   * @return True if the site uses SSL technology, otherwise false.
045   */
046  default boolean isSSL() {
047    return url().contains("https");
048  }
049
050  default UUID getUUID(String username) {
051    JSONObject object = sendRequestJSON(username);
052
053    UUID id = (object != null && object.containsKey("uuid"))? UUID.fromString(object.get("uuid").toString())
054        : null;
055
056    if(id != null) {
057      PluginCore.uuidProvider().store(new UUIDPair(id, username));
058    }
059    return id;
060  }
061
062  static String dashUUIDString(String uuid) {
063    return uuid.replaceAll(PluginCore.UUID_MATCHER_PATTERN.pattern(), "$1-$2-$3-$4-$5");
064  }
065
066  default JSONObject sendRequestJSON(final String linkAddition) {
067    return (JSONObject) JSONValue.parse(sendRequest(linkAddition));
068  }
069
070  default String sendRequest(final String linkAddition) {
071    StringBuilder builder = new StringBuilder();
072    HttpsURLConnection connection = null;
073    try {
074
075      TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
076        public X509Certificate[] getAcceptedIssuers(){return null;}
077        public void checkClientTrusted(X509Certificate[] certs, String authType){
078          //empty
079        }
080        public void checkServerTrusted(X509Certificate[] certs, String authType){
081          //empty
082        }
083      }};
084
085      SSLContext sc = SSLContext.getInstance("TLS");
086      sc.init(null, trustAllCerts, new SecureRandom());
087      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
088
089      connection = (HttpsURLConnection) new URL(url() + linkAddition).openConnection();
090      connection.setConnectTimeout(15000);
091      connection.setReadTimeout(60000);
092      connection.setRequestMethod("GET");
093      connection.setRequestProperty("Accept", "application/json");
094      BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
095      String response;
096      while((response = reader.readLine()) != null) {
097        builder.append(response);
098      }
099      reader.close();
100    } catch (Exception e) {
101      return "";
102    } finally {
103      if(connection != null) {
104        connection.disconnect();
105      }
106    }
107    return builder.toString();
108  }
109}