[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.auth.password.PasswordVerifier;
import pro.gravit.launchserver.helper.LegacySessionHelper; import pro.gravit.launchserver.helper.LegacySessionHelper;
import pro.gravit.launchserver.manangers.AuthManager; import pro.gravit.launchserver.manangers.AuthManager;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.launchserver.socket.response.auth.AuthResponse; import pro.gravit.launchserver.socket.response.auth.AuthResponse;
import pro.gravit.utils.helper.SecurityHelper; 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 @Override
public void init(LaunchServer server) { public void init(LaunchServer server) {
this.server = 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 { protected boolean updateServerID(User user, String serverID) throws IOException {
try (Connection c = getSQLConfig().getConnection()) { try (Connection c = getSQLConfig().getConnection()) {
SQLUser SQLUser = (SQLUser) user; SQLUser SQLUser = (SQLUser) user;
@ -328,12 +347,10 @@ public UUID getUUID() {
return uuid; return uuid;
} }
@Override
public String getServerId() { public String getServerId() {
return serverId; return serverId;
} }
@Override
public String getAccessToken() { public String getAccessToken() {
return accessToken; return accessToken;
} }
@ -372,6 +389,11 @@ public User getUser() {
return user; return user;
} }
@Override
public String getMinecraftAccessToken() {
return user.getAccessToken();
}
@Override @Override
public long getExpireIn() { public long getExpireIn() {
return 0; return 0;

View file

@ -75,11 +75,6 @@ public AuthManager.AuthReport authorize(User user, AuthResponse.AuthContext cont
public abstract void init(LaunchServer server); 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) { public List<GetAvailabilityAuthRequestEvent.AuthAvailabilityDetails> getDetails(Client client) {
return List.of(new AuthPasswordDetails()); return List.of(new AuthPasswordDetails());
} }
@ -292,22 +287,9 @@ public void invoke(String... args) throws Exception {
return map; return map;
} }
public User checkServer(Client client, String username, String serverID) throws IOException { public abstract 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 boolean joinServer(Client client, String username, String accessToken, String serverID) throws IOException { public abstract boolean joinServer(Client client, String username, UUID uuid, 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);
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T isSupport(Class<T> clazz) { public <T> T isSupport(Class<T> clazz) {

View file

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

View file

@ -67,7 +67,7 @@ public User checkServer(Client client, String username, String serverID) throws
} }
@Override @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 return false; // Authorization not supported
} }

View file

@ -4,6 +4,7 @@
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.auth.AuthException; import pro.gravit.launchserver.auth.AuthException;
import pro.gravit.launchserver.manangers.AuthManager; import pro.gravit.launchserver.manangers.AuthManager;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.launchserver.socket.response.auth.AuthResponse; import pro.gravit.launchserver.socket.response.auth.AuthResponse;
import java.io.IOException; import java.io.IOException;
@ -46,7 +47,12 @@ public void init(LaunchServer server) {
} }
@Override @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; return false;
} }

View file

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

View file

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

View file

@ -20,6 +20,7 @@
import java.util.Base64; import java.util.Base64;
import java.util.Date; import java.util.Date;
import java.util.UUID;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
@ -93,7 +94,7 @@ public VerifySecureLevelKeyRequestEvent onSuccessVerify(Client client) {
} }
@Override @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); return !enableHardwareFeature || (client.trustLevel != null && client.trustLevel.hardwareInfo != null);
} }

View file

@ -2,8 +2,10 @@
import pro.gravit.launchserver.socket.Client; import pro.gravit.launchserver.socket.Client;
import java.util.UUID;
public interface JoinServerProtectHandler { 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; return true;
} }
} }

View file

@ -115,7 +115,7 @@ public AuthReport auth(AuthResponse.AuthContext context, AuthRequest.AuthPasswor
context.client.sessionObject = session; context.client.sessionObject = session;
internalAuth(context.client, context.authType, context.pair, user.getUsername(), user.getUUID(), user.getPermissions(), true); 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)) { 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); 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)); 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; 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) { public PlayerProfile getPlayerProfile(Client client) {

View file

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