mirror of
https://github.com/GravitLauncher/Launcher
synced 2025-01-09 09:09:46 +03:00
Отказ от minimal-json в пользу gson
This commit is contained in:
parent
d4d4f78387
commit
ad924961e6
11 changed files with 189 additions and 314 deletions
|
@ -44,7 +44,6 @@ public static void registerHandlers() {
|
||||||
registerHandler("binaryFile", BinaryFileAuthHandler::new);
|
registerHandler("binaryFile", BinaryFileAuthHandler::new);
|
||||||
registerHandler("textFile", TextFileAuthHandler::new);
|
registerHandler("textFile", TextFileAuthHandler::new);
|
||||||
registerHandler("mysql", MySQLAuthHandler::new);
|
registerHandler("mysql", MySQLAuthHandler::new);
|
||||||
registerHandler("json", JsonAuthHandler::new);
|
|
||||||
registredHandl = true;
|
registredHandl = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,123 +0,0 @@
|
||||||
package ru.gravit.launchserver.auth.handler;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import com.eclipsesource.json.Json;
|
|
||||||
import com.eclipsesource.json.JsonObject;
|
|
||||||
import com.eclipsesource.json.JsonValue;
|
|
||||||
|
|
||||||
import ru.gravit.launchserver.auth.provider.AuthProviderResult;
|
|
||||||
import ru.gravit.utils.HTTPRequest;
|
|
||||||
import ru.gravit.utils.helper.IOHelper;
|
|
||||||
import ru.gravit.utils.helper.VerifyHelper;
|
|
||||||
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
|
|
||||||
import ru.gravit.launcher.serialize.config.entry.StringConfigEntry;
|
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public class JsonAuthHandler extends AuthHandler {
|
|
||||||
|
|
||||||
private final URL url;
|
|
||||||
private final URL urlCheckServer;
|
|
||||||
private final URL urlJoinServer;
|
|
||||||
private final URL urlUsernameToUUID;
|
|
||||||
private final URL urlUUIDToUsername;
|
|
||||||
private final String userKeyName;
|
|
||||||
private final String serverIDKeyName;
|
|
||||||
private final String accessTokenKeyName;
|
|
||||||
private final String uuidKeyName;
|
|
||||||
private final String responseErrorKeyName;
|
|
||||||
private final String responseOKKeyName;
|
|
||||||
|
|
||||||
protected JsonAuthHandler(BlockConfigEntry block) {
|
|
||||||
super(block);
|
|
||||||
String configUrl = block.getEntryValue("url", StringConfigEntry.class);
|
|
||||||
String configUrlCheckServer = block.getEntryValue("urlCheckServer", StringConfigEntry.class);
|
|
||||||
String configUrlJoinServer = block.getEntryValue("urlJoinServer", StringConfigEntry.class);
|
|
||||||
String configUrlUsernameUUID = block.getEntryValue("urlUsernameToUUID", StringConfigEntry.class);
|
|
||||||
String configUrlUUIDUsername = block.getEntryValue("urlUUIDToUsername", StringConfigEntry.class);
|
|
||||||
userKeyName = VerifyHelper.verify(block.getEntryValue("userKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "Username key name can't be empty");
|
|
||||||
serverIDKeyName = VerifyHelper.verify(block.getEntryValue("serverIDKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "ServerID key name can't be empty");
|
|
||||||
uuidKeyName = VerifyHelper.verify(block.getEntryValue("UUIDKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "UUID key name can't be empty");
|
|
||||||
accessTokenKeyName = VerifyHelper.verify(block.getEntryValue("accessTokenKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "AccessToken key name can't be empty");
|
|
||||||
responseErrorKeyName = VerifyHelper.verify(block.getEntryValue("responseErrorKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "Response error key can't be empty");
|
|
||||||
responseOKKeyName = VerifyHelper.verify(block.getEntryValue("responseOKKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "Response okay key can't be empty");
|
|
||||||
url = IOHelper.convertToURL(configUrl);
|
|
||||||
urlCheckServer = IOHelper.convertToURL(configUrlCheckServer);
|
|
||||||
urlJoinServer = IOHelper.convertToURL(configUrlJoinServer);
|
|
||||||
urlUsernameToUUID = IOHelper.convertToURL(configUrlUsernameUUID);
|
|
||||||
urlUUIDToUsername = IOHelper.convertToURL(configUrlUUIDUsername);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public UUID auth(AuthProviderResult authResult) throws IOException {
|
|
||||||
JsonObject request = Json.object().add(userKeyName, authResult.username).add(accessTokenKeyName, authResult.accessToken);
|
|
||||||
JsonObject result = jsonRequestChecked(request, url);
|
|
||||||
String value;
|
|
||||||
if ((value = result.getString(uuidKeyName, null)) != null)
|
|
||||||
return UUID.fromString(value);
|
|
||||||
throw new IOException("Service error");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public UUID checkServer(String username, String serverID) throws IOException {
|
|
||||||
JsonObject request = Json.object().add(userKeyName, username).add(serverIDKeyName, serverID);
|
|
||||||
JsonObject result = jsonRequestChecked(request, urlCheckServer);
|
|
||||||
String value;
|
|
||||||
if ((value = result.getString(uuidKeyName, null)) != null)
|
|
||||||
return UUID.fromString(value);
|
|
||||||
throw new IOException("Service error");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean joinServer(String username, String accessToken, String serverID) throws IOException {
|
|
||||||
JsonObject request = Json.object().add(userKeyName, username).add(serverIDKeyName, serverID).add(accessTokenKeyName, accessToken);
|
|
||||||
HTTPRequest.jsonRequest(request, urlJoinServer);
|
|
||||||
return request.getString(responseOKKeyName, null).equals("OK");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public UUID usernameToUUID(String username) throws IOException {
|
|
||||||
JsonObject request = Json.object().add(userKeyName, username);
|
|
||||||
JsonObject result = jsonRequestChecked(request, urlUsernameToUUID);
|
|
||||||
String value;
|
|
||||||
if ((value = result.getString(uuidKeyName, null)) != null)
|
|
||||||
return UUID.fromString(value);
|
|
||||||
throw new IOException("Service error");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String uuidToUsername(UUID uuid) throws IOException {
|
|
||||||
JsonObject request = Json.object().add(uuidKeyName, uuid.toString());
|
|
||||||
JsonObject result = jsonRequestChecked(request, urlUUIDToUsername);
|
|
||||||
String value;
|
|
||||||
if ((value = result.getString(userKeyName, null)) != null)
|
|
||||||
return value;
|
|
||||||
throw new IOException("Service error");
|
|
||||||
}
|
|
||||||
|
|
||||||
public JsonObject jsonRequestChecked(JsonObject object, URL url) throws IOException {
|
|
||||||
JsonValue result = HTTPRequest.jsonRequest(object, url);
|
|
||||||
if (!result.isObject())
|
|
||||||
authError("Authentication server response is malformed");
|
|
||||||
|
|
||||||
JsonObject response = result.asObject();
|
|
||||||
String value;
|
|
||||||
|
|
||||||
if ((value = response.getString(responseErrorKeyName, null)) != null)
|
|
||||||
authError(value);
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,38 +1,79 @@
|
||||||
package ru.gravit.launchserver.auth.hwid;
|
package ru.gravit.launchserver.auth.hwid;
|
||||||
|
|
||||||
import com.eclipsesource.json.Json;
|
import com.google.gson.Gson;
|
||||||
import com.eclipsesource.json.JsonArray;
|
import com.google.gson.JsonElement;
|
||||||
import com.eclipsesource.json.JsonObject;
|
import ru.gravit.utils.HTTPRequest;
|
||||||
import com.eclipsesource.json.JsonValue;
|
|
||||||
import ru.gravit.utils.helper.IOHelper;
|
import ru.gravit.utils.helper.IOHelper;
|
||||||
import ru.gravit.utils.helper.LogHelper;
|
import ru.gravit.utils.helper.LogHelper;
|
||||||
import ru.gravit.utils.helper.VerifyHelper;
|
|
||||||
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
|
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
|
||||||
import ru.gravit.launcher.serialize.config.entry.StringConfigEntry;
|
import ru.gravit.launcher.serialize.config.entry.StringConfigEntry;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.OutputStreamWriter;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public final class JsonHWIDHandler extends HWIDHandler {
|
public final class JsonHWIDHandler extends HWIDHandler {
|
||||||
private static final int TIMEOUT = Integer.parseInt(
|
private static final int TIMEOUT = Integer.parseInt(
|
||||||
System.getProperty("launcher.connection.timeout", Integer.toString(1500)));
|
System.getProperty("launcher.connection.timeout", Integer.toString(1500)));
|
||||||
|
private static final Gson gson = new Gson();
|
||||||
|
|
||||||
private final URL url;
|
private final URL url;
|
||||||
private final URL urlBan;
|
private final URL urlBan;
|
||||||
private final URL urlUnBan;
|
private final URL urlUnBan;
|
||||||
private final URL urlGet;
|
private final URL urlGet;
|
||||||
private final String loginKeyName;
|
|
||||||
private final String hddKeyName;
|
public class banRequest
|
||||||
private final String cpuKeyName;
|
{
|
||||||
private final String biosKeyName;
|
long hwid_hdd;
|
||||||
private final String isBannedKeyName;
|
|
||||||
|
public banRequest(long hwid_hdd, long hwid_cpu, long hwid_bios) {
|
||||||
|
this.hwid_hdd = hwid_hdd;
|
||||||
|
this.hwid_cpu = hwid_cpu;
|
||||||
|
this.hwid_bios = hwid_bios;
|
||||||
|
}
|
||||||
|
|
||||||
|
long hwid_cpu;
|
||||||
|
long hwid_bios;
|
||||||
|
}
|
||||||
|
public class checkRequest
|
||||||
|
{
|
||||||
|
public checkRequest(String username, long hwid_hdd, long hwid_cpu, long hwid_bios) {
|
||||||
|
this.username = username;
|
||||||
|
this.hwid_hdd = hwid_hdd;
|
||||||
|
this.hwid_cpu = hwid_cpu;
|
||||||
|
this.hwid_bios = hwid_bios;
|
||||||
|
}
|
||||||
|
|
||||||
|
String username;
|
||||||
|
long hwid_hdd;
|
||||||
|
long hwid_cpu;
|
||||||
|
long hwid_bios;
|
||||||
|
|
||||||
|
}
|
||||||
|
public class Result
|
||||||
|
{
|
||||||
|
String error;
|
||||||
|
}
|
||||||
|
public class BannedResult
|
||||||
|
{
|
||||||
|
boolean isBanned;
|
||||||
|
String error;
|
||||||
|
}
|
||||||
|
public class HWIDResult
|
||||||
|
{
|
||||||
|
long hwid_hdd;
|
||||||
|
long hwid_cpu;
|
||||||
|
long hwid_bios;
|
||||||
|
}
|
||||||
|
public class HWIDRequest
|
||||||
|
{
|
||||||
|
public HWIDRequest(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
String username;
|
||||||
|
}
|
||||||
|
|
||||||
JsonHWIDHandler(BlockConfigEntry block) {
|
JsonHWIDHandler(BlockConfigEntry block) {
|
||||||
super(block);
|
super(block);
|
||||||
|
@ -40,16 +81,6 @@ public final class JsonHWIDHandler extends HWIDHandler {
|
||||||
String configUrlBan = block.getEntryValue("urlBan", StringConfigEntry.class);
|
String configUrlBan = block.getEntryValue("urlBan", StringConfigEntry.class);
|
||||||
String configUrlUnBan = block.getEntryValue("urlUnBan", StringConfigEntry.class);
|
String configUrlUnBan = block.getEntryValue("urlUnBan", StringConfigEntry.class);
|
||||||
String configUrlGet = block.getEntryValue("urlGet", StringConfigEntry.class);
|
String configUrlGet = block.getEntryValue("urlGet", StringConfigEntry.class);
|
||||||
loginKeyName = VerifyHelper.verify(block.getEntryValue("loginKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "Login key name can't be empty");
|
|
||||||
hddKeyName = VerifyHelper.verify(block.getEntryValue("hddKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "HDD key name can't be empty");
|
|
||||||
cpuKeyName = VerifyHelper.verify(block.getEntryValue("cpuKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "CPU key can't be empty");
|
|
||||||
biosKeyName = VerifyHelper.verify(block.getEntryValue("biosKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "Bios key can't be empty");
|
|
||||||
isBannedKeyName = VerifyHelper.verify(block.getEntryValue("isBannedKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "Response username key can't be empty");
|
|
||||||
url = IOHelper.convertToURL(configUrl);
|
url = IOHelper.convertToURL(configUrl);
|
||||||
urlBan = IOHelper.convertToURL(configUrlBan);
|
urlBan = IOHelper.convertToURL(configUrlBan);
|
||||||
urlUnBan = IOHelper.convertToURL(configUrlUnBan);
|
urlUnBan = IOHelper.convertToURL(configUrlUnBan);
|
||||||
|
@ -59,9 +90,11 @@ public final class JsonHWIDHandler extends HWIDHandler {
|
||||||
@Override
|
@Override
|
||||||
public void ban(List<HWID> l_hwid) throws HWIDException {
|
public void ban(List<HWID> l_hwid) throws HWIDException {
|
||||||
for (HWID hwid : l_hwid) {
|
for (HWID hwid : l_hwid) {
|
||||||
JsonObject request = Json.object().add(hddKeyName, hwid.getHwid_hdd()).add(cpuKeyName, hwid.getHwid_cpu()).add(biosKeyName, hwid.getHwid_bios());
|
banRequest request = new banRequest(hwid.getHwid_hdd(),hwid.getHwid_cpu(),hwid.getHwid_bios());
|
||||||
try {
|
try {
|
||||||
request(request, urlBan);
|
JsonElement result = HTTPRequest.jsonRequest(gson.toJsonTree(request), urlBan);
|
||||||
|
Result r = gson.fromJson(result,Result.class);
|
||||||
|
if(r.error != null) throw new HWIDException(r.error);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LogHelper.error(e);
|
LogHelper.error(e);
|
||||||
throw new HWIDException("HWID service error");
|
throw new HWIDException("HWID service error");
|
||||||
|
@ -69,48 +102,19 @@ public void ban(List<HWID> l_hwid) throws HWIDException {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonObject request(JsonObject request, URL url) throws HWIDException, IOException {
|
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
||||||
connection.setDoInput(true);
|
|
||||||
connection.setDoOutput(true);
|
|
||||||
connection.setRequestMethod("POST");
|
|
||||||
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
|
||||||
connection.setRequestProperty("Accept", "application/json");
|
|
||||||
if (TIMEOUT > 0)
|
|
||||||
connection.setConnectTimeout(TIMEOUT);
|
|
||||||
|
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), Charset.forName("UTF-8"));
|
|
||||||
writer.write(request.toString());
|
|
||||||
writer.flush();
|
|
||||||
writer.close();
|
|
||||||
|
|
||||||
InputStreamReader reader;
|
|
||||||
int statusCode = connection.getResponseCode();
|
|
||||||
|
|
||||||
if (200 <= statusCode && statusCode < 300)
|
|
||||||
reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
|
|
||||||
else
|
|
||||||
reader = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8);
|
|
||||||
JsonValue content = Json.parse(reader);
|
|
||||||
if (!content.isObject())
|
|
||||||
throw new HWIDException("HWID server response is malformed");
|
|
||||||
|
|
||||||
JsonObject response = content.asObject();
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void check0(HWID hwid, String username) throws HWIDException {
|
public void check0(HWID hwid, String username) throws HWIDException {
|
||||||
JsonObject request = Json.object().add(loginKeyName, username).add(hddKeyName, hwid.getHwid_hdd()).add(cpuKeyName, hwid.getHwid_cpu()).add(biosKeyName, hwid.getHwid_bios());
|
checkRequest request = new checkRequest(username,hwid.getHwid_hdd(),hwid.getHwid_cpu(),hwid.getHwid_bios());
|
||||||
JsonObject response;
|
|
||||||
try {
|
try {
|
||||||
response = request(request, url);
|
JsonElement result = HTTPRequest.jsonRequest(gson.toJsonTree(request), urlBan);
|
||||||
|
BannedResult r = gson.fromJson(result,BannedResult.class);
|
||||||
|
if(r.error != null) throw new HWIDException(r.error);
|
||||||
|
boolean isBanned = r.isBanned;
|
||||||
|
if (isBanned) throw new HWIDException("You will BANNED!");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LogHelper.error(e);
|
LogHelper.error(e);
|
||||||
throw new HWIDException("HWID service error");
|
throw new HWIDException("HWID service error");
|
||||||
}
|
}
|
||||||
boolean isBanned = response.getBoolean(isBannedKeyName, false);
|
|
||||||
if (isBanned) throw new HWIDException("You will BANNED!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -120,34 +124,30 @@ public void close() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<HWID> getHwid(String username) throws HWIDException {
|
public List<HWID> getHwid(String username) throws HWIDException {
|
||||||
JsonObject request = Json.object().add(loginKeyName, username);
|
ArrayList<HWID> hwids = new ArrayList<>();
|
||||||
JsonObject responce;
|
HWIDRequest request = new HWIDRequest(username);
|
||||||
try {
|
try {
|
||||||
responce = request(request, urlGet);
|
JsonElement result = HTTPRequest.jsonRequest(gson.toJsonTree(request), urlBan);
|
||||||
|
HWIDResult[] r = gson.fromJson(result,HWIDResult[].class);
|
||||||
|
for( HWIDResult hw : r)
|
||||||
|
{
|
||||||
|
hwids.add(HWID.gen(hw.hwid_hdd,hw.hwid_bios,hw.hwid_cpu));
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LogHelper.error(e);
|
LogHelper.error(e);
|
||||||
throw new HWIDException("HWID service error");
|
throw new HWIDException("HWID service error");
|
||||||
}
|
}
|
||||||
JsonArray array = responce.get("hwids").asArray();
|
|
||||||
ArrayList<HWID> hwids = new ArrayList<>();
|
|
||||||
for (JsonValue i : array) {
|
|
||||||
long hdd, cpu, bios;
|
|
||||||
JsonObject object = i.asObject();
|
|
||||||
hdd = object.getLong(hddKeyName, 0);
|
|
||||||
cpu = object.getLong(cpuKeyName, 0);
|
|
||||||
bios = object.getLong(biosKeyName, 0);
|
|
||||||
HWID hwid = HWID.gen(hdd, cpu, bios);
|
|
||||||
hwids.add(hwid);
|
|
||||||
}
|
|
||||||
return hwids;
|
return hwids;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void unban(List<HWID> l_hwid) throws HWIDException {
|
public void unban(List<HWID> l_hwid) throws HWIDException {
|
||||||
for (HWID hwid : l_hwid) {
|
for (HWID hwid : l_hwid) {
|
||||||
JsonObject request = Json.object().add(hddKeyName, hwid.getHwid_hdd()).add(cpuKeyName, hwid.getHwid_cpu()).add(biosKeyName, hwid.getHwid_bios());
|
banRequest request = new banRequest(hwid.getHwid_hdd(),hwid.getHwid_cpu(),hwid.getHwid_bios());
|
||||||
try {
|
try {
|
||||||
request(request, urlUnBan);
|
JsonElement result = HTTPRequest.jsonRequest(gson.toJsonTree(request), urlUnBan);
|
||||||
|
Result r = gson.fromJson(result,Result.class);
|
||||||
|
if(r.error != null) throw new HWIDException(r.error);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LogHelper.error(e);
|
LogHelper.error(e);
|
||||||
throw new HWIDException("HWID service error");
|
throw new HWIDException("HWID service error");
|
||||||
|
|
|
@ -3,60 +3,57 @@
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
import com.eclipsesource.json.Json;
|
import com.google.gson.Gson;
|
||||||
import com.eclipsesource.json.JsonObject;
|
import com.google.gson.JsonElement;
|
||||||
import com.eclipsesource.json.JsonValue;
|
|
||||||
|
|
||||||
import ru.gravit.launchserver.LaunchServer;
|
import ru.gravit.launchserver.LaunchServer;
|
||||||
import ru.gravit.launchserver.auth.ClientPermissions;
|
import ru.gravit.launchserver.auth.ClientPermissions;
|
||||||
import ru.gravit.utils.HTTPRequest;
|
import ru.gravit.utils.HTTPRequest;
|
||||||
import ru.gravit.utils.helper.IOHelper;
|
import ru.gravit.utils.helper.IOHelper;
|
||||||
import ru.gravit.utils.helper.SecurityHelper;
|
import ru.gravit.utils.helper.SecurityHelper;
|
||||||
import ru.gravit.utils.helper.VerifyHelper;
|
|
||||||
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
|
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
|
||||||
import ru.gravit.launcher.serialize.config.entry.StringConfigEntry;
|
import ru.gravit.launcher.serialize.config.entry.StringConfigEntry;
|
||||||
|
|
||||||
public final class JsonAuthProvider extends AuthProvider {
|
public final class JsonAuthProvider extends AuthProvider {
|
||||||
|
private final Gson gson = new Gson();
|
||||||
private final URL url;
|
private final URL url;
|
||||||
private final String userKeyName;
|
public class authResult
|
||||||
private final String passKeyName;
|
{
|
||||||
private final String ipKeyName;
|
String username;
|
||||||
private final String responseUserKeyName;
|
String error;
|
||||||
private final String responsePermissionKeyName;
|
long permissions;
|
||||||
private final String responseErrorKeyName;
|
}
|
||||||
|
public class authRequest
|
||||||
|
{
|
||||||
|
public authRequest(String username, String password, String ip) {
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
this.ip = ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
String username;
|
||||||
|
String password;
|
||||||
|
String ip;
|
||||||
|
}
|
||||||
|
|
||||||
JsonAuthProvider(BlockConfigEntry block, LaunchServer server) {
|
JsonAuthProvider(BlockConfigEntry block, LaunchServer server) {
|
||||||
super(block, server);
|
super(block, server);
|
||||||
String configUrl = block.getEntryValue("url", StringConfigEntry.class);
|
String configUrl = block.getEntryValue("url", StringConfigEntry.class);
|
||||||
userKeyName = VerifyHelper.verify(block.getEntryValue("userKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "Username key name can't be empty");
|
|
||||||
passKeyName = VerifyHelper.verify(block.getEntryValue("passKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "Password key name can't be empty");
|
|
||||||
ipKeyName = VerifyHelper.verify(block.getEntryValue("ipKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "IP key can't be empty");
|
|
||||||
responseUserKeyName = VerifyHelper.verify(block.getEntryValue("responseUserKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "Response username key can't be empty");
|
|
||||||
responseErrorKeyName = VerifyHelper.verify(block.getEntryValue("responseErrorKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "Response error key can't be empty");
|
|
||||||
responsePermissionKeyName = VerifyHelper.verify(block.getEntryValue("responsePermissionKeyName", StringConfigEntry.class),
|
|
||||||
VerifyHelper.NOT_EMPTY, "Response error key can't be empty");
|
|
||||||
url = IOHelper.convertToURL(configUrl);
|
url = IOHelper.convertToURL(configUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AuthProviderResult auth(String login, String password, String ip) throws IOException {
|
public AuthProviderResult auth(String login, String password, String ip) throws IOException {
|
||||||
JsonObject request = Json.object().add(userKeyName, login).add(passKeyName, password).add(ipKeyName, ip);
|
authRequest authRequest = new authRequest(login,password,ip);
|
||||||
JsonValue content = HTTPRequest.jsonRequest(request, url);
|
JsonElement request = gson.toJsonTree(authRequest);
|
||||||
if (!content.isObject())
|
JsonElement content = HTTPRequest.jsonRequest(request, url);
|
||||||
|
if (!content.isJsonObject())
|
||||||
return authError("Authentication server response is malformed");
|
return authError("Authentication server response is malformed");
|
||||||
|
|
||||||
JsonObject response = content.asObject();
|
authResult result = gson.fromJson(content, authResult.class);
|
||||||
String value;
|
if (result.username != null)
|
||||||
|
return new AuthProviderResult(result.username, SecurityHelper.randomStringToken(), new ClientPermissions(result.permissions));
|
||||||
if ((value = response.getString(responseUserKeyName, null)) != null)
|
else if (result.error != null)
|
||||||
return new AuthProviderResult(value, SecurityHelper.randomStringToken(), new ClientPermissions(response.getLong(responsePermissionKeyName, 0)));
|
return authError(result.error);
|
||||||
else if ((value = response.getString(responseErrorKeyName, null)) != null)
|
|
||||||
return authError(value);
|
|
||||||
else
|
else
|
||||||
return authError("Authentication server response is malformed");
|
return authError("Authentication server response is malformed");
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,18 +11,18 @@
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import com.eclipsesource.json.Json;
|
import com.google.gson.Gson;
|
||||||
import com.eclipsesource.json.JsonObject;
|
import com.google.gson.JsonElement;
|
||||||
import com.eclipsesource.json.JsonValue;
|
import com.google.gson.JsonObject;
|
||||||
import com.eclipsesource.json.WriterConfig;
|
|
||||||
|
|
||||||
import ru.gravit.launchserver.LaunchServer;
|
import ru.gravit.launchserver.LaunchServer;
|
||||||
|
import ru.gravit.utils.HTTPRequest;
|
||||||
import ru.gravit.utils.helper.IOHelper;
|
import ru.gravit.utils.helper.IOHelper;
|
||||||
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
|
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
|
||||||
|
|
||||||
public final class MojangAuthProvider extends AuthProvider {
|
public final class MojangAuthProvider extends AuthProvider {
|
||||||
private static final Pattern UUID_REGEX = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})");
|
private static final Pattern UUID_REGEX = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})");
|
||||||
private static final URL URL;
|
private static final URL URL;
|
||||||
|
private final Gson gson = new Gson();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
|
@ -32,52 +32,43 @@ public final class MojangAuthProvider extends AuthProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JsonObject makeJSONRequest(URL url, JsonObject request) throws IOException {
|
|
||||||
// Make authentication request
|
|
||||||
HttpURLConnection connection = IOHelper.newConnectionPost(url);
|
|
||||||
connection.setRequestProperty("Content-Type", "application/json");
|
|
||||||
try (OutputStream output = connection.getOutputStream()) {
|
|
||||||
output.write(request.toString(WriterConfig.MINIMAL).getBytes(StandardCharsets.UTF_8));
|
|
||||||
}
|
|
||||||
connection.getResponseCode(); // Actually make request
|
|
||||||
|
|
||||||
// Read response
|
|
||||||
InputStream errorInput = connection.getErrorStream();
|
|
||||||
try (InputStream input = errorInput == null ? connection.getInputStream() : errorInput) {
|
|
||||||
String charset = connection.getContentEncoding();
|
|
||||||
Charset charsetObject = charset == null ?
|
|
||||||
IOHelper.UNICODE_CHARSET : Charset.forName(charset);
|
|
||||||
|
|
||||||
// Parse response
|
|
||||||
String json = new String(IOHelper.read(input), charsetObject);
|
|
||||||
return json.isEmpty() ? null : Json.parse(json).asObject();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public MojangAuthProvider(BlockConfigEntry block, LaunchServer server) {
|
public MojangAuthProvider(BlockConfigEntry block, LaunchServer server) {
|
||||||
super(block, server);
|
super(block, server);
|
||||||
}
|
}
|
||||||
|
public class mojangAuth
|
||||||
|
{
|
||||||
|
public mojangAuth(String username, String password) {
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
name = "Minecraft";
|
||||||
|
version = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
String name;
|
||||||
|
int version;
|
||||||
|
String username;
|
||||||
|
String password;
|
||||||
|
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public AuthProviderResult auth(String login, String password, String ip) throws Exception {
|
public AuthProviderResult auth(String login, String password, String ip) throws Exception {
|
||||||
JsonObject request = Json.object().
|
mojangAuth mojangAuth = new mojangAuth(login,password);
|
||||||
add("agent", Json.object().add("name", "Minecraft").add("version", 1)).
|
JsonElement request = gson.toJsonTree(mojangAuth);
|
||||||
add("username", login).add("password", password);
|
|
||||||
|
|
||||||
// Verify there's no error
|
// Verify there's no error
|
||||||
JsonObject response = makeJSONRequest(URL, request);
|
JsonObject response = HTTPRequest.jsonRequest(request,URL).getAsJsonObject();
|
||||||
if (response == null)
|
if (response == null)
|
||||||
authError("Empty com.mojang response");
|
authError("Empty com.mojang response");
|
||||||
JsonValue errorMessage = response.get("errorMessage");
|
JsonElement errorMessage = response.get("errorMessage");
|
||||||
if (errorMessage != null)
|
if (errorMessage != null)
|
||||||
authError(errorMessage.asString());
|
authError(errorMessage.getAsString());
|
||||||
|
|
||||||
// Parse JSON data
|
// Parse JSON data
|
||||||
JsonObject selectedProfile = response.get("selectedProfile").asObject();
|
JsonObject selectedProfile = response.get("selectedProfile").getAsJsonObject();
|
||||||
String username = selectedProfile.get("name").asString();
|
String username = selectedProfile.get("name").getAsString();
|
||||||
String accessToken = response.get("clientToken").asString();
|
String accessToken = response.get("clientToken").getAsString();
|
||||||
UUID uuid = UUID.fromString(UUID_REGEX.matcher(selectedProfile.get("id").asString()).replaceFirst("$1-$2-$3-$4-$5"));
|
UUID uuid = UUID.fromString(UUID_REGEX.matcher(selectedProfile.get("id").getAsString()).replaceFirst("$1-$2-$3-$4-$5"));
|
||||||
String launcherToken = response.get("accessToken").asString();
|
String launcherToken = response.get("accessToken").getAsString();
|
||||||
|
|
||||||
// We're done
|
// We're done
|
||||||
return new MojangAuthProviderResult(username, accessToken, uuid, launcherToken);
|
return new MojangAuthProviderResult(username, accessToken, uuid, launcherToken);
|
||||||
|
|
|
@ -9,10 +9,9 @@
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
import com.eclipsesource.json.Json;
|
import com.google.gson.Gson;
|
||||||
import com.eclipsesource.json.JsonObject;
|
import com.google.gson.JsonArray;
|
||||||
import com.eclipsesource.json.WriterConfig;
|
import com.google.gson.JsonParser;
|
||||||
|
|
||||||
import ru.gravit.utils.helper.IOHelper;
|
import ru.gravit.utils.helper.IOHelper;
|
||||||
import ru.gravit.utils.helper.LogHelper;
|
import ru.gravit.utils.helper.LogHelper;
|
||||||
import ru.gravit.utils.helper.SecurityHelper;
|
import ru.gravit.utils.helper.SecurityHelper;
|
||||||
|
@ -22,12 +21,25 @@
|
||||||
import ru.gravit.launchserver.command.CommandException;
|
import ru.gravit.launchserver.command.CommandException;
|
||||||
|
|
||||||
public final class IndexAssetCommand extends Command {
|
public final class IndexAssetCommand extends Command {
|
||||||
|
private static Gson gson = new Gson();
|
||||||
|
private static JsonParser parser = new JsonParser();
|
||||||
|
public static class IndexObject
|
||||||
|
{
|
||||||
|
long size;
|
||||||
|
|
||||||
|
public IndexObject(long size, String hash) {
|
||||||
|
this.size = size;
|
||||||
|
this.hash = hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
String hash;
|
||||||
|
}
|
||||||
private static final class IndexAssetVisitor extends SimpleFileVisitor<Path> {
|
private static final class IndexAssetVisitor extends SimpleFileVisitor<Path> {
|
||||||
private final JsonObject objects;
|
private final JsonArray objects;
|
||||||
private final Path inputAssetDir;
|
private final Path inputAssetDir;
|
||||||
private final Path outputAssetDir;
|
private final Path outputAssetDir;
|
||||||
|
|
||||||
private IndexAssetVisitor(JsonObject objects, Path inputAssetDir, Path outputAssetDir) {
|
private IndexAssetVisitor(JsonArray objects, Path inputAssetDir, Path outputAssetDir) {
|
||||||
this.objects = objects;
|
this.objects = objects;
|
||||||
this.inputAssetDir = inputAssetDir;
|
this.inputAssetDir = inputAssetDir;
|
||||||
this.outputAssetDir = outputAssetDir;
|
this.outputAssetDir = outputAssetDir;
|
||||||
|
@ -40,7 +52,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
|
||||||
|
|
||||||
// Add to index and copy file
|
// Add to index and copy file
|
||||||
String digest = SecurityHelper.toHex(SecurityHelper.digest(DigestAlgorithm.SHA1, file));
|
String digest = SecurityHelper.toHex(SecurityHelper.digest(DigestAlgorithm.SHA1, file));
|
||||||
objects.add(name, Json.object().add("size", attrs.size()).add("hash", digest));
|
IndexObject obj = new IndexObject(attrs.size(),digest);
|
||||||
|
objects.add(gson.toJsonTree(obj));
|
||||||
IOHelper.copy(file, resolveObjectFile(outputAssetDir, digest));
|
IOHelper.copy(file, resolveObjectFile(outputAssetDir, digest));
|
||||||
|
|
||||||
// Continue visiting
|
// Continue visiting
|
||||||
|
@ -93,14 +106,15 @@ public void invoke(String... args) throws Exception {
|
||||||
Files.createDirectory(outputAssetDir);
|
Files.createDirectory(outputAssetDir);
|
||||||
|
|
||||||
// Index objects
|
// Index objects
|
||||||
JsonObject objects = Json.object();
|
JsonArray objects = new JsonArray();
|
||||||
LogHelper.subInfo("Indexing objects");
|
LogHelper.subInfo("Indexing objects");
|
||||||
IOHelper.walk(inputAssetDir, new IndexAssetVisitor(objects, inputAssetDir, outputAssetDir), false);
|
IOHelper.walk(inputAssetDir, new IndexAssetVisitor(objects, inputAssetDir, outputAssetDir), false);
|
||||||
|
|
||||||
// Write index file
|
// Write index file
|
||||||
LogHelper.subInfo("Writing asset index file: '%s'", indexFileName);
|
LogHelper.subInfo("Writing asset index file: '%s'", indexFileName);
|
||||||
|
|
||||||
try (BufferedWriter writer = IOHelper.newWriter(resolveIndexFile(outputAssetDir, indexFileName))) {
|
try (BufferedWriter writer = IOHelper.newWriter(resolveIndexFile(outputAssetDir, indexFileName))) {
|
||||||
Json.object().add(OBJECTS_DIR, objects).writeTo(writer, WriterConfig.MINIMAL);
|
writer.write(gson.toJson(objects));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finished
|
// Finished
|
||||||
|
|
|
@ -4,11 +4,9 @@
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import com.eclipsesource.json.Json;
|
import com.google.gson.*;
|
||||||
import com.eclipsesource.json.JsonObject;
|
|
||||||
import com.eclipsesource.json.JsonObject.Member;
|
|
||||||
|
|
||||||
import ru.gravit.utils.helper.IOHelper;
|
import ru.gravit.utils.helper.IOHelper;
|
||||||
import ru.gravit.utils.helper.LogHelper;
|
import ru.gravit.utils.helper.LogHelper;
|
||||||
import ru.gravit.launchserver.LaunchServer;
|
import ru.gravit.launchserver.LaunchServer;
|
||||||
|
@ -16,6 +14,8 @@
|
||||||
import ru.gravit.launchserver.command.CommandException;
|
import ru.gravit.launchserver.command.CommandException;
|
||||||
|
|
||||||
public final class UnindexAssetCommand extends Command {
|
public final class UnindexAssetCommand extends Command {
|
||||||
|
private static Gson gson = new Gson();
|
||||||
|
private static JsonParser parser = new JsonParser();
|
||||||
public UnindexAssetCommand(LaunchServer server) {
|
public UnindexAssetCommand(LaunchServer server) {
|
||||||
super(server);
|
super(server);
|
||||||
}
|
}
|
||||||
|
@ -49,17 +49,17 @@ public void invoke(String... args) throws Exception {
|
||||||
JsonObject objects;
|
JsonObject objects;
|
||||||
LogHelper.subInfo("Reading asset index file: '%s'", indexFileName);
|
LogHelper.subInfo("Reading asset index file: '%s'", indexFileName);
|
||||||
try (BufferedReader reader = IOHelper.newReader(IndexAssetCommand.resolveIndexFile(inputAssetDir, indexFileName))) {
|
try (BufferedReader reader = IOHelper.newReader(IndexAssetCommand.resolveIndexFile(inputAssetDir, indexFileName))) {
|
||||||
objects = Json.parse(reader).asObject().get(IndexAssetCommand.OBJECTS_DIR).asObject();
|
objects = parser.parse(reader).getAsJsonObject().get("objects").getAsJsonObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore objects
|
// Restore objects
|
||||||
LogHelper.subInfo("Unindexing %d objects", objects.size());
|
LogHelper.subInfo("Unindexing %d objects", objects.size());
|
||||||
for (Member member : objects) {
|
for (Map.Entry<String, JsonElement> member : objects.entrySet()) {
|
||||||
String name = member.getName();
|
String name = member.getKey();
|
||||||
LogHelper.subInfo("Unindexing: '%s'", name);
|
LogHelper.subInfo("Unindexing: '%s'", name);
|
||||||
|
|
||||||
// Copy hashed file to target
|
// Copy hashed file to target
|
||||||
String hash = member.getValue().asObject().get("hash").asString();
|
String hash = member.getValue().getAsJsonObject().get("hash").getAsString();
|
||||||
Path source = IndexAssetCommand.resolveObjectFile(inputAssetDir, hash);
|
Path source = IndexAssetCommand.resolveObjectFile(inputAssetDir, hash);
|
||||||
IOHelper.copy(source, outputAssetDir.resolve(name));
|
IOHelper.copy(source, outputAssetDir.resolve(name));
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,10 +17,8 @@
|
||||||
|
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
|
|
||||||
import com.eclipsesource.json.Json;
|
import com.google.gson.Gson;
|
||||||
import com.eclipsesource.json.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.eclipsesource.json.WriterConfig;
|
|
||||||
|
|
||||||
import ru.gravit.launcher.*;
|
import ru.gravit.launcher.*;
|
||||||
import ru.gravit.launcher.hasher.DirWatcher;
|
import ru.gravit.launcher.hasher.DirWatcher;
|
||||||
import ru.gravit.launcher.hasher.FileNameMatcher;
|
import ru.gravit.launcher.hasher.FileNameMatcher;
|
||||||
|
@ -42,6 +40,7 @@
|
||||||
import ru.gravit.launcher.serialize.stream.StreamObject;
|
import ru.gravit.launcher.serialize.stream.StreamObject;
|
||||||
|
|
||||||
public final class ClientLauncher {
|
public final class ClientLauncher {
|
||||||
|
private static Gson gson = new Gson();
|
||||||
private static final class ClassPathFileVisitor extends SimpleFileVisitor<Path> {
|
private static final class ClassPathFileVisitor extends SimpleFileVisitor<Path> {
|
||||||
private final Collection<Path> result;
|
private final Collection<Path> result;
|
||||||
|
|
||||||
|
@ -170,7 +169,6 @@ public void write(HOutput output) throws IOException {
|
||||||
private static final Path NATIVES_DIR = IOHelper.toPath("natives");
|
private static final Path NATIVES_DIR = IOHelper.toPath("natives");
|
||||||
private static final Path RESOURCEPACKS_DIR = IOHelper.toPath("resourcepacks");
|
private static final Path RESOURCEPACKS_DIR = IOHelper.toPath("resourcepacks");
|
||||||
private static PublicURLClassLoader classLoader;
|
private static PublicURLClassLoader classLoader;
|
||||||
|
|
||||||
private static void addClientArgs(Collection<String> args, ClientProfile profile, Params params) {
|
private static void addClientArgs(Collection<String> args, ClientProfile profile, Params params) {
|
||||||
PlayerProfile pp = params.pp;
|
PlayerProfile pp = params.pp;
|
||||||
|
|
||||||
|
@ -185,16 +183,16 @@ private static void addClientArgs(Collection<String> args, ClientProfile profile
|
||||||
if (version.compareTo(ClientProfile.Version.MC1710) >= 0) {
|
if (version.compareTo(ClientProfile.Version.MC1710) >= 0) {
|
||||||
// Add user properties
|
// Add user properties
|
||||||
Collections.addAll(args, "--userType", "mojang");
|
Collections.addAll(args, "--userType", "mojang");
|
||||||
JsonObject properties = Json.object();
|
JsonObject properties = new JsonObject();
|
||||||
if (pp.skin != null) {
|
if (pp.skin != null) {
|
||||||
properties.add(Launcher.SKIN_URL_PROPERTY, Json.array(pp.skin.url));
|
properties.addProperty(Launcher.SKIN_URL_PROPERTY, pp.skin.url);
|
||||||
properties.add(Launcher.SKIN_DIGEST_PROPERTY, Json.array(SecurityHelper.toHex(pp.skin.digest)));
|
properties.addProperty(Launcher.SKIN_DIGEST_PROPERTY, SecurityHelper.toHex(pp.skin.digest));
|
||||||
}
|
}
|
||||||
if (pp.cloak != null) {
|
if (pp.cloak != null) {
|
||||||
properties.add(Launcher.CLOAK_URL_PROPERTY, Json.array(pp.cloak.url));
|
properties.addProperty(Launcher.CLOAK_URL_PROPERTY, pp.cloak.url);
|
||||||
properties.add(Launcher.CLOAK_DIGEST_PROPERTY, Json.array(SecurityHelper.toHex(pp.cloak.digest)));
|
properties.addProperty(Launcher.CLOAK_DIGEST_PROPERTY, SecurityHelper.toHex(pp.cloak.digest));
|
||||||
}
|
}
|
||||||
Collections.addAll(args, "--userProperties", properties.toString(WriterConfig.MINIMAL));
|
Collections.addAll(args, "--userProperties", ClientLauncher.gson.toJson(properties));
|
||||||
|
|
||||||
// Add asset index
|
// Add asset index
|
||||||
Collections.addAll(args, "--assetIndex", profile.getAssetIndex());
|
Collections.addAll(args, "--assetIndex", profile.getAssetIndex());
|
||||||
|
|
|
@ -10,9 +10,8 @@
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import com.eclipsesource.json.Json;
|
import com.google.gson.JsonObject;
|
||||||
import com.eclipsesource.json.JsonObject;
|
import com.google.gson.JsonParser;
|
||||||
|
|
||||||
import ru.gravit.launcher.LauncherAPI;
|
import ru.gravit.launcher.LauncherAPI;
|
||||||
import ru.gravit.utils.helper.IOHelper;
|
import ru.gravit.utils.helper.IOHelper;
|
||||||
import ru.gravit.utils.helper.LogHelper;
|
import ru.gravit.utils.helper.LogHelper;
|
||||||
|
@ -22,6 +21,7 @@
|
||||||
import ru.gravit.launcher.profiles.ClientProfile;
|
import ru.gravit.launcher.profiles.ClientProfile;
|
||||||
|
|
||||||
public final class ServerPinger {
|
public final class ServerPinger {
|
||||||
|
private JsonParser parser = new JsonParser();
|
||||||
public static final class Result {
|
public static final class Result {
|
||||||
@LauncherAPI
|
@LauncherAPI
|
||||||
public final int onlinePlayers;
|
public final int onlinePlayers;
|
||||||
|
@ -184,10 +184,10 @@ private Result modernPing(HInput input, HOutput output) throws IOException {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse JSON response
|
// Parse JSON response
|
||||||
JsonObject object = Json.parse(response).asObject();
|
JsonObject object = parser.parse(response).getAsJsonObject();
|
||||||
JsonObject playersObject = object.get("players").asObject();
|
JsonObject playersObject = object.get("players").getAsJsonObject();
|
||||||
int online = playersObject.get("online").asInt();
|
int online = playersObject.get("online").getAsInt();
|
||||||
int max = playersObject.get("max").asInt();
|
int max = playersObject.get("max").getAsInt();
|
||||||
|
|
||||||
// Return ping status
|
// Return ping status
|
||||||
return new Result(online, max, response);
|
return new Result(online, max, response);
|
||||||
|
|
|
@ -4,6 +4,5 @@
|
||||||
dependencies {
|
dependencies {
|
||||||
compileOnly 'org.fusesource.jansi:jansi:1.17.1'
|
compileOnly 'org.fusesource.jansi:jansi:1.17.1'
|
||||||
compileOnly 'org.javassist:javassist:3.23.1-GA'
|
compileOnly 'org.javassist:javassist:3.23.1-GA'
|
||||||
compile 'com.eclipsesource.minimal-json:minimal-json:0.9.4'
|
|
||||||
compile 'com.google.code.gson:gson:2.8.5'
|
compile 'com.google.code.gson:gson:2.8.5'
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,13 +9,13 @@
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import com.eclipsesource.json.Json;
|
import com.google.gson.JsonElement;
|
||||||
import com.eclipsesource.json.JsonObject;
|
import com.google.gson.JsonParser;
|
||||||
import com.eclipsesource.json.JsonValue;
|
|
||||||
import ru.gravit.utils.helper.IOHelper;
|
import ru.gravit.utils.helper.IOHelper;
|
||||||
|
|
||||||
public class HTTPRequest {
|
public class HTTPRequest {
|
||||||
private static final int TIMEOUT = 10;
|
private static final int TIMEOUT = 10;
|
||||||
|
private static final JsonParser parser = new JsonParser();
|
||||||
|
|
||||||
public static int sendCrashreport(String strurl, byte[] data) throws IOException {
|
public static int sendCrashreport(String strurl, byte[] data) throws IOException {
|
||||||
URL url = new URL(strurl);
|
URL url = new URL(strurl);
|
||||||
|
@ -36,7 +36,7 @@ public static int sendCrashreport(String strurl, String data) throws IOException
|
||||||
return sendCrashreport(strurl, data.getBytes(IOHelper.UNICODE_CHARSET));
|
return sendCrashreport(strurl, data.getBytes(IOHelper.UNICODE_CHARSET));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JsonValue jsonRequest(JsonObject request, URL url) throws IOException {
|
public static JsonElement jsonRequest(JsonElement request, URL url) throws IOException {
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
connection.setDoInput(true);
|
connection.setDoInput(true);
|
||||||
connection.setDoOutput(true);
|
connection.setDoOutput(true);
|
||||||
|
@ -58,7 +58,7 @@ public static JsonValue jsonRequest(JsonObject request, URL url) throws IOExcept
|
||||||
reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
|
reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
|
||||||
else
|
else
|
||||||
reader = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8);
|
reader = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8);
|
||||||
JsonValue content = Json.parse(reader);
|
JsonElement content = parser.parse(reader);
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue