|
| 1 | +/* |
| 2 | + * This file is part of ViaProxyAuthHook - https://github.com/ViaVersionAddons/ViaProxyAuthHook |
| 3 | + * Copyright (C) 2024-2024 RK_01/RaphiMC and contributors |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package net.raphimc.authhook; |
| 19 | + |
| 20 | +import com.google.common.cache.CacheBuilder; |
| 21 | +import com.google.gson.JsonArray; |
| 22 | +import com.google.gson.JsonObject; |
| 23 | +import com.mojang.authlib.GameProfile; |
| 24 | +import io.netty.bootstrap.ServerBootstrap; |
| 25 | +import io.netty.channel.*; |
| 26 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 27 | +import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 28 | +import io.netty.handler.codec.http.*; |
| 29 | +import net.raphimc.authhook.config.AuthHookConfig; |
| 30 | +import net.raphimc.viaproxy.proxy.session.ProxyConnection; |
| 31 | + |
| 32 | +import java.net.InetSocketAddress; |
| 33 | +import java.net.URI; |
| 34 | +import java.net.http.HttpClient; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | + |
| 39 | +public class AuthHookHttpServer { |
| 40 | + |
| 41 | + private final InetSocketAddress bindAddress; |
| 42 | + private final ChannelFuture channelFuture; |
| 43 | + private final Map<String, ProxyConnection> pendingConnections = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).<String, ProxyConnection>build().asMap(); |
| 44 | + |
| 45 | + public AuthHookHttpServer(final InetSocketAddress bindAddress) { |
| 46 | + this.bindAddress = bindAddress; |
| 47 | + this.channelFuture = new ServerBootstrap() |
| 48 | + .group(new NioEventLoopGroup(0)) |
| 49 | + .channel(NioServerSocketChannel.class) |
| 50 | + .option(ChannelOption.SO_BACKLOG, 128) |
| 51 | + .childOption(ChannelOption.TCP_NODELAY, true) |
| 52 | + .childOption(ChannelOption.SO_KEEPALIVE, true) |
| 53 | + .childHandler(new ChannelInitializer<>() { |
| 54 | + @Override |
| 55 | + protected void initChannel(Channel channel) { |
| 56 | + channel.pipeline().addLast("http_codec", new HttpServerCodec()); |
| 57 | + channel.pipeline().addLast("http_handler", new SimpleChannelInboundHandler<>() { |
| 58 | + @Override |
| 59 | + protected void channelRead0(ChannelHandlerContext ctx, Object msg) { |
| 60 | + if (!(msg instanceof HttpRequest request)) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (request.uri().startsWith("/" + AuthHookConfig.secretKey + "/")) { |
| 65 | + final String uri = request.uri().substring(AuthHookConfig.secretKey.length() + 1); |
| 66 | + if (request.method().equals(HttpMethod.GET) && uri.startsWith("/session/minecraft/hasJoined")) { |
| 67 | + final QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.uri()); |
| 68 | + if (queryStringDecoder.parameters().containsKey("username") && queryStringDecoder.parameters().containsKey("serverId")) { |
| 69 | + final String username = queryStringDecoder.parameters().get("username").get(0); |
| 70 | + final String serverId = queryStringDecoder.parameters().get("serverId").get(0); |
| 71 | + |
| 72 | + final ProxyConnection proxyConnection = pendingConnections.remove(serverId + "_" + username); |
| 73 | + if (proxyConnection != null) { |
| 74 | + final GameProfile gameProfile = proxyConnection.getGameProfile(); |
| 75 | + final JsonObject responseObj = new JsonObject(); |
| 76 | + responseObj.addProperty("name", gameProfile.getName()); |
| 77 | + responseObj.addProperty("id", gameProfile.getId().toString().replace("-", "")); |
| 78 | + if (!gameProfile.getProperties().isEmpty()) { |
| 79 | + final JsonArray propertiesArray = new JsonArray(); |
| 80 | + gameProfile.getProperties().forEach((key, value) -> { |
| 81 | + final JsonObject propertyObj = new JsonObject(); |
| 82 | + propertyObj.addProperty("name", key); |
| 83 | + propertyObj.addProperty("value", value.getValue()); |
| 84 | + if (value.hasSignature()) { |
| 85 | + propertyObj.addProperty("signature", value.getSignature()); |
| 86 | + } |
| 87 | + propertiesArray.add(propertyObj); |
| 88 | + }); |
| 89 | + responseObj.add("properties", propertiesArray); |
| 90 | + } |
| 91 | + |
| 92 | + final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, ctx.alloc().buffer()); |
| 93 | + response.content().writeBytes(responseObj.toString().getBytes()); |
| 94 | + response.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON); |
| 95 | + response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes()); |
| 96 | + response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); |
| 97 | + ctx.writeAndFlush(response).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE).addListener(ChannelFutureListener.CLOSE); |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + final HttpClient httpClient = HttpClient.newHttpClient(); |
| 104 | + httpClient.sendAsync(java.net.http.HttpRequest.newBuilder().uri(URI.create("https://sessionserver.mojang.com" + uri)).build(), java.net.http.HttpResponse.BodyHandlers.ofByteArray()) |
| 105 | + .thenAccept(response -> { |
| 106 | + final FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(response.statusCode()), ctx.alloc().buffer()); |
| 107 | + fullHttpResponse.content().writeBytes(response.body()); |
| 108 | + for (Map.Entry<String, List<String>> entry : response.headers().map().entrySet()) { |
| 109 | + if (!entry.getKey().startsWith(":")) { |
| 110 | + fullHttpResponse.headers().set(entry.getKey(), entry.getValue().get(0)); |
| 111 | + } |
| 112 | + } |
| 113 | + fullHttpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); |
| 114 | + ctx.writeAndFlush(fullHttpResponse).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE).addListener(ChannelFutureListener.CLOSE); |
| 115 | + }); |
| 116 | + } else { |
| 117 | + ctx.close(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 123 | + ctx.close(); |
| 124 | + } |
| 125 | + }); |
| 126 | + } |
| 127 | + }) |
| 128 | + .bind(bindAddress) |
| 129 | + .syncUninterruptibly(); |
| 130 | + } |
| 131 | + |
| 132 | + public void addPendingConnection(final String serverIdHash, final ProxyConnection connection) { |
| 133 | + this.pendingConnections.put(serverIdHash + "_" + connection.getGameProfile().getName(), connection); |
| 134 | + } |
| 135 | + |
| 136 | + public void stop() { |
| 137 | + if (this.channelFuture != null) { |
| 138 | + this.channelFuture.channel().close(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public Channel getChannel() { |
| 143 | + return this.channelFuture.channel(); |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments