Added account refresh cache of 10 seconds

This commit is contained in:
RaphiMC 2023-06-19 18:28:28 +02:00
parent 68d4a4b72d
commit cdcdfaa60e
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
6 changed files with 39 additions and 17 deletions

View File

@ -24,7 +24,6 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.raphimc.mcauth.step.bedrock.StepMCChain;
import net.raphimc.mcauth.step.java.StepPlayerCertificates;
import net.raphimc.mcauth.util.MicrosoftConstants;
import net.raphimc.netminecraft.packet.PacketTypes;
import net.raphimc.netminecraft.packet.impl.login.C2SLoginHelloPacket1_19_3;
import net.raphimc.netminecraft.packet.impl.login.C2SLoginKeyPacket1_19;
@ -41,7 +40,6 @@ import net.raphimc.viaproxy.proxy.session.ProxyConnection;
import net.raphimc.viaproxy.saves.impl.accounts.BedrockAccount;
import net.raphimc.viaproxy.saves.impl.accounts.MicrosoftAccount;
import net.raphimc.viaproxy.util.logging.Logger;
import org.apache.http.impl.client.CloseableHttpClient;
import java.security.PrivateKey;
import java.security.PublicKey;
@ -59,12 +57,7 @@ public class ExternalInterface {
Logger.u_info("auth", proxyConnection.getC2P().remoteAddress(), proxyConnection.getGameProfile(), "Filling player data");
try {
if (Options.MC_ACCOUNT != null) {
synchronized (ViaProxy.saveManager.accountsSave) {
try (final CloseableHttpClient httpClient = MicrosoftConstants.createHttpClient()) {
Options.MC_ACCOUNT.refresh(httpClient);
}
ViaProxy.saveManager.save();
}
ViaProxy.saveManager.accountsSave.ensureRefreshed(Options.MC_ACCOUNT);
proxyConnection.setGameProfile(Options.MC_ACCOUNT.getGameProfile());
final UserConnection user = proxyConnection.getUserConnection();
@ -127,8 +120,7 @@ public class ExternalInterface {
try {
AuthLibServices.SESSION_SERVICE.joinServer(Options.MC_ACCOUNT.getGameProfile(), microsoftAccount.getMcProfile().prevResult().prevResult().access_token(), serverIdHash);
} catch (Throwable e) {
e.printStackTrace();
proxyConnection.kickClient("§cFailed to authenticate with Mojang servers! Please try again later.");
proxyConnection.kickClient("§cFailed to authenticate with Mojang servers! Please try again in a couple of seconds.");
}
} else {
proxyConnection.kickClient("§cThis server is in online mode and requires a valid authentication mode.");

View File

@ -20,9 +20,12 @@ package net.raphimc.viaproxy.saves.impl;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.raphimc.mcauth.util.MicrosoftConstants;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.saves.AbstractSave;
import net.raphimc.viaproxy.saves.impl.accounts.Account;
import net.raphimc.viaproxy.saves.impl.accounts.OfflineAccount;
import org.apache.http.impl.client.CloseableHttpClient;
import java.util.ArrayList;
import java.util.Collections;
@ -49,7 +52,7 @@ public class NewAccountsSave extends AbstractSave {
}
@Override
public JsonElement save() throws Throwable {
public JsonElement save() {
final JsonArray array = new JsonArray();
for (Account account : this.accounts) {
final JsonObject jsonObject = account.toJson();
@ -79,6 +82,16 @@ public class NewAccountsSave extends AbstractSave {
this.accounts.remove(account);
}
public void ensureRefreshed(final Account account) throws Throwable {
synchronized (this) {
try (final CloseableHttpClient httpClient = MicrosoftConstants.createHttpClient()) {
if (account.refresh(httpClient)) {
ViaProxy.saveManager.save();
}
}
}
}
public List<Account> getAccounts() {
return Collections.unmodifiableList(this.accounts);
}

View File

@ -25,10 +25,12 @@ import java.util.UUID;
public abstract class Account {
private long lastRefresh = 0L;
public Account() {
}
public abstract JsonObject toJson() throws Throwable;
public abstract JsonObject toJson();
public abstract String getName();
@ -40,6 +42,12 @@ public abstract class Account {
public abstract String getDisplayString();
public abstract void refresh(final CloseableHttpClient httpClient) throws Throwable;
public boolean refresh(final CloseableHttpClient httpClient) throws Exception {
if (System.currentTimeMillis() - this.lastRefresh < 10_000L) {
return false;
}
this.lastRefresh = System.currentTimeMillis();
return true;
}
}

View File

@ -31,7 +31,7 @@ public class BedrockAccount extends Account {
private StepMCChain.MCChain mcChain;
private StepPlayFabToken.PlayFabToken playFabToken;
public BedrockAccount(final JsonObject jsonObject) throws Throwable {
public BedrockAccount(final JsonObject jsonObject) throws Exception {
this.mcChain = MinecraftAuth.BEDROCK_DEVICE_CODE_LOGIN.fromJson(jsonObject.getAsJsonObject("mc_chain"));
if (jsonObject.has("play_fab_token")) {
try {
@ -80,7 +80,9 @@ public class BedrockAccount extends Account {
}
@Override
public void refresh(CloseableHttpClient httpClient) throws Exception {
public boolean refresh(CloseableHttpClient httpClient) throws Exception {
if (!super.refresh(httpClient)) return false;
this.mcChain = MinecraftAuth.BEDROCK_DEVICE_CODE_LOGIN.refresh(httpClient, this.mcChain);
try {
@ -92,6 +94,8 @@ public class BedrockAccount extends Account {
this.playFabToken = null;
this.playFabToken = MinecraftAuth.BEDROCK_PLAY_FAB_TOKEN.getFromInput(httpClient, this.mcChain.prevResult().fullXblSession());
}
return true;
}
}

View File

@ -80,7 +80,9 @@ public class MicrosoftAccount extends Account {
}
@Override
public void refresh(CloseableHttpClient httpClient) throws Exception {
public boolean refresh(CloseableHttpClient httpClient) throws Exception {
if (!super.refresh(httpClient)) return false;
this.mcProfile = MinecraftAuth.JAVA_DEVICE_CODE_LOGIN.refresh(httpClient, this.mcProfile);
try {
@ -92,6 +94,8 @@ public class MicrosoftAccount extends Account {
this.playerCertificates = null;
this.playerCertificates = MinecraftAuth.JAVA_PLAYER_CERTIFICATES.getFromInput(httpClient, this.mcProfile.prevResult().prevResult());
}
return true;
}
}

View File

@ -62,7 +62,8 @@ public class OfflineAccount extends Account {
}
@Override
public void refresh(CloseableHttpClient httpClient) {
public boolean refresh(CloseableHttpClient httpClient) {
return false;
}
}