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 static String dashUUIDString(String uuid) { 039 040 return uuid.replaceAll(PluginCore.UUID_MATCHER_PATTERN.pattern(), "$1-$2-$3-$4-$5"); 041 } 042 043 /** 044 * @return The URL for this UUID API Service. 045 */ 046 String url(); 047 048 /** 049 * @return True if the site uses SSL technology, otherwise false. 050 */ 051 default boolean isSSL() { 052 053 return url().contains("https"); 054 } 055 056 default UUID getUUID(String username) { 057 058 JSONObject object = sendRequestJSON(username); 059 060 UUID id = (object != null && object.containsKey("uuid"))? UUID.fromString(object.get("uuid").toString()) 061 : null; 062 063 if(id != null) { 064 PluginCore.uuidProvider().store(new UUIDPair(id, username)); 065 } 066 return id; 067 } 068 069 default JSONObject sendRequestJSON(final String linkAddition) { 070 071 return (JSONObject)JSONValue.parse(sendRequest(linkAddition)); 072 } 073 074 default String sendRequest(final String linkAddition) { 075 076 StringBuilder builder = new StringBuilder(); 077 HttpsURLConnection connection = null; 078 try { 079 080 TrustManager[] trustAllCerts = new TrustManager[]{ new X509TrustManager() { 081 public X509Certificate[] getAcceptedIssuers() { return null; } 082 083 public void checkClientTrusted(X509Certificate[] certs, String authType) { 084 //empty 085 } 086 087 public void checkServerTrusted(X509Certificate[] certs, String authType) { 088 //empty 089 } 090 } }; 091 092 SSLContext sc = SSLContext.getInstance("TLS"); 093 sc.init(null, trustAllCerts, new SecureRandom()); 094 HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 095 096 connection = (HttpsURLConnection)new URL(url() + linkAddition).openConnection(); 097 connection.setConnectTimeout(15000); 098 connection.setReadTimeout(60000); 099 connection.setRequestMethod("GET"); 100 connection.setRequestProperty("Accept", "application/json"); 101 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 102 String response; 103 while((response = reader.readLine()) != null) { 104 builder.append(response); 105 } 106 reader.close(); 107 } catch(Exception e) { 108 return ""; 109 } finally { 110 if(connection != null) { 111 connection.disconnect(); 112 } 113 } 114 return builder.toString(); 115 } 116}