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:
Christian Koop 2023-03-04 13:08:16 +01:00
parent 6145021ecb
commit 5199043a4b
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
1 changed files with 50 additions and 44 deletions

View File

@ -3,11 +3,13 @@ package com.songoda.core.utils;
import org.bukkit.Bukkit;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
@ -19,14 +21,11 @@ import java.util.UUID;
public class SongodaAuth {
public static boolean isAuthorized(boolean allowOffline) {
String productId = "%%__PLUGIN__%%";
try {
Integer.parseInt(productId);
} catch (NumberFormatException e) {
// Self compiled
if (isPluginSelfCompiled(productId)) {
return true;
}
UUID uuid = getUUID();
UUID serverUuid = getUUID();
try {
URL url = new URL("https://marketplace.songoda.com/api/v2/products/license/validate");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
@ -34,49 +33,42 @@ public class SongodaAuth {
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
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()) {
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
byte[] requestBody = requestBodyJson.getBytes(StandardCharsets.UTF_8);
os.write(requestBody, 0, requestBody.length);
}
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());
}
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");
}
JSONObject jsonResponse = readHttpResponseJson(con);
if (jsonResponse.containsKey("error")) {
Bukkit.getLogger().warning("Error validating license: " + jsonResponse.get("error"));
return false;
}
} catch (Exception e) {
return (boolean) jsonResponse.get("valid");
} catch (Exception ex) {
return allowOffline;
}
}
public static UUID getUUID() {
File serverProperties = new File(new File("."), "server.properties");
Properties prop = new Properties();
File serverProperties = new File("./server.properties");
try {
Properties prop = new Properties();
prop.load(new FileReader(serverProperties));
String uuid = prop.getProperty("uuid");
if (uuid == null || uuid.isEmpty()) {
UUID newUUID = UUID.randomUUID();
prop.setProperty("uuid", newUUID.toString());
prop.store(new FileWriter(serverProperties), null);
return newUUID;
} else {
if (uuid != null && !uuid.isEmpty()) {
return UUID.fromString(uuid);
}
UUID newUUID = UUID.randomUUID();
prop.setProperty("uuid", newUUID.toString());
prop.store(new FileWriter(serverProperties), null);
return newUUID;
} 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();
con.setRequestMethod("GET");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
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());
}
JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
return jsonObject.get("ip").toString();
}
JSONObject jsonResponse = readHttpResponseJson(con);
return jsonResponse.get("ip").toString();
} catch (Exception 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;
}
}