[FEATURE] Support 1.20.2

This commit is contained in:
Gravita 2023-10-03 19:16:02 +07:00
parent 52e8f693b5
commit b72fb643d4
14 changed files with 84 additions and 76 deletions

View file

@ -14,6 +14,7 @@
import pro.gravit.launchserver.auth.password.PasswordVerifier;
import pro.gravit.launchserver.helper.LegacySessionHelper;
import pro.gravit.launchserver.manangers.AuthManager;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.launchserver.socket.response.auth.AuthResponse;
import pro.gravit.utils.helper.SecurityHelper;
@ -162,6 +163,25 @@ public AuthManager.AuthReport authorize(String login, AuthResponse.AuthContext c
}
}
@Override
public User checkServer(Client client, String username, String serverID) throws IOException {
SQLUser user = (SQLUser) getUserByUsername(username);
if (user == null) {
return null;
}
if (user.getUsername().equals(username) && user.getServerId().equals(serverID)) {
return user;
}
return null;
}
@Override
public boolean joinServer(Client client, String username, UUID uuid, String accessToken, String serverID) throws IOException {
SQLUser user = (SQLUser) client.getUser();
if (user == null) return false;
return user.getUsername().equals(username) && user.getAccessToken().equals(accessToken) && updateServerID(user, serverID);
}
@Override
public void init(LaunchServer server) {
this.server = server;
@ -225,7 +245,6 @@ protected void updateAuth(User user, String accessToken) throws IOException {
}
}
@Override
protected boolean updateServerID(User user, String serverID) throws IOException {
try (Connection c = getSQLConfig().getConnection()) {
SQLUser SQLUser = (SQLUser) user;
@ -328,12 +347,10 @@ public UUID getUUID() {
return uuid;
}
@Override
public String getServerId() {
return serverId;
}
@Override
public String getAccessToken() {
return accessToken;
}
@ -372,6 +389,11 @@ public User getUser() {
return user;
}
@Override
public String getMinecraftAccessToken() {
return user.getAccessToken();
}
@Override
public long getExpireIn() {
return 0;

View file

@ -75,11 +75,6 @@ public AuthManager.AuthReport authorize(User user, AuthResponse.AuthContext cont
public abstract void init(LaunchServer server);
// Auth Handler methods
protected boolean updateServerID(User user, String serverID) throws IOException {
throw new UnsupportedOperationException();
}
public List<GetAvailabilityAuthRequestEvent.AuthAvailabilityDetails> getDetails(Client client) {
return List.of(new AuthPasswordDetails());
}
@ -292,22 +287,9 @@ public void invoke(String... args) throws Exception {
return map;
}
public User checkServer(Client client, String username, String serverID) throws IOException {
User user = getUserByUsername(username);
if (user == null) {
return null;
}
if (user.getUsername().equals(username) && user.getServerId().equals(serverID)) {
return user;
}
return null;
}
public abstract User checkServer(Client client, String username, String serverID) throws IOException;
public boolean joinServer(Client client, String username, String accessToken, String serverID) throws IOException {
User user = client.getUser();
if (user == null) return false;
return user.getUsername().equals(username) && user.getAccessToken().equals(accessToken) && updateServerID(user, serverID);
}
public abstract boolean joinServer(Client client, String username, UUID uuid, String accessToken, String serverID) throws IOException;
@SuppressWarnings("unchecked")
public <T> T isSupport(Class<T> clazz) {

View file

@ -281,21 +281,14 @@ public List<String> getClientApiFeatures() {
return apiFeatures;
}
@Override
protected boolean updateServerID(User user, String serverID) throws IOException {
var result = requester.send(requester.post(updateServerIdUrl, new UpdateServerIdRequest(user.getUsername(), user.getUUID(), serverID),
null), Void.class);
return result.isSuccessful();
}
@Override
public User checkServer(Client client, String username, String serverID) throws IOException {
return requester.send(requester.post(checkServerUrl, new CheckServerRequest(username, serverID), bearerToken), HttpUser.class).getOrThrow();
}
@Override
public boolean joinServer(Client client, String username, String accessToken, String serverID) throws IOException {
var result = requester.send(requester.post(joinServerUrl, new JoinServerRequest(username, accessToken, serverID), bearerToken), Void.class);
public boolean joinServer(Client client, String username, UUID uuid, String accessToken, String serverID) throws IOException {
var result = requester.send(requester.post(joinServerUrl, new JoinServerRequest(username, uuid, accessToken, serverID), bearerToken), Void.class);
return result.isSuccessful();
}
@ -361,11 +354,13 @@ public static class GetHardwareListResponse {
public static class JoinServerRequest {
public String username;
public UUID uuid;
public String accessToken;
public String serverId;
public JoinServerRequest(String username, String accessToken, String serverId) {
public JoinServerRequest(String username, UUID uuid, String accessToken, String serverId) {
this.username = username;
this.uuid = uuid;
this.accessToken = accessToken;
this.serverId = serverId;
}
@ -421,14 +416,16 @@ public HardwareRequest(byte[] key) {
public static class HttpUserSession implements UserSession {
private String id;
private HttpUser user;
private String minecraftAccessToken;
private long expireIn;
public HttpUserSession() {
}
public HttpUserSession(String id, HttpUser user, long expireIn) {
public HttpUserSession(String id, HttpUser user, String minecraftAccessToken, long expireIn) {
this.id = id;
this.user = user;
this.minecraftAccessToken = minecraftAccessToken;
this.expireIn = expireIn;
}
@ -442,6 +439,11 @@ public User getUser() {
return user;
}
@Override
public String getMinecraftAccessToken() {
return minecraftAccessToken;
}
@Override
public long getExpireIn() {
return expireIn;
@ -452,6 +454,7 @@ public String toString() {
return "HttpUserSession{" +
"id='" + id + '\'' +
", user=" + user +
", minecraftAccessToken='" + minecraftAccessToken + '\'' +
", expireIn=" + expireIn +
'}';
}
@ -581,16 +584,6 @@ public UUID getUUID() {
return uuid;
}
@Override
public String getServerId() {
return serverId;
}
@Override
public String getAccessToken() {
return accessToken;
}
@Override
public ClientPermissions getPermissions() {
return permissions;

View file

@ -94,13 +94,6 @@ public AuthManager.AuthReport authorize(String login, AuthResponse.AuthContext c
}
}
@Override
protected boolean updateServerID(User user, String serverID) {
MemoryUser memoryUser = (MemoryUser) user;
memoryUser.serverId = serverID;
return true;
}
@Override
public User checkServer(Client client, String username, String serverID) {
synchronized (memory) {
@ -116,7 +109,7 @@ public User checkServer(Client client, String username, String serverID) {
}
@Override
public boolean joinServer(Client client, String username, String accessToken, String serverID) {
public boolean joinServer(Client client, String username, UUID uuid, String accessToken, String serverID) {
return true;
}
@ -158,16 +151,6 @@ public UUID getUUID() {
return uuid;
}
@Override
public String getServerId() {
return serverId;
}
@Override
public String getAccessToken() {
return accessToken;
}
@Override
public ClientPermissions getPermissions() {
return permissions;
@ -208,6 +191,11 @@ public User getUser() {
return user;
}
@Override
public String getMinecraftAccessToken() {
return "IGNORED";
}
@Override
public long getExpireIn() {
return expireIn;

View file

@ -67,7 +67,7 @@ public User checkServer(Client client, String username, String serverID) throws
}
@Override
public boolean joinServer(Client client, String username, String accessToken, String serverID) {
public boolean joinServer(Client client, String username, UUID uuid, String accessToken, String serverID) {
return false; // Authorization not supported
}

View file

@ -4,6 +4,7 @@
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.auth.AuthException;
import pro.gravit.launchserver.manangers.AuthManager;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.launchserver.socket.response.auth.AuthResponse;
import java.io.IOException;
@ -46,7 +47,12 @@ public void init(LaunchServer server) {
}
@Override
protected boolean updateServerID(User user, String serverID) {
public User checkServer(Client client, String username, String serverID) throws IOException {
return null;
}
@Override
public boolean joinServer(Client client, String username, UUID uuid, String accessToken, String serverID) throws IOException {
return false;
}

View file

@ -9,10 +9,6 @@ public interface User {
UUID getUUID();
String getServerId();
String getAccessToken();
ClientPermissions getPermissions();
default boolean isBanned() {

View file

@ -5,5 +5,7 @@ public interface UserSession {
User getUser();
String getMinecraftAccessToken();
long getExpireIn();
}

View file

@ -20,6 +20,7 @@
import java.util.Base64;
import java.util.Date;
import java.util.UUID;
import static java.util.concurrent.TimeUnit.SECONDS;
@ -93,7 +94,7 @@ public VerifySecureLevelKeyRequestEvent onSuccessVerify(Client client) {
}
@Override
public boolean onJoinServer(String serverID, String username, Client client) {
public boolean onJoinServer(String serverID, String username, UUID uuid, Client client) {
return !enableHardwareFeature || (client.trustLevel != null && client.trustLevel.hardwareInfo != null);
}

View file

@ -2,8 +2,10 @@
import pro.gravit.launchserver.socket.Client;
import java.util.UUID;
public interface JoinServerProtectHandler {
default boolean onJoinServer(String serverID, String username, Client client) {
default boolean onJoinServer(String serverID, String username, UUID uuid, Client client) {
return true;
}
}

View file

@ -115,7 +115,7 @@ public AuthReport auth(AuthResponse.AuthContext context, AuthRequest.AuthPasswor
context.client.sessionObject = session;
internalAuth(context.client, context.authType, context.pair, user.getUsername(), user.getUUID(), user.getPermissions(), true);
if (context.authType == AuthResponse.ConnectTypes.CLIENT && server.config.protectHandler.allowGetAccessToken(context)) {
return AuthReport.ofMinecraftAccessToken(user.getAccessToken(), session);
return AuthReport.ofMinecraftAccessToken(session.getMinecraftAccessToken(), session);
}
return AuthReport.ofMinecraftAccessToken(null, session);
}
@ -166,9 +166,9 @@ public CheckServerReport checkServer(Client client, String username, String serv
else return CheckServerReport.ofUser(user, getPlayerProfile(client.auth, user));
}
public boolean joinServer(Client client, String username, String accessToken, String serverID) throws IOException {
public boolean joinServer(Client client, String username, UUID uuid, String accessToken, String serverID) throws IOException {
if (client.auth == null) return false;
return client.auth.core.joinServer(client, username, accessToken, serverID);
return client.auth.core.joinServer(client, username, uuid, accessToken, serverID);
}
public PlayerProfile getPlayerProfile(Client client) {

View file

@ -10,11 +10,14 @@
import pro.gravit.launchserver.socket.response.SimpleResponse;
import pro.gravit.utils.HookException;
import java.util.UUID;
public class JoinServerResponse extends SimpleResponse {
private transient final Logger logger = LogManager.getLogger();
public String serverID;
public String accessToken;
public String username;
public UUID uuid;
@Override
public String getType() {
@ -27,7 +30,7 @@ public void execute(ChannelHandlerContext ctx, Client client) {
sendError("Permissions denied");
return;
}
if (username == null || accessToken == null || serverID == null) {
if ((username == null && uuid == null) || accessToken == null || serverID == null) {
sendError("Invalid request");
return;
}
@ -35,13 +38,13 @@ public void execute(ChannelHandlerContext ctx, Client client) {
try {
server.authHookManager.joinServerHook.hook(this, client);
if (server.config.protectHandler instanceof JoinServerProtectHandler joinServerProtectHandler) {
success = joinServerProtectHandler.onJoinServer(serverID, username, client);
success = joinServerProtectHandler.onJoinServer(serverID, username, uuid, client);
if (!success) {
sendResult(new JoinServerRequestEvent(false));
return;
}
}
success = server.authManager.joinServer(client, username, accessToken, serverID);
success = server.authManager.joinServer(client, username, uuid, accessToken, serverID);
if (success) {
logger.debug("joinServer: {} accessToken: {} serverID: {}", username, accessToken, serverID);
}

View file

@ -16,4 +16,5 @@ private ClientProfileVersions() {
public static final ClientProfile.Version MINECRAFT_1_18 = ClientProfile.Version.of("1.18");
public static final ClientProfile.Version MINECRAFT_1_19 = ClientProfile.Version.of("1.19");
public static final ClientProfile.Version MINECRAFT_1_20 = ClientProfile.Version.of("1.20");
public static final ClientProfile.Version MINECRAFT_1_20_2 = ClientProfile.Version.of("1.20.2");
}

View file

@ -6,12 +6,16 @@
import pro.gravit.launcher.request.websockets.WebSocketRequest;
import pro.gravit.utils.helper.VerifyHelper;
import java.util.UUID;
public final class JoinServerRequest extends Request<JoinServerRequestEvent> implements WebSocketRequest {
// Instance
@LauncherNetworkAPI
public final String username;
@LauncherNetworkAPI
public final UUID uuid;
@LauncherNetworkAPI
public final String accessToken;
@LauncherNetworkAPI
public final String serverID;
@ -19,10 +23,18 @@ public final class JoinServerRequest extends Request<JoinServerRequestEvent> imp
public JoinServerRequest(String username, String accessToken, String serverID) {
this.username = username;
this.uuid = null;
this.accessToken = accessToken;
this.serverID = VerifyHelper.verifyServerID(serverID);
}
public JoinServerRequest(UUID uuid, String accessToken, String serverID) {
this.username = null;
this.uuid = uuid;
this.accessToken = accessToken;
this.serverID = serverID;
}
@Override
public String getType() {
return "joinServer";