[FEATURE] clientData доступен всем Response по умолчанию

This commit is contained in:
Gravit 2019-03-12 15:05:41 +07:00
parent 124f1ddbeb
commit 96254b11ea
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
19 changed files with 61 additions and 55 deletions

View file

@ -4,12 +4,13 @@
import ru.gravit.launcher.serialize.HOutput;
import ru.gravit.launcher.serialize.SerializeLimits;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.Client;
import java.io.IOException;
public final class PingResponse extends Response {
public PingResponse(LaunchServer server, long id, HInput input, HOutput output, String ip) {
super(server, id, input, output, ip);
public PingResponse(LaunchServer server, long id, HInput input, HOutput output, String ip, Client clientData) {
super(server, id, input, output, ip, clientData);
}
@Override

View file

@ -11,6 +11,7 @@
import ru.gravit.launchserver.response.profile.ProfileByUUIDResponse;
import ru.gravit.launchserver.response.profile.ProfileByUsernameResponse;
import ru.gravit.launchserver.response.update.*;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.utils.helper.LogHelper;
import java.io.IOException;
@ -21,13 +22,13 @@ public abstract class Response {
@FunctionalInterface
public interface Factory<R> {
Response newResponse(LaunchServer server, long id, HInput input, HOutput output, String ip);
Response newResponse(LaunchServer server, long id, HInput input, HOutput output, String ip, Client clientData);
}
private static final Map<Integer, Factory<?>> RESPONSES = new ConcurrentHashMap<>(8);
public static Response getResponse(int type, LaunchServer server, long session, HInput input, HOutput output, String ip) {
return RESPONSES.get(type).newResponse(server, session, input, output, ip);
public static Response getResponse(int type, LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
return RESPONSES.get(type).newResponse(server, session, input, output, ip, clientData);
}
public static void registerResponse(int type, Factory<?> factory) {
@ -72,15 +73,18 @@ public static void requestError(String message) throws RequestException {
protected final String ip;
protected final Client clientData;
protected final long session;
protected Response(LaunchServer server, long session, HInput input, HOutput output, String ip) {
protected Response(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
this.server = server;
this.input = input;
this.output = output;
this.ip = ip;
this.session = session;
this.clientData = clientData;
}

View file

@ -11,8 +11,8 @@
import java.io.IOException;
public class ExecCommandResponse extends Response {
public ExecCommandResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public ExecCommandResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
@Override

View file

@ -31,8 +31,8 @@ private static String echo(int length) {
return new String(chars);
}
public AuthResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public AuthResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
public static class AuthContext {
@ -81,7 +81,6 @@ public void reply() throws Exception {
debug("Login: '%s', Password: '%s'", login, echo(password.length()));
AuthProviderResult result;
AuthProvider provider = server.config.authProvider[auth_id];
Client clientData = server.sessionManager.getClient(session);
clientData.type = Client.Type.USER;
AuthContext context = new AuthContext(session, login, password.length(), customText, client, hwid_str, false);
try {

View file

@ -28,8 +28,8 @@ private static String echo(int length) {
return new String(chars);
}
public AuthServerResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public AuthServerResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
@Override
@ -52,7 +52,6 @@ public void reply() throws Exception {
debug("ServerLogin: '%s', Password: '%s'", login, echo(password.length()));
AuthProviderResult result;
AuthProvider provider = server.config.authProvider[auth_id];
Client clientData = server.sessionManager.getClient(session);
try {
if (server.limiter.isLimit(ip)) {
AuthProvider.authError(server.config.authRejectString);

View file

@ -4,14 +4,15 @@
import ru.gravit.launcher.serialize.HOutput;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.response.Response;
import ru.gravit.launchserver.socket.Client;
public class ChangeServerResponse extends Response {
public static boolean needChange = false;
public static String address;
public static int port;
public ChangeServerResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public ChangeServerResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
@Override

View file

@ -16,8 +16,8 @@
public final class CheckServerResponse extends Response {
public CheckServerResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public CheckServerResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
@Override
@ -26,7 +26,6 @@ public void reply() throws IOException {
String serverID = VerifyHelper.verifyServerID(input.readASCII(41)); // With minus sign
String client = input.readString(SerializeLimits.MAX_CLIENT);
debug("Username: %s, Server ID: %s", username, serverID);
Client clientData = server.sessionManager.getClient(session);
if (!clientData.isAuth || clientData.type != Client.Type.SERVER) {
requestError("Access denied");
return;

View file

@ -15,8 +15,8 @@
public final class JoinServerResponse extends Response {
public JoinServerResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public JoinServerResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
@Override
@ -24,7 +24,6 @@ public void reply() throws IOException {
String username = VerifyHelper.verifyUsername(input.readString(SerializeLimits.MAX_LOGIN));
String accessToken = SecurityHelper.verifyToken(input.readASCII(-SecurityHelper.TOKEN_STRING_LENGTH));
String serverID = VerifyHelper.verifyServerID(input.readASCII(SerializeLimits.MAX_SERVERID)); // With minus sign
Client clientData = server.sessionManager.getClient(session);
if (!clientData.isAuth || clientData.type != Client.Type.USER) {
requestError("Access denied");
return;

View file

@ -11,14 +11,13 @@
import java.util.Collection;
public class SetProfileResponse extends Response {
public SetProfileResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public SetProfileResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
@Override
public void reply() throws Exception {
String client = input.readString(SerializeLimits.MAX_CLIENT);
Client clientData = server.sessionManager.getClient(session);
if (!clientData.isAuth) requestError("You not auth");
Collection<ClientProfile> profiles = server.getProfiles();
for (ClientProfile p : profiles) {

View file

@ -5,6 +5,7 @@
import ru.gravit.launcher.serialize.SerializeLimits;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.response.Response;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.utils.helper.VerifyHelper;
import java.io.IOException;
@ -12,8 +13,8 @@
public final class BatchProfileByUsernameResponse extends Response {
public BatchProfileByUsernameResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public BatchProfileByUsernameResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
@Override

View file

@ -7,6 +7,7 @@
import ru.gravit.launcher.serialize.SerializeLimits;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.response.Response;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.utils.helper.LogHelper;
import java.io.IOException;
@ -37,8 +38,8 @@ public static PlayerProfile getProfile(LaunchServer server, UUID uuid, String us
return new PlayerProfile(uuid, username, skin, cloak);
}
public ProfileByUUIDResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public ProfileByUUIDResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
@Override

View file

@ -5,6 +5,7 @@
import ru.gravit.launcher.serialize.SerializeLimits;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.response.Response;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.utils.helper.VerifyHelper;
import java.io.IOException;
@ -24,8 +25,8 @@ public static void writeProfile(LaunchServer server, HOutput output, String user
ProfileByUUIDResponse.getProfile(server, uuid, username, client).write(output);
}
public ProfileByUsernameResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public ProfileByUsernameResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
@Override

View file

@ -13,8 +13,8 @@
public final class LauncherResponse extends Response {
public LauncherResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public LauncherResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
@Override
@ -25,17 +25,16 @@ public void reply() throws IOException {
requestError("Missing launcher binary");
return;
}
Client client = server.sessionManager.getOrNewClient(session);
byte[] digest = input.readByteArray(SerializeLimits.MAX_DIGEST);
if (!Arrays.equals(bytes.getDigest(), digest)) {
writeNoError(output);
output.writeBoolean(true);
output.writeByteArray(bytes.getBytes(), 0);
client.checkSign = false;
clientData.checkSign = false;
return;
}
writeNoError(output);
output.writeBoolean(false);
client.checkSign = true;
clientData.checkSign = true;
}
}

View file

@ -5,14 +5,15 @@
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.binary.LauncherBinary;
import ru.gravit.launchserver.response.Response;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.utils.helper.SecurityHelper;
import java.io.IOException;
public final class LegacyLauncherResponse extends Response {
public LegacyLauncherResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public LegacyLauncherResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
@Override

View file

@ -14,16 +14,15 @@
public final class ProfilesResponse extends Response {
public ProfilesResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public ProfilesResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
@Override
public void reply() throws IOException {
// Resolve launcher binary
Client client = server.sessionManager.getClient(session);
input.readBoolean();
if (client.type == Client.Type.USER && !client.checkSign) {
if (clientData.type == Client.Type.USER && !clientData.checkSign) {
LogHelper.warning("User session: %d ip %s try get profiles", session, ip);
requestError("Access denied");
return;

View file

@ -6,14 +6,15 @@
import ru.gravit.launcher.serialize.signed.SignedObjectHolder;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.response.Response;
import ru.gravit.launchserver.socket.Client;
import java.util.Map.Entry;
import java.util.Set;
public final class UpdateListResponse extends Response {
public UpdateListResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public UpdateListResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
@Override

View file

@ -24,8 +24,8 @@
public final class UpdateResponse extends Response {
public UpdateResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip);
public UpdateResponse(LaunchServer server, long session, HInput input, HOutput output, String ip, Client clientData) {
super(server, session, input, output, ip, clientData);
}
@Override
@ -37,7 +37,6 @@ public void reply() throws IOException {
requestError(String.format("Unknown update dir: %s", updateDirName));
return;
}
Client clientData = server.sessionManager.getClient(session);
if (!clientData.isAuth || clientData.type != Client.Type.USER || clientData.profile == null) {
requestError("Access denied");
return;

View file

@ -82,12 +82,12 @@ private Handshake readHandshake(HInput input, HOutput output) throws IOException
return new Handshake(type, session);
}
private void respond(Integer type, HInput input, HOutput output, long session, String ip) throws Exception {
private void respond(Integer type, HInput input, HOutput output, long session, String ip, Client clientData) throws Exception {
if (server.serverSocketHandler.logConnections)
LogHelper.info("Connection #%d from %s", session, ip);
// Choose response based on type
Response response = Response.getResponse(type, server, session, input, output, ip);
Response response = Response.getResponse(type, server, session, input, output, ip, clientData);
// Reply
response.reply();
@ -115,11 +115,13 @@ public void run() {
context.ip = IOHelper.getIP(socket.getRemoteSocketAddress());
context.session = handshake.session;
context.type = handshake.type;
Client clientData = server.sessionManager.getOrNewClient(context.session);
context.client = clientData;
// Start response
if (socketHookManager.preHook(context)) {
try {
respond(handshake.type, input, output, handshake.session, context.ip);
respond(handshake.type, input, output, handshake.session, context.ip, clientData);
socketHookManager.postHook(context);
} catch (RequestException e) {
if (server.socketHookManager.errorHook(context, e)) {

View file

@ -4,9 +4,10 @@
import ru.gravit.launcher.serialize.HOutput;
public class SocketContext {
HInput input;
HOutput output;
long session;
String ip;
Integer type;
public HInput input;
public HOutput output;
public long session;
public String ip;
public Integer type;
public Client client;
}