ДА БЛЯ Я ЗАБЕАЛСЯ ФИКСИТЬ ПОМОГИТЕ Я КОНЧЕННЫЫЫЙ
This commit is contained in:
@@ -10,25 +10,16 @@ import me.sashegdev.zernmc.launcher.utils.ZAnsi;
|
|||||||
import me.sashegdev.zernmc.launcher.utils.ZHttpClient;
|
import me.sashegdev.zernmc.launcher.utils.ZHttpClient;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
|
||||||
import java.net.http.HttpClient;
|
|
||||||
import java.net.http.HttpRequest;
|
|
||||||
import java.net.http.HttpResponse;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.time.Duration;
|
|
||||||
|
|
||||||
public class AuthManager {
|
public class AuthManager {
|
||||||
|
|
||||||
private static final Path AUTH_FILE = Config.getConfigDir().resolve("auth.json");
|
private static final Path AUTH_FILE = Config.getConfigDir().resolve("auth.json");
|
||||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||||
private static final HttpClient HTTP = HttpClient.newBuilder()
|
|
||||||
.connectTimeout(Duration.ofSeconds(15))
|
|
||||||
.version(HttpClient.Version.HTTP_1_1)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
private static volatile AuthSession session = null; // ← volatile для потокобезопасности
|
private static volatile AuthSession session = null;
|
||||||
|
|
||||||
public static boolean loadSavedSession() {
|
public static boolean loadSavedSession() {
|
||||||
if (!Files.exists(AUTH_FILE)) return false;
|
if (!Files.exists(AUTH_FILE)) return false;
|
||||||
@@ -59,10 +50,9 @@ public class AuthManager {
|
|||||||
try {
|
try {
|
||||||
String body = GSON.toJson(new LoginRequest(username, password));
|
String body = GSON.toJson(new LoginRequest(username, password));
|
||||||
|
|
||||||
// Логируем для отладки
|
|
||||||
System.out.println(ZAnsi.cyan("[AUTH] Отправка запроса: " + endpoint));
|
System.out.println(ZAnsi.cyan("[AUTH] Отправка запроса: " + endpoint));
|
||||||
|
|
||||||
HttpResponse<String> resp = post(endpoint, body);
|
SimpleHttpResponse resp = post(endpoint, body);
|
||||||
|
|
||||||
System.out.println(ZAnsi.cyan("[AUTH] Ответ: HTTP " + resp.statusCode()));
|
System.out.println(ZAnsi.cyan("[AUTH] Ответ: HTTP " + resp.statusCode()));
|
||||||
|
|
||||||
@@ -72,7 +62,6 @@ public class AuthManager {
|
|||||||
saveSession();
|
saveSession();
|
||||||
return AuthResult.ok();
|
return AuthResult.ok();
|
||||||
} else if (resp.statusCode() == 422) {
|
} else if (resp.statusCode() == 422) {
|
||||||
// Ошибка валидации — парсим детали
|
|
||||||
return AuthResult.fail("Ошибка валидации: " + extractError(resp.body()));
|
return AuthResult.fail("Ошибка валидации: " + extractError(resp.body()));
|
||||||
} else {
|
} else {
|
||||||
return AuthResult.fail(extractError(resp.body()));
|
return AuthResult.fail(extractError(resp.body()));
|
||||||
@@ -109,7 +98,6 @@ public class AuthManager {
|
|||||||
public static String getAccessToken() {
|
public static String getAccessToken() {
|
||||||
if (session == null) return "0";
|
if (session == null) return "0";
|
||||||
if (isAccessTokenExpired()) {
|
if (isAccessTokenExpired()) {
|
||||||
// ВАЖНО: в UI-контексте это плохо, но пока оставим
|
|
||||||
tryRefresh();
|
tryRefresh();
|
||||||
}
|
}
|
||||||
return session != null && session.accessToken != null ? session.accessToken : "0";
|
return session != null && session.accessToken != null ? session.accessToken : "0";
|
||||||
@@ -124,7 +112,7 @@ public class AuthManager {
|
|||||||
if (session == null || session.refreshToken == null) return false;
|
if (session == null || session.refreshToken == null) return false;
|
||||||
try {
|
try {
|
||||||
String body = "{\"refresh_token\":\"" + session.refreshToken + "\"}";
|
String body = "{\"refresh_token\":\"" + session.refreshToken + "\"}";
|
||||||
HttpResponse<String> resp = post("/auth/refresh", body);
|
SimpleHttpResponse resp = post("/auth/refresh", body);
|
||||||
|
|
||||||
if (resp.statusCode() == 200) {
|
if (resp.statusCode() == 200) {
|
||||||
AuthSession newSession = GSON.fromJson(resp.body(), AuthSession.class);
|
AuthSession newSession = GSON.fromJson(resp.body(), AuthSession.class);
|
||||||
@@ -148,42 +136,60 @@ public class AuthManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static HttpResponse<String> post(String endpoint, String jsonBody) throws Exception {
|
private static SimpleHttpResponse post(String endpoint, String jsonBody) throws Exception {
|
||||||
String fullUrl = ZHttpClient.getBaseUrl() + endpoint;
|
String fullUrl = ZHttpClient.getBaseUrl() + endpoint;
|
||||||
|
|
||||||
HttpRequest req = HttpRequest.newBuilder()
|
java.net.HttpURLConnection conn = null;
|
||||||
.uri(URI.create(fullUrl))
|
try {
|
||||||
.header("Content-Type", "application/json; charset=utf-8")
|
java.net.URL url = java.net.URI.create(fullUrl).toURL();
|
||||||
.header("Accept", "application/json")
|
conn = (java.net.HttpURLConnection) url.openConnection();
|
||||||
.header("User-Agent", "ZernMC-Launcher/1.0")
|
conn.setRequestMethod("POST");
|
||||||
// .header("Connection", "close")
|
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
|
||||||
.timeout(Duration.ofSeconds(15))
|
conn.setRequestProperty("Accept", "application/json");
|
||||||
.POST(HttpRequest.BodyPublishers.ofString(jsonBody, StandardCharsets.UTF_8))
|
conn.setRequestProperty("User-Agent", "ZernMC-Launcher/1.0");
|
||||||
.build();
|
conn.setDoOutput(true);
|
||||||
|
conn.setConnectTimeout(15000);
|
||||||
|
conn.setReadTimeout(15000);
|
||||||
|
|
||||||
return HTTP.send(req, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
try (java.io.OutputStream os = conn.getOutputStream()) {
|
||||||
|
byte[] input = jsonBody.getBytes(StandardCharsets.UTF_8);
|
||||||
|
os.write(input, 0, input.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
int statusCode = conn.getResponseCode();
|
||||||
|
|
||||||
|
java.io.InputStream is = (statusCode >= 200 && statusCode < 300)
|
||||||
|
? conn.getInputStream()
|
||||||
|
: conn.getErrorStream();
|
||||||
|
|
||||||
|
String responseBody;
|
||||||
|
try (java.util.Scanner scanner = new java.util.Scanner(is, StandardCharsets.UTF_8.name())) {
|
||||||
|
responseBody = scanner.useDelimiter("\\A").hasNext() ? scanner.next() : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return new SimpleHttpResponse(statusCode, responseBody);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
if (conn != null) conn.disconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String extractError(String body) {
|
private static String extractError(String body) {
|
||||||
try {
|
try {
|
||||||
JsonObject json = JsonParser.parseString(body).getAsJsonObject();
|
JsonObject json = JsonParser.parseString(body).getAsJsonObject();
|
||||||
|
|
||||||
// FastAPI формат
|
|
||||||
if (json.has("detail")) {
|
if (json.has("detail")) {
|
||||||
// Может быть строкой или массивом
|
|
||||||
if (json.get("detail").isJsonArray()) {
|
if (json.get("detail").isJsonArray()) {
|
||||||
return json.getAsJsonArray("detail").get(0).getAsJsonObject()
|
return json.getAsJsonArray("detail").get(0).getAsJsonObject()
|
||||||
.get("msg").getAsString();
|
.get("msg").getAsString();
|
||||||
}
|
}
|
||||||
return json.get("detail").getAsString();
|
return json.get("detail").getAsString();
|
||||||
}
|
}
|
||||||
// Наш формат
|
|
||||||
if (json.has("error")) {
|
if (json.has("error")) {
|
||||||
return json.get("error").getAsString();
|
return json.get("error").getAsString();
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {}
|
} catch (Exception ignored) {}
|
||||||
|
|
||||||
// Если не смогли распарсить — возвращаем первые 200 символов
|
|
||||||
return body.length() > 200 ? body.substring(0, 200) + "..." : body;
|
return body.length() > 200 ? body.substring(0, 200) + "..." : body;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,7 +207,7 @@ public class AuthManager {
|
|||||||
public static String activatePass(String passCode) {
|
public static String activatePass(String passCode) {
|
||||||
try {
|
try {
|
||||||
String json = "{\"pass_code\":\"" + passCode.toUpperCase() + "\"}";
|
String json = "{\"pass_code\":\"" + passCode.toUpperCase() + "\"}";
|
||||||
HttpResponse<String> resp = post("/auth/pass/activate", json);
|
SimpleHttpResponse resp = post("/auth/pass/activate", json);
|
||||||
|
|
||||||
if (resp.statusCode() == 200) {
|
if (resp.statusCode() == 200) {
|
||||||
return "Проходка успешно активирована!";
|
return "Проходка успешно активирована!";
|
||||||
@@ -239,3 +245,17 @@ public class AuthManager {
|
|||||||
public static AuthResult fail(String msg) { return new AuthResult(false, msg); }
|
public static AuthResult fail(String msg) { return new AuthResult(false, msg); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ====================== ВСПОМОГАТЕЛЬНЫЙ КЛАСС ======================
|
||||||
|
class SimpleHttpResponse {
|
||||||
|
final int statusCode;
|
||||||
|
final String body;
|
||||||
|
|
||||||
|
SimpleHttpResponse(int statusCode, String body) {
|
||||||
|
this.statusCode = statusCode;
|
||||||
|
this.body = body;
|
||||||
|
}
|
||||||
|
|
||||||
|
int statusCode() { return statusCode; }
|
||||||
|
String body() { return body; }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user