| 1 | package net.minecraft.server.jsonrpc;
|
| 2 |
|
| 3 | import com.google.common.net.HostAndPort;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import io.netty.handler.ssl.SslContext;
|
| 6 | import net.minecraft.server.dedicated.DedicatedServerProperties;
|
| 7 | import net.minecraft.server.dedicated.DedicatedServerSettings;
|
| 8 | import net.minecraft.server.jsonrpc.internalapi.MinecraftApi;
|
| 9 | import net.minecraft.server.jsonrpc.security.AuthenticationHandler;
|
| 10 | import net.minecraft.server.jsonrpc.security.JsonRpcSslContextProvider;
|
| 11 | import net.minecraft.server.jsonrpc.security.SecurityConfig;
|
| 12 | import net.minecraft.server.notifications.NotificationManager;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 | import org.slf4j.Logger;
|
| 15 |
|
| 16 | public class JsonRpc {
|
| 17 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 18 |
|
| 19 | public static @Nullable ManagementServer create(final DedicatedServerSettings settings, final NotificationManager notificationManager) {
|
| 20 | DedicatedServerProperties properties = settings.getProperties();
|
| 21 | if (properties.managementServerEnabled) {
|
| 22 | String managementServerSecret = properties.managementServerSecret;
|
| 23 | if (!SecurityConfig.isValid(managementServerSecret)) {
|
| 24 | throw new IllegalStateException("Invalid management server secret, must be 40 alphanumeric characters");
|
| 25 | }
|
| 26 |
|
| 27 | String managementHost = properties.managementServerHost;
|
| 28 | int managementPort = properties.managementServerPort;
|
| 29 | HostAndPort hostAndPort = HostAndPort.fromParts(managementHost, managementPort);
|
| 30 | SecurityConfig securityConfig = new SecurityConfig(managementServerSecret);
|
| 31 | String allowedOrigins = properties.managementServerAllowedOrigins;
|
| 32 | AuthenticationHandler authenticationHandler = new AuthenticationHandler(securityConfig, allowedOrigins);
|
| 33 | LOGGER.info("Starting json RPC server on {}", hostAndPort);
|
| 34 | ManagementServer jsonRpcServer = new ManagementServer(hostAndPort, authenticationHandler);
|
| 35 | MinecraftApi minecraftApi = MinecraftApi.of(notificationManager);
|
| 36 | minecraftApi.notificationManager().registerService(new JsonRpcNotificationService(minecraftApi, jsonRpcServer));
|
| 37 | if (properties.managementServerTlsEnabled) {
|
| 38 | SslContext sslContext = createSslContext(properties);
|
| 39 | jsonRpcServer.startWithTls(minecraftApi, sslContext);
|
| 40 | } else {
|
| 41 | jsonRpcServer.startWithoutTls(minecraftApi);
|
| 42 | }
|
| 43 |
|
| 44 | jsonRpcServer.scheduleHeartbeat(notificationManager, properties.statusHeartbeatInterval.get().intValue());
|
| 45 | return jsonRpcServer;
|
| 46 | } else {
|
| 47 | return null;
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | private static SslContext createSslContext(final DedicatedServerProperties properties) {
|
| 52 | try {
|
| 53 | return JsonRpcSslContextProvider.createFrom(properties.managementServerTlsKeystore, properties.managementServerTlsKeystorePassword);
|
| 54 | } catch (Exception e) {
|
| 55 | JsonRpcSslContextProvider.printInstructions();
|
| 56 | throw new IllegalStateException("Failed to configure TLS for the server management protocol", e);
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 |
|