Repository navigation
Expand file tree
/
Copy pathSessionServiceHandler.java
More file actions
87 lines (78 loc) · 4.46 KB
/
Copy pathSessionServiceHandler.java
File metadata and controls
87 lines (78 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* This file is part of ViaProxyAuthHook - https://github.com/ViaVersionAddons/ViaProxyAuthHook
* Copyright (C) 2024-2026 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.authhook.httpserver.handler;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile;
import com.viaversion.viaversion.util.Either;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.QueryStringDecoder;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
import java.net.URI;
import java.util.Map;
public class SessionServiceHandler implements RedirectedHostHandler {
private final Map<String, ProxyConnection> pendingConnections;
public SessionServiceHandler(final Map<String, ProxyConnection> pendingConnections) {
this.pendingConnections = pendingConnections;
}
@Override
public Either<URI, FullHttpResponse> handleRequest(final String host, final HttpRequest request) {
if (request.method().equals(HttpMethod.GET) && request.uri().startsWith("/session/minecraft/hasJoined")) {
final QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.uri());
if (queryStringDecoder.parameters().containsKey("username") && queryStringDecoder.parameters().containsKey("serverId")) {
final String username = queryStringDecoder.parameters().get("username").get(0);
final String serverId = queryStringDecoder.parameters().get("serverId").get(0);
final ProxyConnection proxyConnection = this.pendingConnections.remove(serverId + "_" + username);
if (proxyConnection != null) {
final GameProfile gameProfile = proxyConnection.getGameProfile();
final JsonObject responseObj = new JsonObject();
responseObj.addProperty("name", gameProfile.name());
responseObj.addProperty("id", gameProfile.id().toString().replace("-", ""));
if (!gameProfile.properties().isEmpty()) {
final JsonArray propertiesArray = new JsonArray();
gameProfile.properties().forEach((key, value) -> {
final JsonObject propertyObj = new JsonObject();
propertyObj.addProperty("name", key);
propertyObj.addProperty("value", value.value());
if (value.hasSignature()) {
propertyObj.addProperty("signature", value.signature());
}
propertiesArray.add(propertyObj);
});
responseObj.add("properties", propertiesArray);
}
final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.buffer());
response.content().writeBytes(responseObj.toString().getBytes());
response.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
return Either.right(response);
}
}
}
return Either.left(URI.create(host + request.uri()));
}
}