mirror of
https://github.com/songoda/SongodaCore.git
synced 2024-11-27 12:35:12 +01:00
Minor code refractoring on SongodaAuth
Just trying to make it a bit more readable and easier to understand at a glance.
This commit is contained in:
parent
6145021ecb
commit
5199043a4b
@ -3,11 +3,13 @@ package com.songoda.core.utils;
|
|||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
import org.json.simple.parser.JSONParser;
|
import org.json.simple.parser.JSONParser;
|
||||||
|
import org.json.simple.parser.ParseException;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
@ -19,14 +21,11 @@ import java.util.UUID;
|
|||||||
public class SongodaAuth {
|
public class SongodaAuth {
|
||||||
public static boolean isAuthorized(boolean allowOffline) {
|
public static boolean isAuthorized(boolean allowOffline) {
|
||||||
String productId = "%%__PLUGIN__%%";
|
String productId = "%%__PLUGIN__%%";
|
||||||
try {
|
if (isPluginSelfCompiled(productId)) {
|
||||||
Integer.parseInt(productId);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
// Self compiled
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
UUID uuid = getUUID();
|
UUID serverUuid = getUUID();
|
||||||
try {
|
try {
|
||||||
URL url = new URL("https://marketplace.songoda.com/api/v2/products/license/validate");
|
URL url = new URL("https://marketplace.songoda.com/api/v2/products/license/validate");
|
||||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||||
@ -34,49 +33,42 @@ public class SongodaAuth {
|
|||||||
con.setRequestProperty("Content-Type", "application/json");
|
con.setRequestProperty("Content-Type", "application/json");
|
||||||
con.setRequestProperty("Accept", "application/json");
|
con.setRequestProperty("Accept", "application/json");
|
||||||
con.setDoOutput(true);
|
con.setDoOutput(true);
|
||||||
String jsonInputString = "{\"product_id\":" + productId + ",\"license\":\"" + uuid + "\",\"user_id\":\"%%__USER__%%\"}";
|
|
||||||
|
String requestBodyJson = "{\"product_id\":" + productId + ",\"license\":\"" + serverUuid + "\",\"user_id\":\"%%__USER__%%\"}";
|
||||||
try (OutputStream os = con.getOutputStream()) {
|
try (OutputStream os = con.getOutputStream()) {
|
||||||
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
|
byte[] requestBody = requestBodyJson.getBytes(StandardCharsets.UTF_8);
|
||||||
os.write(input, 0, input.length);
|
os.write(requestBody, 0, requestBody.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
|
JSONObject jsonResponse = readHttpResponseJson(con);
|
||||||
StringBuilder response = new StringBuilder();
|
if (jsonResponse.containsKey("error")) {
|
||||||
String responseLine;
|
Bukkit.getLogger().warning("Error validating license: " + jsonResponse.get("error"));
|
||||||
while ((responseLine = br.readLine()) != null) {
|
return false;
|
||||||
response.append(responseLine.trim());
|
|
||||||
}
|
|
||||||
|
|
||||||
JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
|
|
||||||
if (jsonObject.get("error") != null) {
|
|
||||||
//Got an error, return false and print error
|
|
||||||
Bukkit.getLogger().warning("Error validating license: " + jsonObject.get("error"));
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return (boolean) jsonObject.get("valid");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
|
return (boolean) jsonResponse.get("valid");
|
||||||
|
} catch (Exception ex) {
|
||||||
return allowOffline;
|
return allowOffline;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UUID getUUID() {
|
public static UUID getUUID() {
|
||||||
File serverProperties = new File(new File("."), "server.properties");
|
File serverProperties = new File("./server.properties");
|
||||||
Properties prop = new Properties();
|
|
||||||
try {
|
try {
|
||||||
|
Properties prop = new Properties();
|
||||||
prop.load(new FileReader(serverProperties));
|
prop.load(new FileReader(serverProperties));
|
||||||
|
|
||||||
String uuid = prop.getProperty("uuid");
|
String uuid = prop.getProperty("uuid");
|
||||||
if (uuid == null || uuid.isEmpty()) {
|
if (uuid != null && !uuid.isEmpty()) {
|
||||||
UUID newUUID = UUID.randomUUID();
|
|
||||||
prop.setProperty("uuid", newUUID.toString());
|
|
||||||
prop.store(new FileWriter(serverProperties), null);
|
|
||||||
return newUUID;
|
|
||||||
} else {
|
|
||||||
return UUID.fromString(uuid);
|
return UUID.fromString(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UUID newUUID = UUID.randomUUID();
|
||||||
|
prop.setProperty("uuid", newUUID.toString());
|
||||||
|
prop.store(new FileWriter(serverProperties), null);
|
||||||
|
return newUUID;
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new RuntimeException("Could not fetch UUID for server", ex);
|
throw new RuntimeException("Could not determine UUID for server", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,20 +78,34 @@ public class SongodaAuth {
|
|||||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||||
con.setRequestMethod("GET");
|
con.setRequestMethod("GET");
|
||||||
con.setRequestProperty("Accept", "application/json");
|
con.setRequestProperty("Accept", "application/json");
|
||||||
con.setDoOutput(true);
|
|
||||||
|
|
||||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
|
JSONObject jsonResponse = readHttpResponseJson(con);
|
||||||
StringBuilder response = new StringBuilder();
|
return jsonResponse.get("ip").toString();
|
||||||
String responseLine;
|
|
||||||
while ((responseLine = br.readLine()) != null) {
|
|
||||||
response.append(responseLine.trim());
|
|
||||||
}
|
|
||||||
|
|
||||||
JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
|
|
||||||
return jsonObject.get("ip").toString();
|
|
||||||
}
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new RuntimeException("Could not fetch IP address", ex);
|
throw new RuntimeException("Could not fetch IP address", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static JSONObject readHttpResponseJson(HttpURLConnection con) throws IOException, ParseException {
|
||||||
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
|
||||||
|
String responseLine;
|
||||||
|
while ((responseLine = br.readLine()) != null) {
|
||||||
|
response.append(responseLine.trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
return (JSONObject) new JSONParser().parse(response.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isPluginSelfCompiled(String productId) {
|
||||||
|
try {
|
||||||
|
Integer.parseInt(productId);
|
||||||
|
return false;
|
||||||
|
} catch (NumberFormatException ignore) {
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user