Made account part of ProxyConnection

This commit is contained in:
RaphiMC 2023-09-27 22:00:59 +02:00
parent 6d1834fc58
commit b27d387418
No known key found for this signature in database
GPG key ID: 0F6BB0657A03AC94
6 changed files with 109 additions and 87 deletions

View file

@ -36,11 +36,9 @@ public class ViaProxyClassicMPPassProvider extends ClassicMPPassProvider {
@Override @Override
public String getMpPass(UserConnection user) { public String getMpPass(UserConnection user) {
final String mppass = ProxyConnection.fromUserConnection(user).getClassicMpPass(); final String mppass = ProxyConnection.fromUserConnection(user).getUserOptions().classicMpPass();
if (mppass != null && !mppass.isEmpty() && !mppass.equals("0")) { if (mppass != null && !mppass.isEmpty() && !mppass.equals("0")) {
return mppass; return mppass;
} else if (Options.CLASSIC_MP_PASS != null) {
return Options.CLASSIC_MP_PASS;
} else if (Options.BETACRAFT_AUTH) { } else if (Options.BETACRAFT_AUTH) {
final HandshakeStorage handshakeStorage = user.get(HandshakeStorage.class); final HandshakeStorage handshakeStorage = user.get(HandshakeStorage.class);
return getBetacraftMpPass(user, user.getProtocolInfo().getUsername(), handshakeStorage.getHostname(), handshakeStorage.getPort()); return getBetacraftMpPass(user, user.getProtocolInfo().getUsername(), handshakeStorage.getHostname(), handshakeStorage.getPort());

View file

@ -42,6 +42,7 @@ import net.raphimc.viaproxy.proxy.proxy2server.Proxy2ServerHandler;
import net.raphimc.viaproxy.proxy.session.BedrockProxyConnection; import net.raphimc.viaproxy.proxy.session.BedrockProxyConnection;
import net.raphimc.viaproxy.proxy.session.DummyProxyConnection; import net.raphimc.viaproxy.proxy.session.DummyProxyConnection;
import net.raphimc.viaproxy.proxy.session.ProxyConnection; import net.raphimc.viaproxy.proxy.session.ProxyConnection;
import net.raphimc.viaproxy.proxy.session.UserOptions;
import net.raphimc.viaproxy.proxy.util.CloseAndReturn; import net.raphimc.viaproxy.proxy.util.CloseAndReturn;
import net.raphimc.viaproxy.proxy.util.ExceptionUtil; import net.raphimc.viaproxy.proxy.util.ExceptionUtil;
import net.raphimc.viaproxy.proxy.util.HAProxyUtil; import net.raphimc.viaproxy.proxy.util.HAProxyUtil;
@ -137,7 +138,7 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler<IPacket> {
String connectIP = Options.CONNECT_ADDRESS; String connectIP = Options.CONNECT_ADDRESS;
int connectPort = Options.CONNECT_PORT; int connectPort = Options.CONNECT_PORT;
VersionEnum serverVersion = Options.PROTOCOL_VERSION; VersionEnum serverVersion = Options.PROTOCOL_VERSION;
String classicMpPass = null; String classicMpPass = Options.CLASSIC_MP_PASS;
if (Options.INTERNAL_SRV_MODE) { if (Options.INTERNAL_SRV_MODE) {
final ArrayHelper arrayHelper = ArrayHelper.instanceOf(handshakeParts[0].split("\7")); final ArrayHelper arrayHelper = ArrayHelper.instanceOf(handshakeParts[0].split("\7"));
@ -211,7 +212,7 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler<IPacket> {
this.proxyConnection.getC2P().attr(ProxyConnection.PROXY_CONNECTION_ATTRIBUTE_KEY).set(this.proxyConnection); this.proxyConnection.getC2P().attr(ProxyConnection.PROXY_CONNECTION_ATTRIBUTE_KEY).set(this.proxyConnection);
this.proxyConnection.setClientVersion(clientVersion); this.proxyConnection.setClientVersion(clientVersion);
this.proxyConnection.setC2pConnectionState(packet.intendedState); this.proxyConnection.setC2pConnectionState(packet.intendedState);
this.proxyConnection.setClassicMpPass(classicMpPass); this.proxyConnection.setUserOptions(new UserOptions(classicMpPass, Options.MC_ACCOUNT));
this.proxyConnection.getPacketHandlers().add(new StatusPacketHandler(this.proxyConnection)); this.proxyConnection.getPacketHandlers().add(new StatusPacketHandler(this.proxyConnection));
this.proxyConnection.getPacketHandlers().add(new CustomPayloadPacketHandler(this.proxyConnection)); this.proxyConnection.getPacketHandlers().add(new CustomPayloadPacketHandler(this.proxyConnection));
this.proxyConnection.getPacketHandlers().add(new CompressionPacketHandler(this.proxyConnection)); this.proxyConnection.getPacketHandlers().add(new CompressionPacketHandler(this.proxyConnection));

View file

@ -38,6 +38,7 @@ import net.raphimc.viaproxy.protocolhack.viaproxy.signature.storage.ChatSession1
import net.raphimc.viaproxy.protocolhack.viaproxy.signature.storage.ChatSession1_19_1; import net.raphimc.viaproxy.protocolhack.viaproxy.signature.storage.ChatSession1_19_1;
import net.raphimc.viaproxy.protocolhack.viaproxy.signature.storage.ChatSession1_19_3; import net.raphimc.viaproxy.protocolhack.viaproxy.signature.storage.ChatSession1_19_3;
import net.raphimc.viaproxy.proxy.session.ProxyConnection; import net.raphimc.viaproxy.proxy.session.ProxyConnection;
import net.raphimc.viaproxy.saves.impl.accounts.Account;
import net.raphimc.viaproxy.saves.impl.accounts.BedrockAccount; import net.raphimc.viaproxy.saves.impl.accounts.BedrockAccount;
import net.raphimc.viaproxy.saves.impl.accounts.MicrosoftAccount; import net.raphimc.viaproxy.saves.impl.accounts.MicrosoftAccount;
import net.raphimc.viaproxy.util.logging.Logger; import net.raphimc.viaproxy.util.logging.Logger;
@ -57,15 +58,14 @@ public class ExternalInterface {
public static void fillPlayerData(final ProxyConnection proxyConnection) { public static void fillPlayerData(final ProxyConnection proxyConnection) {
Logger.u_info("auth", proxyConnection.getC2P().remoteAddress(), proxyConnection.getGameProfile(), "Filling player data"); Logger.u_info("auth", proxyConnection.getC2P().remoteAddress(), proxyConnection.getGameProfile(), "Filling player data");
try { try {
if (Options.MC_ACCOUNT != null) { if (proxyConnection.getUserOptions().account() != null) {
ViaProxy.saveManager.accountsSave.ensureRefreshed(Options.MC_ACCOUNT); final Account account = proxyConnection.getUserOptions().account();
ViaProxy.saveManager.accountsSave.ensureRefreshed(account);
proxyConnection.setGameProfile(Options.MC_ACCOUNT.getGameProfile()); proxyConnection.setGameProfile(account.getGameProfile());
final UserConnection user = proxyConnection.getUserConnection(); final UserConnection user = proxyConnection.getUserConnection();
if (Options.CHAT_SIGNING && proxyConnection.getServerVersion().isNewerThanOrEqualTo(VersionEnum.r1_19) && Options.MC_ACCOUNT instanceof MicrosoftAccount) { if (Options.CHAT_SIGNING && proxyConnection.getServerVersion().isNewerThanOrEqualTo(VersionEnum.r1_19) && account instanceof MicrosoftAccount microsoftAccount) {
final MicrosoftAccount microsoftAccount = (MicrosoftAccount) Options.MC_ACCOUNT;
final StepPlayerCertificates.PlayerCertificates playerCertificates = microsoftAccount.getPlayerCertificates(); final StepPlayerCertificates.PlayerCertificates playerCertificates = microsoftAccount.getPlayerCertificates();
final Instant expiresAt = Instant.ofEpochMilli(playerCertificates.expireTimeMs()); final Instant expiresAt = Instant.ofEpochMilli(playerCertificates.expireTimeMs());
final long expiresAtMillis = playerCertificates.expireTimeMs(); final long expiresAtMillis = playerCertificates.expireTimeMs();
@ -84,8 +84,7 @@ public class ExternalInterface {
user.put(new ChatSession1_19_0(user, uuid, privateKey, new ProfileKey(expiresAtMillis, publicKeyBytes, playerCertificates.legacyPublicKeySignature()))); user.put(new ChatSession1_19_0(user, uuid, privateKey, new ProfileKey(expiresAtMillis, publicKeyBytes, playerCertificates.legacyPublicKeySignature())));
user.put(new ChatSession1_19_1(user, uuid, privateKey, new ProfileKey(expiresAtMillis, publicKeyBytes, keySignature))); user.put(new ChatSession1_19_1(user, uuid, privateKey, new ProfileKey(expiresAtMillis, publicKeyBytes, keySignature)));
user.put(new ChatSession1_19_3(user, uuid, privateKey, new ProfileKey(expiresAtMillis, publicKeyBytes, keySignature))); user.put(new ChatSession1_19_3(user, uuid, privateKey, new ProfileKey(expiresAtMillis, publicKeyBytes, keySignature)));
} else if (proxyConnection.getServerVersion().equals(VersionEnum.bedrockLatest) && Options.MC_ACCOUNT instanceof BedrockAccount) { } else if (proxyConnection.getServerVersion().equals(VersionEnum.bedrockLatest) && account instanceof BedrockAccount bedrockAccount) {
final BedrockAccount bedrockAccount = (BedrockAccount) Options.MC_ACCOUNT;
final StepMCChain.MCChain mcChain = bedrockAccount.getMcChain(); final StepMCChain.MCChain mcChain = bedrockAccount.getMcChain();
final UUID deviceId = mcChain.prevResult().initialXblSession().prevResult2().id(); final UUID deviceId = mcChain.prevResult().initialXblSession().prevResult2().id();
@ -116,10 +115,9 @@ public class ExternalInterface {
} catch (TimeoutException e) { } catch (TimeoutException e) {
proxyConnection.kickClient("§cAuthentication cancelled! You need to install OpenAuthMod in order to join this server."); proxyConnection.kickClient("§cAuthentication cancelled! You need to install OpenAuthMod in order to join this server.");
} }
} else if (Options.MC_ACCOUNT instanceof MicrosoftAccount) { } else if (proxyConnection.getUserOptions().account() instanceof MicrosoftAccount microsoftAccount) {
final MicrosoftAccount microsoftAccount = (MicrosoftAccount) Options.MC_ACCOUNT;
try { try {
AuthLibServices.SESSION_SERVICE.joinServer(Options.MC_ACCOUNT.getGameProfile(), microsoftAccount.getMcProfile().prevResult().prevResult().access_token(), serverIdHash); AuthLibServices.SESSION_SERVICE.joinServer(microsoftAccount.getGameProfile(), microsoftAccount.getMcProfile().prevResult().prevResult().access_token(), serverIdHash);
} catch (Throwable e) { } catch (Throwable e) {
proxyConnection.kickClient("§cFailed to authenticate with Mojang servers! Please try again in a couple of seconds."); proxyConnection.kickClient("§cFailed to authenticate with Mojang servers! Please try again in a couple of seconds.");
} }

View file

@ -56,6 +56,26 @@ public class DummyProxyConnection extends ProxyConnection {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public GameProfile getGameProfile() {
return null;
}
@Override
public void setGameProfile(GameProfile gameProfile) {
throw new UnsupportedOperationException();
}
@Override
public C2SLoginHelloPacket1_7 getLoginHelloPacket() {
throw new UnsupportedOperationException();
}
@Override
public void setLoginHelloPacket(C2SLoginHelloPacket1_7 loginHelloPacket) {
throw new UnsupportedOperationException();
}
@Override @Override
public void setKeyForPreNettyEncryption(Key key) { public void setKeyForPreNettyEncryption(Key key) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@ -67,22 +87,7 @@ public class DummyProxyConnection extends ProxyConnection {
} }
@Override @Override
public void setGameProfile(GameProfile gameProfile) { public UserConnection getUserConnection() {
throw new UnsupportedOperationException();
}
@Override
public GameProfile getGameProfile() {
return null;
}
@Override
public void setLoginHelloPacket(C2SLoginHelloPacket1_7 loginHelloPacket) {
throw new UnsupportedOperationException();
}
@Override
public C2SLoginHelloPacket1_7 getLoginHelloPacket() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -92,7 +97,12 @@ public class DummyProxyConnection extends ProxyConnection {
} }
@Override @Override
public UserConnection getUserConnection() { public UserOptions getUserOptions() {
throw new UnsupportedOperationException();
}
@Override
public void setUserOptions(UserOptions userOptions) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@ -116,16 +126,6 @@ public class DummyProxyConnection extends ProxyConnection {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void setClassicMpPass(String classicMpPass) {
throw new UnsupportedOperationException();
}
@Override
public String getClassicMpPass() {
throw new UnsupportedOperationException();
}
@Override @Override
public boolean isClosed() { public boolean isClosed() {
return false; return false;

View file

@ -68,18 +68,20 @@ public class ProxyConnection extends NetClient {
private final Map<Integer, CompletableFuture<ByteBuf>> customPayloadListener = new ConcurrentHashMap<>(); private final Map<Integer, CompletableFuture<ByteBuf>> customPayloadListener = new ConcurrentHashMap<>();
private ServerAddress serverAddress; private ServerAddress serverAddress;
private VersionEnum serverVersion;
private VersionEnum serverVersion;
private VersionEnum clientVersion; private VersionEnum clientVersion;
private GameProfile gameProfile; private GameProfile gameProfile;
private C2SLoginHelloPacket1_7 loginHelloPacket; private C2SLoginHelloPacket1_7 loginHelloPacket;
private Key storedSecretKey;
private UserConnection userConnection; private UserConnection userConnection;
private UserOptions userOptions;
private ConnectionState c2pConnectionState = ConnectionState.HANDSHAKING; private ConnectionState c2pConnectionState = ConnectionState.HANDSHAKING;
private ConnectionState p2sConnectionState = ConnectionState.HANDSHAKING; private ConnectionState p2sConnectionState = ConnectionState.HANDSHAKING;
private Key storedSecretKey;
private String classicMpPass;
public ProxyConnection(final Supplier<ChannelHandler> handlerSupplier, final Function<Supplier<ChannelHandler>, ChannelInitializer<Channel>> channelInitializerSupplier, final Channel c2p) { public ProxyConnection(final Supplier<ChannelHandler> handlerSupplier, final Function<Supplier<ChannelHandler>, ChannelInitializer<Channel>> channelInitializerSupplier, final Channel c2p) {
super(handlerSupplier, channelInitializerSupplier); super(handlerSupplier, channelInitializerSupplier);
this.c2p = c2p; this.c2p = c2p;
@ -128,6 +130,30 @@ public class ProxyConnection extends NetClient {
return this.serverVersion; return this.serverVersion;
} }
public VersionEnum getClientVersion() {
return this.clientVersion;
}
public void setClientVersion(final VersionEnum clientVersion) {
this.clientVersion = clientVersion;
}
public GameProfile getGameProfile() {
return this.gameProfile;
}
public void setGameProfile(final GameProfile gameProfile) {
this.gameProfile = gameProfile;
}
public C2SLoginHelloPacket1_7 getLoginHelloPacket() {
return this.loginHelloPacket;
}
public void setLoginHelloPacket(final C2SLoginHelloPacket1_7 loginHelloPacket) {
this.loginHelloPacket = loginHelloPacket;
}
public void setKeyForPreNettyEncryption(final Key key) { public void setKeyForPreNettyEncryption(final Key key) {
this.storedSecretKey = key; this.storedSecretKey = key;
} }
@ -136,36 +162,28 @@ public class ProxyConnection extends NetClient {
this.getChannel().attr(MCPipeline.ENCRYPTION_ATTRIBUTE_KEY).set(new AESEncryption(this.storedSecretKey)); this.getChannel().attr(MCPipeline.ENCRYPTION_ATTRIBUTE_KEY).set(new AESEncryption(this.storedSecretKey));
} }
public void setClientVersion(final VersionEnum clientVersion) { public UserConnection getUserConnection() {
this.clientVersion = clientVersion; return this.userConnection;
}
public VersionEnum getClientVersion() {
return this.clientVersion;
}
public void setGameProfile(final GameProfile gameProfile) {
this.gameProfile = gameProfile;
}
public GameProfile getGameProfile() {
return this.gameProfile;
}
public void setLoginHelloPacket(final C2SLoginHelloPacket1_7 loginHelloPacket) {
this.loginHelloPacket = loginHelloPacket;
}
public C2SLoginHelloPacket1_7 getLoginHelloPacket() {
return this.loginHelloPacket;
} }
public void setUserConnection(final UserConnection userConnection) { public void setUserConnection(final UserConnection userConnection) {
this.userConnection = userConnection; this.userConnection = userConnection;
} }
public UserConnection getUserConnection() { public UserOptions getUserOptions() {
return this.userConnection; return this.userOptions;
}
public void setUserOptions(final UserOptions userOptions) {
this.userOptions = userOptions;
}
public ConnectionState getC2pConnectionState() {
return this.c2pConnectionState;
}
public ConnectionState getP2sConnectionState() {
return this.p2sConnectionState;
} }
public void setC2pConnectionState(final ConnectionState connectionState) { public void setC2pConnectionState(final ConnectionState connectionState) {
@ -215,14 +233,6 @@ public class ProxyConnection extends NetClient {
} }
} }
public ConnectionState getC2pConnectionState() {
return this.c2pConnectionState;
}
public ConnectionState getP2sConnectionState() {
return this.p2sConnectionState;
}
public CompletableFuture<ByteBuf> sendCustomPayload(final String channel, final ByteBuf data) { public CompletableFuture<ByteBuf> sendCustomPayload(final String channel, final ByteBuf data) {
if (channel.length() > 20) throw new IllegalStateException("Channel name can't be longer than 20 characters"); if (channel.length() > 20) throw new IllegalStateException("Channel name can't be longer than 20 characters");
final CompletableFuture<ByteBuf> future = new CompletableFuture<>(); final CompletableFuture<ByteBuf> future = new CompletableFuture<>();
@ -264,14 +274,6 @@ public class ProxyConnection extends NetClient {
return false; return false;
} }
public void setClassicMpPass(final String classicMpPass) {
this.classicMpPass = classicMpPass;
}
public String getClassicMpPass() {
return this.classicMpPass;
}
public void kickClient(final String message) throws CloseAndReturn { public void kickClient(final String message) throws CloseAndReturn {
Logger.u_err("kick", this.c2p.remoteAddress(), this.getGameProfile(), message.replaceAll("§.", "")); Logger.u_err("kick", this.c2p.remoteAddress(), this.getGameProfile(), message.replaceAll("§.", ""));

View file

@ -0,0 +1,23 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2023 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.proxy.session;
import net.raphimc.viaproxy.saves.impl.accounts.Account;
public record UserOptions(String classicMpPass, Account account) {
}